Skip to content

Commit 5e7731f

Browse files
The double factorial is the postfix n!! (#2187)
Mathematics writes the double factorial n!!, as it writes the factorial n!, and 5!! was a parse error. It is Entity.DoubleFactorialf, MathS.DoubleFactorial, written n!! and exported as SymPy's factorial2: the product of the positive integers up to n with its parity, 0!! and (-1)!! being the empty product 1, and below them n!! = (n + 2)!!/(n + 2), so (-3)!! is -1 and (-5)!! is 1/3. A negative even integer has none, and nothing off the integers has one: the extension through the gamma function gives 0!! = sqrt(2/pi), so no extension is the double factorial's. (n!)! keeps its parentheses. Three marks are the triple factorial in the literature, which the library does not have, so n!!! is refused rather than read as (n!!)!. The refusals of doublefactorial and factorial2 now point to n!!, and the native smoke test evaluates one. 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 3dbb55b commit 5e7731f

29 files changed

Lines changed: 3241 additions & 2898 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ the expression's own domain as its condition ([#2142](https://github.com/asc-com
290290
| `"(1 - cos(a))/(1 + cos(a))/tan(a/2)^2".Simplify()` | `(1 - cos(a)) / ((cos(a) + 1) * tan(a / 2) ^ 2)` | `1 provided not cos(a) + 1 = 0 and not tan(a / 2) = 0 and not cos(a / 2) = 0` |
291291
| `"(cos(2*x) - 1)^2 + sin(2*x)^2 - 4*sin(x)^2".Simplify()` ([#2133](https://github.com/asc-community/AngouriMath/issues/2133)) | `2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2` | `0` |
292292

293+
### The double factorial is the postfix `n!!`
294+
295+
**Not silent: `n!!` is now the double factorial, where it was a parse error.** It is the product of the positive integers
296+
up to `n` with its parity, `0!!` and `(-1)!!` being `1`, and below them `n!! = (n + 2)!!/(n + 2)`. It has no value at a
297+
negative even integer or off the integers, since the extension through the gamma function does not give `0!! = 1`.
298+
`(n!)!` keeps its parentheses, and `n!!!`, the triple factorial, is refused rather than read as `(n!!)!`
299+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
300+
301+
| Input | Was (2.6.0) | Now |
302+
|---|---|---|
303+
| `"7!!".ToEntity().Simplify()` | throws `UnhandledParseException` | `105` |
304+
| `"(-5)!!".ToEntity().Simplify()` | throws `UnhandledParseException` | `1/3` |
305+
| `"n!!".ToEntity()` | throws `UnhandledParseException` | `n!!` |
306+
| `"n!!!".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
307+
293308
### `re` and `im` are the real and the imaginary part
294309

295310
**Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
246246
[Tests/UnitTests/Convenience/RealAndImaginaryPartTest.cs]
247247
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
248248

249+
[Tests/UnitTests/Convenience/DoubleFactorialTest.cs]
250+
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
251+
249252
[Tests/UnitTests/Convenience/IversonTest.cs]
250253
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
251254

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,33 @@ public static Entity VectorLength(Matrix v)
14841484
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14851485
public static Entity Im(Entity z) => new Imf(z);
14861486

1487+
/// <summary>The double factorial <c>n!!</c>: <c>n (n - 2) (n - 4) ...</c> down to 1 or 2</summary>
1488+
/// <param name="n">An integer, other than a negative even one</param>
1489+
/// <returns>The <see cref="Entity.DoubleFactorialf"/> node</returns>
1490+
/// <remarks>
1491+
/// <c>0!!</c> and <c>(-1)!!</c> are <c>1</c>, a negative odd <c>n</c> follows from
1492+
/// <c>n!! = (n + 2)!!/(n + 2)</c>, and off the integers it has no value. It is not <c>(n!)!</c>.
1493+
/// https://github.com/asc-community/AngouriMath/issues/2182
1494+
/// </remarks>
1495+
/// <example>
1496+
/// <code>
1497+
/// using System;
1498+
/// using static AngouriMath.MathS;
1499+
///
1500+
/// Console.WriteLine(DoubleFactorial(7).Simplify());
1501+
/// Console.WriteLine(DoubleFactorial(8).Simplify());
1502+
/// Console.WriteLine(DoubleFactorial(-5).Simplify());
1503+
/// </code>
1504+
/// Prints
1505+
/// <code>
1506+
/// 105
1507+
/// 384
1508+
/// 1/3
1509+
/// </code>
1510+
/// </example>
1511+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1512+
public static Entity DoubleFactorial(Entity n) => new DoubleFactorialf(n);
1513+
14871514
/// <summary>The aleph number <c>aleph(k)</c>, the size of an infinite set</summary>
14881515
/// <param name="index">The index, a whole number from zero</param>
14891516
/// <returns>The <see cref="Entity.Alephf"/> node</returns>

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ options
8282
// atom is defined later
8383
factorial_expression returns[Entity value]
8484
: p = atom '!' { $value = MathS.Factorial($p.value); }
85+
/* The double factorial, n (n - 2) (n - 4) ..., which is not (n!)!: that keeps its parentheses.
86+
Three marks are the triple factorial in the literature, which the library does not have, so
87+
they are refused rather than read as (n!!)!. https://github.com/asc-community/AngouriMath/issues/2182 */
88+
| p = atom '!!' { $value = MathS.DoubleFactorial($p.value); }
89+
| p = atom '!!!' { throw new UnrecognizedFunctionParseException("n!!! is the triple factorial, which AngouriMath does not have; write (n!!)! for the factorial of a double factorial"); }
8590
| p = atom { $value = $p.value; }
8691
;
8792
@@ -698,8 +703,8 @@ atom returns[Entity value]
698703
| 'unitstep(' args = function_arguments ')' { $value = NotAFunctionHere("unitstep", "write iverson(x >= 0), which is 1 at 0"); }
699704
| 'isprime(' args = function_arguments ')' { $value = NotAFunctionHere("isprime", "write n in PP"); }
700705
| 'pochhammer(' args = function_arguments ')' { $value = NotAFunctionHere("pochhammer", "(x)_n is the rising factorial to some and the falling one to others; write risingfactorial or fallingfactorial"); }
701-
| 'doublefactorial(' args = function_arguments ')' { $value = NotAFunctionHere("doublefactorial", "the double factorial is to be the postfix n!!, which is not implemented yet"); }
702-
| 'factorial2(' args = function_arguments ')' { $value = NotAFunctionHere("factorial2", "the double factorial is to be the postfix n!!, which is not implemented yet"); }
706+
| 'doublefactorial(' args = function_arguments ')' { $value = NotAFunctionHere("doublefactorial", "the double factorial is the postfix n!!"); }
707+
| 'factorial2(' args = function_arguments ')' { $value = NotAFunctionHere("factorial2", "the double factorial is the postfix n!!"); }
703708
| 'fractionalpart(' args = function_arguments ')' { $value = NotAFunctionHere("fractionalpart", "write x mod 1, which is in [0, 1)"); }
704709
| 'quotient(' args = function_arguments ')' { $value = NotAFunctionHere("quotient", "write floor(a / b)"); }
705710
| 'domain(' args = function_arguments ')'

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

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

0 commit comments

Comments
 (0)