Skip to content

Commit 08a3c49

Browse files
Route the gcd and lowest-terms calls added since #2003 through GreatestCommonDivisors (#2006)
#2003 put the library's 177 gcd and lowest-terms call sites through GreatestCommonDivisors, whose results are PeterO's and whose large-operand case is about 800 times faster. Seventeen sites written since, in PartialFractions, IndefiniteIntegralSolver and RothsteinTrager, called EInteger.Gcd and ERational.ToLowestTerms directly again. On the 35 rows the PRs that wrote them moved, the answers and allocation are identical: the operands those rows see are small. The change keeps the contract in one place for the operands that are not. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8c46d55 commit 08a3c49

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,10 @@ private static (Entity P, Entity Q)[]? OverTheGaussianRoots(
11701170
|| Read(2) is not { } leading || leading.IsZero || Read(1) is not { } linear || Read(0) is not { } free)
11711171
return null;
11721172
// M = x^2 + s x + t = (x - ρ)^2 + σ^2.
1173-
var s = linear.Divide(leading).ToLowestTerms();
1174-
var t = free.Divide(leading).ToLowestTerms();
1175-
var rho = s.Negate().Divide(ERational.FromInt32(2)).ToLowestTerms();
1176-
var sigmaSquared = t.Subtract(rho.Multiply(rho)).ToLowestTerms();
1173+
var s = linear.Divide(leading).InLowestTerms();
1174+
var t = free.Divide(leading).InLowestTerms();
1175+
var rho = s.Negate().Divide(ERational.FromInt32(2)).InLowestTerms();
1176+
var sigmaSquared = t.Subtract(rho.Multiply(rho)).InLowestTerms();
11771177
if (sigmaSquared.Sign <= 0)
11781178
return null;
11791179
var (sigmaAbove, sigmaBelow) = (sigmaSquared.Numerator.Sqrt(), sigmaSquared.Denominator.Sqrt());
@@ -1296,8 +1296,8 @@ bool TakeIn(Entity side, bool above)
12961296
next[m].Im.Add(binomial[m].Im.Multiply(rho.Negate())).Add(binomial[m].Re.Multiply(sigma)));
12971297
}
12981298
binomial = next;
1299-
real[j] = binomial.Select(c => c.Re.ToLowestTerms()).ToArray();
1300-
imaginary[j] = binomial.Select(c => c.Im.ToLowestTerms()).ToArray();
1299+
real[j] = binomial.Select(c => c.Re.InLowestTerms()).ToArray();
1300+
imaginary[j] = binomial.Select(c => c.Im.InLowestTerms()).ToArray();
13011301
}
13021302

13031303
// The digits in powers of M: c_j (x - r)^(-j) and its conjugate are 2 Re(c_j (x - r̄)^j)/M^j,
@@ -1343,7 +1343,7 @@ private static ERational Power(ERational value, int exponent)
13431343
{
13441344
var result = ERational.One;
13451345
for (var k = 0; k < exponent; k++)
1346-
result = result.Multiply(value).ToLowestTerms();
1346+
result = result.Multiply(value).InLowestTerms();
13471347
return result;
13481348
}
13491349

