Skip to content

Commit 61bd8e7

Browse files
The rational canonical form keeps the points where the expression has no value (#1740)
Dividing by a quotient moves its denominator into the numerator, and the form gave the result a value where the expression had none: 1/(1/x) was x. The denominators a quotient turns over are kept as a condition now, with the factors a cancellation removes and the denominator under a vanishing numerator, written square-free and without what the reduced denominator still excludes. The form gathers through the step AsSingleFraction takes. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9a82a17 commit 61bd8e7

4 files changed

Lines changed: 167 additions & 130 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,29 @@ power of the secant or the cosecant under a root goes on now too. The cosecant's
11751175
| `"sqrt(1 + csc(x)^2)".ToEntity().Integrate("x")` | `integral(...)` | logarithms and an arctangent in `sqrt(2 tan(x)^2 + 1)`, times `sgn(tan(x))` |
11761176
| `"1/sqrt(-1 + csc(x)^2)".ToEntity().Integrate("x")` | `integral(...)` | `sgn(tan(x)) ln(1 + tan(x)^2)/2` |
11771177

1178+
### The rational canonical form keeps the points where the expression has no value
1179+
1180+
**Wrong answers, silent.** `CanonicalizeAsRationalFunction` and `Transformation.RationalCanonicalization` kept the condition a
1181+
cancelled factor owes, `x/x` being `1 provided not x = 0`, and dropped the one that dividing by a
1182+
quotient owes: `1/(1/x)` was `x`, which is `0` at zero, where `1/(1/x)` has no value. The
1183+
denominator a quotient turns over is now kept as a condition too, and so is the denominator under a
1184+
numerator that vanishes: `0/x` is `0 provided not x = 0`. The condition is written canonically,
1185+
because it is part of a form whose point is that equal trees mean equal functions: the square-free
1186+
part of what was excluded, without what the reduced denominator still excludes. So `x^2/x^2` and
1187+
`x/x` meet, and a cancelled factor that the denominator still has is not repeated. The form now
1188+
gathers through the first step of `AsSingleFraction`, so the two agree on what a single quotient is
1189+
and on where it has a value. [#1618](https://github.com/asc-community/AngouriMath/issues/1618).
1190+
Both columns measured on a build, `v2.5.0` against this change.
1191+
1192+
| `….ToEntity().CanonicalizeAsRationalFunction()` of | Was (2.5.0) | Is |
1193+
|---|---|---|
1194+
| `"1/(1/x)"` | `x` | `x provided not x = 0` |
1195+
| `"1/(1 + 1/x)"` | `x / (x + 1)` | `x / (x + 1) provided not x = 0` |
1196+
| `"(a/b)/(c/d)"` | `a * d / (b * c)` | `a * d / (b * c) provided not d = 0` |
1197+
| `"0/x"` | `0` | `0 provided not x = 0` |
1198+
| `"x^2/x^2"` | `1 provided not x ^ 2 = 0` | `1 provided not x = 0` |
1199+
| `"(x^2 - 1)/(x^2 + 2x + 1) + 1/(x + 1)"` | `x / (x + 1) provided not x ^ 2 + 2 * x + 1 = 0` | `x / (x + 1)` |
1200+
11781201
### A partial-fraction coefficient with symbols in it is in lowest terms, its rational content included
11791202

11801203
**Improvement, not silent.** The decomposition over written factors with symbols among their

‎Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,18 @@ private static class PolynomialFactorizationHolder
352352
/// expressions must not quietly hand back a normalisation that merely resembles one.
353353
/// </para>
354354
/// <para>
355-
/// The expression is gathered into a single quotient — which is the part nothing else
356-
/// in the library does, and without which <c>1/x + 1/y</c> and <c>(x + y)/(x*y)</c>
357-
/// could never meet — then reduced by the multivariate greatest common divisor and
358-
/// scaled so the denominator is monic in the lexicographic monomial order.
355+
/// The expression is written as a single fraction, as <see cref="AsSingleFraction"/>
356+
/// writes it -- without which <c>1/x + 1/y</c> and <c>(x + y)/(x*y)</c> could never
357+
/// meet -- then reduced by the multivariate greatest common divisor and scaled so the
358+
/// denominator is monic in the lexicographic monomial order.
359359
/// </para>
360360
/// <para>
361361
/// <b>Cancelling carries its condition.</b> <c>x/x</c> is not <c>1</c>, so where a
362362
/// factor of positive degree comes out the answer says the factor is nonzero, as the
363-
/// rest of the library already does. Gathering over a common denominator widens
363+
/// rest of the library already does; and so does turning a quotient over, since
364+
/// <c>1/(1/x)</c> is not <c>x</c> either. Gathering over a common denominator widens
364365
/// nothing by itself: a sum is defined exactly where its terms are.
366+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1618">#1618</a>
365367
/// </para>
366368
/// <para>
367369
/// Nothing runs this by default. See

‎Sources/AngouriMath/Functions/Algebra/Polynomials/RationalFunction.cs‎

Lines changed: 91 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ namespace AngouriMath.Functions
3030
/// <a href="https://github.com/asc-community/AngouriMath/issues/934">#934</a>.
3131
/// </para>
3232
/// <para>
33-
/// Four steps, and only the first is new. The expression is gathered into a single
34-
/// quotient — nothing else in the library does that, and without it <c>1/x + 1/y</c> and
33+
/// Four steps. The expression is written as a single fraction, by the step
34+
/// <see cref="Entity.AsSingleFraction"/> takes -- without it <c>1/x + 1/y</c> and
3535
/// <c>(x + y)/(x*y)</c> could never meet. Then the numerator and denominator are divided
3636
/// by their multivariate greatest common divisor, which
3737
/// <see cref="PolynomialGcd"/> computes and verifies. Then both are scaled so that the
@@ -40,12 +40,14 @@ namespace AngouriMath.Functions
4040
/// </para>
4141
/// <para>
4242
/// <b>The domain is preserved rather than assumed away.</b> Cancelling a common factor
43-
/// widens the domain — <c>x/x</c> is not <c>1</c> — so where a factor of positive degree
44-
/// is cancelled the answer carries the condition that it is nonzero, which is what the
45-
/// library already does elsewhere and what keeps "equal trees means equal expressions"
46-
/// true rather than nearly true. Gathering over a common denominator does not widen
47-
/// anything: a sum is defined exactly where its terms are, and the product of the
48-
/// denominators vanishes exactly where one of them does.
43+
/// widens the domain — <c>x/x</c> is not <c>1</c> — and so does dividing by a quotient,
44+
/// which moves its denominator into the numerator: <c>1/(1/x)</c> is not <c>x</c>. So the
45+
/// answer carries the condition that neither vanishes, which is what the library already
46+
/// does elsewhere and what keeps "equal trees means equal expressions" true rather than
47+
/// nearly true; the condition is written canonically too, square-free and without what
48+
/// the denominator already excludes. A sum is defined exactly where its terms are, and the
49+
/// common denominator vanishes exactly where one of theirs does, so gathering one widens
50+
/// nothing. <a href="https://github.com/asc-community/AngouriMath/issues/1618">#1618</a>
4951
/// </para>
5052
/// </remarks>
5153
internal static class RationalFunction
@@ -57,9 +59,10 @@ internal static class RationalFunction
5759
private const int MaxComplexity = 256;
5860

5961
/// <summary>
60-
/// Raising to a power is where a gathered quotient explodes, so the exponent is
61-
/// bounded before <see cref="MultivariatePolynomial.Power"/> is asked; that has its
62-
/// own bound on the number of terms, which catches the rest.
62+
/// Raising a quotient to a power is where a gathered quotient explodes, so a power of
63+
/// anything but a polynomial is bounded before anything is multiplied out; a polynomial's
64+
/// power has <see cref="MultivariatePolynomial.Power"/>'s own bound on the number of
65+
/// terms.
6366
/// </summary>
6467
private const int MaxExponent = 32;
6568

@@ -84,23 +87,24 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
8487
indices[variables[i]] = i;
8588
var variableCount = variables.Length;
8689

87-
if (!TryGather(expr, indices, variableCount, out var numerator, out var denominator))
90+
if (!TryGather(expr, indices, variableCount, out var numerator, out var denominator, out var excluded))
8891
return false;
89-
// A vanishing denominator is not a rational function, and a vanishing numerator
90-
// is zero however it was written.
92+
// A vanishing denominator is not a rational function. A vanishing numerator is zero
93+
// wherever the expression has a value: everywhere its denominator does not vanish.
9194
if (denominator.IsZero)
9295
return false;
96+
var order = new int[variableCount];
97+
for (var i = 0; i < order.Length; i++)
98+
order[i] = i;
9399
if (numerator.IsZero)
94100
{
95-
canonical = Integer.Create(0);
101+
if (excluded.Multiply(denominator) is not { } wholeDenominator
102+
|| Conditioned(Integer.Create(0), wholeDenominator, MultivariatePolynomial.One(variableCount), order, variables) is not { } zero)
103+
return false;
104+
canonical = zero;
96105
return true;
97106
}
98107

99-
var order = new int[variableCount];
100-
for (var i = 0; i < order.Length; i++)
101-
order[i] = i;
102-
103-
var cancelled = MultivariatePolynomial.One(variableCount);
104108
if (variableCount > 0
105109
&& PolynomialGcd.Gcd(numerator, denominator, order, 0) is { } divisor
106110
&& !divisor.IsConstant)
@@ -115,9 +119,11 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
115119
|| reducedBottom.Multiply(divisor) is not { } checkedBottom
116120
|| !checkedBottom.SameAs(denominator))
117121
return false;
122+
if (excluded.Multiply(divisor) is not { } withTheCancelled)
123+
return false;
118124
numerator = reducedTop;
119125
denominator = reducedBottom;
120-
cancelled = divisor;
126+
excluded = withTheCancelled;
121127
}
122128

