Skip to content

factorization(n) and primefactors(n): a prime factorisation as a node - #2471

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
feature/factorization-node
Nov 16, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
feature/factorization-node

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

The second half of #2449, built from the design note (the first half, a product over a set, is #2467).

factorization(n) is the prime factorisation of a positive integer: the finitely supported map from each prime to its exponent in n, mathlib's Nat.factorization. It is its own node, PrimeFactorizationf, rather than a held product, since Simplify would fold 2^3 * 3^2 * 5 back to 360 and a factorisation is not the number; its pairs are cached in Factors, computed by the same factoriser Factorize uses (Pollard rho, past the longs). primefactors(n) is its support, a finite set.

input was (2.7.0) now
factorization(360) factorization * 360 factorization(360), Factors = (2,3), (3,2), (5,1)
factorization(1) / (97) / (2^64+1) a product the empty map / (97,1) / (274177,1), (67280421310721,1)
factorization(0), (-12), (1/2) 0, products NaN, as primenu, primeomega, mobius and divisorsigma give at 0 and −12
factorization(n) a product as written; its domain condition is n in ZZ+
primefactors(360) / (1) primefactors * 360 { 2, 3, 5 } / { }
product(p ^ valuation(n, p), p in primefactors(n)) throws n, at 1, 2, 97, 360, 1024, 9699690, 2^64+1 and 3^40·7^3
factorize(360) a product (2.7.0), refused since #2453 still refused; the message now names factorization(n)
  • Codomain Any for both, as matrices, transpose, cross, sets and powerset have: no domain of numbers.
  • An integer with a factor neither proven prime nor split is left as written.
  • MathS.NumberTheory.Factorization(Entity) and MathS.NumberTheory.PrimeFactors(Entity).
  • Printing round-trips. LaTeX is 2^{3} \cdot 3^{2} \cdot 5 (1 for the empty map, \operatorname{factorization}(n) for a symbol). CSharpMath reads that back as the number 360, not as the node: that is the notation, since a factorisation written as a product cannot be told from the product, so there is nothing for CSharpMath to change.
  • SymPy: sympy.factorint(n), the dict, which is the map itself, and sympy.FiniteSet(*sympy.primefactors(n)); refused with a variable, where SymPy raises.
  • Compile refuses both, naming them: their value is not a number.
  • The grammar gains the two rules after 'valuation(', and 'factorize(' changes only its message.

Recorded in BREAKING-CHANGES.md (Was measured on the 2.7.0 package). PrimeFactorizationNodeTest: 46 cases. Full check: 20,574 unit tests, 0 failed; MCP 448, Terminal 69, F# 134, Interactive 19; AOT OK; BoundCheck 0 disagreements, RootCheck 596/596, CasBench 120/121 with 0 wrong, SimpSweep 10463/10463, RuleCheck clean, CanonCheck and Confluence at their baselines.

Closes #2449.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.25743% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.96%. Comparing base (73b77bf) to head (4e10913).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...th/Core/Entity/Discrete/Entity.Discrete.Classes.cs 40.00% 18 Missing ⚠️
...unctions/Output/Latex/Latex.Arithmetics.Classes.cs 78.57% 2 Missing and 1 partial ⚠️
...AngouriMath/Functions/TreeAnalyzer/Sort.Classes.cs 0.00% 2 Missing ⚠️
...Evaluation.Discrete/Evaluation.Discrete.Classes.cs 95.00% 0 Missing and 1 partial ⚠️
...ions/Output/ToSympy/ToSympy.Arithmetics.Classes.cs 83.33% 0 Missing and 1 partial ⚠️
Sources/AngouriMath/Functions/Substitute.cs 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2471      +/-   ##
==========================================
- Coverage   86.97%   86.96%   -0.01%     
==========================================
  Files         324      324              
  Lines       71849    71984     +135     
  Branches    20955    20977      +22     
==========================================
+ Hits        62492    62603     +111     
- Misses       5024     5044      +20     
- Partials     4333     4337       +4     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.49% <74.25%> (-0.02%) ⬇️
fsharp 32.97% <ø> (ø)
interactive 39.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
Sources/AngouriMath/Convenience/MathS.cs 90.82% <100.00%> (+0.05%) ⬆️
Sources/AngouriMath/Core/Antlr/AngouriMathLexer.cs 99.27% <ø> (+<0.01%) ⬆️
...ources/AngouriMath/Core/Antlr/AngouriMathParser.cs 88.12% <ø> (+0.07%) ⬆️
Sources/AngouriMath/Core/CostModel.cs 97.26% <100.00%> (+0.03%) ⬆️
Sources/AngouriMath/Core/Domains.Classes.cs 100.00% <100.00%> (ø)
...AngouriMath/Core/Transformations/EqualityOracle.cs 92.15% <100.00%> (ø)
...Math/Core/Transformations/Matching/MatchPattern.cs 86.53% <100.00%> (+0.02%) ⬆️
...urces/AngouriMath/Functions/Compilation/Binders.cs 95.00% <100.00%> (+1.45%) ⬆️
...nuous/Solvers/EquationSolver/InvertNode.Classes.cs 62.11% <100.00%> (+0.26%) ⬆️
...ns/Output/ToString/ToString.Arithmetics.Classes.cs 100.00% <100.00%> (ø)
... and 6 more

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…tors(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>
@Rafael-SOWNet

Copy link
Copy Markdown
Member Author

Rebased onto 73b77bf after #2470 (shader source) merged. Both PRs had changed Binders.cs: the factorisation's refusal (NotANumber) now goes through #2470's Binders.Refusal, so the shader declines factorization(n) with Compile's own message, and a row in ShadersTest.WhatCompileRefusesIsDeclinedWithItsMessage pins that. Shaders, factorisation, compile, binder, API, round-trip, catalogue and refused-names tests on the result: 2,205 passed.

@Rafael-SOWNet
Rafael-SOWNet force-pushed the feature/factorization-node branch from 22f4356 to 4e10913 Compare November 16, 2026 01:39
@Rafael-SOWNet
Rafael-SOWNet merged commit 423d3aa into master Nov 16, 2026
37 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Nov 16, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the feature/factorization-node branch November 16, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

A product over a set, and integer factorisation in an expression

1 participant