Skip to content

Commit 18d2715

Browse files
Rafael-SOWNetclaude
andcommitted
The beta function and the p-norm of a vector
beta(a, b) and norm(v, p) were parse errors, and are now Entity.Betaf and Entity.Normf, built by MathS.Beta and MathS.Norm. With one argument each is still what it was, beta(x) the product beta * x and norm([3, 4]) the product norm * [3, 4]: the parser hands a call without a second argument back as the name and a bracket, so it binds as before, norm(x)^2 being norm * x ^ 2. beta alone and beta (x) with a space are as gamma's are. The beta function is Gamma(a) Gamma(b)/Gamma(a + b), DLMF 5.12.1. It is exact at a whole argument, (n - 1)!/(b (b + 1) ... (b + n - 1)), so beta(1, b) is 1/b, and at two half-whole ones, a rational times pi. It has no value where a or b is 0, -1, -2, ..., unless a + b is one too: the other argument is then whole and the value is the rational function's, beta(2, -3) = 1/6, the limit along a whole n, as mpmath's beta takes it. Where only a + b is one it is 0. Elsewhere it is the ratio of the library's gamma functions at the working precision, real or complex, taken with sixteen more digits and no bound on the exponent, so B(100.5, 100.25) is 1.3e-61 where each gamma is past 1e156. It agrees with mpmath to 1e-58 at ten points, negative, complex and large among them, and with the gamma ratio to 1e-195 at 200 digits. Its arguments are put in one order, beta(b, a) simplifying to the node beta(a, b), and its derivative is B(a, b)(psi(a) - psi(a + b)) in a. It draws as \operatorname{B}, exports to sympy.beta, and compiles in both compilers, through a double-precision ratio of gammas taken through their logarithms past the doubles; MathS.Shaders declines it, as it does x!. The norm is (|v_1|^p + ... + |v_n|^p)^(1/p) for a real p >= 1 and the largest modulus at p = +oo, taken when simplified of a vector written out, a column or a row, real or complex; norm(v, 2) is computed as abs(v), the same expression. A p that is a number below 1, where there is no norm, or off the real line, and a matrix written out that is not a vector, are refused when parsed; a symbolic p stays as written with the condition p >= 1, and is NaN below 1 once given. It draws as \left\| v \right\|_{p} and exports to the SymPy matrix's norm. The node left where the vector is not written out has no compiled form, and Binders.Refusal says to simplify first. Part of #2182. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 413e8fc commit 18d2715

46 files changed

Lines changed: 2585 additions & 1359 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎BREAKING-CHANGES.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,47 @@ even numbers and the negative whole numbers, and at a whole `s >= 2` with a whol
462462
| `"hurwitzzeta(s, a)".ToEntity()` | throws `UnrecognizedFunctionParseException` | `zeta(s, a)` |
463463
| `"zeta (s)".ToEntity()` | `zeta * s` | unchanged |
464464

