Skip to content

Commit e2d74d9

Browse files
The binomial coefficient is a node, computed for a whole lower argument by the falling factorial and kept as itself otherwise (#1411)
binomial(n, k) is a function, Entity.Binomialf, spelled binomial(n, k) and MathS.Binomial(n, k). A node rather than n!/(k! (n - k)!): that quotient loses the integrality, has no value at a negative n where the falling factorial n (n - 1) ... (n - k + 1)/k! does, and no rule can read Pascal's identity or the binomial theorem in a ratio of three factorials. A whole k is computed for any n -- binomial(5, 2) is 10, binomial(-1, 3) is -1, binomial(1/2, 2) is -1/8 -- with 0 below k = 0 and above a non-negative whole n, and binomial(n, 0) is 1 for any n; a symbolic argument keeps the node; a k that is not whole evaluates numerically through the gamma function and has no value where n is a negative whole number, which the domain condition states. LaTeX is \binom{n}{k}, SymPy is sympy.binomial; the parser is regenerated for the new name, and the public API baseline carries the node. The first item of #1409 (the proofs book's chapter 8), as #809's survey specified it: DERIVED_BUT_PRESERVE, next to the factorial. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 06df32e commit e2d74d9

26 files changed

Lines changed: 1082 additions & 762 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,27 @@ asked. Only under that substitution; `Simplify` still combines nothing.
428428
| `"1/sqrt(atanh(tanh(a + b*x)))".Integrate("x")` | unevaluated after 49 s | `2 (1/2 ln(e^(2 b x + 2 a)))^(1/2)/(1/2)/(2 b)`, 0.3 s |
429429
| `"atanh(tanh(a + b*x))^(1/2)".Integrate("x")` | unevaluated | `2 (1/2 ln(e^(2 b x + 2 a)))^(3/2)/(3/2)/(2 b)` |
430430

