Skip to content

Commit 2d5b90a

Browse files
gcd and lcm compile at whole arguments; the binders are refused alike, saying to evaluate first (#2443)
gcd and lcm compile in FastExpression and in the LINQ compiler, over Complex and over double: exactly at whole arguments, rounded once by EFloat, and NaN at any other. Evaluation takes them over the rationals too, but a compiled argument is a double, and the double nearest 0.1 is not 1/10. A whole double past 2^53 is read as the integer it is. Both are non-negative, as evaluation gives them: gcd(-4, 6) = 2, gcd(0, 5) = 5, gcd(0, 0) = 0, lcm(-4, 6) = 12, lcm(0, 3) = 0. gcd(12, 18, 8) is gcd(gcd(12, 18), 8) and compiles nested. The calculus nodes and the binders -- derivative, integral, limit, sum, product, a sum, a maximum, a minimum, argmax and argmin over a set, apply and lambda -- stay refused with UncompilableNodeException, by one check both compilers run, whose message names the node and says to call .Evaled or Simplify() first and compile the result. The LINQ compiler said the bound variable was not among the arguments, and FastExpression asked for a bug report for a sum, a product or a lambda. The tests compare all three compiled forms with evaluation exactly, as doubles, at every pair of 14 whole arguments, among them -12, 0, 2^60, 3 * 2^58 and two numbers near 10^15 whose lcm is past 2^53; check NaN at 1/2, 5/2, -3/2, i, 1 + i, NaN and infinity beside 0, 1 and 6; and assert the refusal and its message for each binder in both compilers, and that the evaluated expression compiles. gcd(x, 6) at 4, by Compile: UncompilableNodeException (2.7.0), (2, 0) now. Part of #2195. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent eec3588 commit 2d5b90a

16 files changed

Lines changed: 359 additions & 30 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4040

4141
---
4242

43+
## Unreleased — since 2.7.0
44+
45+
### `gcd` and `lcm` compile at whole arguments
46+
47+
**Not silent: what was refused is compiled.** `gcd` and `lcm` were refused by both compilers with
48+
`UncompilableNodeException`. They compile now, exactly at whole arguments, rounded once, and to `NaN` at any other.
49+
Evaluation takes them over the rationals too, but a compiled argument is a double, and the double nearest `0.1` is not
50+
`1/10`, so a compiled value there would be exact for the wrong number. Both are non-negative, as evaluation has them
51+
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
52+
53+
| Input | Was (2.7.0) | Now |
54+
|---|---|---|
55+
| `"gcd(x, 6)".ToEntity().Compile("x").Call(4)` | `UncompilableNodeException` | `(2, 0)` |
56+
| `"lcm(x, 6)".ToEntity().Compile<double, double>("x")(4)` | `UncompilableNodeException` | `12` |
57+
| `"gcd(x, 6)".ToEntity().Compile<double, double>("x")(2.5)` | `UncompilableNodeException` | `NaN` |
58+
59+
The calculus nodes and the binders — `derivative`, `integral`, `limit`, `sum`, `product`, an extremum over a set,
60+
`lambda` and `apply` — are still refused, by both compilers with `UncompilableNodeException` as before; the message now
61+
names the node and says to compile `.Evaled` or `Simplify()` instead. The LINQ compiler said the bound variable was not
62+
among the arguments.
63+
64+
---
65+
4366
## 2.7.0 — since 2.6.0
4467

4568
**Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used

‎Sources/.editorconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,15 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10221022
[AngouriMath/Functions/Compilation/StatementPositions.cs]
10231023
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
10241024

1025+
[AngouriMath/Functions/Compilation/Binders.cs]
1026+
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
1027+
1028+
[Tests/UnitTests/Common/GcdLcmCompilationTest.cs]
1029+
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
1030+
1031+
[Tests/UnitTests/Common/BinderCompilationRefusalTest.cs]
1032+
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
1033+
10251034
[Tests/UnitTests/Common/StatementInANumericPositionTest.cs]
10261035
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
10271036

‎Sources/AngouriMath/Docs/Usage/Exceptions.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ take.
154154

155155
`Compile` was asked for a function it cannot build. Two distinct causes, and the message says which:
156156

157-
- a node with no compiled form — a derivative, an integral, a limit, a comparison of a matrix;
157+
- a node with no compiled form — a comparison of a matrix, or a calculus node or a binder: a
158+
derivative, an integral, a limit, a sum, a product, an extremum over a set, a lambda and its
159+
application. For those the message says to call `.Evaled` or `Simplify()` first and compile the
160+
result, which is computed once there rather than at every call;
158161
- a variable the expression mentions that the compilation was not given. The message names the
159162
variable and lists the ones it *was* given, since `Compile("x")` on `a * x` failing used to
160163
surface as a bare `KeyNotFoundException` about some dictionary.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath.Core.Exceptions;
9+
using static AngouriMath.Entity;
10+
11+
namespace AngouriMath.Core.Compilation
12+
{
13+
/// <summary>
14+
/// The calculus nodes and the binders -- a derivative, an integral, a limit, a sum, a product, an extremum over a
15+
/// set, a lambda and its application -- have no compiled form, and both compilers refuse them alike. Each is
16+
/// computed by evaluation, which compiling them would have to repeat at every call, as slowly as evaluating; so the
17+
/// caller evaluates once and compiles what comes back. The LINQ compiler reached the bound variable first and said
18+
/// it was not among the arguments, which named the symptom rather than the node.
19+
/// https://github.com/asc-community/AngouriMath/issues/2195
20+
/// </summary>
21+
internal static class Binders
22+
{
23+
internal static void Check(Entity expression)
24+
{
25+
foreach (var node in expression.Nodes)
26+
if (Kind(node) is { } kind)
27+
throw new UncompilableNodeException(
28+
$"`{node.Stringize()}` is {kind}, which has no compiled form. "
29+
+ (node is Lambda
30+
? "Apply it to an argument, call .Evaled or Simplify() on that, and compile the result."
31+
: "Call .Evaled or Simplify() on the expression first, and compile the result."));
32+
}
33+
34+
private static string? Kind(Entity node) => node switch
35+
{
36+
Derivativef => "a derivative",
37+
Integralf { Range: null } => "an integral",
38+
Integralf => "a definite integral",
39+
Limitf => "a limit",
40+
Summationf => "a sum",
41+
Productf => "a product",
42+
SumOverSetf => "a sum over a set",
43+
Maximumf => "a maximum over a set",
44+
Minimumf => "a minimum over a set",
45+
Argmaxf => "the set of points where a maximum is taken",
46+
Argminf => "the set of points where a minimum is taken",
47+
CalculusOperator => "a binder",
48+
Application => "an application of a function",
49+
Lambda => "a function rather than a number",
50+
_ => null
51+
};
52+
}
53+
}

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -340,24 +340,6 @@ private protected override void CompileNode(Compiler compiler)
340340
}
341341
}
342342