465+
### The beta function
466+
467+
**Not silent: what was a parse error now parses.** `beta(a, b)` threw `UnhandledParseException`. It is now
468+
`Entity.Betaf`, built by `MathS.Beta`: `Gamma(a) Gamma(b)/Gamma(a + b)` ([DLMF 5.12.1](https://dlmf.nist.gov/5.12.E1)),
469+
exact at a whole argument, `(n - 1)!/(b (b + 1) ... (b + n - 1))`, and at two half-whole ones, a rational times `pi`,
470+
and evaluated to the working precision elsewhere, real or complex. It has no value where `a` or `b` is `0, -1, -2, ...`,
471+
unless `a + b` is one too, where the other argument is whole and the value is that of the rational function, as
472+
mpmath's `beta` takes it. With one argument, `beta(x)` is still the product `beta * x`, and `beta` alone and
473+
`beta (x)` with a space are as they were ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
474+
475+
| Input | Was (2.7.0) | Now |
476+
|---|---|---|
477+
| `"beta(2, 3)".ToEntity().Simplify()` | throws `UnhandledParseException` | `1/12` |
478+
| `"beta(1/2, 1/2)".ToEntity().Simplify()` | throws `UnhandledParseException` | `pi` |
479+
| `"beta(2.5, 1.3)".ToEntity().Evaled` | throws `UnhandledParseException` | `0.2541541901764604945390…` |
480+
| `"beta(1, b)".ToEntity().Simplify()` | throws `UnhandledParseException` | `1 / b` |
481+
| `"beta(2, -3)".ToEntity().Simplify()` | throws `UnhandledParseException` | `1/6` |
482+
| `"beta(0, 1/2)".ToEntity().Simplify()` | throws `UnhandledParseException` | `NaN`, a pole |
483+
| `"beta(x)".ToEntity()` | `beta * x` | unchanged |
484+
485+
### The p-norm of a vector
486+
487+
**Not silent: what was a parse error now parses, or is refused saying why.** `norm(v, p)` threw
488+
`UnhandledParseException`. It is now `Entity.Normf`, built by `MathS.Norm`: `(|v_1|^p + ... + |v_n|^p)^(1/p)` for a
489+
real `p >= 1`, the largest modulus at `p = +oo`, taken when simplified of a vector written out, a column or a row,
490+
whose entries may be complex. `norm(v, 2)` is `abs(v)`, the same expression. A `p` that is a number below `1`, where
491+
there is no norm, and a matrix written out that is not a vector throw `InvalidArgumentParseException`; a symbolic `p`
492+
stays as written, with the condition `p >= 1`. With one argument, `norm(v)` is still the product `norm * v`
493+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
494+
495+
| Input | Was (2.7.0) | Now |
496+
|---|---|---|
497+
| `"norm([3, 4], 1)".ToEntity().Simplify()` | throws `UnhandledParseException` | `7` |
498+
| `"norm([3, 4], 3)".ToEntity().Simplify()` | throws `UnhandledParseException` | `91 ^ (1/3)` |
499+
| `"norm([3, 4], +oo)".ToEntity().Simplify()` | throws `UnhandledParseException` | `4` |
500+
| `"norm([3, 4], 2)".ToEntity().Simplify()` | throws `UnhandledParseException` | `5`, as `abs([3, 4])` |
501+
| `"norm([x, y], p)".ToEntity().Simplify()` | throws `UnhandledParseException` | `norm([x, y], p)` |
502+
| `"norm(v, 1/2)".ToEntity()` | throws `UnhandledParseException` | throws `InvalidArgumentParseException`: `p >= 1` |
503+
| `"norm([[1, 2], [3, 4]], 2)".ToEntity()` | throws `UnhandledParseException` | throws `InvalidArgumentParseException`: not a vector |
504+
| `"norm([3, 4])".ToEntity().Simplify()` | `[3 * norm, 4 * norm]` | unchanged |
505+
465506
### The gradient, the divergence and the curl
466507

467508
**Not silent: what was refused or a product now parses or is refused differently.** `gradient(f, x)`,

‎Sources/.editorconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
268268
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.{Floors,Rounding}.Classes.cs]
269269
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
270270

271+
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.{Beta,Norm}.Classes.cs]
272+
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
273+
274+
[AngouriMath/Core/Entity/Continuous/Number/Beta.cs]
275+
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
276+
271277
[Utils/Utils/CiTiming.cs]
272278
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
273279

@@ -327,6 +333,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
327333
[Tests/UnitTests/Convenience/BinomialTest.cs]
328334
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
329335

336+
[Tests/UnitTests/Convenience/{Beta,Norm}Test.cs]
337+
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
338+
330339
[Tests/UnitTests/Convenience/MaxMinOffTheRealLineTest.cs]
331340
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
332341

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,27 @@ public static Entity VectorLength(Matrix v)
17831783
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
17841784
public static Entity Zeta(Entity s, Entity a) => new Zetaf(s, a);
17851785

