Skip to content

Give a rational function in nine to sixteen variables a canonical form - #2748

Open
Rafael-SOWNet wants to merge 2 commits into
masterfrom
fix-2721-rational-canonicalization-many-variables
Open

Rafael-SOWNet wants to merge 2 commits into
masterfrom
fix-2721-rational-canonicalization-many-variables

Conversation

@Rafael-SOWNet

@Rafael-SOWNet Rafael-SOWNet commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

What was wrong

RationalFunction.TryCanonicalize refused any expression with more than MultivariatePolynomial.MaxVariables = 8 variables. With no canonical candidate, Simplify fell back on pairwise rules. Those rules meet two terms only where the canonical order puts them side by side, so the answer depended on how the names sorted. The issue's nine-variable sum came back cancelled when the variable was t and unchanged when it was x.

Why the cap was there. It was not a cost guard. A packed monomial was a single ulong with one byte per variable, so it had room for eight variables, and a ninth would have overwritten another variable's byte. Cost is bounded elsewhere: by MaxTerms, MaxIntermediateTerms, the complexity limit of 256, and the 64 limit on the candidate Simplify offers.

The fix

  • A Monomial is now two words, which holds sixteen variables. Lexicographic order is still a word-by-word comparison. Every exponent is at most 127, so the top bit of every byte is clear. That lets products, quotients, divisibility, lcm, total degree and coprimality run a word at a time instead of looping per variable. A polynomial in eight variables or fewer leaves the second word zero and hashes the same as before.

  • Only the rational canonical form reads up to sixteen variables. It uses a new TryParse(expr, indices, mostVariables) overload. MaxVariables stays at 8 for the solvers, the integrator, the evaluator and the quantifier rules: their searches grow with the variable count, and none was measured past eight. The modular elimination in PartialFractions keeps its own one-word packing for those eight.

  • Seventeen or more variables are still refused.

  • Point 2: a refusal now says why. There is a new TransformationResult.Refusal, also printed after the input in ToString(). It names the bound reached and the value that passed it:

    • complexity above 256
    • more than 16 variables
    • a quotient raised to a power above 32
    • a degree above 127, naming the variable
    • more than 512 terms
    • the gcd passing 4096 intermediate terms or 256 steps
    • a zero denominator

    Where the input is not a rational function over Q, it names the first part that is not, e.g. sin(x) or pi. The refusal is kept as a value until the transformation formats it, so Simplify, which asks on every rational function it meets, formats nothing. A gcd that passes its bounds is now also a refusal. Before, the quotient came back uncancelled and was offered as the canonical form.

  • Point 3 is "not asked here" in the issue and is left alone.

Why always answering is right here, and not just making the refusal name-independent. Above eight variables the canonical form now answers, so the candidate Simplify gets no longer depends on names. What still differs between namings is the sign the form normalises by, which follows the variables' name order. That was true below nine variables before this change, and the 5–8 variable run below shows it equally on both builds. It is the #2146 class, not this issue.

Measured

Issue rows (master 2a7247653 → this branch). The new value matches the input to 60 digits in mpmath at 5 random rational points:

.Simplify() master branch
E, S = t two fractions over one denominator, provided not n = 0 (-a ^ 2 * c * t * v ^ 2 - a * c * r - n * t * w ^ 2 - u) / (a ^ 2 * c ^ 2 * n - a ^ 2 * m - a ^ 2 * n) provided not n = 0
E, S = x unchanged input (-a ^ 2 * c * v ^ 2 * x - a * c * r - n * w ^ 2 * x - u) / (a ^ 2 * c ^ 2 * n - a ^ 2 * m - a ^ 2 * n) provided not n = 0
RationalCanonicalization no answer the quotient above

Generated variants: sums of fractions sharing a cancellable factor in 9–14 variables, plus the issue row. Each one is renamed 7 ways: random single letters, indexed and Greek names, and reversed order. Values are compared against exact rational evaluation of the input at 3 points per naming.

run single quotient, master → branch wrong values Simplify total ms, master → branch
9–14 vars, seed 1 162/175 → 175/175 0 → 0 2328 → 2261
9–14 vars, seed 2 169/175 → 175/175 0 → 0 2634 → 2123
5–8 vars (control) 171/175 → 175/175 (the 4 misses were the issue row) 0 → 0 2851 → 2276

Of the 25 cases at 5–8 variables, 4 come out with the same complexity under every naming on both builds. That is the sign-normalisation variation described above, and it is the same as before.

Harnesses: CanonCheck, SimpSweep, RuleCheck and PropCheck give identical lists before and after (CanonCheck 2040 findings, matching its baseline; SimpSweep 10463/10463 agree; RuleCheck 0 value changes; PropCheck 1355 checks, 0 failures).

Kernel benchmark (CommonFunctionsInterVersion, both arms on the same machine, one run each): allocation is unchanged on every gated case to within 0.09%. The largest change is SolveMediumHard, +1296 B on 1.41 MB, and CompileHard (ungated, JIT) is +1.5%. Times are within ±3%, except SolveHard at +5.6% from a single run on a loaded machine.

Full unit suite: 22347 passed, 0 failed on the first commit. On the second, 22354 passed with 0 failed, once PublicApi.txt recorded the new Refusal property. CanonCheck and SimpSweep were re-run on the second commit, and their lists are identical to the first.

