Skip to content

Commit b21f383

Browse files
The number-theoretic functions compile (#2217)
mobius, divisorsigma, primenu, primeomega, valuation, prime and primepi compile in FastExpression and in the LINQ compiler, over Complex and over double. prime and valuation were refused with UncompilableNodeException; the other five are new since 2.6.0. Each is computed by the routines the interpreter uses: Primes.Factorization for the functions of a factorisation, IsPrime for the valuation's prime, GetPrime for the n-th prime and CountUpTo for the prime count. So a compiled value is exact at a whole argument and NaN at any other, as evaluation gives, and a number too large to factorise or a prime past the millionth is NaN where the interpreter leaves it as written. The test compares both compilers with evaluation at 68 arguments for each function of a factorisation, including 1099511627776 = 2^40, 600851475143 and the non-whole 5/2 and 1 + i; the divisor sigma at six orders, among them -1, 1/2 and i; the valuation at seven numbers and six bases, two of them not prime; the n-th prime up to the 10000th; and the prime count up to 100000. prime(x) at 10: UncompilableNodeException (2.6.0), 29 now. valuation(x, 2) at 12: UncompilableNodeException (2.6.0), 2 now. Part of #2195. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8f410b8 commit b21f383

11 files changed

Lines changed: 396 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ factorial has no value off the whole numbers or at a negative even one
337337
| `"binomial(x, 2)".ToEntity().Compile("x").Call(5)` | `UncompilableNodeException` | `(10, 0)` |
338338
| `"binomial(x, 1/2)".ToEntity().Compile<double, double>("x")(3)` | `UncompilableNodeException` | `2.037183271576257` |
339339

340+
### The number-theoretic functions compile
341+
342+
**Not silent: what was refused is compiled.** `prime` and `valuation` were refused by both compilers with
343+
`UncompilableNodeException`, and `mobius`, `divisorsigma`, `primenu`, `primeomega` and `primepi`, new since 2.6.0,
344+
compile from the start. Each compiles by the routines the interpreter uses, exactly at a whole argument and to `NaN` at
345+
any other, as evaluation gives. A number too large to factorise, and a prime past the millionth, are `NaN` where the
346+
interpreter leaves them as written ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
347+
348+
| Input | Was (2.6.0) | Now |
349+
|---|---|---|
350+
| `"prime(x)".ToEntity().Compile("x").Call(10)` | `UncompilableNodeException` | `(29, 0)` |
351+
| `"prime(x)".ToEntity().Compile<double, double>("x")(2.5)` | `UncompilableNodeException` | `NaN` |
352+
| `"valuation(x, 2)".ToEntity().Compile("x").Call(12)` | `UncompilableNodeException` | `(2, 0)` |
353+
| `"valuation(x, 2)".ToEntity().Compile<double, double>("x")(0)` | `UncompilableNodeException` | `Infinity` |
354+
340355
### `mod` compiles over `Complex`
341356

342357
**Not silent: what was refused is compiled.** `Compile<Complex, ...>` refused `mod` with `UncompilableNodeException`,

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
375375
[Tests/UnitTests/Common/PolygammaCompilationTest.cs]
376376
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
377377

378+
[AngouriMath/Numerics/NumberTheoreticFunctions.cs]
379+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
380+
381+
[Tests/UnitTests/Common/NumberTheoreticCompilationTest.cs]
382+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
383+
378384
[Tests/UnitTests/Common/CompileMatrixTest.cs]
379385
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
380386

‎Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,87 @@ private protected override void CompileNode(Compiler compiler)
653653
}
654654
}
655655