431+
### `binomial(n, k)` is a function
432+
433+
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled
434+
`binomial(n, k)` and `MathS.Binomial(n, k)`. A whole `k` is computed by the falling factorial
435+
`n (n - 1) … (n - k + 1)/k!` for any `n` — negative and rational ones included — with `0` below
436+
`k = 0` and above a non-negative whole `n`; `binomial(n, 0)` is `1` for any `n`; a symbolic
437+
argument keeps the node, which is the spelling Pascal's identity and the binomial theorem read
438+
rather than a ratio of three factorials; a `k` that is not whole evaluates numerically through the
439+
gamma function, and has no value where `n` is a negative whole number, which its domain condition
440+
says. LaTeX is `\binom{n}{k}`, SymPy is `sympy.binomial`.
441+
442+
| | Was | Is |
443+
|---|---|---|
444+
| `"binomial(5, 2)".ToEntity()` | `UnhandledParseException` | `10` on simplification |
445+
| `"binomial(-1, 3)".ToEntity().Simplify()` | — | `-1` |
446+
| `"binomial(1/2, 2)".ToEntity().Simplify()` | — | `-1/8` |
447+
| `"binomial(n, k)".ToEntity().Simplify()` | — | `binomial(n, k)` |
448+
449+
First item of [#1409](https://github.com/asc-community/AngouriMath/issues/1409), specified in
450+
[#809](https://github.com/asc-community/AngouriMath/issues/809)'s survey.
451+
431452
### A condition the answer states on its own is no longer repeated beside it
432453

433454
**Improvement, not silent.** `Simplify` returned `(1 - ln(x))/x^2 provided not x = 0` for the

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
210210
[Tests/UnitTests/Common/IntegrationDecline.cs]
211211
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
212212

213+
[Tests/UnitTests/Convenience/BinomialTest.cs]
214+
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
215+
213216
[Tests/UnitTests/Common/ExponentialOfALogarithmTest.cs]
214217
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
215218

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,20 @@ public static Entity VectorLength(Matrix v)
12601260
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12611261
public static Entity Gcd(Entity a, Entity b) => new Gcdf(a, b);
12621262

1263+
/// <summary>The binomial coefficient <c>binomial(n, k)</c>, "n choose k"</summary>
1264+
/// <param name="n">The upper argument, which need not be a whole number</param>
1265+
/// <param name="k">The lower argument</param>
1266+
/// <returns>The <see cref="Entity.Binomialf"/> node</returns>
1267+
/// <remarks>
1268+
/// Whole arguments are computed, by the falling factorial where <c>n</c> is negative --
1269+
/// <c>binomial(-1, 3)</c> is <c>-1</c> -- and a whole <c>k</c> below zero, or above a
1270+
/// non-negative whole <c>n</c>, is <c>0</c>. A symbolic argument is kept as the node,
1271+
/// which is the shape Pascal's identity and the binomial theorem read.
1272+
/// https://github.com/asc-community/AngouriMath/issues/1409
1273+
/// </remarks>
1274+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1275+
public static Entity Binomial(Entity n, Entity k) => new Binomialf(n, k);
1276+
12631277
/// <summary>Boolean negation
12641278
/// <a href="https://en.wikipedia.org/wiki/Negation">Wikipedia</a></summary>
12651279
/// <param name="a">Argument node of which Negation function will be taken</param>

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ atom returns[Entity value]
467467
| 'argmax(' args = function_arguments ')' { Assert("argmax", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } argmaxRange ? MathS.Argmax($args.list[0], argmaxRange.Element, argmaxRange.SupSet) : throw new InvalidArgumentParseException("argmax expects its second argument to say which variable ranges over which set, as in argmax(f(t), t in S)"); }
468468
| 'argmin(' args = function_arguments ')' { Assert("argmin", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } argminRange ? MathS.Argmin($args.list[0], argminRange.Element, argminRange.SupSet) : throw new InvalidArgumentParseException("argmin expects its second argument to say which variable ranges over which set, as in argmin(f(t), t in S)"); }
469469
| 'gcd(' args = function_arguments ')' { AssertAtLeast("gcd", 1, $args.list.Count); $value = $args.list.Aggregate((a, b) => MathS.Gcd(a, b)); }
470+
| 'binomial(' args = function_arguments ')' { Assert("binomial", 2, $args.list.Count); $value = MathS.Binomial($args.list[0], $args.list[1]); }
470471
471472
/* Names the library does not have. Each is a function every other CAS spells this way, so
472473
a caller reaches for it, and without these rules each is silently read as a product --

‎Sources/AngouriMath/Core/Antlr/AngouriMath.interp‎

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

‎Sources/AngouriMath/Core/Antlr/AngouriMath.tokens‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,15 @@ T__147=148
149149
T__148=149
150150
T__149=150
151151
T__150=151
152-
NEWLINE=152
153-
NUMBER=153
154-
SPECIALSET=154
155-
BOOLEAN=155
156-
NAN=156
157-
VARIABLE=157
158-
COMMENT=158
159-
WS=159
152+
T__151=152
153+
NEWLINE=153
154+
NUMBER=154
155+
SPECIALSET=155
156+
BOOLEAN=156
157+
NAN=157
158+
VARIABLE=158
159+
COMMENT=159
160+
WS=160
160161
'!'=1
161162
'^'=2
162163
'-'=3
@@ -300,12 +301,13 @@ WS=159
300301
'argmax('=141
301302
'argmin('=142
302303
'gcd('=143
303-
'trunc('=144
304-
'lcm('=145
305-
'erf('=146
306-
'conjugate('=147
307-
'domain('=148
308-
'piecewise('=149
309-
'apply('=150
310-
'lambda('=151
311-
'NaN'=156
304+
'binomial('=144
305+
'trunc('=145
306+
'lcm('=146
307+
'erf('=147
308+
'conjugate('=148
309+
'domain('=149
310+
'piecewise('=150
311+
'apply('=151
312+
'lambda('=152
313+
'NaN'=157

0 commit comments

Comments
 (0)