Skip to content

Commit 282157a

Browse files
The harmonic numbers (#2200)
harmonicnumber(n) and harmonicnumber(n, m) were reserved by name in #2184 and are now Entity.HarmonicNumberf, built by MathS.HarmonicNumber. For a whole n >= 0 they are 1 + 1/2^m + ... + 1/n^m, summed exactly for a rational order: harmonicnumber(4) is 25/12 and harmonicnumber(4, 2) is 205/144. Elsewhere they are zeta(m) - zeta(m, n + 1), as Mathematica's HarmonicNumber is, which is evaluated numerically for a whole m >= 1 through the polygamma function: psi(n + 1) - psi(1) at m = 1, and (-1)^m / (m - 1)! (psi^(m - 1)(1) - psi^(m - 1)(n + 1)) above it. harmonicnumber(1/2) is 2 - 2 ln 2 and agrees with mpmath to 1e-25, as do H(5/2), H(-1/2) and H(1/2, 2). A negative integer n is a pole of the Hurwitz zeta function, and has no value but for a whole order m <= 0, where the harmonic number is a polynomial in n. What is not computed is left as the node. They print as harmonicnumber(n) and harmonicnumber(n, m), draw as \operatorname{H}_{n} and \operatorname{H}_{n}^{(m)}, and export to SymPy's harmonic. 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 533ee4f commit 282157a

29 files changed

Lines changed: 621 additions & 365 deletions

‎BREAKING-CHANGES.md‎

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

501501
### The Möbius, divisor and prime-counting functions
502502

@@ -532,6 +532,19 @@ is ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
532532
| `"hessian(x^3 y^2, [x, y])".ToEntity().Simplify()` | throws `UnhandledParseException` | `[[6 * x * y ^ 2, 6 * x ^ 2 * y], [6 * x ^ 2 * y, 2 * x ^ 3]]` |
533533
| `"jacobian(x y, {x, y})".ToEntity()` | throws `UnhandledParseException` | throws `InvalidArgumentParseException`: a set has no order for the columns |
534534

535+
### The harmonic numbers
536+
537+
**Not silent: `harmonicnumber(n)` and `harmonicnumber(n, m)` are the harmonic numbers, where the one was a product and the
538+
other a parse error.** They are `1 + 1/2^m + ... + 1/n^m` for a whole `n >= 0`, exact for a rational `m`, and
539+
`ζ(m) - ζ(m, n + 1)` elsewhere, as Mathematica's `HarmonicNumber` is: `ψ(n + 1) - ψ(1)` for `m = 1`, evaluated numerically
540+
through `digamma` and `polygamma` for a whole `m >= 1`. A negative integer `n` has no value but for a whole `m <= 0`
541+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
542+
543+
| Input | Was (2.6.0) | Now |
544+
|---|---|---|
545+
| `"harmonicnumber(4)".ToEntity().Simplify()` | `4 * harmonicnumber` | `25/12` |
546+
| `"harmonicnumber(n, 2)".ToEntity()` | throws `UnhandledParseException` | `harmonicnumber(n, 2)` |
547+
535548
### The Catalan numbers
536549

537550
**Not silent: `catalannumber(n)` is the Catalan number, where it was a product.** It is `binomial(2n, n) / (n + 1)` for

‎Sources/.editorconfig‎

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

830830
[Tests/UnitTests/Convenience/CatalanNumberTest.cs]
831831
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
832+
833+
[Tests/UnitTests/Convenience/HarmonicNumberTest.cs]
834+
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/Convenience/MathS.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,14 @@ public static Entity VectorLength(Matrix v)
13871387
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13881388
public static Entity Binomial(Entity n, Entity k) => new Binomialf(n, k);
13891389

1390+
/// <summary>The harmonic number <c>H_n = 1 + 1/2 + ... + 1/n</c></summary>
1391+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
1392+
public static Entity HarmonicNumber(Entity n) => new HarmonicNumberf(n, Integer.One);
1393+
1394+
/// <summary>The generalized harmonic number <c>H_n^(m) = 1 + 1/2^m + ... + 1/n^m</c></summary>
1395+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
1396+
public static Entity HarmonicNumber(Entity n, Entity m) => new HarmonicNumberf(n, m);
1397+
13901398
/// <summary>The Catalan number <c>C_n = binomial(2n, n) / (n + 1)</c></summary>
13911399
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
13921400
public static Entity CatalanNumber(Entity n) => new CatalanNumberf(n);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ atom returns[Entity value]
627627
| 'binomial(' args = function_arguments ')' { Assert("binomial", 2, $args.list.Count); $value = MathS.Binomial($args.list[0], $args.list[1]); }
628628
| 'multinomial(' args = function_arguments ')' { $value = ParsingHelpers.Multinomial($args.list); }
629629
| 'catalannumber(' args = function_arguments ')' { Assert("catalannumber", 1, $args.list.Count); $value = MathS.CatalanNumber($args.list[0]); }
630+
| 'harmonicnumber(' args = function_arguments ')' { if (Assert("harmonicnumber", (2, 1), $args.list.Count)) $value = MathS.HarmonicNumber($args.list[0], $args.list[1]); else $value = MathS.HarmonicNumber($args.list[0]); }
630631
| 'fallingfactorial(' args = function_arguments ')' { Assert("fallingfactorial", 2, $args.list.Count); $value = MathS.FallingFactorial($args.list[0], $args.list[1]); }
631632
| 'factorialpower(' args = function_arguments ')' { Assert("factorialpower", 2, $args.list.Count); $value = MathS.FallingFactorial($args.list[0], $args.list[1]); }
632633
| '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
| 'hurwitzzeta(' args = function_arguments ')' { $value = NotImplementedFunction("hurwitzzeta", "Hurwitz zeta function yet"); }
696697
| 'lambertw(' args = function_arguments ')' { $value = NotImplementedFunction("lambertw", "Lambert W function yet"); }
697698
| 'productlog(' args = function_arguments ')' { $value = NotImplementedFunction("productlog", "Lambert W function yet"); }
698-
| 'harmonicnumber(' args = function_arguments ')' { $value = NotImplementedFunction("harmonicnumber", "harmonic numbers 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
@@ -176,6 +176,7 @@ null
176176
'binomial('
177177
'multinomial('
178178
'catalannumber('
179+
'harmonicnumber('
179180
'fallingfactorial('
180181
'factorialpower('
181182
'risingfactorial('
@@ -215,7 +216,6 @@ null
215216
'hurwitzzeta('
216217
'lambertw('
217218
'productlog('
218-
'harmonicnumber('
219219
'forwarddifference('
220220
'heaviside('
221221
'unitstep('

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

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

0 commit comments

Comments
 (0)