Skip to content

Commit 0348dfc

Browse files
Rafael-SOWNetclaude
andcommitted
The Möbius, divisor and prime-counting functions
mobius(n), divisorsigma(k, n), primenu(n), primeomega(n) and primepi(x) were reserved by name in built by MathS.NumberTheory.Mobius, DivisorSigma, PrimeNu, PrimeOmega and PrimePi. mobius is the Möbius function: 0 where the square of a prime divides n, (-1)^k for a product of k distinct primes, and 1 at 1. moebiusmu, Mathematica's name for it, reads as the same function. divisorsigma(k, n) is sigma_k(n), the order first as the notation and Mathematica's DivisorSigma[k, n] write it. It is the product over the prime powers p^a in n of 1 + p^k + ... + p^(ak), exact for a rational order and an expression for any other number: divisorsigma(1/2, 4) is 3 + sqrt(2). primenu and primeomega are omega and Omega, the number of prime factors without and with multiplicity. The four are defined on the positive integers, NaN at any other number, and left as written for an expression. The factorisation is trial division up to a million. That settles every number up to 10^12, and a larger one whose part left after the small primes is below 10^12; anything else is left as written, and 2^100 is factorised at once. primepi(x) counts the primes up to floor(x) by a sieve over the odd numbers, for a real x up to ten million (94 ms there), and is NaN off the real line. They print by name, draw as \operatorname{\mu}(n), \operatorname{\sigma}_k(n), \operatorname{\omega}(n), \operatorname{\Omega}(n) and \operatorname{\pi}(x), so that a Greek letter is not read back as a variable times a bracket, and export to SymPy's mobius, divisor_sigma(n, k), primenu, primeomega and primepi. Their codomains are the integers, and the non-negative integers for the three counts. The MCP server's syntax document and the syntax page name them, and the native smoke test evaluates them. 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 5e7731f commit 0348dfc

32 files changed