123129
// Scaled so the denominator leads with one, under the same lexicographic monomial
@@ -138,125 +144,86 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
138144
var quotient = denominator.IsConstant
139145
? numerator.ToEntity(variables)
140146
: numerator.ToEntity(variables) / denominator.ToEntity(variables);
141-
142-
canonical = cancelled.IsConstant
143-
? quotient
144-
: new Providedf(quotient, !cancelled.ToEntity(variables).EqualTo(0));
145-
return true;
147+
canonical = Conditioned(quotient, excluded, denominator, order, variables);
148+
return canonical is not null;
146149
}
147150

148151
/// <summary>
149-
/// <paramref name="expr"/> as a single quotient of polynomials, gathering a sum of
150-
/// quotients over a common denominator. The denominator is never zero and never
151-
/// simplified away; it is <c>1</c> for a polynomial.
152+
/// <paramref name="quotient"/> with the condition that it is not at a zero of
153+
/// <paramref name="excluded"/>, where the expression it came from had no value, except
154+
/// where <paramref name="denominator"/> vanishing already says so; <see langword="null"/>
155+
/// where a step of the arithmetic declined.
152156
/// </summary>
153157
/// <remarks>
154-
/// The common denominator is the product rather than the least common multiple. Both
155-
/// are correct and the product is cheaper to build; what it costs is a larger
156-
/// intermediate, which the greatest common divisor then removes — so the answer is the
157-
/// same and only the work in between differs.
158+
/// The condition is part of the form, so it is canonical too: two expressions with the same
159+
/// value and the same points where they have none have to meet. So the polynomial written
160+
/// is the square-free part of <paramref name="excluded"/>, which vanishes exactly where it
161+
/// does -- <c>x^2/x^2</c> and <c>x/x</c> are both <c>1 provided not x = 0</c> -- without the
162+
/// factors it shares with the denominator, and with whole coprime coefficients and a
163+
/// positive leading one. The square-free part is <c>E / gcd(E, dE/dx_1, …, dE/dx_n)</c>:
164+
/// over the rationals a factor to the power <c>k</c> divides each derivative to the power
165+
/// <c>k - 1</c>, and one of them no further.
158166
/// </remarks>
159-
private static bool TryGather(
160-
Entity expr, IReadOnlyDictionary<Variable, int> indices, int variableCount,
161-
[NotNullWhen(true)] out MultivariatePolynomial? numerator,
162-
[NotNullWhen(true)] out MultivariatePolynomial? denominator)
167+
private static Entity? Conditioned(Entity quotient, MultivariatePolynomial excluded, MultivariatePolynomial denominator,
168+
IReadOnlyList<int> order, IReadOnlyList<Variable> variables)
163169
{
164-
numerator = denominator = null;
165-
166-
// A polynomial is its own numerator, and this is the common case, so it is tried
167-
// before the expression is taken apart.
168-
if (MultivariatePolynomial.TryParse(expr, indices) is { } whole)
169-
{
170-
numerator = whole;
171-
denominator = MultivariatePolynomial.One(variableCount);
172-
return true;
173-
}
174-
175-
switch (expr)
176-
{
177-
case Sumf(var left, var right):
178-
return TryCombine(left, right, subtract: false, indices, variableCount,
179-
out numerator, out denominator);
180-
181-
case Minusf(var left, var right):
182-
return TryCombine(left, right, subtract: true, indices, variableCount,
183-
out numerator, out denominator);
184-
185-
case Mulf(var left, var right):
186-
{
187-
if (!TryGather(left, indices, variableCount, out var leftTop, out var leftBottom)
188-
|| !TryGather(right, indices, variableCount, out var rightTop, out var rightBottom))
189-
return false;
190-
if (leftTop.Multiply(rightTop) is not { } top
191-
|| leftBottom.Multiply(rightBottom) is not { } bottom)
192-
return false;
193-
numerator = top;
194-
denominator = bottom;
195-
return true;
196-
}
197-
198-
case Divf(var left, var right):
170+
if (excluded.IsConstant)
171+
return excluded.IsZero ? null : quotient;
172+
var repeated = excluded;
173+
for (var i = 0; i < excluded.VariableCount; i++)
174+
if (excluded.DegreeIn(i) > 0)
199175
{
200-
if (!TryGather(left, indices, variableCount, out var leftTop, out var leftBottom)
201-
|| !TryGather(right, indices, variableCount, out var rightTop, out var rightBottom))
202-
return false;
203-
// Dividing by a quotient that is identically zero is not a rational
204-
// function, and inverting it here would quietly produce one.
205-
if (rightTop.IsZero)
206-
return false;
207-
if (leftTop.Multiply(rightBottom) is not { } top
208-
|| leftBottom.Multiply(rightTop) is not { } bottom)
209-
return false;
210-
numerator = top;
211-
denominator = bottom;
212-
return true;
176+
if (PolynomialGcd.Gcd(repeated, excluded.DerivativeIn(i), order, 0) is not { } common)
177+
return null;
178+
repeated = common;
213179
}
214-
215-
case Powf(var @base, Integer power):
216-
{
217-
var exponent = power.EInteger;
218-
if (exponent.Abs().CompareTo(EInteger.FromInt32(MaxExponent)) > 0)
219-
return false;
220-
if (!TryGather(@base, indices, variableCount, out var baseTop, out var baseBottom))
221-
return false;
222-
var magnitude = exponent.Abs().ToInt32Checked();
223-
var negative = exponent.Sign < 0;
224-
if (negative && baseTop.IsZero)
225-
return false;
226-
var top = negative ? baseBottom : baseTop;
227-
var bottom = negative ? baseTop : baseBottom;
228-
if (top.Power(magnitude) is not { } raisedTop
229-
|| bottom.Power(magnitude) is not { } raisedBottom)
230-
return false;
231-
numerator = raisedTop;
232-
denominator = raisedBottom;
233-
return true;
234-
}
235-
236-
default:
237-
return false;
238-
}
180+
if (excluded.DivideExact(repeated) is not { } squareFree
181+
|| PolynomialGcd.Gcd(squareFree, denominator, order, 0) is not { } shared
182+
|| squareFree.DivideExact(shared) is not { } left)
183+
return null;
184+
return left.IsConstant
185+
? quotient
186+
: new Providedf(quotient, !left.Normalized().ToEntity(variables).EqualTo(0));
239187
}
240188