Fixes #2721

🤖 Generated with Claude Code

A packed monomial was one ulong, a byte to a variable, so it held eight
variables, and RationalFunction.TryCanonicalize refused a ninth. The cap
was the representation, not a cost guard: what the canonical form does
with a polynomial is bounded by MaxTerms, MaxIntermediateTerms and the
complexity of 256 (64 for the candidate Simplify offers). With no
canonical candidate, Simplify fell back on pairwise rules that meet two
terms only where the names sort them side by side, so one nine-variable
sum of fractions came back cancelled with t, untouched with x.

A Monomial is now two words, sixteen variables. Lexicographic order is
still a word-by-word comparison, and the top bit of each byte is clear
(MaxDegree is 127), so products, quotients, divisibility, lcm, total
degree and coprimality are done a word at a time rather than a loop per
variable. A polynomial in eight variables or fewer leaves the second word
zero and hashes as its one word did; the benchmark gate's allocation is
unchanged to 0.09% on every gated case.

Only the rational canonical form reads sixteen: TryParse takes a bound,
and MaxVariables stays 8 for the solvers, the integrator, the evaluator
and the quantifier rules, whose searches grow with the variable count and
were never measured past eight. The modular elimination in
PartialFractions keeps its own one-word packing for the same eight.

Generated variants, 9-14 variables, 7 namings each (random letters,
indexed names, reversed order), 3 seeds: Simplify returns one quotient for
175/175 namings per seed, against 162, 169 and 171 on master; 0 wrong
values against exact evaluation; Simplify time 6.7k ms total against 7.8k.
The variation left between namings is the sign the canonical form
normalises by under the name order, which 5-8 variables show the same on
both builds. CanonCheck, SimpSweep, RuleCheck and PropCheck list the same
findings before and after.

Fixes #2721

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.68670% with 45 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.68%. Comparing base (2a72476) to head (dc34731).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
.../Functions/Algebra/Polynomials/RationalFunction.cs 65.26% 18 Missing and 15 partials ⚠️
...ouriMath/Functions/Algebra/Polynomials/Monomial.cs 88.63% 4 Missing and 1 partial ⚠️
.../Functions/Algebra/Polynomials/PartialFractions.cs 66.66% 0 Missing and 3 partials ⚠️
...ions/Algebra/Polynomials/MultivariatePolynomial.cs 94.59% 1 Missing and 1 partial ⚠️
...urces/AngouriMath/Convenience/MathS.Polynomials.cs 0.00% 0 Missing and 1 partial ⚠️
...ath/Functions/Algebra/Polynomials/PolynomialGcd.cs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #2748    +/-   ##
========================================
  Coverage   86.68%   86.68%            
========================================
  Files         346      347     +1     
  Lines       76971    77078   +107     
  Branches    22648    22672    +24     
========================================
+ Hits        66720    66813    +93     
- Misses       5523     5535    +12     
- Partials     4728     4730     +2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.17% <80.68%> (-0.01%) ⬇️
fsharp 32.79% <ø> (ø)
interactive 40.09% <ø> (ø)

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

Files with missing lines Coverage Δ
...h/Core/Transformations/Transformation.Catalogue.cs 88.88% <100.00%> (+0.21%) ⬆️
...AngouriMath/Core/Transformations/Transformation.cs 100.00% <100.00%> (ø)
...iMath/Core/Transformations/TransformationResult.cs 86.66% <100.00%> (+20.00%) ⬆️
...gouriMath/Functions/Algebra/Groebner/Buchberger.cs 90.00% <100.00%> (-0.28%) ⬇️
...ces/AngouriMath/Functions/Algebra/Groebner/Fglm.cs 89.47% <100.00%> (ø)
...lgebra/Groebner/MultivariatePolynomial.Groebner.cs 97.29% <100.00%> (-0.78%) ⬇️
...lgebra/Polynomials/BivariateHenselFactorization.cs 64.57% <100.00%> (ø)
...ions/Algebra/Polynomials/EvaluationHomomorphism.cs 74.57% <100.00%> (ø)
...ions/Algebra/Polynomials/KroneckerFactorization.cs 47.30% <100.00%> (ø)
...bra/Polynomials/MultivariateHenselFactorization.cs 61.82% <100.00%> (ø)
... and 8 more

... and 8 files with indirect coverage changes

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

TransformationResult read "no answer for ..." both for an expression that
is not a rational function over Q and for one refused at a bound, which
is point 2 of #2721. It now carries a Refusal, printed after the input:
the bound reached and the value that passed it (complexity over 256, more
than 16 variables, a quotient to a power over 32, a degree over 127, more
than 512 terms, the gcd's 4096 intermediate terms or 256 steps), the
first part that is not a rational function over Q, or a zero
denominator. The refusal is a value until the transformation formats it,
so Simplify, which asks on every rational function, formats nothing.

A greatest common divisor that passes its bounds is now a refusal. Before,
the quotient came back uncancelled and was offered as the canonical form,
so two writings of one function could give different trees.

Part of #2721

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

RationalCanonicalization has no answer above eight variables, and Simplify's answer then depends on a variable's name

1 participant