Skip to content

Commit 1286620

Browse files
Take the greatest common divisor of large integers by Euclid rather than PeterO's (#2003)
PeterO's EInteger.Gcd is about 800 times slower than Euclid's algorithm when the gcd is large -- 1,228 ms against 1.5 ms -- and it is under every Rational.Create and every ToLowestTerms. GreatestCommonDivisors takes it by 64-bit Euclid for small operands and System.Numerics.BigInteger for large ones, with results identical to PeterO's, and the 177 gcd and lowest-terms call sites in 31 files go through it. Rational.Create is twice as fast for small numbers and about 5000 times at 2,600 bits; Rubi's 1.1.2.4:565 and 567 go from 204 s to 26 s. Answers are identical on 190 rows; across the chain's runs allocation on the problems answered on both builds falls 6%, and none is lost. 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 2f6e131 commit 1286620

34 files changed

Lines changed: 450 additions & 178 deletions

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
312312
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
313313
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
314314

315+
[AngouriMath/Functions/NumberTheory/GreatestCommonDivisor.cs]
316+
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
317+
315318
[Tests/UnitTests/Calculus/APowerTimesAFunctionOfItsPowerTest.cs]
316319
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
317320

‎Sources/AngouriMath/Convenience/MathS.Polynomials.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ private static bool SameAsOne(MultivariatePolynomial poly)
448448
var denominator = EInteger.One;
449449
foreach (var coefficient in rational)
450450
denominator = coefficient.Denominator
451-
.Divide(coefficient.Denominator.Gcd(denominator)).Multiply(denominator);
451+
.Divide(coefficient.Denominator.GreatestCommonDivisor(denominator)).Multiply(denominator);
452452
var whole = new EInteger[rational.Length];
453453
for (var i = 0; i < whole.Length; i++)
454454
whole[i] = rational[i].Numerator.Multiply(denominator.Divide(rational[i].Denominator));

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static class NumberTheory
192192
/// </code>
193193
/// </example>
194194
public static Integer GreatestCommonDivisor(Integer a, Integer b)
195-
=> a.EInteger.Gcd(b.EInteger);
195+
=> a.EInteger.GreatestCommonDivisor(b.EInteger);
196196

197197
}
198198

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public static Rational Create(ERational value)
127127
+ $"use {nameof(Real)}.{nameof(Real.Create)} instead");
128128

129129
if (!MathS.Settings.DowncastingEnabled)
130-
return new Rational(value.ToLowestTerms());
130+
return new Rational(value.InLowestTerms());
131131

132-
// Call ToLowestTerms() through new RationalNumber first
132+
// Call InLowestTerms() through new RationalNumber first
133133
// before determining whether the denominator equals one
134-
var @return = new Rational(value.ToLowestTerms());
134+
var @return = new Rational(value.InLowestTerms());
135135
if (@return.ERational.Denominator.Equals(1))
136136
return Integer.Create(@return.ERational.Numerator);
137137
else
@@ -208,7 +208,7 @@ public static Rational Create(ERational value)
208208
var rat = FindRationalExactly(inv, iterCount - 1);
209209
if (rat is null)
210210
return null;
211-
return new Rational((intPart * sign + sign / rat.ERational).ToLowestTerms());
211+
return new Rational((intPart * sign + sign / rat.ERational).InLowestTerms());
212212
}
213213
}
214214

‎Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ internal static class Fglm
174174
continue;
175175
var factor = vector[pivot].Divide(rowVectors[row][pivot]);
176176
for (var i = 0; i < vector.Length; i++)
177-
vector[i] = vector[i].Subtract(factor.Multiply(rowVectors[row][i])).ToLowestTerms();
177+
vector[i] = vector[i].Subtract(factor.Multiply(rowVectors[row][i])).InLowestTerms();
178178
for (var i = 0; i < rowCombinations[row].Length && i < combination.Length; i++)
179-
combination[i] = combination[i].Add(factor.Multiply(rowCombinations[row][i])).ToLowestTerms();
179+
combination[i] = combination[i].Add(factor.Multiply(rowCombinations[row][i])).InLowestTerms();
180180
}
181181