Lines changed: 1339 additions & 523 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ the right shape, written out or once its operations are carried out, throws `Inv
440440
**Not silent: these names followed by a bracket now throw `UnrecognizedFunctionParseException` where they were read as
441441
products.** An unknown name followed by a bracket is a product, so `zeta(2)` was `zeta * 2` and simplified to
442442
`2 * zeta`. The long names of the functions the library is to have are refused until each is implemented, as `trunc`
443-
is: `zeta`, `riemannzeta`, `hurwitzzeta`, `lambertw`, `productlog`, `mobius`, `moebiusmu`, `divisorsigma`, `primenu`,
444-
`primeomega`, `primepi`, `harmonicnumber`, `catalannumber`, `multinomial`, `fallingfactorial`, `factorialpower`,
443+
is: `zeta`, `riemannzeta`, `hurwitzzeta`, `lambertw`, `productlog`, `harmonicnumber`, `catalannumber`, `multinomial`, `fallingfactorial`, `factorialpower`,
445444
`risingfactorial`, `forwarddifference`, `laplacian`, `jacobian` and `hessian`. Names that will not be functions,
446445
because what they mean differs between sources or is already written exactly, are refused with the spelling to use:
447446
`heaviside`, `unitstep`, `isprime`, `pochhammer`, `doublefactorial`, `factorial2`, `fractionalpart` and `quotient`.
@@ -454,7 +453,24 @@ Each name without a bracket is still a variable, and short names such as `beta`
454453
| `"zeta(2)".ToEntity().Simplify()` | `2 * zeta` | throws `UnrecognizedFunctionParseException` |
455454
| `"isprime(7)".ToEntity()` | `isprime * 7` | throws `UnrecognizedFunctionParseException`: write `n in PP` |
456455
| `"heaviside(x)".ToEntity()` | `heaviside * x` | throws `UnrecognizedFunctionParseException`: write `iverson(x > 0)`, `iverson(x >= 0)` or `(1 + sgn(x))/2` |
457-
| `"divisorsigma(k, n)".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
456+
| `"multinomial(a, b, c)".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
457+
458+
### The Möbius, divisor and prime-counting functions
459+
460+
**Not silent: `mobius(n)`, `divisorsigma(k, n)`, `primenu(n)`, `primeomega(n)` and `primepi(x)` are now functions, where
461+
they were read as products or, with two arguments, were a parse error.** `mobius` (also `moebiusmu`) is the Möbius
462+
function μ, `divisorsigma(k, n)` the sum of the `k`-th powers of the divisors of `n`, and `primenu` and `primeomega` the
463+
number of prime factors without and with multiplicity, ω and Ω. Each is defined on the positive integers and is `NaN`
464+
elsewhere. `primepi(x)` counts the primes at most the real number `x`
465+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
466+
467+
| Input | Was (2.6.0) | Now |
468+
|---|---|---|
469+
| `"mobius(6)".ToEntity().Simplify()` | `6 * mobius` | `1` |
470+
| `"divisorsigma(1, 6)".ToEntity().Simplify()` | throws `UnhandledParseException` | `12` |
471+
| `"primeomega(12)".ToEntity().Simplify()` | `12 * primeomega` | `3` |
472+
| `"primepi(10)".ToEntity().Evaled` | `primepi * 10` | `4` |
473+
| `"mobius(0)".ToEntity().Simplify()` | `0` | `NaN` |
458474
### A subscript may have a subscript
459475

460476
**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
@@ -805,3 +805,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
805805

806806
[Tests/UnitTests/Common/SimplifyOverASymbolicCommonDenominatorTest.cs]
807807
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
808+
809+
[Tests/UnitTests/Convenience/NumberTheoryFunctionsTest.cs]
810+
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ public static class NumberTheory
5050
/// </example>
5151
public static Entity Phi(Entity integer) => new Phif(integer);
5252

53+
/// <summary>The Möbius function <c>μ(n)</c> of a positive integer</summary>
54+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
55+
public static Entity Mobius(Entity n) => new Mobiusf(n);
56+
57+
/// <summary>The divisor function <c>σ_k(n)</c>: the sum of the <c>k</c>-th powers of the divisors of <c>n</c></summary>
58+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
59+
public static Entity DivisorSigma(Entity order, Entity n) => new DivisorSigmaf(order, n);
60+
61+
/// <summary>The number of distinct prime factors <c>ω(n)</c> of a positive integer</summary>
62+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
63+
public static Entity PrimeNu(Entity n) => new PrimeNuf(n);
64+
65+
/// <summary>The number of prime factors <c>Ω(n)</c> of a positive integer, with multiplicity</summary>
66+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
67+
public static Entity PrimeOmega(Entity n) => new PrimeOmegaf(n);
68+
69+
/// <summary>The prime-counting function <c>π(x)</c>: how many primes are at most <c>x</c></summary>
70+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2182</remarks>
71+
public static Entity PrimePi(Entity x) => new PrimePif(x);
72+
5373
/// <summary>
5474
/// The <c>n</c>-th prime, <c>prime(n)</c>: <c>prime(1)</c> is <c>2</c> and
5575
/// <c>prime(25)</c> is <c>97</c>. Defined on the positive whole numbers, and a

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,12 @@ atom returns[Entity value]
595595
| 'phi(' args = function_arguments ')' { Assert("phi", 1, $args.list.Count); $value = MathS.NumberTheory.Phi($args.list[0]); }
596596
| 'prime(' args = function_arguments ')' { Assert("prime", 1, $args.list.Count); $value = MathS.NumberTheory.Prime($args.list[0]); }
597597
| 'valuation(' args = function_arguments ')' { Assert("valuation", 2, $args.list.Count); $value = MathS.NumberTheory.Valuation($args.list[0], $args.list[1]); }
598+
| 'mobius(' args = function_arguments ')' { Assert("mobius", 1, $args.list.Count); $value = MathS.NumberTheory.Mobius($args.list[0]); }
599+
| 'moebiusmu(' args = function_arguments ')' { Assert("moebiusmu", 1, $args.list.Count); $value = MathS.NumberTheory.Mobius($args.list[0]); }
600+
| 'divisorsigma(' args = function_arguments ')' { Assert("divisorsigma", 2, $args.list.Count); $value = MathS.NumberTheory.DivisorSigma($args.list[0], $args.list[1]); }
601+
| 'primenu(' args = function_arguments ')' { Assert("primenu", 1, $args.list.Count); $value = MathS.NumberTheory.PrimeNu($args.list[0]); }
602+
| 'primeomega(' args = function_arguments ')' { Assert("primeomega", 1, $args.list.Count); $value = MathS.NumberTheory.PrimeOmega($args.list[0]); }
603+
| 'primepi(' args = function_arguments ')' { Assert("primepi", 1, $args.list.Count); $value = MathS.NumberTheory.PrimePi($args.list[0]); }
598604
| 'card(' args = function_arguments ')' { Assert("card", 1, $args.list.Count); $value = MathS.Sets.Card($args.list[0]); }
599605
| 'powerset(' args = function_arguments ')' { Assert("powerset", 1, $args.list.Count); $value = MathS.Sets.PowerSet($args.list[0]); }
600606
| 'union(' args = function_arguments ')' { Assert("union", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } unionRange ? MathS.Sets.IndexedUnion($args.list[0], unionRange.Element, unionRange.SupSet) : throw new InvalidArgumentParseException("union expects its second argument to say which name ranges over which set, as in union(A_i, i in I)"); }
@@ -681,12 +687,6 @@ atom returns[Entity value]
681687
| 'hurwitzzeta(' args = function_arguments ')' { $value = NotImplementedFunction("hurwitzzeta", "Hurwitz zeta function yet"); }
682688
| 'lambertw(' args = function_arguments ')' { $value = NotImplementedFunction("lambertw", "Lambert W function yet"); }
683689
| 'productlog(' args = function_arguments ')' { $value = NotImplementedFunction("productlog", "Lambert W function yet"); }
684-
| 'mobius(' args = function_arguments ')' { $value = NotImplementedFunction("mobius", "Mobius function yet"); }
685-
| 'moebiusmu(' args = function_arguments ')' { $value = NotImplementedFunction("moebiusmu", "Mobius function yet"); }
686-
| 'divisorsigma(' args = function_arguments ')' { $value = NotImplementedFunction("divisorsigma", "divisor function yet"); }
687-
| 'primenu(' args = function_arguments ')' { $value = NotImplementedFunction("primenu", "count of distinct prime factors yet"); }
688-
| 'primeomega(' args = function_arguments ')' { $value = NotImplementedFunction("primeomega", "count of prime factors with multiplicity yet"); }
689-
| 'primepi(' args = function_arguments ')' { $value = NotImplementedFunction("primepi", "prime-counting function yet"); }
690690
| 'harmonicnumber(' args = function_arguments ')' { $value = NotImplementedFunction("harmonicnumber", "harmonic numbers yet"); }
691691
| 'catalannumber(' args = function_arguments ')' { $value = NotImplementedFunction("catalannumber", "Catalan numbers yet"); }
692692
| 'multinomial(' args = function_arguments ')' { $value = NotImplementedFunction("multinomial", "multinomial coefficients yet"); }

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ null
150150
'phi('
151151
'prime('
152152
'valuation('
153+
'mobius('
154+
'moebiusmu('
155+
'divisorsigma('
156+
'primenu('
157+
'primeomega('
158+
'primepi('
153159
'card('
154160
'powerset('
155161
'union('
@@ -201,12 +207,6 @@ null
201207
'hurwitzzeta('
202208
'lambertw('
203209
'productlog('
204-
'mobius('
205-
'moebiusmu('
206-
'divisorsigma('
207-
'primenu('
208-
'primeomega('
209-
'primepi('
210210
'harmonicnumber('
211211
'catalannumber('
212212
'multinomial('

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

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -385,63 +385,63 @@ WS=237
385385
'phi('=148
386386
'prime('=149
387387
'valuation('=150
388-
'card('=151
389-
'powerset('=152
390-
'union('=153
391-
'intersection('=154
392-
'complement('=155
393-
'image('=156
394-
'preimage('=157
395-
'floor('=158
396-
'ceil('=159
397-
'ceiling('=160
398-
'round('=161
399-
'min('=162
400-
'max('=163
401-
'argmax('=164
402-
'argmin('=165
403-
'gcd('=166
404-
'lcm('=167
405-
'binomial('=168
406-
'iverson('=169
407-
'aleph('=170
408-
'erf('=171
409-
'erfc('=172
410-
'erfi('=173
411-
'Ei('=174
412-
'li('=175
413-
'polygamma('=176
414-
'digamma('=177
415-
'Si('=178
416-
'Ci('=179
417-
'Shi('=180
418-
'Chi('=181
419-
'det('=182
420-
'transpose('=183
421-
'dot('=184
422-
'cross('=185
423-
'resultant('=186
424-
'conjugate('=187
425-
'conj('=188
426-
'arg('=189
427-
'Re('=190
428-
'Im('=191
429-
're('=192
430-
'real('=193
431-
'im('=194
432-
'imag('=195
433-
'trunc('=196
434-
'zeta('=197
435-
'riemannzeta('=198
436-
'hurwitzzeta('=199
437-
'lambertw('=200
438-
'productlog('=201
439-
'mobius('=202
440-
'moebiusmu('=203
441-
'divisorsigma('=204
442-
'primenu('=205
443-
'primeomega('=206
444-
'primepi('=207
388+
'mobius('=151
389+
'moebiusmu('=152
390+
'divisorsigma('=153
391+
'primenu('=154
392+
'primeomega('=155
393+
'primepi('=156
394+
'card('=157
395+
'powerset('=158
396+
'union('=159
397+
'intersection('=160
398+
'complement('=161
399+
'image('=162
400+
'preimage('=163
401+
'floor('=164
402+
'ceil('=165
403+
'ceiling('=166
404+
'round('=167
405+
'min('=168
406+
'max('=169
407+
'argmax('=170
408+
'argmin('=171
409+
'gcd('=172
410+
'lcm('=173
411+
'binomial('=174
412+
'iverson('=175
413+
'aleph('=176
414+
'erf('=177
415+
'erfc('=178
416+
'erfi('=179
417+
'Ei('=180
418+
'li('=181
419+
'polygamma('=182
420+
'digamma('=183
421+
'Si('=184
422+
'Ci('=185
423+
'Shi('=186
424+
'Chi('=187
425+
'det('=188
426+
'transpose('=189
427+
'dot('=190
428+
'cross('=191
429+
'resultant('=192
430+
'conjugate('=193
431+
'conj('=194
432+
'arg('=195
433+
'Re('=196
434+
'Im('=197
435+
're('=198
436+
'real('=199
437+
'im('=200
438+
'imag('=201
439+
'trunc('=202
440+
'zeta('=203
441+
'riemannzeta('=204
442+
'hurwitzzeta('=205
443+
'lambertw('=206
444+
'productlog('=207
445445
'harmonicnumber('=208
446446
'catalannumber('=209
447447
'multinomial('=210

0 commit comments

Comments
 (0)