@@ -1364,8 +1364,8 @@ private static ERational Power(ERational value, int exponent)
13641364
{
13651365
var coefficient = remainder[k];
13661366
quotient[k - 2] = coefficient;
1367-
remainder[k - 1] = remainder[k - 1].Subtract(coefficient.Multiply(s)).ToLowestTerms();
1368-
remainder[k - 2] = remainder[k - 2].Subtract(coefficient.Multiply(t)).ToLowestTerms();
1367+
remainder[k - 1] = remainder[k - 1].Subtract(coefficient.Multiply(s)).InLowestTerms();
1368+
remainder[k - 2] = remainder[k - 2].Subtract(coefficient.Multiply(t)).InLowestTerms();
13691369
}
13701370
digits.Add((remainder[0], remainder.Length > 1 ? remainder[1] : ERational.Zero));
13711371
current = quotient;

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ static bool APowerOfAQuotientOfLinearsVanishingAtPlusOrMinusOne(Entity inU, Vari
10891089
var kappa = Functions.PartialFractions.Bare((a2 / (qs * ss)).Simplify());
10901090
if (!VanishesIdentically(b2 - kappa * (ps * ss + qs * rs)) || !VanishesIdentically(c2 - kappa * ps * rs) || VanishesIdentically(c2))
10911091
return null;
1092-
var half = quadraticPower.Negate().Subtract(ERational.FromInt32(1).Divide(ERational.FromInt32(2))).ToLowestTerms();
1092+
var half = quadraticPower.Negate().Subtract(ERational.FromInt32(1).Divide(ERational.FromInt32(2))).InLowestTerms();
10931093
if (!half.Denominator.Equals(EInteger.One) || !half.Numerator.CanFitInInt32() || half.Numerator.Abs().CompareTo(EInteger.FromInt32(12)) > 0)
10941094
return null;
10951095
var bigK = half.Numerator.ToInt32Checked();
@@ -1178,9 +1178,9 @@ static bool APowerOfAQuotientOfLinearsVanishingAtPlusOrMinusOne(Entity inU, Vari
11781178
}
11791179
// L the largest exponent every one is a whole multiple of: the first times g/d, with d the
11801180
// denominators' least common multiple and g the numerators' greatest common divisor over it.
1181-
var denominator = ratios.Aggregate(EInteger.One, (lcm, r) => lcm * r.Denominator / lcm.Gcd(r.Denominator));
1181+
var denominator = ratios.Aggregate(EInteger.One, (lcm, r) => lcm * r.Denominator / lcm.GreatestCommonDivisor(r.Denominator));
11821182
var wholes = ratios.Select(r => r.Numerator * (denominator / r.Denominator)).ToList();
1183-
var divisor = wholes.Aggregate(EInteger.Zero, (gcd, n) => gcd.Gcd(n.Abs()));
1183+
var divisor = wholes.Aggregate(EInteger.Zero, (gcd, n) => gcd.GreatestCommonDivisor(n.Abs()));
11841184
if (divisor.IsZero)
11851185
return null;
11861186
var unit = Number.Rational.Create(divisor, denominator);
@@ -1279,7 +1279,7 @@ static bool AFactorAboveAQuadratic(List<KeyValuePair<EInteger, Entity>> terms, b
12791279
if (coefficient.Evaled is not Number.Rational rational || !power.CanFitInInt32() || power.ToInt32Checked() > 2 * IntegerPolynomial.MaxDegree)
12801280
return false;
12811281
rationals[halved ? power.ToInt32Checked() / 2 : power.ToInt32Checked()] = rational.ERational;
1282-
denominator = denominator * rational.ERational.Denominator / denominator.Gcd(rational.ERational.Denominator);
1282+
denominator = denominator * rational.ERational.Denominator / denominator.GreatestCommonDivisor(rational.ERational.Denominator);
12831283
}
12841284
var whole = new EInteger[rationals.Keys.Max() + 1];
12851285
for (var k = 0; k < whole.Length; k++)
@@ -18220,7 +18220,7 @@ private static bool IsAFunctionToRaise(Entity factor, Entity.Variable x)
1822018220
var (exponent, coefficient) = (ERational.Zero, term);
1822118221
if (term.ContainsNode(x) && !TryReadAMonomial(term, x, out exponent, out coefficient))
1822218222
return null;
18223-
read.Add((coefficient, exponent.ToLowestTerms()));
18223+
read.Add((coefficient, exponent.InLowestTerms()));
1822418224
}
1822518225
if (read.Count < 2)
1822618226
return null;
@@ -18232,7 +18232,7 @@ private static bool IsAFunctionToRaise(Entity factor, Entity.Variable x)
1823218232
var (degree, coefficient) = (ERational.Zero, term);
1823318233
if (term.ContainsNode(x) && !TryReadAMonomial(term, x, out degree, out coefficient))
1823418234
return null;
18235-
degree = degree.ToLowestTerms();
18235+
degree = degree.InLowestTerms();
1823618236
inB[degree] = inB.TryGetValue(degree, out var was) ? was + coefficient : coefficient;
1823718237
}
1823818238
var degrees = inB.Keys.Where(degree => !degree.IsZero).OrderBy(degree => degree).ToList();
@@ -18259,7 +18259,7 @@ private static bool IsAFunctionToRaise(Entity factor, Entity.Variable x)
1825918259
var inU = new Dictionary<int, Entity>();
1826018260
foreach (var (coefficient, exponent) in read)
1826118261
{
18262-
var scaled = exponent.Multiply(ERational.FromInt32(k)).Add(ERational.FromInt32(k - 1)).ToLowestTerms();
18262+
var scaled = exponent.Multiply(ERational.FromInt32(k)).Add(ERational.FromInt32(k - 1)).InLowestTerms();
1826318263
if (!scaled.Denominator.Equals(EInteger.One) || !scaled.Numerator.CanFitInInt32())
1826418264
return null;
1826518265
var e = scaled.Numerator.ToInt32Unchecked();

‎Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ internal static bool WantsASumOverRootsAlone(Entity numerator, Entity denominato
370370
private static bool InAPowerOfAShift(IntegerPolynomial poly)
371371
{
372372
var n = poly.Degree;
373-
var shift = ERational.Create(poly[n - 1], poly[n].Multiply(EInteger.FromInt32(n))).ToLowestTerms();
373+
var shift = ERational.Create(poly[n - 1], poly[n].Multiply(EInteger.FromInt32(n))).InLowestTerms();
374374
// Horner in y - s: g = g (y - s) + a_i, from the leading coefficient down.
375375
var g = new ERational[n + 1];
376376
for (var i = 0; i <= n; i++)

0 commit comments

Comments
 (0)