241189
/// <summary>
242-
/// A sum or a difference of two quotients, over the product of their denominators.
190+
/// <paramref name="expr"/> as a single quotient of polynomials, and the polynomial whose
191+
/// zeros are the other points where <paramref name="expr"/> has no value: the product of
192+
/// the denominators that dividing by a quotient turned over. The denominator is never
193+
/// zero and never simplified away; it is <c>1</c> for a polynomial.
243194
/// </summary>
244-
private static bool TryCombine(
245-
Entity left, Entity right, bool subtract,
246-
IReadOnlyDictionary<Variable, int> indices, int variableCount,
195+
/// <remarks>
196+
/// Gathered by <see cref="SingleQuotient.OverLeastCommonDenominator"/>, the step
197+
/// <see cref="Entity.AsSingleFraction"/> takes, so that the two agree on what a single
198+
/// quotient of an expression is and on where it has a value; this form is that quotient
199+
/// reduced and normalised.
200+
/// </remarks>
201+
private static bool TryGather(
202+
Entity expr, IReadOnlyDictionary<Variable, int> indices, int variableCount,
247203
[NotNullWhen(true)] out MultivariatePolynomial? numerator,
248-
[NotNullWhen(true)] out MultivariatePolynomial? denominator)
204+
[NotNullWhen(true)] out MultivariatePolynomial? denominator,
205+
[NotNullWhen(true)] out MultivariatePolynomial? excluded)
249206
{
250-
numerator = denominator = null;
251-
if (!TryGather(left, indices, variableCount, out var leftTop, out var leftBottom)
252-
|| !TryGather(right, indices, variableCount, out var rightTop, out var rightBottom))
253-
return false;
254-
if (leftTop.Multiply(rightBottom) is not { } crossLeft
255-
|| rightTop.Multiply(leftBottom) is not { } crossRight
256-
|| leftBottom.Multiply(rightBottom) is not { } bottom)
207+
numerator = denominator = excluded = null;
208+
foreach (var node in expr.Nodes)
209+
if (node is Powf(var @base, Integer power)
210+
&& power.EInteger.Abs().CompareTo(EInteger.FromInt32(MaxExponent)) > 0
211+
&& MultivariatePolynomial.TryParse(@base, indices) is null)
212+
return false;
213+
var carried = new List<Entity>();
214+
var (top, bottom) = SingleQuotient.OverLeastCommonDenominator(expr, carried);
215+
if (MultivariatePolynomial.TryParse(top, indices) is not { } parsedTop
216+
|| MultivariatePolynomial.TryParse(bottom, indices) is not { } parsedBottom)
257217
return false;
258-
numerator = subtract ? crossLeft.Subtract(crossRight) : crossLeft.Add(crossRight);
259-
denominator = bottom;
218+
var product = MultivariatePolynomial.One(variableCount);
219+
foreach (var turnedOver in carried)
220+
{
221+
if (MultivariatePolynomial.TryParse(turnedOver, indices) is not { } parsed
222+
|| product.Multiply(parsed) is not { } next)
223+
return false;
224+
product = next;
225+
}
226+
(numerator, denominator, excluded) = (parsedTop, parsedBottom, product);
260227
return true;
261228
}
262229
}

0 commit comments

Comments
 (0)