Repository navigation
Expand file tree
/
Copy pathFastExpression.cs
More file actions
460 lines (444 loc) · 23.2 KB
/
Copy pathFastExpression.cs
File metadata and controls
460 lines (444 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//
// Copyright (c) 2019-2022 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//
using System.Runtime.CompilerServices;
using AngouriMath.Core.Exceptions;
using Logic = AngouriMath.Core.Compilation.IntoLinq.ThreeValuedLogic;
namespace AngouriMath.Core
{
public sealed partial class FastExpression
{
internal enum InstructionType
{
PUSH_VAR,
PUSH_CONST,
LOAD_CACHE,
SAVE_CACHE,
// Control flow, to the instruction its Reference names: a jump, and a jump taken
// unless the truth value popped holds, as a case of a piecewise is passed over unless
// its predicate holds.
JUMP = 20,
JUMP_UNLESS_HOLDS,
// 1-arg functions
CALL_SIN = 50,
CALL_COS,
CALL_SECANT,
CALL_COSECANT,
CALL_TAN,
CALL_COTAN,
CALL_ARCSIN,
CALL_ARCCOS,
CALL_ARCTAN,
CALL_ARCCOTAN,
CALL_ARCSECANT,
CALL_ARCCOSECANT,
CALL_FACTORIAL,
CALL_SIGNUM,
CALL_ABS,
CALL_PHI,
CALL_FLOOR,
CALL_CEIL,
CALL_ROUND,
CALL_ERF,
CALL_ERFC,
CALL_ERFI,
CALL_EI,
CALL_LI,
CALL_SI,
CALL_CI,
CALL_SHI,
CALL_CHI,
CALL_CONJ,
CALL_ARG,
CALL_RE,
CALL_IM,
CALL_NOT,
CALL_DOUBLE_FACTORIAL,
CALL_CATALAN_NUMBER,
CALL_MOBIUS,
CALL_PRIME_NU,
CALL_PRIME_OMEGA,
CALL_PRIME,
CALL_PRIME_PI,
// Membership in the set of numbers its reference names, a NumberStatements.NumberSet.
CALL_MEMBER,
// 2-arg functions
CALL_SUM = 100,
CALL_MINUS,
CALL_MUL,
CALL_DIV,
CALL_MOD,
CALL_POW,
// A power to a rational with an odd denominator below ten; Reference is 1 where its numerator is odd.
CALL_REAL_ROOT_POW,
CALL_LOG,
CALL_MAX,
CALL_MIN,
CALL_LESS,
CALL_LESS_OR_EQUAL,
CALL_GREATER,
CALL_GREATER_OR_EQUAL,
CALL_EQUAL,
CALL_AND,
CALL_OR,
CALL_XOR,
CALL_IMPLIES,
CALL_BINOMIAL,
CALL_FALLING_FACTORIAL,
CALL_RISING_FACTORIAL,
CALL_LAMBERTW,
CALL_POLYGAMMA,
CALL_HARMONIC_NUMBER,
CALL_DIVISOR_SIGMA,
CALL_VALUATION,
CALL_GCD,
CALL_LCM,
CALL_DIVIDES,
// 3-arg: whether two numbers are congruent modulo a third.
CALL_CONGRUENT = 150,
}
internal sealed partial record Instruction(InstructionType Type, int Reference = -1, System.Numerics.Complex Value = default)
{
public override string ToString() =>
Type
+ (Reference == -1 ? "" : Reference.ToString())
+ (Type != InstructionType.PUSH_CONST ? "" : Value.ToString());
}
/// <summary>
/// The working stack and the cache of repeated subexpressions, one set per thread
/// rather than one per compiled expression.
/// </summary>
/// <remarks>
/// They used to be instance fields, which made a compiled expression unsafe to call
/// from more than one thread at a time: two calls interleaved their pushes and pops
/// and the second to finish found the stack in a state it did not put it in. Worse,
/// the damage was permanent -- the count check throws before anything is popped, so
/// one racing call left the leftovers behind and every later call failed too, on one
/// thread or many. See https://github.com/asc-community/AngouriMath/issues/637.
/// <para/>
/// Per thread rather than per call so that calling a compiled expression still
/// allocates nothing, which is the point of compiling it. Neither buffer carries
/// anything from one call to the next: the stack is emptied on the way in, and the
/// cache is only ever read at a slot the same call has already written.
/// </remarks>
private sealed class Scratch
{
public readonly Stack<System.Numerics.Complex> Stack = new();
public System.Numerics.Complex[] Cache = System.Array.Empty<System.Numerics.Complex>();
}
[System.ThreadStatic] private static Scratch? scratch;
private readonly Instruction[] instructions;
private readonly int varCount;
private readonly int cacheCount;
/// <summary>
/// You cannot modify this function once it is sealed. The final user will never access to its
/// direct instructions
/// </summary>
internal FastExpression(int varCount, List<Instruction> instructions, int cacheCount)
{
this.varCount = varCount;
this.instructions = instructions.ToArray();
this.cacheCount = cacheCount;
}
/// <summary>Calls the compiled function (synonym to <see cref="Substitute(System.Numerics.Complex[])"/>)</summary>
/// <param name="values">List arguments in the same order in which you compiled the function</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public System.Numerics.Complex Call(params System.Numerics.Complex[] values) => Substitute(values);
/// <summary>Calls the compiled function (synonym to <see cref="Call(System.Numerics.Complex[])"/>)</summary>
/// <param name="values">List arguments in the same order in which you compiled the function</param>
/// <exception cref="WrongNumberOfArgumentsException">
/// Thrown when the length of <paramref name="values"/> does not match the number of variables compiled.
/// </exception>
public System.Numerics.Complex Substitute(params System.Numerics.Complex[] values)
{
if (values.Length != varCount)
throw new WrongNumberOfArgumentsException($"Wrong number of parameters: Expected {varCount} but {values.Length} provided");
var mine = scratch ??= new Scratch();
var stack = mine.Stack;
stack.Clear();
var cache = mine.Cache;
if (cache.Length < cacheCount)
mine.Cache = cache = new System.Numerics.Complex[cacheCount];
for (var next = 0; next < instructions.Length; next++)
{
var instruction = instructions[next];
switch (instruction.Type)
{
// Every jump is forward, so a call ends. A piecewise computes the case it takes
// and no other: https://github.com/asc-community/AngouriMath/issues/2195
case InstructionType.JUMP:
next = instruction.Reference - 1;
break;
case InstructionType.JUMP_UNLESS_HOLDS:
if (Truth(stack.Pop()) != true)
next = instruction.Reference - 1;
break;
case InstructionType.PUSH_VAR:
stack.Push(values[instruction.Reference]);
break;
case InstructionType.PUSH_CONST:
stack.Push(instruction.Value);
break;
case InstructionType.LOAD_CACHE:
stack.Push(cache[instruction.Reference]);
break;
case InstructionType.SAVE_CACHE:
cache[instruction.Reference] = stack.Peek();
break;
case InstructionType.CALL_SUM:
stack.Push(stack.Pop() + stack.Pop());
break;
case InstructionType.CALL_MINUS:
stack.Push(stack.Pop() - stack.Pop());
break;
// Real where both sides are, so that an infinity keeps an imaginary part of 0:
// https://github.com/asc-community/AngouriMath/issues/2222
case InstructionType.CALL_MUL:
stack.Push(AngouriMath.Core.Compilation.RealProducts.Product(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_DIV:
stack.Push(AngouriMath.Core.Compilation.RealProducts.Quotient(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_MOD:
stack.Push(Core.Compilation.RealOnly.Mod(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_MAX:
stack.Push(Core.Compilation.RealOnly.Max(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_MIN:
stack.Push(Core.Compilation.RealOnly.Min(stack.Pop(), stack.Pop()));
break;
// Componentwise, as the interpreter takes them, and to even at a half for
// the rounding.
case InstructionType.CALL_FLOOR:
{
var z = stack.Pop();
stack.Push(new System.Numerics.Complex(System.Math.Floor(z.Real), System.Math.Floor(z.Imaginary)));
break;
}
case InstructionType.CALL_CEIL:
{
var z = stack.Pop();
stack.Push(new System.Numerics.Complex(System.Math.Ceiling(z.Real), System.Math.Ceiling(z.Imaginary)));
break;
}
case InstructionType.CALL_ROUND:
{
var z = stack.Pop();
stack.Push(new System.Numerics.Complex(
System.Math.Round(z.Real, System.MidpointRounding.ToEven), System.Math.Round(z.Imaginary, System.MidpointRounding.ToEven)));
break;
}
case InstructionType.CALL_POW:
stack.Push(AngouriMath.Core.Compilation.RealProducts.Power(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_REAL_ROOT_POW:
stack.Push(AngouriMath.Core.Compilation.RealProducts.RealRootPower(stack.Pop(), stack.Pop(), instruction.Reference == 1));
break;
case InstructionType.CALL_SIN:
stack.Push(System.Numerics.Complex.Sin(stack.Pop()));
break;
case InstructionType.CALL_COS:
stack.Push(System.Numerics.Complex.Cos(stack.Pop()));
break;
case InstructionType.CALL_SECANT:
stack.Push(1 / System.Numerics.Complex.Cos(stack.Pop()));
break;
case InstructionType.CALL_COSECANT:
stack.Push(1 / System.Numerics.Complex.Sin(stack.Pop()));
break;
case InstructionType.CALL_TAN:
stack.Push(System.Numerics.Complex.Tan(stack.Pop()));
break;
case InstructionType.CALL_COTAN:
stack.Push(1 / System.Numerics.Complex.Tan(stack.Pop()));
break;
case InstructionType.CALL_LOG:
{
var value = stack.Pop();
stack.Push(Core.Compilation.ComplexBranches.Log(stack.Pop(), value));
}
break;
case InstructionType.CALL_ARCSIN:
stack.Push(Core.Compilation.ComplexBranches.Arcsin(stack.Pop()));
break;
case InstructionType.CALL_ARCCOS:
stack.Push(Core.Compilation.ComplexBranches.Arccos(stack.Pop()));
break;
case InstructionType.CALL_ARCTAN:
stack.Push(Core.Compilation.ComplexBranches.Arctan(stack.Pop()));
break;
case InstructionType.CALL_ARCCOTAN:
stack.Push(Core.Compilation.ComplexBranches.Arccotan(stack.Pop()));
break;
case InstructionType.CALL_ARCSECANT:
stack.Push(Core.Compilation.ComplexBranches.Arcsecant(stack.Pop()));
break;
case InstructionType.CALL_ARCCOSECANT:
stack.Push(Core.Compilation.ComplexBranches.Arccosecant(stack.Pop()));
break;
// In double precision, as the kernels define them.
// https://github.com/asc-community/AngouriMath/issues/1607
case InstructionType.CALL_ERF:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Erf(stack.Pop()));
break;
case InstructionType.CALL_ERFC:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Erfc(stack.Pop()));
break;
case InstructionType.CALL_ERFI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Erfi(stack.Pop()));
break;
case InstructionType.CALL_EI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ei(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_LI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Li(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_SI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Si(stack.Pop()));
break;
case InstructionType.CALL_CI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ci(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_SHI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Shi(stack.Pop()));
break;
case InstructionType.CALL_CHI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Chi(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_FACTORIAL:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Factorial(stack.Pop()));
break;
case InstructionType.CALL_SIGNUM:
stack.Push(stack.Pop().Signum());
break;
case InstructionType.CALL_ABS:
stack.Push(stack.Pop().Abs());
break;
case InstructionType.CALL_PHI:
stack.Push(AngouriMath.Numerics.SpecialFunctions.Phi(stack.Pop()));
break;
case InstructionType.CALL_CONJ:
stack.Push(AngouriMath.Numerics.ComplexParts.Conj(stack.Pop()));
break;
case InstructionType.CALL_ARG:
stack.Push(AngouriMath.Numerics.ComplexParts.Arg(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_RE:
stack.Push(AngouriMath.Numerics.ComplexParts.Re(stack.Pop()));
break;
case InstructionType.CALL_IM:
stack.Push(AngouriMath.Numerics.ComplexParts.Im(stack.Pop()));
break;
// A truth value is 1, 0, or NaN where there is none, as evaluation leaves one;
// the comparisons and the connectives are the LINQ compiler's.
// https://github.com/asc-community/AngouriMath/issues/2195
case InstructionType.CALL_LESS:
stack.Push(Truth(Logic.Less(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_LESS_OR_EQUAL:
stack.Push(Truth(Logic.LessOrEqual(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_GREATER:
stack.Push(Truth(Logic.Greater(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_GREATER_OR_EQUAL:
stack.Push(Truth(Logic.GreaterOrEqual(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_EQUAL:
stack.Push(Truth(Logic.Equal(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_NOT:
stack.Push(Truth(Logic.Not(Truth(stack.Pop()))));
break;
case InstructionType.CALL_AND:
stack.Push(Truth(Logic.And(Truth(stack.Pop()), Truth(stack.Pop()))));
break;
case InstructionType.CALL_OR:
stack.Push(Truth(Logic.Or(Truth(stack.Pop()), Truth(stack.Pop()))));
break;
case InstructionType.CALL_XOR:
stack.Push(Truth(Logic.Xor(Truth(stack.Pop()), Truth(stack.Pop()))));
break;
case InstructionType.CALL_IMPLIES:
stack.Push(Truth(Logic.Implies(Truth(stack.Pop()), Truth(stack.Pop()))));
break;
// The binomial coefficient, the factorial powers and the double factorial.
// https://github.com/asc-community/AngouriMath/issues/2195
case InstructionType.CALL_BINOMIAL:
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.Binomial(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_FALLING_FACTORIAL:
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.FallingFactorial(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_RISING_FACTORIAL:
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.RisingFactorial(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_LAMBERTW:
stack.Push(AngouriMath.Numerics.LambertWFunction.LambertW(stack.Pop(), Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
break;
case InstructionType.CALL_POLYGAMMA:
stack.Push(AngouriMath.Numerics.PolygammaFunction.Polygamma(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_HARMONIC_NUMBER:
stack.Push(AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_MOBIUS:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(stack.Pop()));
break;
case InstructionType.CALL_PRIME_NU:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimeNu(stack.Pop()));
break;
case InstructionType.CALL_PRIME_OMEGA:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimeOmega(stack.Pop()));
break;
case InstructionType.CALL_PRIME:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Prime(stack.Pop()));
break;
case InstructionType.CALL_PRIME_PI:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimePi(stack.Pop()));
break;
case InstructionType.CALL_DIVISOR_SIGMA:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.DivisorSigma(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_VALUATION:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_GCD:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Gcd(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_LCM:
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Lcm(stack.Pop(), stack.Pop()));
break;
case InstructionType.CALL_MEMBER:
stack.Push(Truth(AngouriMath.Numerics.NumberStatements.In(stack.Pop(), instruction.Reference)));
break;
case InstructionType.CALL_DIVIDES:
stack.Push(Truth(AngouriMath.Numerics.NumberStatements.Divides(stack.Pop(), stack.Pop())));
break;
case InstructionType.CALL_CATALAN_NUMBER:
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.CatalanNumber(stack.Pop()));
break;
case InstructionType.CALL_DOUBLE_FACTORIAL:
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.DoubleFactorial(stack.Pop()));
break;
case InstructionType.CALL_CONGRUENT:
stack.Push(Truth(AngouriMath.Numerics.NumberStatements.Congruent(stack.Pop(), stack.Pop(), stack.Pop())));
break;
}
}
if (stack.Count != 1)
throw new AngouriBugException($"Unused values remain in the stack: {stack.Count} instead of 1");
return stack.Pop();
}
/// <summary>A truth value on the stack: 1 where it holds, 0 where it does not, NaN where it has none.</summary>
private static System.Numerics.Complex Truth(bool? value) => value is { } holds ? (holds ? 1 : 0) : double.NaN;
/// <summary>The truth value a number on the stack stands for: none but for exactly 1 or 0.</summary>
private static bool? Truth(System.Numerics.Complex value) => value == 1 ? true : value == 0 ? false : null;
/// <summary>Might be useful for debug if a function works too slowly</summary>
public override string ToString() => string.Join<Instruction>(" \n| ", instructions);
}
}