Skip to content

Commit 4e10913

Browse files
Rafael-SOWNetclaude
andcommitted
The prime factorisation of an integer: factorization(n), and primefactors(n)
factorization(n) is the map from each prime to its exponent in n, mathlib's Nat.factorization: a node of its own, PrimeFactorizationf, that stays as written at a positive integer and holds the map in Factors, computed once by the factoriser MathS.NumberTheory.Factorize uses. It is not the held product 2^3 * 3^2 * 5, which evaluates to 360, and not the number, so factorization(360) = 360 is left undecided. primefactors(n), the set node PrimeFactorsf, is its support and lists { 2, 3, 5 }. With valuation (#1453) and the product over a set (#2467), product(p ^ valuation(n, p), p in primefactors(n)) is n, measured at 1, a prime, 360, 2^64 + 1 and 3^40 * 7^3. Both are NaN at a number that is not a positive integer, as primenu and the other functions of a factorisation are: 0 has none and a negative integer is -1 times one (#2466 refuses both in Factorize). A symbol, or an integer with a factor neither proven prime nor split, is left as written. Codomain Any, as a matrix, a transpose and a set have, being no number. Printed factorization(360), and 2^{3} \cdot 3^{2} \cdot 5 in LaTeX once the map is known (1 for factorization(1)); primefactors(n) prints as written. SymPy: sympy.factorint(n), the dict that is the map, and FiniteSet(*primefactors(n)); refused for an argument with a variable, where SymPy raises. Both compilers refuse each, naming it, since evaluating gives no number to compile. factorize(360) is still refused, and its message now names factorization(n). Grammar: two rules, factorization( and primefactors(, after valuation(; the factorize( action's message. The unmodified grammar regenerated to an empty diff first. Part of #2449 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 73b77bf commit 4e10913

33 files changed

Lines changed: 2174 additions & 1499 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ are as they were. With `Simplify`, the case `y = 0` of:
9494
| `integral(1/(t^2+y^2), t, -oo, +oo)` | `0 provided (y = 0)` | `+oo provided (y = 0)` |
9595
| `integral(1/(t^2+y^2), t, 1, 2)` | `(1/2) provided (y = 0)` | unchanged |
9696