656+
// The number-theoretic functions, exact at a whole argument and NaN at any other.
657+
// https://github.com/asc-community/AngouriMath/issues/2195
658+
public partial record Mobiusf
659+
{
660+
private protected override bool CompilesItself => true;
661+
662+
private protected override void CompileNode(Compiler compiler)
663+
{
664+
Argument.InnerCompile(compiler);
665+
compiler.Instructions.Add(new(InstructionType.CALL_MOBIUS));
666+
}
667+
}
668+
669+
public partial record PrimeNuf
670+
{
671+
private protected override bool CompilesItself => true;
672+
673+
private protected override void CompileNode(Compiler compiler)
674+
{
675+
Argument.InnerCompile(compiler);
676+
compiler.Instructions.Add(new(InstructionType.CALL_PRIME_NU));
677+
}
678+
}
679+
680+
public partial record PrimeOmegaf
681+
{
682+
private protected override bool CompilesItself => true;
683+
684+
private protected override void CompileNode(Compiler compiler)
685+
{
686+
Argument.InnerCompile(compiler);
687+
compiler.Instructions.Add(new(InstructionType.CALL_PRIME_OMEGA));
688+
}
689+
}
690+
691+
public partial record Primef
692+
{
693+
private protected override bool CompilesItself => true;
694+
695+
private protected override void CompileNode(Compiler compiler)
696+
{
697+
Argument.InnerCompile(compiler);
698+
compiler.Instructions.Add(new(InstructionType.CALL_PRIME));
699+
}
700+
}
701+
702+
public partial record PrimePif
703+
{
704+
private protected override bool CompilesItself => true;
705+
706+
private protected override void CompileNode(Compiler compiler)
707+
{
708+
Argument.InnerCompile(compiler);
709+
compiler.Instructions.Add(new(InstructionType.CALL_PRIME_PI));
710+
}
711+
}
712+
713+
public partial record DivisorSigmaf
714+
{
715+
private protected override bool CompilesItself => true;
716+
717+
private protected override void CompileNode(Compiler compiler)
718+
{
719+
Argument.InnerCompile(compiler);
720+
Order.InnerCompile(compiler);
721+
compiler.Instructions.Add(new(InstructionType.CALL_DIVISOR_SIGMA));
722+
}
723+
}
724+
725+
public partial record Valuationf
726+
{
727+
private protected override bool CompilesItself => true;
728+
729+
private protected override void CompileNode(Compiler compiler)
730+
{
731+
Prime.InnerCompile(compiler);
732+
Argument.InnerCompile(compiler);
733+
compiler.Instructions.Add(new(InstructionType.CALL_VALUATION));
734+
}
735+
}
736+
656737
public partial record Maxf
657738
{
658739
private protected override bool CompilesItself => true;

‎Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ internal enum InstructionType
5757
CALL_NOT,
5858
CALL_DOUBLE_FACTORIAL,
5959
CALL_CATALAN_NUMBER,
60+
CALL_MOBIUS,
61+
CALL_PRIME_NU,
62+
CALL_PRIME_OMEGA,
63+
CALL_PRIME,
64+
CALL_PRIME_PI,
6065

6166
// 2-arg functions
6267
CALL_SUM = 100,
@@ -85,6 +90,8 @@ internal enum InstructionType
8590
CALL_LAMBERTW,
8691
CALL_POLYGAMMA,
8792
CALL_HARMONIC_NUMBER,
93+
CALL_DIVISOR_SIGMA,
94+
CALL_VALUATION,
8895

8996
// 3-arg: a value where its predicate holds, and the one below it elsewhere.
9097
CALL_SELECT = 150,
@@ -368,6 +375,27 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
368375
case InstructionType.CALL_HARMONIC_NUMBER:
369376
stack.Push(AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(stack.Pop(), stack.Pop()));
370377
break;
378+
case InstructionType.CALL_MOBIUS:
379+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(stack.Pop()));
380+
break;
381+
case InstructionType.CALL_PRIME_NU:
382+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimeNu(stack.Pop()));
383+
break;
384+
case InstructionType.CALL_PRIME_OMEGA:
385+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimeOmega(stack.Pop()));
386+
break;
387+
case InstructionType.CALL_PRIME:
388+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Prime(stack.Pop()));
389+
break;
390+
case InstructionType.CALL_PRIME_PI:
391+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.PrimePi(stack.Pop()));
392+
break;
393+
case InstructionType.CALL_DIVISOR_SIGMA:
394+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.DivisorSigma(stack.Pop(), stack.Pop()));
395+
break;
396+
case InstructionType.CALL_VALUATION:
397+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(stack.Pop(), stack.Pop()));
398+
break;
371399
case InstructionType.CALL_CATALAN_NUMBER:
372400
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.CatalanNumber(stack.Pop()));
373401
break;

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ Chif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Chi",
158158
Factorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Factorial", 1, newE.Type), newE),
159159
DoubleFactorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("DoubleFactorial", 1, newE.Type), newE),
160160
CatalanNumberf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("CatalanNumber", 1, newE.Type), newE),
161+
// The number-theoretic functions, exact at a whole argument and NaN at any other.
162+
// https://github.com/asc-community/AngouriMath/issues/2195
163+
Mobiusf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Mobius", 1, newE.Type), newE),
164+
PrimeNuf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("PrimeNu", 1, newE.Type), newE),
165+
PrimeOmegaf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("PrimeOmega", 1, newE.Type), newE),
166+
Primef when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Prime", 1, newE.Type), newE),
167+
PrimePif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("PrimePi", 1, newE.Type), newE),
161168
Phif => Expression.Call(GetDef("Phi", 1, e.Type), e),
162169

163170
// The conjugate, the argument and the parts, widened to double as the special
@@ -249,6 +256,10 @@ Polygammaf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDou
249256
=> Expression.Call(GetDef("Polygamma", 2, newLeft.Type), newLeft, newRight),
250257
HarmonicNumberf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
251258
=> Expression.Call(GetDef("HarmonicNumber", 2, newLeft.Type), newLeft, newRight),
259+
DivisorSigmaf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
260+
=> Expression.Call(GetDef("DivisorSigma", 2, newLeft.Type), newLeft, newRight),
261+
Valuationf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
262+
=> Expression.Call(GetDef("Valuation", 2, newLeft.Type), newLeft, newRight),
252263
Maxf => Expression.Call(GetDef("Max", 2, left.Type), left, right),
253264
Minf => Expression.Call(GetDef("Min", 2, left.Type), left, right),
254265

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,23 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
356356
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
357357
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
358358