1786+
/// <summary>The beta function, <c>B(a, b) = Gamma(a) Gamma(b)/Gamma(a + b)</c> (DLMF 5.12.1)</summary>
1787+
/// <param name="a">The first argument, any complex number but <c>0, -1, -2, ...</c></param>
1788+
/// <param name="b">The second argument, any complex number but <c>0, -1, -2, ...</c></param>
1789+
/// <returns>The <see cref="Entity.Betaf"/> node</returns>
1790+
/// <remarks>
1791+
/// Where one argument is <c>0, -1, -2, ...</c> and so is <c>a + b</c>, the other is a whole number <c>n</c>
1792+
/// and the value is that of <c>(n - 1)!/(b (b + 1) ... (b + n - 1))</c>, so <c>B(1, b) = 1/b</c> wherever
1793+
/// either side has a value. https://github.com/asc-community/AngouriMath/issues/2182
1794+
/// </remarks>
1795+
public static Entity Beta(Entity a, Entity b) => new Betaf(a, b);
1796+
1797+
/// <summary>
1798+
/// The p-norm of a vector, <c>(|v_1|^p + ... + |v_n|^p)^(1/p)</c> for <c>p &gt;= 1</c>, the largest modulus
1799+
/// at <c>p = +oo</c>; the 2-norm is <see cref="Abs(Entity)"/> of the vector
1800+
/// </summary>
1801+
/// <param name="v">The vector, a column or a row</param>
1802+
/// <param name="p">The order, a real number at least <c>1</c>, or <c>+oo</c></param>
1803+
/// <returns>The <see cref="Entity.Normf"/> node</returns>
1804+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
1805+
public static Entity Norm(Entity v, Entity p) => new Normf(v, p);
1806+
17861807
/// <summary>The upper incomplete gamma function, <c>Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt</c></summary>
17871808
/// <param name="parameter">The parameter <c>a</c>, any complex number</param>
17881809
/// <param name="lowerLimit">The lower limit <c>z</c>, any complex number; <c>0</c> only where <c>Re a > 0</c></param>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,10 @@ atom returns[Entity value]
685685
| 'zeta(' args = function_arguments ')' { if (Assert("zeta", (1, 2), $args.list.Count)) $value = MathS.Zeta($args.list[0]); else $value = MathS.Zeta($args.list[0], $args.list[1]); }
686686
| 'riemannzeta(' args = function_arguments ')' { Assert("riemannzeta", 1, $args.list.Count); $value = MathS.Zeta($args.list[0]); }
687687
| 'hurwitzzeta(' args = function_arguments ')' { Assert("hurwitzzeta", 2, $args.list.Count); $value = MathS.Zeta($args.list[0], $args.list[1]); }
688+
/* The beta function B(a, b), named by its symbol as gamma is. A call with one argument is not
689+
this rule's: Parser.cs hands it back as the name and a bracket, so beta(x) is still beta * x.
690+
https://github.com/asc-community/AngouriMath/issues/2182 */
691+
| 'beta(' args = function_arguments ')' { Assert("beta", 2, $args.list.Count); $value = MathS.Beta($args.list[0], $args.list[1]); }
688692
/* The trigonometric and hyperbolic integrals. https://github.com/asc-community/AngouriMath/issues/1501 */
689693
| 'Si(' args = function_arguments ')' { Assert("Si", 1, $args.list.Count); $value = MathS.Si($args.list[0]); }
690694
| 'Ci(' args = function_arguments ')' { Assert("Ci", 1, $args.list.Count); $value = MathS.Ci($args.list[0]); }
@@ -707,6 +711,9 @@ atom returns[Entity value]
707711
| 'gradient(' args = function_arguments ')' { $value = ParsingHelpers.Gradient($args.list); }
708712
| 'divergence(' args = function_arguments ')' { $value = ParsingHelpers.Divergence($args.list); }
709713
| 'curl(' args = function_arguments ')' { $value = ParsingHelpers.Curl($args.list); }
714+
/* The p-norm of a vector. With one argument it is still the product Parser.cs leaves it as,
715+
norm * v. https://github.com/asc-community/AngouriMath/issues/2182 */
716+
| 'norm(' args = function_arguments ')' { $value = ParsingHelpers.Norm($args.list); }
710717
| 'resultant(' args = function_arguments ')' { Assert("resultant", 3, $args.list.Count); $value = ParsingHelpers.Resultant($args.list[0], $args.list[1], $args.list[2]); }
711718
/* The complex conjugate, by its name and by the shorter one other systems use.
712719
https://github.com/asc-community/AngouriMath/issues/2145 */

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

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

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

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ T__237=238
239239
T__238=239
240240
T__239=240
241241
T__240=241
242-
NEWLINE=242
243-
NUMBER=243
244-
SPECIALSET=244
245-
BOOLEAN=245
246-
NAN=246
247-
UNICODE_SYNONYM=247
248-
SUPERSCRIPT=248
249-
VARIABLE=249
250-
COMMENT=250
251-
WS=251
242+
T__241=242
243+
T__242=243
244+
NEWLINE=244
245+
NUMBER=245
246+
SPECIALSET=246
247+
BOOLEAN=247
248+
NAN=248
249+
UNICODE_SYNONYM=249
250+
SUPERSCRIPT=250
251+
VARIABLE=251
252+
COMMENT=252
253+
WS=253
252254
'['=1
253255
']'=2
254256
'!'=3
@@ -445,49 +447,51 @@ WS=251
445447
'zeta('=194
446448
'riemannzeta('=195
447449
'hurwitzzeta('=196
448-
'Si('=197
449-
'Ci('=198
450-
'Shi('=199
451-
'Chi('=200
452-
'det('=201
453-
'transpose('=202
454-
'trace('=203
455-
'dot('=204
456-
'cross('=205
457-
'laplacian('=206
458-
'forwarddifference('=207
459-
'jacobian('=208
460-
'hessian('=209
461-
'gradient('=210
462-
'divergence('=211
463-
'curl('=212
464-
'resultant('=213
465-
'conjugate('=214
466-
'conj('=215
467-
'arg('=216
468-
'Re('=217
469-
'Im('=218
470-
're('=219
471-
'real('=220
472-
'im('=221
473-
'imag('=222
474-
'trunc('=223
475-
'heaviside('=224
476-
'unitstep('=225
477-
'isprime('=226
478-
'pochhammer('=227
479-
'doublefactorial('=228
480-
'factorial2('=229
481-
'fractionalpart('=230
482-
'quotient('=231
483-
'factorize('=232
484-
'nextprime('=233
485-
'divisors('=234
486-
'φ('=235
487-
'ϕ('=236
488-
'domain('=237
489-
'piecewise('=238
490-
'apply('=239
491-
'lambda('=240
492-
'partialderivative('=241
493-
'NaN'=246
450+
'beta('=197
451+
'Si('=198
452+
'Ci('=199
453+
'Shi('=200
454+
'Chi('=201
455+
'det('=202
456+
'transpose('=203
457+
'trace('=204
458+
'dot('=205
459+
'cross('=206
460+
'laplacian('=207
461+
'forwarddifference('=208
462+
'jacobian('=209
463+
'hessian('=210
464+
'gradient('=211
465+
'divergence('=212
466+
'curl('=213
467+
'norm('=214
468+
'resultant('=215
469+
'conjugate('=216
470+
'conj('=217
471+
'arg('=218
472+
'Re('=219
473+
'Im('=220
474+
're('=221
475+
'real('=222
476+
'im('=223
477+
'imag('=224
478+
'trunc('=225
479+
'heaviside('=226
480+
'unitstep('=227
481+
'isprime('=228
482+
'pochhammer('=229
483+
'doublefactorial('=230
484+
'factorial2('=231
485+
'fractionalpart('=232
486+
'quotient('=233
487+
'factorize('=234
488+
'nextprime('=235
489+
'divisors('=236
490+
'φ('=237
491+
'ϕ('=238
492+
'domain('=239
493+
'piecewise('=240
494+
'apply('=241
495+
'lambda('=242
496+
'partialderivative('=243
497+
'NaN'=248

0 commit comments

Comments
 (0)