Skip to content

Commit f3272dc

Browse files
Evaluate a binomial with a whole upper argument and a fractional lower one (#2211)
binomial(n, k) with a whole n and a k that is not whole was left as written, and EvalNumerical threw: the gamma ratio was guarded to an n that is not an integer. It is Γ(n + 1)/(Γ(k + 1) Γ(n - k + 1)) for any n, and binomial(3, 1/2) is 32/(5 pi). For a negative whole n, a pole of Γ(n + 1), it has no value, as the node's domain condition says, and is NaN. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 15ce64f commit f3272dc

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,18 @@ It is the forward difference with a step of 1, as Mathematica's `DifferenceDelta
619619
| `"forwarddifference(k^2, k)".ToEntity().Simplify()` | throws `UnhandledParseException` | `1 + 2 * k` |
620620
| `"forwarddifference(2^k, k)".ToEntity()` | throws `UnhandledParseException` | `2 ^ (k + 1) - 2 ^ k` |
621621

622+
### A binomial with a whole upper argument and a lower one that is not whole is evaluated
623+
624+
**Not silent: what threw is a number.** `binomial(n, k)` with a whole `n` and a `k` that is not whole was left as
625+
written, so `EvalNumerical` threw `CannotEvalException`. It is the gamma ratio `Γ(n + 1)/(Γ(k + 1) Γ(n - k + 1))`, as
626+
for any other `n`, and for a negative whole `n` it has no value: `Γ(n + 1)` has a pole there
627+
([#1411](https://github.com/asc-community/AngouriMath/issues/1411)).
628+
629+
| Input | Was (2.6.0) | Now |
630+
|---|---|---|
631+
| `"binomial(3, 1/2)".ToEntity().EvalNumerical()` | `CannotEvalException` | `2.0371832715762602978…`, `32/(5 pi)` |
632+
| `"binomial(-3, 1/2)".ToEntity().EvalNumerical()` | `CannotEvalException` | `NaN` |
633+
622634
### The harmonic numbers
623635

624636
**Not silent: `harmonicnumber(n)` and `harmonicnumber(n, m)` are the harmonic numbers, where the one was a product and the

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,12 @@ protected override Entity InnerSimplify(bool isExact) =>
566566
// negative n included: binomial(-1, 3) = (-1)(-2)(-3)/3! = -1.
567567
(Number top, Integer bottom) when bottom.EInteger.CanFitInInt32() => FallingFactorialOver(top, bottom.EInteger.ToInt32Unchecked()),
568568
(_, Integer { IsZero: true }) => Integer.Create(1),
569-
// Through the gamma function only numerically: there is no exact form.
569+
// A k that is not whole has no value with a negative whole n, a pole of Γ(n + 1).
570+
(Integer { EInteger.Sign: < 0 }, Complex bottom) when bottom is not Integer => MathS.NaN,
571+
// Through the gamma function only numerically: there is no exact form. A whole n
572+
// takes it too: binomial(3, 1/2) is 32/(5 pi).
570573
(Complex top, Complex bottom) when !isExact
571-
&& top is not Integer && Number.Factorial(top) is var topF && Number.Factorial(bottom) is var bottomF
574+
&& Number.Factorial(top) is var topF && Number.Factorial(bottom) is var bottomF
572575
&& Number.Factorial(top - bottom) is var restF && !topF.IsNaN && !bottomF.IsNaN && !restF.IsNaN
573576
=> topF / (bottomF * restF),
574577
_ => null

‎Sources/Tests/UnitTests/Convenience/BinomialTest.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public void AFractionalLowerArgumentEvaluatesThroughTheGammaFunction()
6767
Assert.True(Math.Abs((double)value.RealPart - 2.5) < 1e-9, $"binomial(5/2, 3/2) evaluated to {value}");
6868
}
6969

70+
/// <summary>
71+
/// A whole upper argument with a lower one that is not whole goes through the gamma function
72+
/// as well, against mpmath to 25 digits: <c>binomial(3, 1/2)</c> is <c>32/(5 pi)</c>. It was
73+
/// left unevaluated. With a negative whole upper argument there is no value.
74+
/// </summary>
75+
[Theory]
76+
[InlineData("binomial(3, 1/2)", "2.03718327157626029784171217117")]
77+
[InlineData("binomial(0, 1/2)", "0.63661977236758134307553505349")]
78+
[InlineData("binomial(4, 3/2)", "5.43248872420336079424456578978")]
79+
[InlineData("binomial(3, -1/2)", "0.291026181653751471120244595881")]
80+
public void AWholeUpperArgumentWithAFractionalLowerOneEvaluatesThroughTheGammaFunction(string input, string expected)
81+
{
82+
var value = Assert.IsAssignableFrom<Entity.Number.Real>(input.ToEntity().EvalNumerical());
83+
Assert.True(value.EDecimal.Subtract(PeterO.Numbers.EDecimal.FromString(expected)).Abs().CompareTo(PeterO.Numbers.EDecimal.FromString("1e-25")) < 0, $"{input} evaluated to {value}");
84+
}
85+
86+
[Fact]
87+
public void ANegativeWholeUpperArgumentWithAFractionalLowerOneHasNoValue()
88+
=> Assert.Equal(MathS.NaN, "binomial(-3, 1/2)".ToEntity().EvalNumerical());
89+
7090
/// <summary>
7191
/// Every whole k has a value, whatever n is; a k that is not whole has one unless n is
7292
/// a negative whole number, where <c>Γ(n + 1)</c> has a pole.

0 commit comments

Comments
 (0)