343-
public partial record Derivativef
344-
{
345-
private protected override void CompileNode(Compiler compiler) =>
346-
throw new UncompilableNodeException($"A derivative cannot be compiled: `{Stringize()}`");
347-
}
348-
349-
public partial record Integralf
350-
{
351-
private protected override void CompileNode(Compiler compiler) =>
352-
throw new UncompilableNodeException($"An integral cannot be compiled: `{Stringize()}`");
353-
}
354-
355-
public partial record Limitf
356-
{
357-
private protected override void CompileNode(Compiler compiler) =>
358-
throw new UncompilableNodeException($"A limit cannot be compiled: `{Stringize()}`");
359-
}
360-
361343
// The floors, the rounding and the extremes, which the solver's numerical search needs
362344
// compiled to run on `floor(x) = x/2 + 1/3` or `max(x, 1) = 2 x` at all.
363345
// https://github.com/asc-community/AngouriMath/issues/1603
@@ -761,6 +743,32 @@ private protected override void CompileNode(Compiler compiler)
761743
}
762744
}
763745

746+
// The gcd and the lcm, exact at whole arguments and NaN at any other.
747+
// https://github.com/asc-community/AngouriMath/issues/2195
748+
public partial record Gcdf
749+
{
750+
private protected override bool CompilesItself => true;
751+
752+
private protected override void CompileNode(Compiler compiler)
753+
{
754+
Right.InnerCompile(compiler);
755+
Left.InnerCompile(compiler);
756+
compiler.Instructions.Add(new(InstructionType.CALL_GCD));
757+
}
758+
}
759+
760+
public partial record Lcmf
761+
{
762+
private protected override bool CompilesItself => true;
763+
764+
private protected override void CompileNode(Compiler compiler)
765+
{
766+
Right.InnerCompile(compiler);
767+
Left.InnerCompile(compiler);
768+
compiler.Instructions.Add(new(InstructionType.CALL_LCM));
769+
}
770+
}
771+
764772
// A membership whose set the compilers rewrite into a statement about the element is gone by the time
765773
// a node is compiled, and what remains is one in a set of numbers, decided where evaluation decides it.
766774
// https://github.com/asc-community/AngouriMath/issues/2195
@@ -1076,6 +1084,7 @@ internal static FastExpression Compile(Entity func, IEnumerable<Variable> variab
10761084
func = func.InnerSimplified;
10771085
func = Compilation.CompiledMembership.Rewritten(func);
10781086
func = Functions.ExponentialQuotients.Rewritten(func);
1087+
Compilation.Binders.Check(func);
10791088
Compilation.StatementPositions.Check(func);
10801089
var visited = new HashSet<Entity>();
10811090
var cache = new Dictionary<Entity, int>();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ internal enum InstructionType
100100
CALL_HARMONIC_NUMBER,
101101
CALL_DIVISOR_SIGMA,
102102
CALL_VALUATION,
103+
CALL_GCD,
104+
CALL_LCM,
103105
CALL_DIVIDES,
104106

105107
// 3-arg: whether two numbers are congruent modulo a third.
@@ -418,6 +420,12 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
418420
case InstructionType.CALL_VALUATION:
419421
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(stack.Pop(), stack.Pop()));
420422
break;
423+
case InstructionType.CALL_GCD:
424+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Gcd(stack.Pop(), stack.Pop()));
425+
break;
426+
case InstructionType.CALL_LCM:
427+
stack.Push(AngouriMath.Numerics.NumberTheoreticFunctions.Lcm(stack.Pop(), stack.Pop()));
428+
break;
421429
case InstructionType.CALL_MEMBER:
422430
stack.Push(Truth(AngouriMath.Numerics.NumberStatements.In(stack.Pop(), instruction.Reference)));
423431
break;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ DivisorSigmaf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeast
321321
=> Expression.Call(GetDef("DivisorSigma", 2, newLeft.Type), newLeft, newRight),
322322
Valuationf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
323323
=> Expression.Call(GetDef("Valuation", 2, newLeft.Type), newLeft, newRight),
324+
// Exact at whole arguments and NaN at any other. https://github.com/asc-community/AngouriMath/issues/2195
325+
Gcdf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
326+
=> Expression.Call(GetDef("Gcd", 2, newLeft.Type), newLeft, newRight),
327+
Lcmf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
328+
=> Expression.Call(GetDef("Lcm", 2, newLeft.Type), newLeft, newRight),
324329
Dividesf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
325330
=> newLeft.Type == typeof(System.Numerics.Complex)
326331
? Expression.Call(complexDivisibility, newLeft, ConvertType(newRight, typeof(System.Numerics.Complex)))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal static TDelegate Compile<TDelegate>(
3434
expr = TensorCompilation.SimplifiedAroundMatrixArguments(expr, IsMatrixArgument);
3535
expr = CompiledMembership.Rewritten(expr);
3636
expr = Functions.ExponentialQuotients.Rewritten(expr);
37+
Binders.Check(expr);
3738
StatementPositions.Check(expr);
3839
// Two matrices whose shapes are written into the expression and do not fit are refused here, rather than by
3940
// a delegate that throws at every point it is called at. https://github.com/asc-community/AngouriMath/issues/526

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,8 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
369369
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
370370
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
371371

372-
// The functions of a factorisation, the valuation, the n-th prime and the prime-counting function, by the
373-
// interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
372+
// The functions of a factorisation, the valuation, the n-th prime, the prime-counting function, the gcd and
373+
// the lcm, by the interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
374374
public static System.Numerics.Complex Mobius(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(a);
375375
public static double Mobius(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.Mobius(a);
376376
public static System.Numerics.Complex PrimeNu(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.PrimeNu(a);
@@ -385,6 +385,10 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
385385
public static double DivisorSigma(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.DivisorSigma(a, b);
386386
public static System.Numerics.Complex Valuation(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(a, b);
387387
public static double Valuation(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.Valuation(a, b);
388+
public static System.Numerics.Complex Gcd(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.Gcd(a, b);
389+
public static double Gcd(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.Gcd(a, b);
390+
public static System.Numerics.Complex Lcm(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.NumberTheoreticFunctions.Lcm(a, b);
391+
public static double Lcm(double a, double b) => AngouriMath.Numerics.NumberTheoreticFunctions.Lcm(a, b);
388392

389393
/// <summary>
390394
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
@@ -602,6 +606,10 @@ public static bool IsNaN(System.Numerics.Complex a)
602606
{ ("DivisorSigma", 2, typeof(double)), Def<double>((a, b) => DivisorSigma(a, b)) },
603607
{ ("Valuation", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Valuation(a, b)) },
604608
{ ("Valuation", 2, typeof(double)), Def<double>((a, b) => Valuation(a, b)) },
609+
{ ("Gcd", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Gcd(a, b)) },
610+
{ ("Gcd", 2, typeof(double)), Def<double>((a, b) => Gcd(a, b)) },
611+
{ ("Lcm", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Lcm(a, b)) },
612+
{ ("Lcm", 2, typeof(double)), Def<double>((a, b) => Lcm(a, b)) },
605613
};
606614

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
var combinatorialBinary = new[]{ "Binomial", "FallingFactorial", "RisingFactorial" };
2222
// The number-theoretic functions, exact at a whole argument and NaN at any other.
2323
var numberTheoretic = new[]{ "Mobius", "PrimeNu", "PrimeOmega", "Prime", "PrimePi" };
24-
var numberTheoreticBinary = new[]{ "DivisorSigma", "Valuation" };
24+
var numberTheoreticBinary = new[]{ "DivisorSigma", "Valuation", "Gcd", "Lcm" };
2525
var types = new[]{ "System.Numerics.Complex", "double", "float", "long", "int", "BigInteger" };
2626
#>
2727
//
@@ -266,8 +266,8 @@ namespace AngouriMath.Core.Compilation.IntoLinq
266266
public static System.Numerics.Complex HarmonicNumber(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
267267
public static double HarmonicNumber(double a, double b) => AngouriMath.Numerics.PolygammaFunction.HarmonicNumber(a, b);
268268

269-
// The functions of a factorisation, the valuation, the n-th prime and the prime-counting function, by the
270-
// interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
269+
// The functions of a factorisation, the valuation, the n-th prime, the prime-counting function, the gcd and
270+
// the lcm, by the interpreter's routines. https://github.com/asc-community/AngouriMath/issues/2195
271271
<# foreach (var name in numberTheoretic) { #>
272272
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a);
273273
public static double <#= name #>(double a) => AngouriMath.Numerics.NumberTheoreticFunctions.<#= name #>(a);

0 commit comments

Comments
 (0)