359+
// The functions of a factorisation, the valuation, the n-th prime and the prime-counting function, by the
360+
// interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
361+
public static System.Numerics.Complex Mobius(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(a);
362+
public static double Mobius(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(a);
363+
public static System.Numerics.Complex PrimeNu(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimeNu(a);
364+
public static double PrimeNu(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimeNu(a);
365+
public static System.Numerics.Complex PrimeOmega(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimeOmega(a);
366+
public static double PrimeOmega(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimeOmega(a);
367+
public static System.Numerics.Complex Prime(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.Prime(a);
368+
public static double Prime(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.Prime(a);
369+
public static System.Numerics.Complex PrimePi(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimePi(a);
370+
public static double PrimePi(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimePi(a);
371+
public static System.Numerics.Complex DivisorSigma(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.DivisorSigma(a, b);
372+
public static double DivisorSigma(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.DivisorSigma(a, b);
373+
public static System.Numerics.Complex Valuation(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(a, b);
374+
public static double Valuation(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(a, b);
375+
359376
/// <summary>
360377
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
361378
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -556,6 +573,20 @@ public static bool IsNaN(System.Numerics.Complex a)
556573
{ ("LambertW", 2, typeof(double)), Def<double>((a, b) => LambertW(a, b)) },
557574
{ ("Polygamma", 2, typeof(double)), Def<double>((a, b) => Polygamma(a, b)) },
558575
{ ("HarmonicNumber", 2, typeof(double)), Def<double>((a, b) => HarmonicNumber(a, b)) },
576+
{ ("Mobius", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Mobius(a)) },
577+
{ ("Mobius", 1, typeof(double)), Def<double>(a => Mobius(a)) },
578+
{ ("PrimeNu", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => PrimeNu(a)) },
579+
{ ("PrimeNu", 1, typeof(double)), Def<double>(a => PrimeNu(a)) },
580+
{ ("PrimeOmega", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => PrimeOmega(a)) },
581+
{ ("PrimeOmega", 1, typeof(double)), Def<double>(a => PrimeOmega(a)) },
582+
{ ("Prime", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Prime(a)) },
583+
{ ("Prime", 1, typeof(double)), Def<double>(a => Prime(a)) },
584+
{ ("PrimePi", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => PrimePi(a)) },
585+
{ ("PrimePi", 1, typeof(double)), Def<double>(a => PrimePi(a)) },
586+
{ ("DivisorSigma", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => DivisorSigma(a, b)) },
587+
{ ("DivisorSigma", 2, typeof(double)), Def<double>((a, b) => DivisorSigma(a, b)) },
588+
{ ("Valuation", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Valuation(a, b)) },
589+
{ ("Valuation", 2, typeof(double)), Def<double>((a, b) => Valuation(a, b)) },
559590
};
560591

561592
// An expression tree is how C# spells "the MethodInfo of this method" without a string:

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// https://github.com/asc-community/AngouriMath/issues/2195
2020
var combinatorial = new[]{ "DoubleFactorial", "CatalanNumber" };
2121
var combinatorialBinary = new[]{ "Binomial", "FallingFactorial", "RisingFactorial" };
22+
// The number-theoretic functions, exact at a whole argument and NaN at any other.
23+
var numberTheoretic = new[]{ "Mobius", "PrimeNu", "PrimeOmega", "Prime", "PrimePi" };
24+
var numberTheoreticBinary = new[]{ "DivisorSigma", "Valuation" };
2225
var types = new[]{ "System.Numerics.Complex", "double", "float", "long", "int", "BigInteger" };
2326
#>
2427
//
@@ -244,6 +247,17 @@ namespace AngouriMath.Core.Compilation.IntoLinq
244247
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
245248
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
246249

250+
// The functions of a factorisation, the valuation, the n-th prime and the prime-counting function, by the
251+
// interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
252+
<# foreach (var name in numberTheoretic) { #>
253+
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a);
254+
public static double <#= name #>(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a);
255+
<# } #>
256+
<# foreach (var name in numberTheoreticBinary) { #>
257+
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a, b);
258+
public static double <#= name #>(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a, b);
259+
<# } #>
260+
247261
/// <summary>
248262
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
249263
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -302,6 +316,16 @@ namespace AngouriMath.Core.Compilation.IntoLinq
302316
{ ("LambertW", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => LambertW(a, b)) },
303317
{ ("Polygamma", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => Polygamma(a, b)) },
304318
{ ("HarmonicNumber", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => HarmonicNumber(a, b)) },
319+
<# } #>
320+
<# foreach (var name in numberTheoretic) { #>
321+
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
322+
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },
323+
<# } #>
324+
<# } #>
325+
<# foreach (var name in numberTheoreticBinary) { #>
326+
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
327+
{ ("<#= name #>", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => <#= name #>(a, b)) },
328+
<# } #>
305329
<# } #>
306330
};
307331

0 commit comments

Comments
 (0)