Skip to content

Commit 2d109df

Browse files
The multinomial coefficient (#2197)
multinomial(k_1, ..., k_m) was reserved by name in #2184. It is now (k_1 + ... + k_m)! / (k_1! ... k_m!), written so when it is parsed, as Happypig375 agreed on #2182. The factorial is the gamma function off the whole numbers, so the coefficient is symmetric in its arguments everywhere, and it has no value where an argument or their sum is a negative integer, a pole of the gamma function. multinomial(2, 3, 4) is 1260, multinomial(1/2, 1/2) is 4 / pi, and multinomial(1/2, 1/3) agrees with mpmath to 1e-25. It is not built as the product of binomial coefficients the multinomial theorem factors it into. binomial is 0 at a negative whole lower index, and that product depends on the order of the arguments there: multinomial(-3, 1) would be -2 and multinomial(1, -3) would be 0. Part of #2182. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 1e2daa0 commit 2d109df

15 files changed

Lines changed: 482 additions & 376 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ the right shape, written out or once its operations are carried out, throws `Inv
467467
**Not silent: these names followed by a bracket now throw `UnrecognizedFunctionParseException` where they were read as
468468
products.** An unknown name followed by a bracket is a product, so `zeta(2)` was `zeta * 2` and simplified to
469469
`2 * zeta`. The long names of the functions the library is to have are refused until each is implemented, as `trunc`
470-
is: `zeta`, `riemannzeta`, `hurwitzzeta`, `lambertw`, `productlog`, `harmonicnumber`, `catalannumber`, `multinomial` and
470+
is: `zeta`, `riemannzeta`, `hurwitzzeta`, `lambertw`, `productlog`, `harmonicnumber`, `catalannumber` and
471471
`forwarddifference`. Names that will not be functions,
472472
because what they mean differs between sources or is already written exactly, are refused with the spelling to use:
473473
`heaviside`, `unitstep`, `isprime`, `pochhammer`, `doublefactorial`, `factorial2`, `fractionalpart` and `quotient`.
@@ -480,7 +480,7 @@ Each name without a bracket is still a variable, and short names such as `beta`
480480
| `"zeta(2)".ToEntity().Simplify()` | `2 * zeta` | throws `UnrecognizedFunctionParseException` |
481481
| `"isprime(7)".ToEntity()` | `isprime * 7` | throws `UnrecognizedFunctionParseException`: write `n in PP` |
482482
| `"heaviside(x)".ToEntity()` | `heaviside * x` | throws `UnrecognizedFunctionParseException`: write `iverson(x > 0)`, `iverson(x >= 0)` or `(1 + sgn(x))/2` |
483-
| `"multinomial(a, b, c)".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
483+
| `"harmonicnumber(n, m)".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
484484

485485
### The Möbius, divisor and prime-counting functions
486486

@@ -530,6 +530,19 @@ and `Γ(x + n)/Γ(x)`. LaTeX draws them with Knuth's bars, `x^{\underline{n}}` a
530530
| `"risingfactorial(3, 2)".ToEntity().Simplify()` | throws `UnhandledParseException` | `12` |
531531
| `"fallingfactorial(5, 1/2)".ToEntity().EvalNumerical()` | throws `UnhandledParseException` | `2.29257989505120015013822761269...` |
532532

533+
### The multinomial coefficient
534+
535+
**Not silent: `multinomial(k_1, ..., k_m)` is `(k_1 + ... + k_m)! / (k_1! ... k_m!)`, where with two or more arguments it
536+
was a parse error and with one it was a product.** It is written in the factorial, which is the gamma function off the
537+
whole numbers, so it is symmetric in its arguments and has no value where an argument or their sum is a negative
538+
integer ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
539+
540+
| Input | Was (2.6.0) | Now |
541+
|---|---|---|
542+
| `"multinomial(2, 3, 4)".ToEntity().Simplify()` | throws `UnhandledParseException` | `1260` |
543+
| `"multinomial(1/2, 1/2)".ToEntity().Simplify()` | throws `UnhandledParseException` | `4 / pi` |
544+
| `"multinomial(n)".ToEntity()` | `multinomial * n` | `n! / n!` |
545+
533546
### A subscript may have a subscript
534547

535548
**Not silent: what was a parse error is a name.** A name took at most one `_`, so `x_1_2` and `X_a_beta` were lexer

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
820820

821821
[Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs]
822822
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
823+
824+
[Tests/UnitTests/Convenience/MultinomialTest.cs]
825+
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ atom returns[Entity value]
625625
| 'gcd(' args = function_arguments ')' { AssertAtLeast("gcd", 1, $args.list.Count); $value = $args.list.Aggregate((a, b) => MathS.Gcd(a, b)); }
626626
| 'lcm(' args = function_arguments ')' { AssertAtLeast("lcm", 1, $args.list.Count); $value = $args.list.Aggregate((a, b) => MathS.Lcm(a, b)); }
627627
| 'binomial(' args = function_arguments ')' { Assert("binomial", 2, $args.list.Count); $value = MathS.Binomial($args.list[0], $args.list[1]); }
628+
| 'multinomial(' args = function_arguments ')' { $value = ParsingHelpers.Multinomial($args.list); }
628629
| 'fallingfactorial(' args = function_arguments ')' { Assert("fallingfactorial", 2, $args.list.Count); $value = MathS.FallingFactorial($args.list[0], $args.list[1]); }
629630
| 'factorialpower(' args = function_arguments ')' { Assert("factorialpower", 2, $args.list.Count); $value = MathS.FallingFactorial($args.list[0], $args.list[1]); }
630631
| 'risingfactorial(' args = function_arguments ')' { Assert("risingfactorial", 2, $args.list.Count); $value = MathS.RisingFactorial($args.list[0], $args.list[1]); }
@@ -695,7 +696,6 @@ atom returns[Entity value]
695696
| 'productlog(' args = function_arguments ')' { $value = NotImplementedFunction("productlog", "Lambert W function yet"); }
696697
| 'harmonicnumber(' args = function_arguments ')' { $value = NotImplementedFunction("harmonicnumber", "harmonic numbers yet"); }
697698
| 'catalannumber(' args = function_arguments ')' { $value = NotImplementedFunction("catalannumber", "Catalan numbers yet"); }
698-
| 'multinomial(' args = function_arguments ')' { $value = NotImplementedFunction("multinomial", "multinomial coefficients yet"); }
699699
| 'forwarddifference(' args = function_arguments ')' { $value = NotImplementedFunction("forwarddifference", "forward difference yet"); }
700700
/* Names that will not be functions, refused with the spelling to use: what they mean is
701701
ambiguous between sources, or is already written exactly another way. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ null
174174
'gcd('
175175
'lcm('
176176
'binomial('
177+
'multinomial('
177178
'fallingfactorial('
178179
'factorialpower('
179180
'risingfactorial('
@@ -215,7 +216,6 @@ null
215216
'productlog('
216217
'harmonicnumber('
217218
'catalannumber('
218-
'multinomial('
219219
'forwarddifference('
220220
'heaviside('
221221
'unitstep('

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

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -409,48 +409,48 @@ WS=237
409409
'gcd('=172
410410
'lcm('=173
411411
'binomial('=174
412-
'fallingfactorial('=175
413-
'factorialpower('=176
414-
'risingfactorial('=177
415-
'iverson('=178
416-
'aleph('=179
417-
'erf('=180
418-
'erfc('=181
419-
'erfi('=182
420-
'Ei('=183
421-
'li('=184
422-
'polygamma('=185
423-
'digamma('=186
424-
'Si('=187
425-
'Ci('=188
426-
'Shi('=189
427-
'Chi('=190
428-
'det('=191
429-
'transpose('=192
430-
'dot('=193
431-
'cross('=194
432-
'laplacian('=195
433-
'jacobian('=196
434-
'hessian('=197
435-
'resultant('=198
436-
'conjugate('=199
437-
'conj('=200
438-
'arg('=201
439-
'Re('=202
440-
'Im('=203
441-
're('=204
442-
'real('=205
443-
'im('=206
444-
'imag('=207
445-
'trunc('=208
446-
'zeta('=209
447-
'riemannzeta('=210
448-
'hurwitzzeta('=211
449-
'lambertw('=212
450-
'productlog('=213
451-
'harmonicnumber('=214
452-
'catalannumber('=215
453-
'multinomial('=216
412+
'multinomial('=175
413+
'fallingfactorial('=176
414+
'factorialpower('=177
415+
'risingfactorial('=178
416+
'iverson('=179
417+
'aleph('=180
418+
'erf('=181
419+
'erfc('=182
420+
'erfi('=183
421+
'Ei('=184
422+
'li('=185
423+
'polygamma('=186
424+
'digamma('=187
425+
'Si('=188
426+
'Ci('=189
427+
'Shi('=190
428+
'Chi('=191
429+
'det('=192
430+
'transpose('=193
431+
'dot('=194
432+
'cross('=195
433+
'laplacian('=196
434+
'jacobian('=197
435+
'hessian('=198
436+
'resultant('=199
437+
'conjugate('=200
438+
'conj('=201
439+
'arg('=202
440+
'Re('=203
441+
'Im('=204
442+
're('=205
443+
'real('=206
444+
'im('=207
445+
'imag('=208
446+
'trunc('=209
447+
'zeta('=210
448+
'riemannzeta('=211
449+
'hurwitzzeta('=212
450+
'lambertw('=213
451+
'productlog('=214
452+
'harmonicnumber('=215
453+
'catalannumber('=216
454454
'forwarddifference('=217
455455
'heaviside('=218
456456
'unitstep('=219

0 commit comments

Comments
 (0)