182182
var pivotAt = -1;

‎Sources/AngouriMath/Functions/Algebra/Groebner/MultivariatePolynomial.Groebner.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static MultivariatePolynomial Term(int variableCount, ulong monomial, E
4141
{
4242
var built = new Dictionary<ulong, ERational>();
4343
if (!coefficient.IsZero)
44-
built[monomial] = coefficient.ToLowestTerms();
44+
built[monomial] = coefficient.InLowestTerms();
4545
return new(variableCount, built);
4646
}
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,6 @@ private static RationalPolynomial Derivative(RationalPolynomial poly)
628628

629629
private static EInteger Lcm(EInteger left, EInteger right)
630630
=> left.IsZero || right.IsZero ? EInteger.One
631-
: left.Divide(left.Gcd(right)).Multiply(right);
631+
: left.Divide(left.GreatestCommonDivisor(right)).Multiply(right);
632632
}
633633
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ private static bool NextPoint(int[] point, int attempt, int count)
185185
if (Evaluate(pair.Value, others, point, depth + 1) is not { } inner)
186186
return null;
187187
total = total.Add(inner.Multiply(ERational.Create(at.Pow(pair.Key), EInteger.One)))
188-
.ToLowestTerms();
188+
.InLowestTerms();
189189
}
190190
return total;
191191
}
192192

193193
private static EInteger Lcm(EInteger left, EInteger right)
194-
=> left.Divide(left.Gcd(right)).Multiply(right);
194+
=> left.Divide(left.GreatestCommonDivisor(right)).Multiply(right);
195195
}
196196
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ internal EInteger Content()
194194
{
195195
var content = EInteger.Zero;
196196
foreach (var coefficient in coefficients)
197-
content = content.Gcd(coefficient);
197+
content = content.GreatestCommonDivisor(coefficient);
198198
return content;
199199
}
200200

@@ -276,7 +276,7 @@ internal static IntegerPolynomial Gcd(IntegerPolynomial left, IntegerPolynomial
276276
if (right.IsZero)
277277
return left.PrimitivePart().ScaleBy(left.Content());
278278

279-
var contentGcd = left.Content().Gcd(right.Content());
279+
var contentGcd = left.Content().GreatestCommonDivisor(right.Content());
280280
var a = left.PrimitivePart();
281281
var b = right.PrimitivePart();
282282
if (a.Degree < b.Degree)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private static (EInteger Residue, EInteger Modulus)? Residue(Entity divisor, Ent
390390
return null;
391391
// s k + e = 0 mod m: solvable where g = gcd(s, m) divides e, and then k is one class
392392
// modulo m / g, found by trying its members.
393-
var g = s.EInteger.Gcd(m);
393+
var g = s.EInteger.GreatestCommonDivisor(m);
394394
if (!e.EInteger.Remainder(g).IsZero)
395395
return (EInteger.Zero, EInteger.Zero);
396396
var classModulus = m.Divide(g);
@@ -408,7 +408,7 @@ private static (EInteger Residue, EInteger Modulus)? Residue(Entity divisor, Ent
408408
/// </summary>
409409
private static (EInteger Residue, EInteger Modulus)? Combined(EInteger r1, EInteger m1, EInteger r2, EInteger m2)
410410
{
411-
var combined = m1.Divide(m1.Gcd(m2)).Multiply(m2);
411+
var combined = m1.Divide(m1.GreatestCommonDivisor(m2)).Multiply(m2);
412412
if (combined.CompareTo(EInteger.FromInt32(MaxModulus)) > 0)
413413
return null;
414414
for (var t = r1; t.CompareTo(combined) < 0; t = t.Add(m1))

0 commit comments

Comments
 (0)