97+
### `factorization(n)` and `primefactors(n)` are functions
98+
99+
**Not silent: what was a product is now a node.** `factorization(n)` and `primefactors(n)` were read as the products
100+
`factorization * n` and `primefactors * n`. `factorization(n)` is now the prime factorisation of `n`, the map from each
101+
prime to its exponent (mathlib's `Nat.factorization`): a node that keeps the map, printed `factorization(360)` and
102+
`2^{3} \cdot 3^{2} \cdot 5` in LaTeX, and not the number, so `factorization(360) = 360` is left undecided.
103+
`primefactors(n)` is its support, the set of the primes dividing `n`. Both are defined on the positive integers and
104+
`NaN` at any other number, as `primenu` is: 0 has no factorisation and a negative integer is `-1` times one. With
105+
`valuation(n, p)` and the product over a set, `product(p ^ valuation(n, p), p in primefactors(n))` is `n`
106+
([#2449](https://github.com/asc-community/AngouriMath/issues/2449)). `factorize(360)` is still refused, and its message
107+
now names `factorization(n)`.
108+
109+
| Input | Was (2.7.0) | Now |
110+
|---|---|---|
111+
| `"factorization(360)".ToEntity().Evaled` | `factorization * 360` | `factorization(360)`, holding `2 ↦ 3, 3 ↦ 2, 5 ↦ 1` |
112+
| `"factorization(0)".ToEntity().Evaled` | `0` | `NaN` |
113+
| `"primefactors(360)".ToEntity().Evaled` | `primefactors * 360` | `{ 2, 3, 5 }` |
114+
| `"product(p ^ valuation(360, p), p in primefactors(360))".ToEntity().Evaled` | throws `FunctionArgumentCountException` | `360` |
115+
97116
### `factorize`, `nextprime`, `divisors`, `gradient`, `divergence` and `φ` are refused by name
98117

99118
**Not silent: these names followed by a bracket now throw `UnrecognizedFunctionParseException` where they were read as

‎Sources/.editorconfig‎

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

11301130
[Tests/UnitTests/Convenience/ShadersTest.cs]
11311131
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
1132+
1133+
[Tests/UnitTests/Convenience/PrimeFactorizationNodeTest.cs]
1134+
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,44 @@ public static class NumberTheory
104104
/// </example>
105105
public static Entity Valuation(Entity argument, Entity prime) => new Valuationf(argument, prime);
106106

107+
/// <summary>
108+
/// The prime factorisation of a positive integer, <c>factorization(n)</c>: the map from
109+
/// each prime to its exponent in <c>n</c>, mathlib's <c>Nat.factorization</c>. A node of
110+
/// its own, which holds the map in <see cref="Entity.PrimeFactorizationf.Factors"/> and
111+
/// is not the number <c>n</c>; <c>NaN</c> at any other number. For the list of pairs
112+
/// alone, <see cref="Factorize(Integer)"/>.
113+
/// https://github.com/asc-community/AngouriMath/issues/2449
114+
/// </summary>
115+
/// <example>
116+
/// <code>
117+
/// var factorization = (Entity.PrimeFactorizationf)MathS.NumberTheory.Factorization(360).Evaled;
118+
/// Console.WriteLine(factorization);
119+
/// Console.WriteLine(factorization.Latexize());
120+
/// </code>
121+
/// Prints
122+
/// <code>
123+
/// factorization(360)
124+
/// 2^{3} \cdot 3^{2} \cdot 5
125+
/// </code>
126+
/// </example>
127+
public static Entity Factorization(Entity integer) => new PrimeFactorizationf(integer);
128+
129+
/// <summary>
130+
/// The set of the primes dividing a positive integer, <c>primefactors(n)</c>, the support
131+
/// of <see cref="Factorization(Entity)"/>; <c>NaN</c> at any other number.
132+
/// https://github.com/asc-community/AngouriMath/issues/2449
133+
/// </summary>
134+
/// <example>
135+
/// <code>
136+
/// Console.WriteLine(MathS.NumberTheory.PrimeFactors(360).Evaled);
137+
/// </code>
138+
/// Prints
139+
/// <code>
140+
/// { 2, 3, 5 }
141+
/// </code>
142+
/// </example>
143+
public static Set PrimeFactors(Entity integer) => new Set.PrimeFactorsf(integer);
144+
107145
/// <summary>
108146
/// The statement that <paramref name="divisor"/> divides <paramref name="dividend"/>,
109147
/// written <c>a divides b</c>: for integers, that <c>b mod a = 0</c>.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ atom returns[Entity value]
625625
| 'phi(' args = function_arguments ')' { Assert("phi", 1, $args.list.Count); $value = MathS.NumberTheory.Phi($args.list[0]); }
626626
| 'prime(' args = function_arguments ')' { Assert("prime", 1, $args.list.Count); $value = MathS.NumberTheory.Prime($args.list[0]); }
627627
| 'valuation(' args = function_arguments ')' { Assert("valuation", 2, $args.list.Count); $value = MathS.NumberTheory.Valuation($args.list[0], $args.list[1]); }
628+
| 'factorization(' args = function_arguments ')' { Assert("factorization", 1, $args.list.Count); $value = MathS.NumberTheory.Factorization($args.list[0]); }
629+
| 'primefactors(' args = function_arguments ')' { Assert("primefactors", 1, $args.list.Count); $value = MathS.NumberTheory.PrimeFactors($args.list[0]); }
628630
| 'mobius(' args = function_arguments ')' { Assert("mobius", 1, $args.list.Count); $value = MathS.NumberTheory.Mobius($args.list[0]); }
629631
| 'moebiusmu(' args = function_arguments ')' { Assert("moebiusmu", 1, $args.list.Count); $value = MathS.NumberTheory.Mobius($args.list[0]); }
630632
| 'divisorsigma(' args = function_arguments ')' { Assert("divisorsigma", 2, $args.list.Count); $value = MathS.NumberTheory.DivisorSigma($args.list[0], $args.list[1]); }
@@ -743,7 +745,7 @@ atom returns[Entity value]
743745
with what can be written today: factorize(360) was factorize * 360, and φ(10) was φ * 10.
744746
factorize, nextprime and divisors: https://github.com/asc-community/AngouriMath/issues/2449.
745747
gradient and divergence: https://github.com/asc-community/AngouriMath/issues/2182 */
746-
| 'factorize(' args = function_arguments ')' { $value = NotImplementedFunction("factorize", "factorization node yet; in C#, MathS.NumberTheory.Factorize(n) gives the prime factors of an integer and Factorize() factors an expression"); }
748+
| 'factorize(' args = function_arguments ')' { $value = NotAFunctionHere("factorize", "the prime factorisation of an integer is factorization(n), and in C# Factorize() factors an expression"); }
747749
| 'nextprime(' args = function_arguments ')' { $value = NotImplementedFunction("nextprime", "nextprime yet; prime(primepi(n) + 1) is the least prime greater than n"); }
748750
| 'divisors(' args = function_arguments ')' { $value = NotImplementedFunction("divisors", "set of divisors yet; divisorsigma(0, n) counts them and divisorsigma(1, n) is their sum"); }
749751
| 'gradient(' args = function_arguments ')' { $value = NotImplementedFunction("gradient", "gradient yet; jacobian([f], [x, y]) is the row of partial derivatives of f"); }

‎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: 102 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,18 @@ T__233=234
235235
T__234=235
236236
T__235=236
237237
T__236=237
238-
NEWLINE=238
239-
NUMBER=239
240-
SPECIALSET=240
241-
BOOLEAN=241
242-
NAN=242
243-
UNICODE_SYNONYM=243
244-
SUPERSCRIPT=244
245-
VARIABLE=245
246-
COMMENT=246
247-
WS=247
238+
T__237=238
239+
T__238=239
240+
NEWLINE=240
241+
NUMBER=241
242+
SPECIALSET=242
243+
BOOLEAN=243
244+
NAN=244
245+
UNICODE_SYNONYM=245
246+
SUPERSCRIPT=246
247+
VARIABLE=247
248+
COMMENT=248
249+
WS=249
248250
'['=1
249251
']'=2
250252
'!'=3
@@ -395,91 +397,93 @@ WS=247
395397
'phi('=148
396398
'prime('=149
397399
'valuation('=150
398-
'mobius('=151
399-
'moebiusmu('=152
400-
'divisorsigma('=153
401-
'primenu('=154
402-
'primeomega('=155
403-
'primepi('=156
404-
'card('=157
405-
'powerset('=158
406-
'union('=159
407-
'intersection('=160
408-
'complement('=161
409-
'image('=162
410-
'preimage('=163
411-
'floor('=164
412-
'ceil('=165
413-
'ceiling('=166
414-
'round('=167
415-
'min('=168
416-
'max('=169
417-
'argmax('=170
418-
'argmin('=171
419-
'gcd('=172
420-
'lcm('=173
421-
'binomial('=174
422-
'multinomial('=175
423-
'catalannumber('=176
424-
'harmonicnumber('=177
425-
'fallingfactorial('=178
426-
'factorialpower('=179
427-
'risingfactorial('=180
428-
'iverson('=181
429-
'aleph('=182
430-
'erf('=183
431-
'erfc('=184
432-
'erfi('=185
433-
'Ei('=186
434-
'li('=187
435-
'lambertw('=188
436-
'productlog('=189
437-
'polygamma('=190
438-
'digamma('=191
439-
'Si('=192
440-
'Ci('=193
441-
'Shi('=194
442-
'Chi('=195
443-
'det('=196
444-
'transpose('=197
445-
'trace('=198
446-
'dot('=199
447-
'cross('=200
448-
'laplacian('=201
449-
'forwarddifference('=202
450-
'jacobian('=203
451-
'hessian('=204
452-
'resultant('=205
453-
'conjugate('=206
454-
'conj('=207
455-
'arg('=208
456-
'Re('=209
457-
'Im('=210
458-
're('=211
459-
'real('=212
460-
'im('=213
461-
'imag('=214
462-
'trunc('=215
463-
'zeta('=216
464-
'riemannzeta('=217
465-
'hurwitzzeta('=218
466-
'heaviside('=219
467-
'unitstep('=220
468-
'isprime('=221
469-
'pochhammer('=222
470-
'doublefactorial('=223
471-
'factorial2('=224
472-
'fractionalpart('=225
473-
'quotient('=226
474-
'factorize('=227
475-
'nextprime('=228
476-
'divisors('=229
477-
'gradient('=230
478-
'divergence('=231
479-
'φ('=232
480-
'ϕ('=233
481-
'domain('=234
482-
'piecewise('=235
483-
'apply('=236
484-
'lambda('=237
485-
'NaN'=242
400+
'factorization('=151
401+
'primefactors('=152
402+
'mobius('=153
403+
'moebiusmu('=154
404+
'divisorsigma('=155
405+
'primenu('=156
406+
'primeomega('=157
407+
'primepi('=158
408+
'card('=159
409+
'powerset('=160
410+
'union('=161
411+
'intersection('=162
412+
'complement('=163
413+
'image('=164
414+
'preimage('=165
415+
'floor('=166
416+
'ceil('=167
417+
'ceiling('=168
418+
'round('=169
419+
'min('=170
420+
'max('=171
421+
'argmax('=172
422+
'argmin('=173
423+
'gcd('=174
424+
'lcm('=175
425+
'binomial('=176
426+
'multinomial('=177
427+
'catalannumber('=178
428+
'harmonicnumber('=179
429+
'fallingfactorial('=180
430+
'factorialpower('=181
431+
'risingfactorial('=182
432+
'iverson('=183
433+
'aleph('=184
434+
'erf('=185
435+
'erfc('=186
436+
'erfi('=187
437+
'Ei('=188
438+
'li('=189
439+
'lambertw('=190
440+
'productlog('=191
441+
'polygamma('=192
442+
'digamma('=193
443+
'Si('=194
444+
'Ci('=195
445+
'Shi('=196
446+
'Chi('=197
447+
'det('=198
448+
'transpose('=199
449+
'trace('=200
450+
'dot('=201
451+
'cross('=202
452+
'laplacian('=203
453+
'forwarddifference('=204
454+
'jacobian('=205
455+
'hessian('=206
456+
'resultant('=207
457+
'conjugate('=208
458+
'conj('=209
459+
'arg('=210
460+
'Re('=211
461+
'Im('=212
462+
're('=213
463+
'real('=214
464+
'im('=215
465+
'imag('=216
466+
'trunc('=217
467+
'zeta('=218
468+
'riemannzeta('=219
469+
'hurwitzzeta('=220
470+
'heaviside('=221
471+
'unitstep('=222
472+
'isprime('=223
473+
'pochhammer('=224
474+
'doublefactorial('=225
475+
'factorial2('=226
476+
'fractionalpart('=227
477+
'quotient('=228
478+
'factorize('=229
479+
'nextprime('=230
480+
'divisors('=231
481+
'gradient('=232
482+
'divergence('=233
483+
'φ('=234
484+
'ϕ('=235
485+
'domain('=236
486+
'piecewise('=237
487+
'apply('=238
488+
'lambda('=239
489+
'NaN'=244

0 commit comments

Comments
 (0)