diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 74fff6705..dc1e9ea43 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -312,6 +312,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/NumberTheory/ResidueClasses.cs] 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 +[AngouriMath/Functions/NumberTheory/GreatestCommonDivisor.cs] +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 + [Tests/UnitTests/Calculus/APowerTimesAFunctionOfItsPowerTest.cs] 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 diff --git a/Sources/AngouriMath/Convenience/MathS.Polynomials.cs b/Sources/AngouriMath/Convenience/MathS.Polynomials.cs index aabf21c4d..9797f22e0 100644 --- a/Sources/AngouriMath/Convenience/MathS.Polynomials.cs +++ b/Sources/AngouriMath/Convenience/MathS.Polynomials.cs @@ -448,7 +448,7 @@ private static bool SameAsOne(MultivariatePolynomial poly) var denominator = EInteger.One; foreach (var coefficient in rational) denominator = coefficient.Denominator - .Divide(coefficient.Denominator.Gcd(denominator)).Multiply(denominator); + .Divide(coefficient.Denominator.GreatestCommonDivisor(denominator)).Multiply(denominator); var whole = new EInteger[rational.Length]; for (var i = 0; i < whole.Length; i++) whole[i] = rational[i].Numerator.Multiply(denominator.Divide(rational[i].Denominator)); diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 6252e36a3..02812987f 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -192,7 +192,7 @@ public static class NumberTheory /// /// public static Integer GreatestCommonDivisor(Integer a, Integer b) - => a.EInteger.Gcd(b.EInteger); + => a.EInteger.GreatestCommonDivisor(b.EInteger); } diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs index 1afff767d..727ba11d4 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs @@ -127,11 +127,11 @@ public static Rational Create(ERational value) + $"use {nameof(Real)}.{nameof(Real.Create)} instead"); if (!MathS.Settings.DowncastingEnabled) - return new Rational(value.ToLowestTerms()); + return new Rational(value.InLowestTerms()); - // Call ToLowestTerms() through new RationalNumber first + // Call InLowestTerms() through new RationalNumber first // before determining whether the denominator equals one - var @return = new Rational(value.ToLowestTerms()); + var @return = new Rational(value.InLowestTerms()); if (@return.ERational.Denominator.Equals(1)) return Integer.Create(@return.ERational.Numerator); else @@ -208,7 +208,7 @@ public static Rational Create(ERational value) var rat = FindRationalExactly(inv, iterCount - 1); if (rat is null) return null; - return new Rational((intPart * sign + sign / rat.ERational).ToLowestTerms()); + return new Rational((intPart * sign + sign / rat.ERational).InLowestTerms()); } } diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs index 0954546d1..f3c99b250 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs @@ -174,9 +174,9 @@ internal static class Fglm continue; var factor = vector[pivot].Divide(rowVectors[row][pivot]); for (var i = 0; i < vector.Length; i++) - vector[i] = vector[i].Subtract(factor.Multiply(rowVectors[row][i])).ToLowestTerms(); + vector[i] = vector[i].Subtract(factor.Multiply(rowVectors[row][i])).InLowestTerms(); for (var i = 0; i < rowCombinations[row].Length && i < combination.Length; i++) - combination[i] = combination[i].Add(factor.Multiply(rowCombinations[row][i])).ToLowestTerms(); + combination[i] = combination[i].Add(factor.Multiply(rowCombinations[row][i])).InLowestTerms(); } var pivotAt = -1; diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/MultivariatePolynomial.Groebner.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/MultivariatePolynomial.Groebner.cs index adb4d958e..c166e7dc1 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/MultivariatePolynomial.Groebner.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/MultivariatePolynomial.Groebner.cs @@ -41,7 +41,7 @@ internal static MultivariatePolynomial Term(int variableCount, ulong monomial, E { var built = new Dictionary(); if (!coefficient.IsZero) - built[monomial] = coefficient.ToLowestTerms(); + built[monomial] = coefficient.InLowestTerms(); return new(variableCount, built); } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/BivariateHenselFactorization.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/BivariateHenselFactorization.cs index b7738631c..1bda46b8f 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/BivariateHenselFactorization.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/BivariateHenselFactorization.cs @@ -628,6 +628,6 @@ private static RationalPolynomial Derivative(RationalPolynomial poly) private static EInteger Lcm(EInteger left, EInteger right) => left.IsZero || right.IsZero ? EInteger.One - : left.Divide(left.Gcd(right)).Multiply(right); + : left.Divide(left.GreatestCommonDivisor(right)).Multiply(right); } } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/EvaluationHomomorphism.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/EvaluationHomomorphism.cs index 393519fff..ca3a96f54 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/EvaluationHomomorphism.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/EvaluationHomomorphism.cs @@ -185,12 +185,12 @@ private static bool NextPoint(int[] point, int attempt, int count) if (Evaluate(pair.Value, others, point, depth + 1) is not { } inner) return null; total = total.Add(inner.Multiply(ERational.Create(at.Pow(pair.Key), EInteger.One))) - .ToLowestTerms(); + .InLowestTerms(); } return total; } private static EInteger Lcm(EInteger left, EInteger right) - => left.Divide(left.Gcd(right)).Multiply(right); + => left.Divide(left.GreatestCommonDivisor(right)).Multiply(right); } } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/IntegerPolynomial.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/IntegerPolynomial.cs index 9eaf33c02..c9ab2bce5 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/IntegerPolynomial.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/IntegerPolynomial.cs @@ -194,7 +194,7 @@ internal EInteger Content() { var content = EInteger.Zero; foreach (var coefficient in coefficients) - content = content.Gcd(coefficient); + content = content.GreatestCommonDivisor(coefficient); return content; } @@ -276,7 +276,7 @@ internal static IntegerPolynomial Gcd(IntegerPolynomial left, IntegerPolynomial if (right.IsZero) return left.PrimitivePart().ScaleBy(left.Content()); - var contentGcd = left.Content().Gcd(right.Content()); + var contentGcd = left.Content().GreatestCommonDivisor(right.Content()); var a = left.PrimitivePart(); var b = right.PrimitivePart(); if (a.Degree < b.Degree) diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/IversonSum.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/IversonSum.cs index e264c87fe..8876f3d28 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/IversonSum.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/IversonSum.cs @@ -390,7 +390,7 @@ private static (EInteger Residue, EInteger Modulus)? Residue(Entity divisor, Ent return null; // s k + e = 0 mod m: solvable where g = gcd(s, m) divides e, and then k is one class // modulo m / g, found by trying its members. - var g = s.EInteger.Gcd(m); + var g = s.EInteger.GreatestCommonDivisor(m); if (!e.EInteger.Remainder(g).IsZero) return (EInteger.Zero, EInteger.Zero); var classModulus = m.Divide(g); @@ -408,7 +408,7 @@ private static (EInteger Residue, EInteger Modulus)? Residue(Entity divisor, Ent /// private static (EInteger Residue, EInteger Modulus)? Combined(EInteger r1, EInteger m1, EInteger r2, EInteger m2) { - var combined = m1.Divide(m1.Gcd(m2)).Multiply(m2); + var combined = m1.Divide(m1.GreatestCommonDivisor(m2)).Multiply(m2); if (combined.CompareTo(EInteger.FromInt32(MaxModulus)) > 0) return null; for (var t = r1; t.CompareTo(combined) < 0; t = t.Add(m1)) diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/KroneckerFactorization.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/KroneckerFactorization.cs index bfba7a99c..12e72f1a7 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/KroneckerFactorization.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/KroneckerFactorization.cs @@ -221,7 +221,7 @@ bool Peel(MultivariatePolynomial rest, int depth, int at) } private static EInteger Lcm(EInteger left, EInteger right) - => left.Divide(left.Gcd(right)).Multiply(right); + => left.Divide(left.GreatestCommonDivisor(right)).Multiply(right); /// /// Reading each exponent back as one digit of the numeral — or diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/MultivariatePolynomial.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/MultivariatePolynomial.cs index 7744f6dc2..1ef9da7fe 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/MultivariatePolynomial.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/MultivariatePolynomial.cs @@ -105,7 +105,7 @@ internal static MultivariatePolynomial Constant(int variableCount, ERational val { var terms = new Dictionary(); if (!value.IsZero) - terms[0] = value.ToLowestTerms(); + terms[0] = value.InLowestTerms(); return new(variableCount, terms); } @@ -177,7 +177,7 @@ private static void Accumulate(Dictionary into, ulong monomial into[monomial] = value; return; } - var sum = existing.Add(value).ToLowestTerms(); + var sum = existing.Add(value).InLowestTerms(); if (sum.IsZero) into.Remove(monomial); else @@ -194,7 +194,7 @@ private static void Accumulate(Dictionary into, ulong monomial { if (!TryMultiplyMonomials(left.Key, right.Key, VariableCount, out var monomial)) return null; - Accumulate(result, monomial, left.Value.Multiply(right.Value).ToLowestTerms()); + Accumulate(result, monomial, left.Value.Multiply(right.Value).InLowestTerms()); if (result.Count > maxTerms) return null; } @@ -231,7 +231,7 @@ internal MultivariatePolynomial ScaleBy(ERational factor) return Zero(VariableCount); var result = new Dictionary(terms.Count); foreach (var term in terms) - result[term.Key] = term.Value.Multiply(factor).ToLowestTerms(); + result[term.Key] = term.Value.Multiply(factor).InLowestTerms(); return new(VariableCount, result); } @@ -324,7 +324,7 @@ internal MultivariatePolynomial DerivativeIn(int variable) if (power == 0) continue; Accumulate(result, Without(term.Key, variable) | Pack(variable, power - 1), - term.Value.Multiply(ERational.FromInt32(power)).ToLowestTerms()); + term.Value.Multiply(ERational.FromInt32(power)).InLowestTerms()); } return new(VariableCount, result); } @@ -347,7 +347,7 @@ internal MultivariatePolynomial DerivativeIn(int variable) return this; if (IsConstant) { - var value = ConstantValue.ToLowestTerms(); + var value = ConstantValue.InLowestTerms(); if (value.Sign < 0) return null; var (above, below) = (value.Numerator.Sqrt(), value.Denominator.Sqrt()); @@ -443,7 +443,7 @@ internal MultivariatePolynomial LeadingCoefficientIn(int variable) // no later step can make it one -- so this is a "no" and not a "gave up". if (!TryDivideMonomials(lead, divisorLead, VariableCount, out var monomial)) return null; - var value = rest.terms[lead].Divide(divisorValue).ToLowestTerms(); + var value = rest.terms[lead].Divide(divisorValue).InLowestTerms(); quotient[monomial] = value; if (divisor.MultiplyByTerm(monomial, value) is not { } product) { @@ -468,7 +468,7 @@ internal MultivariatePolynomial LeadingCoefficientIn(int variable) { if (!TryMultiplyMonomials(term.Key, monomial, VariableCount, out var product)) return null; - result[product] = term.Value.Multiply(value).ToLowestTerms(); + result[product] = term.Value.Multiply(value).InLowestTerms(); } return new(VariableCount, result); } @@ -526,7 +526,7 @@ internal MultivariatePolynomial Normalized(out ERational scale) denominators = Lcm(denominators, term.Value.Denominator); var numerators = EInteger.Zero; foreach (var term in terms) - numerators = numerators.Gcd( + numerators = numerators.GreatestCommonDivisor( term.Value.Numerator.Multiply(denominators.Divide(term.Value.Denominator))); if (numerators.IsZero) return this; @@ -536,7 +536,7 @@ internal MultivariatePolynomial Normalized(out ERational scale) return ScaleBy(scale); } - private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.Gcd(b)).Multiply(b); + private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.GreatestCommonDivisor(b)).Multiply(b); internal bool SameAs(MultivariatePolynomial other) { @@ -606,7 +606,7 @@ internal bool SameAs(MultivariatePolynomial other) var inverse = ERational.One.Divide(parsed.ConstantValue); var result = ERational.One; for (var i = 0; i < -power; i++) - result = result.Multiply(inverse).ToLowestTerms(); + result = result.Multiply(inverse).InLowestTerms(); return Constant(variables.Count, result); } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs index f9f92cb32..6a175312a 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs @@ -1849,13 +1849,13 @@ private static bool TrySolveByLifting(ERational[][] augmented, int rows, int wid var common = EInteger.One; for (var column = 0; column <= width; column++) { - var reduced = augmented[row][column].ToLowestTerms(); + var reduced = augmented[row][column].InLowestTerms(); augmented[row][column] = reduced; - common = common.Multiply(reduced.Denominator).Divide(common.Gcd(reduced.Denominator)); + common = common.Multiply(reduced.Denominator).Divide(common.GreatestCommonDivisor(reduced.Denominator)); } a[row] = new EInteger[width + 1]; for (var column = 0; column <= width; column++) - a[row][column] = augmented[row][column].Multiply(ERational.FromEInteger(common)).ToLowestTerms().Numerator; + a[row][column] = augmented[row][column].Multiply(ERational.FromEInteger(common)).InLowestTerms().Numerator; } var prime = EInteger.FromInt64(LiftingPrime); static long Mod(EInteger value, EInteger prime) @@ -2032,7 +2032,7 @@ static long Inverse(long value) var sum = ERational.Zero; for (var j = 0; j < rank; j++) sum = sum.Add(solution[j].Multiply(ERational.FromEInteger(s[i][j]))); - holds = sum.Subtract(ERational.FromEInteger(t[i])).ToLowestTerms().IsZero; + holds = sum.Subtract(ERational.FromEInteger(t[i])).InLowestTerms().IsZero; } if (holds) break; @@ -2047,14 +2047,14 @@ static long Inverse(long value) var sum = ERational.Zero; for (var j = 0; j < rank; j++) sum = sum.Add(solution[j].Multiply(ERational.FromEInteger(a[row][pivotColumns[j]]))); - if (!sum.Subtract(ERational.FromEInteger(a[row][width])).ToLowestTerms().IsZero) + if (!sum.Subtract(ERational.FromEInteger(a[row][width])).InLowestTerms().IsZero) return false; } values = new Entity[width]; for (var column = 0; column < width; column++) values[column] = Integer.Zero; for (var j = 0; j < rank; j++) - values[pivotColumns[j]] = Rational.Create(solution[j].ToLowestTerms()); + values[pivotColumns[j]] = Rational.Create(solution[j].InLowestTerms()); return true; } @@ -2079,7 +2079,7 @@ private static bool TryReconstructRational(EInteger u, EInteger m, out ERational } if (t1.IsZero || t1.Abs().CompareTo(bound) > 0) return false; - value = ERational.Create(t1.Sign < 0 ? r1.Negate() : r1, t1.Abs()).ToLowestTerms(); + value = ERational.Create(t1.Sign < 0 ? r1.Negate() : r1, t1.Abs()).InLowestTerms(); return true; } @@ -2132,7 +2132,7 @@ private static bool TrySolveOverRationals(Entity[][] matrix, Entity[] rhs, [NotN // that is not grows with every row it is used against. var pivotValue = augmented[rank][column]; for (var k = 0; k <= width; k++) - augmented[rank][k] = augmented[rank][k].Divide(pivotValue).ToLowestTerms(); + augmented[rank][k] = augmented[rank][k].Divide(pivotValue).InLowestTerms(); for (var row = 0; row < rows; row++) { if (row == rank) @@ -2141,7 +2141,7 @@ private static bool TrySolveOverRationals(Entity[][] matrix, Entity[] rhs, [NotN if (factor.IsZero) continue; for (var k = 0; k <= width; k++) - augmented[row][k] = augmented[row][k].Subtract(factor.Multiply(augmented[rank][k])).ToLowestTerms(); + augmented[row][k] = augmented[row][k].Subtract(factor.Multiply(augmented[rank][k])).InLowestTerms(); } pivotColumnOfRow[rank] = column; rank++; @@ -2153,7 +2153,7 @@ private static bool TrySolveOverRationals(Entity[][] matrix, Entity[] rhs, [NotN for (var column = 0; column < width; column++) values[column] = Integer.Zero; for (var row = 0; row < rank; row++) - values[pivotColumnOfRow[row]] = Rational.Create(augmented[row][width].ToLowestTerms()); + values[pivotColumnOfRow[row]] = Rational.Create(augmented[row][width].InLowestTerms()); return true; } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactoring.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactoring.cs index b60a05151..54f2613fd 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactoring.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactoring.cs @@ -414,7 +414,7 @@ private static IEnumerable RationalRootCandidates(ERational[] coeffic return candidates.Distinct(); } - private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.Gcd(b)).Multiply(b); + private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.GreatestCommonDivisor(b)).Multiply(b); private static IEnumerable Divisors(EInteger n) { diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactorization.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactorization.cs index 67770ad61..1cd738c3e 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactorization.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialFactorization.cs @@ -222,7 +222,7 @@ internal static bool IsWorthFactoringToSolve(Entity expr, Variable x) if (FactorPrimitive(primitive) is not { } parts) return null; - return new Factorization(ERational.Create(content, denominator).ToLowestTerms(), parts); + return new Factorization(ERational.Create(content, denominator).InLowestTerms(), parts); } /// @@ -577,7 +577,7 @@ private static bool NextCombination(int[] indices, int poolCount) return true; } - private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.Gcd(b)).Multiply(b); + private static EInteger Lcm(EInteger a, EInteger b) => a.Divide(a.GreatestCommonDivisor(b)).Multiply(b); /// A polynomial written as a rational constant times powers of irreducibles. internal readonly struct Factorization diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalFunction.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalFunction.cs index 1abc165db..ceb494fd1 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalFunction.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalFunction.cs @@ -135,7 +135,7 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity return false; if (leading.CompareTo(ERational.One) != 0) { - var inverse = ERational.One.Divide(leading).ToLowestTerms(); + var inverse = ERational.One.Divide(leading).InLowestTerms(); numerator = numerator.ScaleBy(inverse); denominator = denominator.ScaleBy(inverse); } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalPolynomial.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalPolynomial.cs index 1f3ee6bb7..69b05ab23 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalPolynomial.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/RationalPolynomial.cs @@ -56,14 +56,14 @@ internal static RationalPolynomial Create(IReadOnlyList coefficientsL return Zero; var trimmed = new ERational[length]; for (var i = 0; i < length; i++) - trimmed[i] = coefficientsLowestFirst[i].ToLowestTerms(); + trimmed[i] = coefficientsLowestFirst[i].InLowestTerms(); return new(trimmed); } [ConstantField] internal static readonly RationalPolynomial Zero = new(NoCoefficients); internal static RationalPolynomial Constant(ERational value) - => value.IsZero ? Zero : new(new[] { value.ToLowestTerms() }); + => value.IsZero ? Zero : new(new[] { value.InLowestTerms() }); internal static RationalPolynomial One => Constant(ERational.One); @@ -129,7 +129,7 @@ internal RationalPolynomial Multiply(RationalPolynomial other) { if (other.coefficients[j].IsZero) continue; - product[i + j] = product[i + j].Add(coefficients[i].Multiply(other.coefficients[j])).ToLowestTerms(); + product[i + j] = product[i + j].Add(coefficients[i].Multiply(other.coefficients[j])).InLowestTerms(); } } return Create(product); @@ -170,11 +170,11 @@ internal bool TryDivide( { if (working[power].IsZero) continue; - var factor = working[power].Divide(divisor.Leading).ToLowestTerms(); + var factor = working[power].Divide(divisor.Leading).InLowestTerms(); quotientCoefficients[power - divisor.Degree] = factor; for (var i = 0; i <= divisor.Degree; i++) working[power - divisor.Degree + i] = - working[power - divisor.Degree + i].Subtract(factor.Multiply(divisor[i])).ToLowestTerms(); + working[power - divisor.Degree + i].Subtract(factor.Multiply(divisor[i])).InLowestTerms(); } quotient = Create(quotientCoefficients); diff --git a/Sources/AngouriMath/Functions/Algebra/SumOverSet.cs b/Sources/AngouriMath/Functions/Algebra/SumOverSet.cs index ccb8764b1..a1ea708d9 100644 --- a/Sources/AngouriMath/Functions/Algebra/SumOverSet.cs +++ b/Sources/AngouriMath/Functions/Algebra/SumOverSet.cs @@ -212,7 +212,7 @@ internal static class SumOverSet rational[MultivariatePolynomial.PowerOf(term.Key, 0)] = term.Value; var denominator = EInteger.One; foreach (var coefficient in rational) - denominator = denominator.Divide(denominator.Gcd(coefficient.Denominator)).Multiply(coefficient.Denominator); + denominator = denominator.Divide(denominator.GreatestCommonDivisor(coefficient.Denominator)).Multiply(coefficient.Denominator); var whole = new EInteger[rational.Length]; for (var i = 0; i < whole.Length; i++) whole[i] = rational[i].Numerator.Multiply(denominator.Divide(rational[i].Denominator)); diff --git a/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs b/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs index 4ef9e3e90..3ccdda17a 100644 --- a/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs +++ b/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs @@ -344,7 +344,7 @@ private static (Entity Numerator, Integer Denominator)? WholeOverAWholeNumber(En Divf(_, Integer { EInteger: var by }) when !by.IsZero => by.Abs(), _ => EInteger.One, }; - denominator = denominator.Divide(denominator.Gcd(divisor)).Multiply(divisor); + denominator = denominator.Divide(denominator.GreatestCommonDivisor(divisor)).Multiply(divisor); } if (denominator.Equals(EInteger.One)) return null; diff --git a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs index c4bc8a3fe..ed4051fe3 100644 --- a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs +++ b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs @@ -931,7 +931,7 @@ private static void CollectAtoms(Entity statement, List into) || rest.ContainsNode(x) || slope.Evaled is not Integer { EInteger: var step }) return null; var m = modulus.Abs(); - return step.IsZero ? EInteger.One : m.Divide(m.Gcd(step.Abs())); + return step.IsZero ? EInteger.One : m.Divide(m.GreatestCommonDivisor(step.Abs())); } /// @@ -967,7 +967,7 @@ private static (EInteger Period, EInteger Threshold)? Settled(Entity body, Varia continue; if (DivisibilityPeriod(atom, x) is { } repeats) { - period = period.Divide(period.Gcd(repeats)).Multiply(repeats); + period = period.Divide(period.GreatestCommonDivisor(repeats)).Multiply(repeats); if (period.CompareTo(EInteger.FromInt32(LargestPrefix)) > 0) return null; any = true; @@ -1056,7 +1056,7 @@ void Conjuncts(Entity statement) continue; if (DivisibilityPeriod(conjunct, x) is { } repeats) { - period = period.Divide(period.Gcd(repeats)).Multiply(repeats); + period = period.Divide(period.GreatestCommonDivisor(repeats)).Multiply(repeats); repeating = true; continue; } @@ -1168,7 +1168,7 @@ void Tell(Entity verdict) } // The units modulo M, each the residue of infinitely many primes past the threshold. for (var residue = EInteger.One; residue.CompareTo(modulus) <= 0; residue += 1) - if (residue.Gcd(modulus).Equals(EInteger.One)) + if (residue.GreatestCommonDivisor(modulus).Equals(EInteger.One)) Tell(past.Substitute(x, Integer.Create(residue)).InnerSimplified(isExact)); if (Tally(kind, holds, fails, undecided, distinct: true) is not { } verdict) return null; @@ -1203,7 +1203,7 @@ void Tell(Entity verdict) return null; if (Period(first, x, set) is not { } one || Period(second, x, set) is not { } another) return null; - return one * another / one.Gcd(another); + return one * another / one.GreatestCommonDivisor(another); // An equation or an order comparison between residues -- a^7 mod 7 = a mod 7, // Fermat's little theorem as Sullivan and Mackey write it (Prob 8.9.26) -- repeats // with the least common multiple of the moduli it mentions. @@ -1216,7 +1216,7 @@ void Tell(Entity verdict) return rounding; if (ResiduePeriod(lhs, x, set) is not { } l || ResiduePeriod(rhs, x, set) is not { } r) return null; - return l * r / l.Gcd(r); + return l * r / l.GreatestCommonDivisor(r); default: return body.ContainsNode(x) ? null : EInteger.One; } @@ -1281,7 +1281,7 @@ private static bool OnlyAbout(Entity condition, Variable x) return null; if (ResiduePeriod(first, x, set) is not { } one || ResiduePeriod(second, x, set) is not { } another) return null; - return one * another / one.Gcd(another); + return one * another / one.GreatestCommonDivisor(another); default: return null; } @@ -1318,7 +1318,7 @@ private static bool OnlyAbout(Entity condition, Variable x) { rounded = true; var q = slope.Denominator.Abs(); - period = period * q / period.Gcd(q); + period = period * q / period.GreatestCommonDivisor(q); } drift = drift.Add(coefficient.Multiply(slope)); } @@ -1354,7 +1354,7 @@ private static bool OnlyAbout(Entity condition, Variable x) { if (power.Base.Evaled is not Integer { EInteger: var @base } || !power.Exponent.Vars.All(v => v == x) || !IsWholePolynomial(power.Exponent)) return null; - if (!@base.Gcd(modulus).Equals(EInteger.One)) + if (!@base.GreatestCommonDivisor(modulus).Equals(EInteger.One)) return null; var order = EInteger.One; var value = @base.Mod(modulus); @@ -1365,7 +1365,7 @@ private static bool OnlyAbout(Entity condition, Variable x) } if (!value.Equals(EInteger.One)) return null; - period = period * order / period.Gcd(order); + period = period * order / period.GreatestCommonDivisor(order); replaced = replaced.Substitute(power, Variable.CreateUnique(expr, "power_" + count++)); } return IsWholePolynomial(replaced) ? period : null; diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/CubeRootLogarithmAnsatz.cs b/Sources/AngouriMath/Functions/Continuous/Integration/CubeRootLogarithmAnsatz.cs index fbde838e9..8abcf5ec4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/CubeRootLogarithmAnsatz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/CubeRootLogarithmAnsatz.cs @@ -380,16 +380,16 @@ internal static bool TryReadOverAQuadratic(Entity value, Variable c, RationalPol { if (!TryReadOverAQuadratic(l, c, modulus, out var l0, out var l1) || !TryReadOverAQuadratic(r, c, modulus, out var r0, out var r1)) return false; - a0 = l0.Add(r0).ToLowestTerms(); - a1 = l1.Add(r1).ToLowestTerms(); + a0 = l0.Add(r0).InLowestTerms(); + a1 = l1.Add(r1).InLowestTerms(); return true; } case Minusf(var l, var r): { if (!TryReadOverAQuadratic(l, c, modulus, out var l0, out var l1) || !TryReadOverAQuadratic(r, c, modulus, out var r0, out var r1)) return false; - a0 = l0.Subtract(r0).ToLowestTerms(); - a1 = l1.Subtract(r1).ToLowestTerms(); + a0 = l0.Subtract(r0).InLowestTerms(); + a1 = l1.Subtract(r1).InLowestTerms(); return true; } case Mulf(var l, var r): @@ -398,8 +398,8 @@ internal static bool TryReadOverAQuadratic(Entity value, Variable c, RationalPol return false; // (l0 + l1 c)(r0 + r1 c), with c^2 = -m1 c - m0. var cross = l1.Multiply(r1); - a0 = l0.Multiply(r0).Subtract(m0.Multiply(cross)).ToLowestTerms(); - a1 = l0.Multiply(r1).Add(l1.Multiply(r0)).Subtract(m1.Multiply(cross)).ToLowestTerms(); + a0 = l0.Multiply(r0).Subtract(m0.Multiply(cross)).InLowestTerms(); + a1 = l0.Multiply(r1).Add(l1.Multiply(r0)).Subtract(m1.Multiply(cross)).InLowestTerms(); return true; } case Divf(var l, var r): @@ -407,8 +407,8 @@ internal static bool TryReadOverAQuadratic(Entity value, Variable c, RationalPol if (!TryReadOverAQuadratic(l, c, modulus, out var l0, out var l1) || !TryReadOverAQuadratic(r, c, modulus, out var r0, out var r1) || !r1.IsZero || r0.IsZero) return false; - a0 = l0.Divide(r0).ToLowestTerms(); - a1 = l1.Divide(r0).ToLowestTerms(); + a0 = l0.Divide(r0).InLowestTerms(); + a1 = l1.Divide(r0).InLowestTerms(); return true; } case Powf(var b, Integer k) when k.EInteger.CanFitInInt32() && k.EInteger.Sign >= 0 && k.EInteger.ToInt32Unchecked() <= 8: @@ -419,8 +419,8 @@ internal static bool TryReadOverAQuadratic(Entity value, Variable c, RationalPol for (var i = 0; i < k.EInteger.ToInt32Unchecked(); i++) { var cross = a1.Multiply(b1); - var next0 = a0.Multiply(b0).Subtract(m0.Multiply(cross)).ToLowestTerms(); - a1 = a0.Multiply(b1).Add(a1.Multiply(b0)).Subtract(m1.Multiply(cross)).ToLowestTerms(); + var next0 = a0.Multiply(b0).Subtract(m0.Multiply(cross)).InLowestTerms(); + a1 = a0.Multiply(b1).Add(a1.Multiply(b0)).Subtract(m1.Multiply(cross)).InLowestTerms(); a0 = next0; } return true; @@ -728,7 +728,7 @@ void Gather(Entity e, bool underneath) { var f = part.Factor; if (f.Degree == 1) - roots.Add(Rational.Create(ERational.Create(f[0], f[1]).Negate().ToLowestTerms())); + roots.Add(Rational.Create(ERational.Create(f[0], f[1]).Negate().InLowestTerms())); else if (f.Degree == 2) quadratics.Add(RationalPolynomial.FromInteger(f).ToEntity(x)); } @@ -767,16 +767,16 @@ private static IEnumerable LinesOverAQuadraticFactor(Entity quadratic, E var pp = q[1]; var qq = q[0]; yield return rootOfM; - var discriminantOverThree = qq.Multiply(ERational.FromInt32(4)).Subtract(pp.Multiply(pp)).Divide(ERational.FromInt32(3)).ToLowestTerms(); + var discriminantOverThree = qq.Multiply(ERational.FromInt32(4)).Subtract(pp.Multiply(pp)).Divide(ERational.FromInt32(3)).InLowestTerms(); if (!TryRationalSquareRoot(discriminantOverThree, out var s)) yield break; foreach (var sign in new[] { 1, -1 }) { - var t = pp.Add(s.Multiply(ERational.FromInt32(sign))).Divide(ERational.FromInt32(2)).ToLowestTerms(); - var cubeOfU = ERational.One.Divide(pp.Multiply(qq).Subtract(t.Multiply(qq).Multiply(ERational.FromInt32(3))).Add(t.Multiply(t).Multiply(t))).ToLowestTerms(); + var t = pp.Add(s.Multiply(ERational.FromInt32(sign))).Divide(ERational.FromInt32(2)).InLowestTerms(); + var cubeOfU = ERational.One.Divide(pp.Multiply(qq).Subtract(t.Multiply(qq).Multiply(ERational.FromInt32(3))).Add(t.Multiply(t).Multiply(t))).InLowestTerms(); if (!TryRationalCubeRoot(cubeOfU, out var u)) continue; - var v = t.Multiply(u).ToLowestTerms(); + var v = t.Multiply(u).InLowestTerms(); yield return rootOfM * (Rational.Create(u) * x + Rational.Create(v)); } } @@ -790,7 +790,7 @@ private static bool TryRationalSquareRoot(ERational value, out ERational root) var d = value.Denominator.Sqrt(); if (!n.Multiply(n).Equals(value.Numerator) || !d.Multiply(d).Equals(value.Denominator)) return false; - root = ERational.Create(n, d).ToLowestTerms(); + root = ERational.Create(n, d).InLowestTerms(); return true; } @@ -803,7 +803,7 @@ private static bool TryRationalCubeRoot(ERational value, out ERational root) var d = magnitude.Denominator.Root(3); if (!n.Pow(3).Equals(magnitude.Numerator) || !d.Pow(3).Equals(magnitude.Denominator)) return false; - root = ERational.Create(negative ? n.Negate() : n, d).ToLowestTerms(); + root = ERational.Create(negative ? n.Negate() : n, d).InLowestTerms(); return true; } @@ -867,7 +867,7 @@ private set root = c * c / Integer.Create(cubeOfSquare); if (root is null) return null; - var scale = ERational.Create(negative ? cube.Negate() : cube, magnitude.Denominator).ToLowestTerms(); + var scale = ERational.Create(negative ? cube.Negate() : cube, magnitude.Denominator).InLowestTerms(); return Rational.Create(scale) * root; } diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index f6787daff..4ad691491 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -2897,7 +2897,7 @@ private static Entity WithTheDenominatorInPowersOf(Entity value, IReadOnlyList 0) @@ -8345,7 +8345,7 @@ private static bool TryReadABinomialDifferential( if (!Read(expr, 1) || bracket is null) return false; - var lowest = exponentFound.ToLowestTerms(); + var lowest = exponentFound.InLowestTerms(); if (lowest.Denominator.Equals(EInteger.One) || !lowest.Numerator.CanFitInInt32() || !lowest.Denominator.CanFitInInt32()) return false; @@ -9032,15 +9032,15 @@ void AddALinear(Entity linear, int power) var denominators = EInteger.One; foreach (var multiple in multiples.Values) { - numerators = numerators.Gcd(multiple.Numerator.Abs()); - denominators = denominators.Multiply(multiple.Denominator).Divide(denominators.Gcd(multiple.Denominator)); + numerators = numerators.GreatestCommonDivisor(multiple.Numerator.Abs()); + denominators = denominators.Multiply(multiple.Denominator).Divide(denominators.GreatestCommonDivisor(multiple.Denominator)); } var step = ERational.Create(numerators, denominators); - if (multiples.Values.Any(multiple => multiple.Divide(step).ToLowestTerms().Numerator.Abs().CompareTo(EInteger.FromInt32(12)) > 0)) + if (multiples.Values.Any(multiple => multiple.Divide(step).InLowestTerms().Numerator.Abs().CompareTo(EInteger.FromInt32(12)) > 0)) return null; var w = Variable.CreateUnique(expr, "w_exp"); var inW = exponentials.Replace(node => - multiples.TryGetValue(node, out var multiple) ? MathS.Pow(w, Number.Integer.Create(multiple.Divide(step).ToLowestTerms().Numerator)) : node); + multiples.TryGetValue(node, out var multiple) ? MathS.Pow(w, Number.Integer.Create(multiple.Divide(step).InLowestTerms().Numerator)) : node); if (inW.ContainsNode(x)) return null; @@ -10936,7 +10936,7 @@ bool Read(Entity node, ERational multiplicity) // `sin^3 cos^(-3/2)` is its negative there. An odd denominator is read as // it always was, the real root: `(c sin^3)^(1/3)` as `c^(1/3) sin`, which // is the integrand wherever that is real. - if ((multiplicity * power.ERational).ToLowestTerms().Denominator.IsEven + if ((multiplicity * power.ERational).InLowestTerms().Denominator.IsEven && @base is not TrigonometricFunction && !(@base is Mulf(Number.Rational { IsPositive: true }, TrigonometricFunction))) distributed = true; @@ -10972,7 +10972,7 @@ private static bool HasAnEvenPowerOfATrigonometricFunction(Entity expr) /// private static bool IsWholeAndFitsAnInt(ERational value, out int whole) { - var lowest = value.ToLowestTerms(); + var lowest = value.InLowestTerms(); if (lowest.Denominator.Equals(EInteger.One) && lowest.Numerator.CanFitInInt32()) { whole = lowest.Numerator.ToInt32Checked(); @@ -10999,7 +10999,7 @@ private static bool IsEvenWhole(ERational value) /// has already said is whole. /// private static int WholeOf(ERational value) - => value.ToLowestTerms().Numerator.ToInt32Checked(); + => value.InLowestTerms().Numerator.ToInt32Checked(); /// /// Reads as a whole power of a secant or a cosecant, however it @@ -13759,7 +13759,7 @@ static bool IsTwice(Entity argument, Entity.Variable x) var power = MathS.Pow(@base, Number.Rational.Create(each)); rebuilt = rebuilt is null ? power : rebuilt * power; } - var reduced = total.ToLowestTerms(); + var reduced = total.InLowestTerms(); return reduced.Denominator.Equals(EInteger.One) && reduced.Numerator.IsEven ? rebuilt : null; } @@ -16201,16 +16201,16 @@ node is Powf(var @base, var power) && @base.ContainsNode(u) var denominators = EInteger.One; foreach (var slope in slopes) { - numerators = numerators.Gcd(slope.ERational.Numerator.Abs()); + numerators = numerators.GreatestCommonDivisor(slope.ERational.Numerator.Abs()); denominators = denominators.Multiply(slope.ERational.Denominator) - .Divide(denominators.Gcd(slope.ERational.Denominator)); + .Divide(denominators.GreatestCommonDivisor(slope.ERational.Denominator)); } var common = ERational.Create(numerators, denominators); var multiples = new HashSet(); foreach (var slope in slopes) { // Reduced before its numerator is read: `Divide` leaves `2/2` as it is. - var multiple = slope.ERational.Divide(common).ToLowestTerms(); + var multiple = slope.ERational.Divide(common).InLowestTerms(); if (!multiple.IsInteger() || !multiple.Numerator.Abs().CanFitInInt32()) return null; var n = multiple.Numerator.Abs().ToInt32Unchecked(); @@ -16245,7 +16245,7 @@ node is Powf(var @base, var power) && @base.ContainsNode(u) || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var offset) || slope.Evaled is not Number.Rational rational) return node; - var multiple = rational.ERational.Divide(common).ToLowestTerms(); + var multiple = rational.ERational.Divide(common).InLowestTerms(); var n = multiple.Numerator.Abs().ToInt32Unchecked(); var negative = multiple.Numerator.Sign < 0; var (sin, cos) = SineAndCosineOfAMultiple(n, sine, cosine); @@ -16535,7 +16535,7 @@ private static int ExpandedDegree(Entity expr, Entity.Variable x, ERational comm && TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) && slope.Evaled is Number.Rational rational) { - var multiple = rational.ERational.Divide(common).ToLowestTerms(); + var multiple = rational.ERational.Divide(common).InLowestTerms(); return multiple.Numerator.Abs().CanFitInInt32() ? multiple.Numerator.Abs().ToInt32Unchecked() : int.MaxValue / 4; } return 1; @@ -19345,7 +19345,7 @@ private static bool IdentityHoldsExactly(List> colu if (pair.Value.Evaled is not Number.Rational coefficient) return false; var term = coefficient.ERational.Multiply(value.ERational); - sums[pair.Key] = sums.TryGetValue(pair.Key, out var so) ? so.Add(term).ToLowestTerms() : term.ToLowestTerms(); + sums[pair.Key] = sums.TryGetValue(pair.Key, out var so) ? so.Add(term).InLowestTerms() : term.InLowestTerms(); } } foreach (var pair in target) @@ -19353,7 +19353,7 @@ private static bool IdentityHoldsExactly(List> colu if (pair.Value.Evaled is not Number.Rational wanted) return false; var have = sums.TryGetValue(pair.Key, out var sum) ? sum : ERational.Zero; - if (!have.Subtract(wanted.ERational).ToLowestTerms().IsZero) + if (!have.Subtract(wanted.ERational).InLowestTerms().IsZero) return false; sums.Remove(pair.Key); } @@ -19373,7 +19373,7 @@ private static Dictionary PolynomialProduct(Dictionary(); foreach (var pair in rationalProduct) @@ -20370,7 +20370,7 @@ private static bool HasARepeatedFactor(Entity expr, Entity.Variable x) return false; var denominator = EInteger.One; foreach (var coefficient in rational) - denominator = denominator.Multiply(coefficient.Denominator).Divide(denominator.Gcd(coefficient.Denominator)); + denominator = denominator.Multiply(coefficient.Denominator).Divide(denominator.GreatestCommonDivisor(coefficient.Denominator)); var whole = new EInteger[rational.Length]; for (var i = 0; i < whole.Length; i++) whole[i] = rational[i].Numerator.Multiply(denominator.Divide(rational[i].Denominator)); @@ -21430,14 +21430,14 @@ static Dictionary Scaled(Dictionary poly, En (m[row], m[pivot]) = (m[pivot], m[row]); var lead = m[row][column]; for (var j = column; j <= columns; j++) - m[row][j] = m[row][j].Divide(lead).ToLowestTerms(); + m[row][j] = m[row][j].Divide(lead).InLowestTerms(); for (var i = 0; i < rows; i++) { if (i == row || m[i][column].IsZero) continue; var factor = m[i][column]; for (var j = column; j <= columns; j++) - m[i][j] = m[i][j].Subtract(factor.Multiply(m[row][j])).ToLowestTerms(); + m[i][j] = m[i][j].Subtract(factor.Multiply(m[row][j])).InLowestTerms(); } pivotOf[column] = row; row++; @@ -27875,8 +27875,8 @@ private static (Dictionary Monomials, Number.Rational K)? InAF var denominators = EInteger.One; foreach (var q in powers) { - numerators = numerators.Gcd(q.Numerator.Abs()); - denominators = denominators.Multiply(q.Denominator).Divide(denominators.Gcd(q.Denominator)); + numerators = numerators.GreatestCommonDivisor(q.Numerator.Abs()); + denominators = denominators.Multiply(q.Denominator).Divide(denominators.GreatestCommonDivisor(q.Denominator)); } var k = ERational.Create(numerators, denominators); if (powers[0].Sign < 0) @@ -28185,8 +28185,8 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x) var denominators = EInteger.One; foreach (var slope in slopes) { - numerators = numerators.Gcd(slope.Numerator.Abs()); - denominators = denominators.Multiply(slope.Denominator).Divide(denominators.Gcd(slope.Denominator)); + numerators = numerators.GreatestCommonDivisor(slope.Numerator.Abs()); + denominators = denominators.Multiply(slope.Denominator).Divide(denominators.GreatestCommonDivisor(slope.Denominator)); } if (numerators.IsZero) return null; @@ -28212,7 +28212,7 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x) var rewritten = expr.Replace(node => offsets.TryGetValue(node, out var found) ? (shifted ? Number.Integer.One : MathS.Pow(commonBase, found.Offset)) - * MathS.Pow(u, Number.Integer.Create(found.Slope.Divide(k).ToLowestTerms().Numerator)) + * MathS.Pow(u, Number.Integer.Create(found.Slope.Divide(k).InLowestTerms().Numerator)) : node); if (rewritten.ContainsNode(x)) @@ -29223,7 +29223,7 @@ private static Entity WithNumericBasesUnified(Entity expr, Entity.Variable x) { if (@base.Evaled is not Number.Rational rational || rational.ERational.Sign <= 0 || rational.ERational.Equals(ERational.One)) return expr; - var written = rational.ERational.ToLowestTerms(); + var written = rational.ERational.InLowestTerms(); if (!bases.Contains(written)) bases.Add(written); } @@ -29265,7 +29265,7 @@ private static Entity WithNumericBasesUnified(Entity expr, Entity.Variable x) return expr; return expr.Replace(node => node is Powf(var @base, var exponent) && !@base.ContainsNode(x) && exponent.ContainsNode(x) && exponent is not Number - && @base.Evaled is Number.Rational rational && powers.TryGetValue(rational.ERational.ToLowestTerms(), out var power) + && @base.Evaled is Number.Rational rational && powers.TryGetValue(rational.ERational.InLowestTerms(), out var power) ? MathS.Pow(common, power == 1 ? exponent : (Number.Integer.Create(power) * exponent).InnerSimplified) : node); } @@ -29336,7 +29336,7 @@ private static bool TryGatherExponentialsOfOneLinearForm(Entity expr, Entity.Var foreach (var (_, multiple) in multiples) { denominators = denominators.Lcm(multiple.ERational.Denominator); - numerators = numerators.Gcd(multiple.ERational.Numerator); + numerators = numerators.GreatestCommonDivisor(multiple.ERational.Numerator); } var yScaleRational = ERational.Create(numerators, denominators); // With a whole number for the first slope, and asked to, y is x itself (plus the @@ -29351,7 +29351,7 @@ private static bool TryGatherExponentialsOfOneLinearForm(Entity expr, Entity.Var yScaleRational = ERational.Create(EInteger.One, wholeSlope.EInteger); var yScale = Number.Rational.Create(yScaleRational); foreach (var (node, multiple) in multiples) - exponentials[node] = multiple.ERational.Divide(yScaleRational).ToLowestTerms().Numerator; + exponentials[node] = multiple.ERational.Divide(yScaleRational).InLowestTerms().Numerator; y = (yScale * (slopeOfY * x + offsetOfY)).InnerSimplified; dxOverDy = TreeAnalyzer.IsZero(offsetOfY) && slopeOfY == Number.Integer.One && yScale == Number.Integer.One ? Number.Integer.One @@ -30024,7 +30024,7 @@ bool IsAConstantTimesAPowerOfOneMinusUSquared(Entity polynomial, out Entity cons } if (constantFactor is not null) { - var exponent = Number.Rational.Create(ofOneMinusUSquared.Multiply(root.ERational).ToLowestTerms()); + var exponent = Number.Rational.Create(ofOneMinusUSquared.Multiply(root.ERational).InLowestTerms()); atoms[atom] = MathS.Pow(constantFactor.InnerSimplified, root) * (exponent == Number.Integer.Zero ? Number.Integer.One : MathS.Pow(oneMinusUSquared, exponent)); return atom; } @@ -30033,7 +30033,7 @@ bool IsAConstantTimesAPowerOfOneMinusUSquared(Entity polynomial, out Entity cons if (!radicandTop.ContainsNode(w) && !radicandBottom.ContainsNode(w) && IsAConstantTimesAPowerOfOneMinusUSquared(radicandBottom, out var bottomConstant, out var m) && m > 0) { - var wPower = root.ERational.Multiply(ERational.FromInt32(-2 * m)).ToLowestTerms(); + var wPower = root.ERational.Multiply(ERational.FromInt32(-2 * m)).InLowestTerms(); if (wPower.Denominator.Equals(EInteger.One)) { rationalRadicand = bottomConstant.Evaled is Number.Rational number @@ -30852,7 +30852,7 @@ when two.Equals(EInteger.FromInt32(2)) && e1 == MathS.e && e2 == MathS.e || !inT.Nodes.Any(node => node is Arcsinf or Arccosf or Arctanf or Arccotanf or Logf && node.ContainsNode(t))) return null; // PeterO keeps a quotient as written: -4/2 has the denominator 2 until it is reduced. - var k = ((m + ERational.One) / n).ToLowestTerms(); + var k = ((m + ERational.One) / n).InLowestTerms(); if (!k.Denominator.Equals(EInteger.One) || !k.Numerator.CanFitInInt32()) return null; var exponent = k.Numerator.ToInt32Unchecked() - 1; @@ -31016,7 +31016,7 @@ when two.Equals(EInteger.FromInt32(2)) && e1 == MathS.e && e2 == MathS.e var cosine = new Dictionary { [0] = ERational.One }; var sine = new Dictionary(); static void Add(Dictionary into, int k, ERational value) - => into[k] = (into.TryGetValue(k, out var old) ? old + value : value).ToLowestTerms(); + => into[k] = (into.TryGetValue(k, out var old) ? old + value : value).InLowestTerms(); var half = ERational.Create(1, 2); for (var step = 0; step < sines + cosines; step++) { @@ -31699,29 +31699,29 @@ static ERational[] Integral(ERational[] p) => // A power of a product splits into the powers of its factors only where it is // whole: (c - a c x)^(9/2) is not c^(9/2) (1 - a x)^(9/2) where both are negative, // and beyond |u| = 1 that integrand is real while the split one changes its sign. - if (!power.ToLowestTerms().Denominator.Equals(EInteger.One) + if (!power.InLowestTerms().Denominator.Equals(EInteger.One) && (MultipleOf(@base, quadratic, 2) is not null || MultipleOf(@base, 1 + u, 1) is not null || MultipleOf(@base, 1 - u, 1) is not null)) return null; if (MultipleOf(@base, quadratic, 2) is { } k2) { plusPower += power; minusPower += power; - constant *= MathS.Pow(k2, Number.Rational.Create(power.ToLowestTerms().Numerator, power.ToLowestTerms().Denominator)); + constant *= MathS.Pow(k2, Number.Rational.Create(power.InLowestTerms().Numerator, power.InLowestTerms().Denominator)); } else if (MultipleOf(@base, 1 + u, 1) is { } kPlus) { plusPower += power; - constant *= MathS.Pow(kPlus, Number.Rational.Create(power.ToLowestTerms().Numerator, power.ToLowestTerms().Denominator)); + constant *= MathS.Pow(kPlus, Number.Rational.Create(power.InLowestTerms().Numerator, power.InLowestTerms().Denominator)); } else if (MultipleOf(@base, 1 - u, 1) is { } kMinus) { minusPower += power; - constant *= MathS.Pow(kMinus, Number.Rational.Create(power.ToLowestTerms().Numerator, power.ToLowestTerms().Denominator)); + constant *= MathS.Pow(kMinus, Number.Rational.Create(power.InLowestTerms().Numerator, power.InLowestTerms().Denominator)); } else rest = below ? rest / factor : rest * factor; } - plusPower = plusPower.ToLowestTerms(); - minusPower = minusPower.ToLowestTerms(); + plusPower = plusPower.InLowestTerms(); + minusPower = minusPower.InLowestTerms(); static bool Whole(ERational r) => r.Denominator.Equals(EInteger.One); static bool HalfOdd(ERational r) => r.Denominator.Equals(EInteger.FromInt32(2)); // Both half an odd number: (1 + u)^A (1 - u)^B is (1 + u)^(A - B) (1 - u^2)^B, one power of @@ -31730,7 +31730,7 @@ static ERational[] Integral(ERational[] p) => if (HalfOdd(plusPower) && HalfOdd(minusPower)) { quadraticPower = minusPower; - plusPower = (plusPower - minusPower).ToLowestTerms(); + plusPower = (plusPower - minusPower).InLowestTerms(); minusPower = ERational.Zero; } else if (!Whole(plusPower) || !Whole(minusPower)) @@ -31804,7 +31804,7 @@ void Put(Entity b2, int k) var u = ((plus - minus) / 2).InnerSimplified; if (!TreeAnalyzer.TryGetPolyLinear(u, x, out var slope, out _) || Functions.PartialFractions.IsZeroAsAValue(slope)) return node; - var twice = (half * 2).ToLowestTerms(); + var twice = (half * 2).InLowestTerms(); if (!twice.Denominator.Equals(EInteger.One) || !twice.Numerator.CanFitInInt32() || twice.IsZero) return node; var n = twice.Numerator.ToInt32Unchecked(); @@ -32442,7 +32442,7 @@ private static (Entity Numerator, Entity Denominator)? WithASharedPowerCancelled if (factor is not Powf(var function, Number.Integer degree) || !degree.EInteger.IsEven || degree.EInteger.Sign <= 0 || TreeAnalyzer.TryGetPolynomial(function, x, out _) || function is Powf(_, not Number.Integer)) return node; - var product = exponent.ERational.Multiply(ERational.FromEInteger(degree.EInteger)).ToLowestTerms(); + var product = exponent.ERational.Multiply(ERational.FromEInteger(degree.EInteger)).InLowestTerms(); if (!product.Denominator.Equals(EInteger.One)) return node; functions.Add((function, product.Numerator)); @@ -37840,13 +37840,13 @@ static Entity SimplifiedExponent(Entity exponent) } static bool IsAWholeNumberOfSteps(ERational difference) { - // ToLowestTerms: a difference of two halves comes back as `8/2` unreduced. - var reduced = difference.ToLowestTerms(); + // InLowestTerms: a difference of two halves comes back as `8/2` unreduced. + var reduced = difference.InLowestTerms(); return reduced.Denominator.Equals(EInteger.One) && reduced.Numerator.Remainder(EInteger.FromInt32(2)).IsZero; } if (terms.Any(term => !IsAWholeNumberOfSteps(highest.Subtract(term.Exponent.ERational)))) return null; - var steps = highest.Subtract(lowest).ToLowestTerms().Numerator.Divide(EInteger.FromInt32(2)); + var steps = highest.Subtract(lowest).InLowestTerms().Numerator.Divide(EInteger.FromInt32(2)); if (steps.CompareTo(EInteger.FromInt32(8)) > 0) return null; if (terms.All(term => term.Exponent is Number.Integer)) @@ -40283,15 +40283,15 @@ static bool SameMonomial((Entity Coefficient, Entity Exponent, List<(Entity Base var denominators = EInteger.One; foreach (var multiple in multiples.Values) { - numerators = numerators.Gcd(multiple.Numerator.Abs()); - denominators = denominators.Multiply(multiple.Denominator).Divide(denominators.Gcd(multiple.Denominator)); + numerators = numerators.GreatestCommonDivisor(multiple.Numerator.Abs()); + denominators = denominators.Multiply(multiple.Denominator).Divide(denominators.GreatestCommonDivisor(multiple.Denominator)); } var step = ERational.Create(numerators, denominators); - if (multiples.Values.Any(multiple => multiple.Divide(step).ToLowestTerms().Numerator.Abs().CompareTo(EInteger.FromInt32(8)) > 0)) + if (multiples.Values.Any(multiple => multiple.Divide(step).InLowestTerms().Numerator.Abs().CompareTo(EInteger.FromInt32(8)) > 0)) return null; var w = Variable.CreateUnique(expr, "w_hyp"); Entity InW(Entity e) => e.Replace(node => - node != outer && multiples.TryGetValue(node, out var multiple) ? MathS.Pow(w, Number.Integer.Create(multiple.Divide(step).ToLowestTerms().Numerator)) : node); + node != outer && multiples.TryGetValue(node, out var multiple) ? MathS.Pow(w, Number.Integer.Create(multiple.Divide(step).InLowestTerms().Numerator)) : node); // The exponent `c (w + s/w) + d`: a multiple of the sine or the cosine in w and nothing else. if (!TreeAnalyzer.TryGetPolynomial(InW(outer.Exponent), w, out var exponentRead) || exponentRead.Keys.Any(degree => degree.Abs().CompareTo(EInteger.One) > 0) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs b/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs index 177cd8ebd..1b41479d0 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs @@ -226,7 +226,7 @@ private static (Entity Rational, RationalPolynomial Above, RationalPolynomial Be var f = irreducible.Factor; Entity? term = f.Degree switch { - 1 => RationalResidue(ERational.Create(f[0], f[1]).Negate().ToLowestTerms(), part.Multiplicity, above, below, derivative, x), + 1 => RationalResidue(ERational.Create(f[0], f[1]).Negate().InLowestTerms(), part.Multiplicity, above, below, derivative, x), 2 => QuadraticResidues(f, part.Multiplicity, above, below, derivative, x), _ => null, }; @@ -447,8 +447,8 @@ private static bool InAPowerOfAShift(IntegerPolynomial poly) IntegerPolynomial f, int degree, RationalPolynomial above, RationalPolynomial below, RationalPolynomial derivative, Variable x) { // f = f2 t^2 + f1 t + f0, roots -f1/(2 f2) ± sqrt(f1^2/(4 f2^2) - f0/f2). - var a = ERational.Create(f[1], f[2].Multiply(EInteger.FromInt32(2))).Negate().ToLowestTerms(); - var w = a.Multiply(a).Subtract(ERational.Create(f[0], f[2])).ToLowestTerms(); + var a = ERational.Create(f[1], f[2].Multiply(EInteger.FromInt32(2))).Negate().InLowestTerms(); + var w = a.Multiply(a).Subtract(ERational.Create(f[0], f[2])).InLowestTerms(); if (w.IsZero) return null; var field = new QuadraticExtension(w); @@ -572,7 +572,7 @@ private static IntegerPolynomial ToInteger(RationalPolynomial poly) } private static EInteger Lcm(EInteger a, EInteger b) - => a.IsZero || b.IsZero ? EInteger.Zero : a.Multiply(b).Abs().Divide(a.Gcd(b)); + => a.IsZero || b.IsZero ? EInteger.Zero : a.Multiply(b).Abs().Divide(a.GreatestCommonDivisor(b)); /// /// Res_x(D, A - t D') as a polynomial in t: of degree at most deg D, @@ -598,7 +598,7 @@ private static EInteger Lcm(EInteger a, EInteger b) var coefficients = (ERational[])values.Clone(); for (var level = 1; level <= degree; level++) for (var k = degree; k >= level; k--) - coefficients[k] = coefficients[k].Subtract(coefficients[k - 1]).Divide(points[k].Subtract(points[k - level])).ToLowestTerms(); + coefficients[k] = coefficients[k].Subtract(coefficients[k - 1]).Divide(points[k].Subtract(points[k - level])).InLowestTerms(); var result = RationalPolynomial.Zero; for (var k = degree; k >= 0; k--) { @@ -657,14 +657,14 @@ private static EInteger Lcm(EInteger a, EInteger b) var common = EInteger.One; for (var column = 0; column < size; column++) { - var reduced = rows[row][column].ToLowestTerms(); + var reduced = rows[row][column].InLowestTerms(); rows[row][column] = reduced; - common = common.Multiply(reduced.Denominator).Divide(common.Gcd(reduced.Denominator)); + common = common.Multiply(reduced.Denominator).Divide(common.GreatestCommonDivisor(reduced.Denominator)); } scale = scale.Multiply(ERational.FromEInteger(common)); a[row] = new EInteger[size]; for (var column = 0; column < size; column++) - a[row][column] = rows[row][column].Multiply(ERational.FromEInteger(common)).ToLowestTerms().Numerator; + a[row][column] = rows[row][column].Multiply(ERational.FromEInteger(common)).InLowestTerms().Numerator; } var sign = 1; var previous = EInteger.One; @@ -700,7 +700,7 @@ private static EInteger Lcm(EInteger a, EInteger b) var determinant = ERational.FromEInteger(a[size - 1][size - 1]); if (sign < 0) determinant = determinant.Negate(); - return determinant.Divide(scale).ToLowestTerms(); + return determinant.Divide(scale).InLowestTerms(); } private static RationalPolynomial Derivative(RationalPolynomial poly) @@ -742,7 +742,7 @@ private static bool TryRationalSquareRoot(ERational value, out ERational root) var denominator = value.Denominator.Sqrt(); if (!numerator.Multiply(numerator).Equals(value.Numerator) || !denominator.Multiply(denominator).Equals(value.Denominator)) return false; - root = ERational.Create(numerator, denominator).ToLowestTerms(); + root = ERational.Create(numerator, denominator).InLowestTerms(); return true; } @@ -751,8 +751,8 @@ private readonly struct Element { internal Element(ERational re, ERational im) { - Re = re.ToLowestTerms(); - Im = im.ToLowestTerms(); + Re = re.InLowestTerms(); + Im = im.InLowestTerms(); } internal ERational Re { get; } diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/SquareRootLogarithmAnsatz.cs b/Sources/AngouriMath/Functions/Continuous/Integration/SquareRootLogarithmAnsatz.cs index 8ec485740..648919e16 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/SquareRootLogarithmAnsatz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/SquareRootLogarithmAnsatz.cs @@ -305,11 +305,11 @@ void Offer(XPoly a, XPoly b, bool imaginary, Field over, EInteger d) // constant line too, alpha^2 = r_0. var squaresOfBeta = new List(); var linear = rem1.Multiply(q1.ERational).Subtract(rem0.Multiply(ERational.FromInt32(2))).Multiply(ERational.FromInt32(2)); - var delta = linear.Multiply(linear).Subtract(discriminant.Multiply(rem1.Multiply(rem1)).Multiply(ERational.FromInt32(4))).ToLowestTerms(); + var delta = linear.Multiply(linear).Subtract(discriminant.Multiply(rem1.Multiply(rem1)).Multiply(ERational.FromInt32(4))).InLowestTerms(); if (TryRationalSquareRoot(delta) is { } rootOfDelta) foreach (var sign in new[] { 1, -1 }) { - var u = linear.Negate().Add(rootOfDelta.Multiply(ERational.FromInt32(sign))).Divide(discriminant.Multiply(ERational.FromInt32(2))).ToLowestTerms(); + var u = linear.Negate().Add(rootOfDelta.Multiply(ERational.FromInt32(sign))).Divide(discriminant.Multiply(ERational.FromInt32(2))).InLowestTerms(); if (u.Sign > 0 && !squaresOfBeta.Any(known => known.Equals(u))) squaresOfBeta.Add(u); } @@ -333,7 +333,7 @@ void Offer(XPoly a, XPoly b, bool imaginary, Field over, EInteger d) var qOver = XPoly.FromCoefficients(over, q.Coefficients); // 1/(s sqrt(d)) is sqrt(d)/(s d). Entity generator = Rational.Create(s) * rootOfD; - Entity inverseOfGenerator = Rational.Create(ERational.One.Divide(s.Multiply(ERational.FromEInteger(d))).ToLowestTerms()) * rootOfD; + Entity inverseOfGenerator = Rational.Create(ERational.One.Divide(s.Multiply(ERational.FromEInteger(d))).InLowestTerms()) * rootOfD; Entity alpha, beta; XPoly inverseOfTwice; if (u.IsZero) @@ -345,13 +345,13 @@ void Offer(XPoly a, XPoly b, bool imaginary, Field over, EInteger d) else { beta = generator; - alpha = over.Normalize(Rational.Create(rem1.Add(u.Multiply(q1.ERational)).Divide(ERational.FromInt32(2)).ToLowestTerms()) * inverseOfGenerator); + alpha = over.Normalize(Rational.Create(rem1.Add(u.Multiply(q1.ERational)).Divide(ERational.FromInt32(2)).InLowestTerms()) * inverseOfGenerator); // (2L)^(-1) modulo q is ((alpha - beta q_1) - beta x)/(2N) for the norm // N = alpha^2 - alpha beta q_1 + beta^2 q_0 of the line, a rational. var norm = over.Normalize(alpha * alpha - alpha * beta * q1 + beta * beta * q0); if (norm.Evaled is not Rational normValue || normValue.ERational.IsZero) continue; - var overTwiceNorm = Rational.Create(ERational.One.Divide(normValue.ERational.Multiply(ERational.FromInt32(2))).ToLowestTerms()); + var overTwiceNorm = Rational.Create(ERational.One.Divide(normValue.ERational.Multiply(ERational.FromInt32(2))).InLowestTerms()); inverseOfTwice = XPoly.FromCoefficients(over, new Dictionary { [0] = over.Normalize((alpha - beta * q1) * overTwiceNorm), @@ -568,7 +568,7 @@ private static (List Factors, ERational Scale, XPoly? Leftover)? FactorOv var coefficients = new Dictionary(); for (var k = 0; k < current.Length; k++) if (!current[k].IsZero) - coefficients[k] = Rational.Create(current[k].Divide(scale).ToLowestTerms()); + coefficients[k] = Rational.Create(current[k].Divide(scale).InLowestTerms()); leftover = XPoly.FromCoefficients(field, coefficients); } return (factors, scale, leftover); @@ -621,11 +621,11 @@ private static Dictionary RationalsAsCoefficients(ERational[] ratio var carry = ERational.Zero; for (var k = coefficients.Length - 1; k >= 1; k--) { - carry = coefficients[k].Add(carry).ToLowestTerms(); + carry = coefficients[k].Add(carry).InLowestTerms(); quotient[k - 1] = carry; carry = carry.Multiply(a); } - return coefficients[0].Add(carry).ToLowestTerms().IsZero ? quotient : null; + return coefficients[0].Add(carry).InLowestTerms().IsZero ? quotient : null; } /// The exact quotient by , or null. @@ -638,10 +638,10 @@ private static Dictionary RationalsAsCoefficients(ERational[] ratio var quotient = new ERational[coefficients.Length - divisor.Length + 1]; for (var k = quotient.Length - 1; k >= 0; k--) { - var term = remainder[k + divisor.Length - 1].Divide(leading).ToLowestTerms(); + var term = remainder[k + divisor.Length - 1].Divide(leading).InLowestTerms(); quotient[k] = term; for (var i = 0; i < divisor.Length; i++) - remainder[k + i] = remainder[k + i].Subtract(term.Multiply(divisor[i])).ToLowestTerms(); + remainder[k + i] = remainder[k + i].Subtract(term.Multiply(divisor[i])).InLowestTerms(); } for (var i = 0; i < divisor.Length - 1; i++) if (!remainder[i].IsZero) @@ -766,7 +766,7 @@ private static (EInteger SquareFree, ERational Root)? SquareFreePart(ERational v if (divisor.CompareTo(bound) > 0) return null; squareFree = squareFree.Multiply(remaining); - return (squareFree, ERational.Create(square, value.Denominator).ToLowestTerms()); + return (squareFree, ERational.Create(square, value.Denominator).InLowestTerms()); } /// The rational square root of a non-negative rational, where it has one. @@ -832,8 +832,8 @@ private static XPoly Unshifted(XPoly inT, ERational a, Field field) { var sum = ERational.Zero; for (var i = 1; i < k; i++) - sum = sum.Add(series[i].Multiply(series[k - i])).ToLowestTerms(); - series.Add(At(k).Subtract(sum).Divide(twoS).ToLowestTerms()); + sum = sum.Add(series[i].Multiply(series[k - i])).InLowestTerms(); + series.Add(At(k).Subtract(sum).Divide(twoS).InLowestTerms()); } return series; } @@ -994,24 +994,24 @@ private static (XPoly?, XPoly?) Solve(List rows, int unknowns, int aCo (ERational, ERational) Times((ERational, ERational) l, (ERational, ERational) r) { var cross = l.Item2.Multiply(r.Item2); - return (l.Item1.Multiply(r.Item1).Subtract(m0.Multiply(cross)).ToLowestTerms(), - l.Item1.Multiply(r.Item2).Add(l.Item2.Multiply(r.Item1)).Subtract(m1.Multiply(cross)).ToLowestTerms()); + return (l.Item1.Multiply(r.Item1).Subtract(m0.Multiply(cross)).InLowestTerms(), + l.Item1.Multiply(r.Item2).Add(l.Item2.Multiply(r.Item1)).Subtract(m1.Multiply(cross)).InLowestTerms()); } // 1/(u + v c): the conjugate over the norm, the modulus being c^2 + m1 c + m0. (ERational, ERational)? Inverse((ERational, ERational) value) { var (u, v) = value; if (v.IsZero) - return u.IsZero ? null : (ERational.One.Divide(u).ToLowestTerms(), ERational.Zero); + return u.IsZero ? null : (ERational.One.Divide(u).InLowestTerms(), ERational.Zero); // (u + v c)(u' + v' c) = 1 with c^2 = -m1 c - m0: solve the 2x2 system. // Real part: u u' - m0 v v' = 1; c part: u v' + v u' - m1 v v' = 0. // From the second, u' = (m1 v v' - u v')/v = v'(m1 v - u)/v; substitute: // v' [u (m1 v - u)/v - m0 v] = 1. - var factor = u.Multiply(m1.Multiply(v).Subtract(u)).Divide(v).Subtract(m0.Multiply(v)).ToLowestTerms(); + var factor = u.Multiply(m1.Multiply(v).Subtract(u)).Divide(v).Subtract(m0.Multiply(v)).InLowestTerms(); if (factor.IsZero) return null; - var vPrime = ERational.One.Divide(factor).ToLowestTerms(); - var uPrime = vPrime.Multiply(m1.Multiply(v).Subtract(u)).Divide(v).ToLowestTerms(); + var vPrime = ERational.One.Divide(factor).InLowestTerms(); + var uPrime = vPrime.Multiply(m1.Multiply(v).Subtract(u)).Divide(v).InLowestTerms(); return (uPrime, vPrime); } var pivotColumns = new List(); @@ -1041,8 +1041,8 @@ private static (XPoly?, XPoly?) Solve(List rows, int unknowns, int aCo for (var k = 0; k < unknowns; k++) { var (t0, t1) = Times(factor, (a0[rank][k], a1[rank][k])); - a0[row][k] = a0[row][k].Subtract(t0).ToLowestTerms(); - a1[row][k] = a1[row][k].Subtract(t1).ToLowestTerms(); + a0[row][k] = a0[row][k].Subtract(t0).InLowestTerms(); + a1[row][k] = a1[row][k].Subtract(t1).InLowestTerms(); } } pivotColumns.Add(column); diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs index 48ff84912..c2150776f 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs @@ -213,7 +213,7 @@ internal static bool ReduceCommonPower(ref Dictionary monomial ? new Entity[] { 0 } // x5 + x3 + x2 - common power is 2, one root is 0, then x3 + x + 1 : Enumerable.Empty(); var powers = monomialsByPower.Keys.ToList(); - var gcdPower = powers.Aggregate((accumulate, current) => accumulate.Gcd(current)); + var gcdPower = powers.Aggregate((accumulate, current) => accumulate.GreatestCommonDivisor(current)); // // // if (gcdPower.IsZero) diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/InequalitySolver/PolynomialSignTable.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/InequalitySolver/PolynomialSignTable.cs index 7ba35d3ee..e8bde9ddb 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/InequalitySolver/PolynomialSignTable.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/InequalitySolver/PolynomialSignTable.cs @@ -198,7 +198,7 @@ private static bool TryRealRootsOf( if (degree == 1) { - var root = ERational.Create(factor[0].Negate(), factor[1]).ToLowestTerms(); + var root = ERational.Create(factor[0].Negate(), factor[1]).InLowestTerms(); // The root is exact; its double is what the sample points are placed from, // and a rational too large to be one at all would put them nowhere. var approximation = root.ToDouble(); @@ -354,7 +354,7 @@ private static bool TryRealRootsOf( private static ERational Midpoint(double left, double right) => ERational.FromDouble(left).Add(ERational.FromDouble(right)) - .Divide(ERational.FromInt32(2)).ToLowestTerms(); + .Divide(ERational.FromInt32(2)).InLowestTerms(); private static ERational Below(double leastRoot, ERational bound) { @@ -381,7 +381,7 @@ private static ERational CauchyBound(IntegerPolynomial poly) if (magnitude.CompareTo(greatest) > 0) greatest = magnitude; } - return ERational.One.Add(ERational.Create(greatest, leading)).ToLowestTerms(); + return ERational.One.Add(ERational.Create(greatest, leading)).InLowestTerms(); } /// @@ -393,10 +393,10 @@ private static ERational Evaluate(IntegerPolynomial poly, ERational point) var result = ERational.Zero; for (var power = poly.Degree; power >= 0; power--) result = result.Multiply(point).Add(ERational.Create(poly[power], EInteger.One)); - return result.ToLowestTerms(); + return result.InLowestTerms(); } private static EInteger Lcm(EInteger left, EInteger right) - => left.Divide(left.Gcd(right)).Multiply(right); + => left.Divide(left.GreatestCommonDivisor(right)).Multiply(right); } } diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs index 28abae30b..b12f540d0 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs @@ -839,12 +839,12 @@ protected override Entity InnerSimplify(bool isExact) (a, b) => (a, b) switch { // gcd is non-negative by convention, and gcd(0, n) is |n|. - (Integer l, Integer r) => Integer.Create(l.EInteger.Gcd(r.EInteger)), + (Integer l, Integer r) => Integer.Create(l.EInteger.GreatestCommonDivisor(r.EInteger)), // gcd(a/b, c/d) = gcd(a, c) / lcm(b, d) -- which is what SymPy gives, // so gcd(1/2, 1/3) is 1/6 rather than an error. (Rational l, Rational r) => Rational.Create( - l.Numerator.EInteger.Gcd(r.Numerator.EInteger), - l.Denominator.EInteger / l.Denominator.EInteger.Gcd(r.Denominator.EInteger) * r.Denominator.EInteger), + l.Numerator.EInteger.GreatestCommonDivisor(r.Numerator.EInteger), + l.Denominator.EInteger / l.Denominator.EInteger.GreatestCommonDivisor(r.Denominator.EInteger) * r.Denominator.EInteger), var (l, r) when l == r => l, // The polynomial case is left to the node: PolynomialGcd already // computes one for the cancellation rule, and wiring it in here is @@ -867,14 +867,14 @@ protected override Entity InnerSimplify(bool isExact) // the only common multiple there is. (Integer l, Integer r) => l.EInteger.IsZero || r.EInteger.IsZero ? Integer.Zero - : Integer.Create((l.EInteger * r.EInteger).Abs() / l.EInteger.Gcd(r.EInteger)), + : Integer.Create((l.EInteger * r.EInteger).Abs() / l.EInteger.GreatestCommonDivisor(r.EInteger)), // lcm(a/b, c/d) = lcm(a, c) / gcd(b, d), the mirror of the gcd's rule and // what SymPy gives: lcm(1/2, 1/3) is 1. (Rational l, Rational r) => l.Numerator.EInteger.IsZero || r.Numerator.EInteger.IsZero ? Integer.Zero : Rational.Create( - (l.Numerator.EInteger * r.Numerator.EInteger).Abs() / l.Numerator.EInteger.Gcd(r.Numerator.EInteger), - l.Denominator.EInteger.Gcd(r.Denominator.EInteger)), + (l.Numerator.EInteger * r.Numerator.EInteger).Abs() / l.Numerator.EInteger.GreatestCommonDivisor(r.Numerator.EInteger), + l.Denominator.EInteger.GreatestCommonDivisor(r.Denominator.EInteger)), var (l, r) when l == r => l, _ => null }, diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs index 0e0b1d90f..6e3acfd84 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs @@ -256,7 +256,7 @@ static Entity Power(Entity @base, int exponent) if (Q(power).Evaled is not Number.Rational coefficient) return null; rational[power] = coefficient.ERational; - denominators = denominators.Divide(denominators.Gcd(rational[power].Denominator)).Multiply(rational[power].Denominator); + denominators = denominators.Divide(denominators.GreatestCommonDivisor(rational[power].Denominator)).Multiply(rational[power].Denominator); } var size = n + m; var sylvester = new EInteger[size, size]; diff --git a/Sources/AngouriMath/Functions/InternalAMExtensions.cs b/Sources/AngouriMath/Functions/InternalAMExtensions.cs index 91e13219f..bdf772f15 100644 --- a/Sources/AngouriMath/Functions/InternalAMExtensions.cs +++ b/Sources/AngouriMath/Functions/InternalAMExtensions.cs @@ -298,7 +298,7 @@ public BigInteger FixedArctanOf(int sixtyFourths) public static EInteger Lcm(this EInteger bigintFirst, EInteger bigintSecond) => bigintFirst.IsZero && bigintSecond.IsZero ? EInteger.Zero - : bigintFirst.Abs().Divide(bigintFirst.Gcd(bigintSecond)).Multiply(bigintSecond.Abs()); + : bigintFirst.Abs().Divide(bigintFirst.GreatestCommonDivisor(bigintSecond)).Multiply(bigintSecond.Abs()); /// , equivalent to nCr /// or diff --git a/Sources/AngouriMath/Functions/NumberTheory/GreatestCommonDivisor.cs b/Sources/AngouriMath/Functions/NumberTheory/GreatestCommonDivisor.cs new file mode 100644 index 000000000..4e0098a13 --- /dev/null +++ b/Sources/AngouriMath/Functions/NumberTheory/GreatestCommonDivisor.cs @@ -0,0 +1,98 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Numerics; +using PeterO.Numbers; + +namespace AngouriMath +{ + /// + /// The greatest common divisor of two integers, and a fraction in lowest terms by it: the + /// numerator and the denominator divided by their greatest common divisor. Both answer + /// exactly what PeterO's and + /// answer, and every greatest common divisor and + /// reduction in the library goes through them instead. + /// + /// + /// + /// PeterO's greatest common divisor is fast on coprime operands and slow, without bound, + /// when the divisor itself is large: on a pair of 2658 and 2249 bits sharing a factor of + /// 1117 bits it takes 1.5 s, where Euclid's algorithm takes 1.6 ms and the BCL's + /// 14 µs; a shared factor of 5000 bits does + /// not finish in 30 s. A sum of two fractions with a large common denominator is exactly + /// such a pair, and every is reduced on creation, so an + /// integrand evaluated at a sample point whose fractions grew to a few thousand bits spent + /// nearly all its time reducing them. + /// + /// + /// Operands within 64 bits run Euclid's algorithm in machine words, which reduces the small + /// fractions the simplifier makes in about half PeterO's time. Larger ones are converted to + /// by their two's-complement bytes, which both types read and write, + /// and the BCL's Lehmer algorithm answers: faster than PeterO's at every size measured, from + /// 64 bits to 100,000, coprime or not, conversion included. Euclid's algorithm on + /// is not an alternative: its divisions make it 500 ms on a coprime + /// pair of 10,000 bits, where PeterO takes 3 ms and the BCL 0.1 ms. + /// + /// + internal static class GreatestCommonDivisors + { + /// + /// The greatest common divisor of and , never + /// negative, and 0 only when both are: what answers. + /// + internal static EInteger GreatestCommonDivisor(this EInteger a, EInteger b) + { + if (a.CanFitInInt64() && b.CanFitInInt64() + && Euclid(Magnitude(a.ToInt64Unchecked()), Magnitude(b.ToInt64Unchecked())) is var divisor + && divisor <= long.MaxValue) + return EInteger.FromInt64((long)divisor); + return InTheBcl(a, b); + } + + /// + /// in lowest terms, the same as + /// gives: a zero is 0/1 with its sign kept, an + /// infinity or a NaN is itself, and anything else is its numerator and denominator + /// divided by their greatest common divisor. + /// + internal static ERational InLowestTerms(this ERational value) + { + if (!value.IsFinite || value.IsZero) + return value.ToLowestTerms(); + var magnitude = value.UnsignedNumerator; + var denominator = value.Denominator; + if (magnitude.CanFitInInt64() && denominator.CanFitInInt64()) + { + var top = magnitude.ToInt64Unchecked(); + var bottom = denominator.ToInt64Unchecked(); + var divisor = (long)Euclid((ulong)top, (ulong)bottom); + if (divisor == 1) + return value; + top /= divisor; + return ERational.Create(EInteger.FromInt64(value.IsNegative ? -top : top), EInteger.FromInt64(bottom / divisor)); + } + var common = InTheBcl(magnitude, denominator); + if (common.Equals(EInteger.One)) + return value; + return ERational.Create(value.Numerator.Divide(common), denominator.Divide(common)); + } + + /// The magnitude of a machine integer, included. + private static ulong Magnitude(long value) => value < 0 ? unchecked((ulong)-value) : (ulong)value; + + private static ulong Euclid(ulong a, ulong b) + { + while (b != 0) + (a, b) = (b, a % b); + return a; + } + + private static EInteger InTheBcl(EInteger a, EInteger b) + => EInteger.FromBytes(BigInteger.GreatestCommonDivisor( + new BigInteger(a.ToBytes(littleEndian: true)), new BigInteger(b.ToBytes(littleEndian: true))).ToByteArray(), littleEndian: true); + } +} diff --git a/Sources/AngouriMath/Functions/NumberTheory/ResidueClasses.cs b/Sources/AngouriMath/Functions/NumberTheory/ResidueClasses.cs index 5eb88f4a3..f8e5acab3 100644 --- a/Sources/AngouriMath/Functions/NumberTheory/ResidueClasses.cs +++ b/Sources/AngouriMath/Functions/NumberTheory/ResidueClasses.cs @@ -102,7 +102,7 @@ internal static Set SolveLinear(Variable x, EInteger a, EInteger b, EInteger n) b = b.Mod(n); if (a.IsZero) return b.IsZero ? MathS.Sets.Z : Set.Empty; - var g = a.Gcd(n); + var g = a.GreatestCommonDivisor(n); if (!b.Remainder(g).IsZero) return Set.Empty; var reduced = n.Divide(g); @@ -116,7 +116,7 @@ internal static Set SolveLinear(Variable x, EInteger a, EInteger b, EInteger n) /// internal static Set Meet(Variable x, EInteger a, EInteger m, EInteger b, EInteger n) { - var g = m.Gcd(n); + var g = m.GreatestCommonDivisor(n); if (!(a - b).Mod(g).IsZero) return Set.Empty; // x = a + m t, and m t = b - a (mod n): a linear congruence in t modulo n. diff --git a/Sources/AngouriMath/Functions/Simplification/NumericContent.cs b/Sources/AngouriMath/Functions/Simplification/NumericContent.cs index 5d267fd2a..751b23eda 100644 --- a/Sources/AngouriMath/Functions/Simplification/NumericContent.cs +++ b/Sources/AngouriMath/Functions/Simplification/NumericContent.cs @@ -74,7 +74,7 @@ internal static Entity Extracted(Entity expr) coefficients.Add(coefficient); // Gcd(0, n) is n, so seeding with zero makes the first term the running answer // without a special case for it, and Gcd is never negative. - content = content.Gcd(coefficient); + content = content.GreatestCommonDivisor(coefficient); } // One is not a factor worth taking out, and zero cannot be: every coefficient being diff --git a/Sources/AngouriMath/Functions/SingleQuotient.cs b/Sources/AngouriMath/Functions/SingleQuotient.cs index 3788e2978..83db7cf6f 100644 --- a/Sources/AngouriMath/Functions/SingleQuotient.cs +++ b/Sources/AngouriMath/Functions/SingleQuotient.cs @@ -164,7 +164,7 @@ private static (Entity Common, Entity ForA, Entity ForB) Over(Entity a, Entity b var (bWhole, bFactors) = Factors(b); if (aWhole.IsZero || bWhole.IsZero) return (Times(a, b), b, a); - var whole = aWhole.Abs().Divide(aWhole.Abs().Gcd(bWhole.Abs())).Multiply(bWhole.Abs()); + var whole = aWhole.Abs().Divide(aWhole.Abs().GreatestCommonDivisor(bWhole.Abs())).Multiply(bWhole.Abs()); var common = new List<(Entity Base, EInteger Power)>(aFactors); foreach (var (@base, power) in bFactors) { diff --git a/Sources/Tests/UnitTests/Core/GreatestCommonDivisorTest.cs b/Sources/Tests/UnitTests/Core/GreatestCommonDivisorTest.cs new file mode 100644 index 000000000..f9ba940b5 --- /dev/null +++ b/Sources/Tests/UnitTests/Core/GreatestCommonDivisorTest.cs @@ -0,0 +1,171 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using PeterO.Numbers; +using Xunit; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Core +{ + /// + /// The library's greatest common divisor and reduction to lowest terms, which every + /// is made through: they must answer exactly what PeterO's + /// and answer, and + /// answer quickly where PeterO's does not -- when the common factor is itself large. + /// + [Trait("Area", "Core")] + public sealed class GreatestCommonDivisorTest + { + private static EInteger RandomInteger(Random random, int bits) + { + var bytes = new byte[bits / 8 + 2]; + random.NextBytes(bytes); + bytes[^1] = 0; + return EInteger.FromBytes(bytes, littleEndian: true).ShiftRight(bytes.Length * 8 - 8 - bits); + } + + private static EInteger Signed(Random random, EInteger value) => random.Next(2) == 0 ? value : value.Negate(); + + /// + /// Pairs PeterO answers in reasonable time, so it can be the reference: random ones from + /// one bit to three thousand, the edges of the machine-word path, and common factors of + /// up to three hundred bits, past which PeterO itself becomes slow. + /// + private static IEnumerable<(EInteger, EInteger)> Pairs() + { + var two = EInteger.FromInt32(2); + var edges = new[] + { + EInteger.Zero, EInteger.One, EInteger.FromInt32(-1), EInteger.FromInt32(6), EInteger.FromInt32(-4), + EInteger.FromInt64(long.MaxValue), EInteger.FromInt64(long.MinValue), EInteger.FromInt64(long.MinValue + 1), + two.Pow(62), two.Pow(63), two.Pow(63).Negate(), two.Pow(63).Add(EInteger.One), two.Pow(64), two.Pow(64).Subtract(EInteger.One), + two.Pow(64).Negate(), two.Pow(200), EInteger.FromInt32(3).Pow(40), EInteger.FromInt32(3).Pow(41).Negate(), + }; + foreach (var a in edges) + foreach (var b in edges) + yield return (a, b); + var random = new Random(1338); + foreach (var bits in new[] { 1, 7, 31, 32, 33, 62, 63, 64, 65, 100, 192, 193, 400, 1000, 3000 }) + for (var i = 0; i < 20; i++) + yield return (Signed(random, RandomInteger(random, bits)), Signed(random, RandomInteger(random, random.Next(1, bits + 1)))); + foreach (var commonBits in new[] { 1, 20, 63, 64, 65, 120, 300 }) + for (var i = 0; i < 6; i++) + { + var common = RandomInteger(random, commonBits).Add(EInteger.One); + yield return (Signed(random, common.Multiply(RandomInteger(random, random.Next(1, 1600)))), + Signed(random, common.Multiply(RandomInteger(random, random.Next(1, 1200))))); + } + } + + [Fact] + public void TheDivisorIsPeterOs() + { + foreach (var (a, b) in Pairs()) + Assert.Equal(a.Gcd(b), a.GreatestCommonDivisor(b)); + } + + [Fact] + public void TheReductionIsPeterOs() + { + foreach (var (a, b) in Pairs()) + { + if (b.IsZero) + continue; + var fraction = ERational.Create(a, b); + var expected = fraction.ToLowestTerms(); + var actual = fraction.InLowestTerms(); + Assert.Equal(expected, actual); + Assert.Equal(expected.IsNegative, actual.IsNegative); + Assert.Equal(expected.Numerator, actual.Numerator); + Assert.Equal(expected.Denominator, actual.Denominator); + // A zero is downcast to the integer 0, whose sign is not the one under test. + if (!a.IsZero) + Assert.Equal(expected, Rational.Create(a, b).ERational); + } + } + + /// A zero keeps its sign, as PeterO's does, and what is not finite is itself. + [Fact] + public void ZerosAndWhatIsNotFinite() + { + foreach (var value in new[] { ERational.Zero, ERational.NegativeZero, ERational.Create(EInteger.Zero, EInteger.FromInt32(7)), + ERational.Zero.Multiply(ERational.Create(-1, 2)), ERational.PositiveInfinity, ERational.NegativeInfinity }) + { + var expected = value.ToLowestTerms(); + var actual = value.InLowestTerms(); + Assert.Equal(expected, actual); + Assert.Equal(expected.IsNegative, actual.IsNegative); + } + Assert.True(ERational.NaN.InLowestTerms().IsNaN()); + } + + /// + /// Common factors of a thousand bits and more, where PeterO takes from a second to more + /// than a minute a pair: checked without it, by the divisor dividing both and leaving + /// cofactors that share nothing -- which PeterO decides quickly, a coprime pair being its + /// fast case. + /// + [Fact] + public void ALargeCommonFactorIsFound() + { + var random = new Random(565); + foreach (var (commonBits, leftBits, rightBits) in new[] { (1000, 1600, 1200), (1000, 800, 600), (1117, 1541, 1132), (5000, 8000, 6000) }) + { + var common = RandomInteger(random, commonBits).Add(EInteger.One); + var a = common.Multiply(RandomInteger(random, leftBits)).Negate(); + var b = common.Multiply(RandomInteger(random, rightBits)); + var divisor = a.GreatestCommonDivisor(b); + Assert.True(a.Remainder(divisor).IsZero && b.Remainder(divisor).IsZero); + Assert.Equal(EInteger.One, a.Divide(divisor).Gcd(b.Divide(divisor))); + Assert.True(divisor.Remainder(common).IsZero); + + var reduced = ERational.Create(a, b).InLowestTerms(); + Assert.Equal(ERational.Create(a.Divide(divisor), b.Divide(divisor)), reduced); + Assert.Equal(EInteger.One, reduced.Numerator.Gcd(reduced.Denominator)); + } + } + + /// + /// The sum that made Rubi's 1.1.2.4:565 time out: two fractions of fifteen hundred bits + /// whose sum shares a factor of 1117 bits between numerator and denominator, which PeterO + /// took 1.5 s to find and the library takes microseconds for. A hundred of them, through + /// arithmetic, on their own thread so that a regression fails the + /// test rather than hanging the run; the bound is a minute, against tens of milliseconds + /// expected and two and a half minutes for PeterO. + /// + [Fact] + public void ALargeCommonFactorIsQuick() + { + static ERational PowerOf(ERational value, int power) + { + var product = ERational.One; + for (var i = 0; i < power; i++) + product = product.Multiply(value); + return product; + } + var x = Rational.Create(PowerOf(ERational.Create(23, 10), 333) * ERational.Create(-41, 2) * PowerOf(ERational.Create(-17, 10), 7) * PowerOf(ERational.Create(4, 5), 4)); + var y = Rational.Create(PowerOf(ERational.Create(23, 10), 331) * ERational.Create(-55, 2) * PowerOf(ERational.Create(-17, 10), 5) * PowerOf(ERational.Create(4, 5), 6)); + var unreduced = x.ERational + y.ERational; + Assert.True(unreduced.Numerator.Abs().GreatestCommonDivisor(unreduced.Denominator).GetUnsignedBitLengthAsInt64() > 1100); + + var sums = Task.Factory.StartNew(() => + { + var sum = (Entity.Number)x; + for (var i = 0; i < 100; i++) + sum = x + y; + return sum; + }, TaskCreationOptions.LongRunning); + Assert.True(sums.Wait(TimeSpan.FromMinutes(1))); + var result = Assert.IsAssignableFrom(sums.Result).ERational; + Assert.Equal(EInteger.One, result.Numerator.Gcd(result.Denominator)); + Assert.Equal(0, result.CompareTo(unreduced)); + } + } +}