Skip to content

Commit 9164e15

Browse files
The combinatorial functions compile (#2213)
binomial, fallingfactorial, risingfactorial and n!! were refused by both compilers. Each compiles now, in double precision and on the interpreter's terms, in CombinatorialFunctions: a whole order is a product, and the reciprocal of one for a negative order, and any other goes through the gamma function, with NaN at a pole of the numerator and 0 at one of the denominator. The double factorial has no value off the whole numbers or at a negative even one. FastExpression has an instruction for each, and the LINQ compiler a Complex and a double definition. Both are checked against evaluation over every pair of fourteen whole, half-whole and complex arguments. Part of #2195 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent e59e609 commit 9164e15

11 files changed

Lines changed: 368 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@ negative even integer or off the integers, since the extension through the gamma
323323
| `"n!!".ToEntity()` | throws `UnhandledParseException` | `n!!` |
324324
| `"n!!!".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
325325

326+
### The binomial coefficient, the factorial powers and the double factorial compile
327+
328+
**Not silent: what was refused is compiled.** `binomial` was refused by both compilers with
329+
`UncompilableNodeException`, and `fallingfactorial`, `risingfactorial` and `n!!`, new since 2.6.0, compile from the
330+
start. Each compiles in double precision and on the interpreter's terms: a whole order is a product, and any other goes
331+
through the gamma function, with `NaN` at a pole of the numerator and `0` at one of the denominator. The double
332+
factorial has no value off the whole numbers or at a negative even one
333+
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
334+
335+
| Input | Was (2.6.0) | Now |
336+
|---|---|---|
337+
| `"binomial(x, 2)".ToEntity().Compile("x").Call(5)` | `UncompilableNodeException` | `(10, 0)` |
338+
| `"binomial(x, 1/2)".ToEntity().Compile<double, double>("x")(3)` | `UncompilableNodeException` | `2.037183271576257` |
339+
326340
### `mod` compiles over `Complex`
327341

328342
**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
@@ -357,6 +357,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
357357
[Tests/UnitTests/Common/FastExpressionStatementTest.cs]
358358
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
359359

360+
[Tests/UnitTests/Common/CombinatorialCompilationTest.cs]
361+
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
362+
363+
[AngouriMath/Numerics/CombinatorialFunctions.cs]
364+
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
365+
360366
[Tests/UnitTests/Common/CompileMatrixTest.cs]
361367
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
362368

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,55 @@ private protected override void CompileNode(Compiler compiler)
555555
}
556556
}
557557

558+
// The binomial coefficient, the factorial powers and the double factorial, on the
559+
// interpreter's terms. https://github.com/asc-community/AngouriMath/issues/2195
560+
public partial record DoubleFactorialf
561+
{
562+
private protected override bool CompilesItself => true;
563+
564+
private protected override void CompileNode(Compiler compiler)
565+
{
566+
Argument.InnerCompile(compiler);
567+
compiler.Instructions.Add(new(InstructionType.CALL_DOUBLE_FACTORIAL));
568+
}
569+
}
570+
571+
public partial record Binomialf
572+
{
573+
private protected override bool CompilesItself => true;
574+
575+
private protected override void CompileNode(Compiler compiler)
576+
{
577+
Lower.InnerCompile(compiler);
578+
Upper.InnerCompile(compiler);
579+
compiler.Instructions.Add(new(InstructionType.CALL_BINOMIAL));
580+
}
581+
}
582+
583+
public partial record FallingFactorialf
584+
{
585+
private protected override bool CompilesItself => true;
586+
587+
private protected override void CompileNode(Compiler compiler)
588+
{
589+
Order.InnerCompile(compiler);
590+
Argument.InnerCompile(compiler);
591+
compiler.Instructions.Add(new(InstructionType.CALL_FALLING_FACTORIAL));
592+
}
593+
}
594+
595+
public partial record RisingFactorialf
596+
{
597+
private protected override bool CompilesItself => true;
598+
599+
private protected override void CompileNode(Compiler compiler)
600+
{
601+
Order.InnerCompile(compiler);
602+
Argument.InnerCompile(compiler);
603+
compiler.Instructions.Add(new(InstructionType.CALL_RISING_FACTORIAL));
604+
}
605+
}
606+
558607
public partial record Maxf
559608
{
560609
private protected override bool CompilesItself => true;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ internal enum InstructionType
5555
CALL_RE,
5656
CALL_IM,
5757
CALL_NOT,
58+
CALL_DOUBLE_FACTORIAL,
5859

5960
// 2-arg functions
6061
CALL_SUM = 100,
@@ -77,6 +78,9 @@ internal enum InstructionType
7778
CALL_IMPLIES,
7879
// A value where its predicate holds, and NaN elsewhere.
7980
CALL_PROVIDED,
81+
CALL_BINOMIAL,
82+
CALL_FALLING_FACTORIAL,
83+
CALL_RISING_FACTORIAL,
8084

8185
// 3-arg: a value where its predicate holds, and the one below it elsewhere.
8286
CALL_SELECT = 150,
@@ -340,6 +344,20 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
340344
stack.Push(Truth(stack.Pop()) == true ? value : double.NaN);
341345
break;
342346
}
347+
// The binomial coefficient, the factorial powers and the double factorial.
348+
// https://github.com/asc-community/AngouriMath/issues/2195
349+
case InstructionType.CALL_BINOMIAL:
350+
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.Binomial(stack.Pop(), stack.Pop()));
351+
break;
352+
case InstructionType.CALL_FALLING_FACTORIAL:
353+
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.FallingFactorial(stack.Pop(), stack.Pop()));
354+
break;
355+
case InstructionType.CALL_RISING_FACTORIAL:
356+
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.RisingFactorial(stack.Pop(), stack.Pop()));
357+
break;
358+
case InstructionType.CALL_DOUBLE_FACTORIAL:
359+
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.DoubleFactorial(stack.Pop()));
360+
break;
343361
case InstructionType.CALL_SELECT:
344362
{
345363
var value = stack.Pop();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Cif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Ci",
156156
Shif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Shi", 1, newE.Type), newE),
157157
Chif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Chi", 1, newE.Type), newE),
158158
Factorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Factorial", 1, newE.Type), newE),
159+
DoubleFactorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("DoubleFactorial", 1, newE.Type), newE),
159160
Phif => Expression.Call(GetDef("Phi", 1, e.Type), e),
160161

161162
// The conjugate, the argument and the parts, widened to double as the special
@@ -233,6 +234,14 @@ Logf when
233234
ShouldBeAtLeastDouble(left) is var newLeft
234235
&& ShouldBeAtLeastDouble(right) is var newRight
235236
=> Expression.Call(GetDef("Log", 2, newRight.Type), newLeft, newRight),
237+
// The binomial coefficient and the factorial powers, widened to double first as the
238+
// special functions are. https://github.com/asc-community/AngouriMath/issues/2195
239+
Binomialf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
240+
=> Expression.Call(GetDef("Binomial", 2, newLeft.Type), newLeft, newRight),
241+
FallingFactorialf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
242+
=> Expression.Call(GetDef("FallingFactorial", 2, newLeft.Type), newLeft, newRight),
243+
RisingFactorialf when ShouldBeAtLeastDouble(left) is var newLeft && ShouldBeAtLeastDouble(right) is var newRight
244+
=> Expression.Call(GetDef("RisingFactorial", 2, newLeft.Type), newLeft, newRight),
236245
Maxf => Expression.Call(GetDef("Max", 2, left.Type), left, right),
237246
Minf => Expression.Call(GetDef("Min", 2, left.Type), left, right),
238247

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
331331
public static System.Numerics.Complex Im(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Im(a);
332332
public static double Im(double a) => AngouriMath.Numerics.ComplexParts.Im(a);
333333

334+
// The binomial coefficient, the factorial powers and the double factorial, on the interpreter's
335+
// terms. https://github.com/asc-community/AngouriMath/issues/2195
336+
public static System.Numerics.Complex DoubleFactorial(System.Numerics.Complex a) => AngouriMath.Numerics.CombinatorialFunctions.DoubleFactorial(a);
337+
public static double DoubleFactorial(double a) => AngouriMath.Numerics.CombinatorialFunctions.DoubleFactorial(a);
338+
public static System.Numerics.Complex Binomial(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.CombinatorialFunctions.Binomial(a, b);
339+
public static double Binomial(double a, double b) => AngouriMath.Numerics.CombinatorialFunctions.Binomial(a, b);
340+
public static System.Numerics.Complex FallingFactorial(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.CombinatorialFunctions.FallingFactorial(a, b);
341+
public static double FallingFactorial(double a, double b) => AngouriMath.Numerics.CombinatorialFunctions.FallingFactorial(a, b);
342+
public static System.Numerics.Complex RisingFactorial(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.CombinatorialFunctions.RisingFactorial(a, b);
343+
public static double RisingFactorial(double a, double b) => AngouriMath.Numerics.CombinatorialFunctions.RisingFactorial(a, b);
344+
334345
/// <summary>
335346
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
336347
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -515,6 +526,14 @@ public static bool IsNaN(System.Numerics.Complex a)
515526
{ ("Re", 1, typeof(double)), Def<double>(a => Re(a)) },
516527
{ ("Im", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Im(a)) },
517528
{ ("Im", 1, typeof(double)), Def<double>(a => Im(a)) },
529+
{ ("DoubleFactorial", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => DoubleFactorial(a)) },
530+
{ ("DoubleFactorial", 1, typeof(double)), Def<double>(a => DoubleFactorial(a)) },
531+
{ ("Binomial", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => Binomial(a, b)) },
532+
{ ("Binomial", 2, typeof(double)), Def<double>((a, b) => Binomial(a, b)) },
533+
{ ("FallingFactorial", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => FallingFactorial(a, b)) },
534+
{ ("FallingFactorial", 2, typeof(double)), Def<double>((a, b) => FallingFactorial(a, b)) },
535+
{ ("RisingFactorial", 2, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>((a, b) => RisingFactorial(a, b)) },
536+
{ ("RisingFactorial", 2, typeof(double)), Def<double>((a, b) => RisingFactorial(a, b)) },
518537
};
519538

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

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
// The conjugate, the argument and the parts, for Complex and double alone in the same way.
1616
// https://github.com/asc-community/AngouriMath/issues/2195
1717
var parts = new[]{ "Conj", "Arg", "Re", "Im" };
18+
// The combinatorial functions, for Complex and double alone in the same way: one argument, and two.
19+
// https://github.com/asc-community/AngouriMath/issues/2195
20+
var combinatorial = new[]{ "DoubleFactorial" };
21+
var combinatorialBinary = new[]{ "Binomial", "FallingFactorial", "RisingFactorial" };
1822
var types = new[]{ "System.Numerics.Complex", "double", "float", "long", "int", "BigInteger" };
1923
#>
2024
//
@@ -217,6 +221,17 @@ namespace AngouriMath.Core.Compilation.IntoLinq
217221
public static double <#= name #>(double a) => AngouriMath.Numerics.ComplexParts.<#= name #>(a);
218222
<# } #>
219223

224+
// The binomial coefficient, the factorial powers and the double factorial, on the interpreter's
225+
// terms. https://github.com/asc-community/AngouriMath/issues/2195
226+
<# foreach (var name in combinatorial) { #>
227+
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a) => AngouriMath.Numerics.CombinatorialFunctions.<#= name #>(a);
228+
public static double <#= name #>(double a) => AngouriMath.Numerics.CombinatorialFunctions.<#= name #>(a);
229+
<# } #>
230+
<# foreach (var name in combinatorialBinary) { #>
231+
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.CombinatorialFunctions.<#= name #>(a, b);
232+
public static double <#= name #>(double a, double b) => AngouriMath.Numerics.CombinatorialFunctions.<#= name #>(a, b);
233+
<# } #>
234+
220235
/// <summary>
221236
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
222237
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
@@ -260,6 +275,16 @@ namespace AngouriMath.Core.Compilation.IntoLinq
260275
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
261276
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },
262277
<# } #>
278+
<# } #>
279+
<# foreach (var name in combinatorial) { #>
280+
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
281+
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },
282+
<# } #>
283+
<# } #>
284+
<# foreach (var name in combinatorialBinary) { #>
285+
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
286+
{ ("<#= name #>", 2, typeof(<#= type #>)), Def<<#= type #>>((a, b) => <#= name #>(a, b)) },
287+
<# } #>
263288
<# } #>
264289
};
265290

0 commit comments

Comments
 (0)