Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,34 @@ before v3, which decides exact comparisons exactly (#1019 §10). Two decimals th
| `sqrt(5)^1000 = 5^500` | `False` | stays written |
| `sqrt(2) < sqrt(2) + 10^(-150)` | `False` | stays written |

### A rational function in nine to sixteen variables has a canonical form

**Silent: a sum of fractions is gathered and cancelled whatever its variables are called.** A monomial held eight
variables, so `Transformation.RationalCanonicalization` had no answer for an expression with nine. `Simplify` then
had only rules that meet two terms where the names sort them side by side, and its answer depended on a variable's
name. A monomial holds sixteen variables now, and the canonical form reads that many. Seventeen or more are still
refused. The solvers, the integrator and the evaluator still read at most eight. Measured on master
(`2a7247653`) and on this branch ([#2721](https://github.com/asc-community/AngouriMath/issues/2721)).

With `D = a^2 * (m + n) * n - (a * c * n)^2` and `E = n / D * (n * w^2 * S + u) + n * a * c / D * (a * v^2 * S + r)`:

| | Was (`2a7247653`) | Now |
|---|---|---|
| `E` with `t` for `S`, `.Simplify()` | `a * c * (a * t * v ^ 2 + r) / (a ^ 2 * (m + n) + -n * (a * c) ^ 2) + (n * t * w ^ 2 + u) / (a ^ 2 * (m + n) + -n * (a * c) ^ 2) provided not n = 0` | `(-a ^ 2 * c * t * v ^ 2 - a * c * r - n * t * w ^ 2 - u) / (a ^ 2 * c ^ 2 * n - a ^ 2 * m - a ^ 2 * n) provided not n = 0` |
| `E` with `x` for `S`, `.Simplify()` | `E`, unchanged | `(-a ^ 2 * c * v ^ 2 * x - a * c * r - n * w ^ 2 * x - u) / (a ^ 2 * c ^ 2 * n - a ^ 2 * m - a ^ 2 * n) provided not n = 0` |
| `RationalCanonicalization.Apply(E)`, either name | no answer | the quotient above |

A refusal now says why, in the new `TransformationResult.Refusal` and after the input in `ToString()`. It names the
bound reached and the value that passed it, or the part that is not a rational function over `Q`. A greatest common
divisor that passes its bounds is now a refusal. Before, the quotient came back uncancelled, so two writings of one
function could give different trees.

| `RationalCanonicalization.Apply(...).ToString()` | Was | Now |
|---|---|---|
| `sin(x) / y + 1` | `rational-canonical-form: no answer for sin(x) / y + 1` | the same, then `: not a rational function over Q: sin(x)` |
| `(1 / x + 1) ^ 40` | `…: no answer for …` | `…: a quotient to the power 40, above the bound of 32` |
| 17 variables | `…: no answer for …` | `…: 17 variables, above the bound of 16` |

### A square system is answered exactly whichever order its equations are in

**Not silent: a decimal answer is now exact, and a refused system has an answer.** The eliminator took the last
Expand Down
2 changes: 1 addition & 1 deletion Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Core/KnownLimitsAreNotBugsTest.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/Algebra/Polynomials/{IntegerPolynomial,PrimeFieldPolynomial,PrimeFieldFactorization,SquareFreeDecomposition,PolynomialFactorization,PolynomialResultant,RationalPolynomial,PartialFractions,RationalFunction,TrigonometricIdentity}.cs]
[AngouriMath/Functions/Algebra/Polynomials/{IntegerPolynomial,PrimeFieldPolynomial,PrimeFieldFactorization,SquareFreeDecomposition,PolynomialFactorization,PolynomialResultant,RationalPolynomial,PartialFractions,RationalFunction,TrigonometricIdentity,Monomial}.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/PatternsTest/ParityTest.cs]
Expand Down
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Convenience/MathS.Polynomials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static class Polynomials

/// <summary>Whether a constant polynomial is 1, so that it need not be printed.</summary>
private static bool SameAsOne(MultivariatePolynomial poly)
=> poly.IsConstant && poly.CoefficientOf(0).CompareTo(ERational.One) == 0;
=> poly.IsConstant && poly.ConstantCoefficient.CompareTo(ERational.One) == 0;

/// <summary>
/// The factorisation of a polynomial in more than one variable, as an expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,17 @@ private sealed class RationalCanonicalizationTransformation : Transformation

protected override Entity? ApplyCore(Entity input)
=> RationalFunction.TryCanonicalize(input, out var canonical) ? canonical : null;

private protected override Entity? ApplyCore(Entity input, out string? refusal)
{
if (RationalFunction.TryCanonicalizeExplained(input, out var canonical, out var why))
{
refusal = null;
return canonical;
}
refusal = why.ToString();
return null;
}
}

private sealed class PolynomialFactorizationTransformation : Transformation
Expand Down
14 changes: 13 additions & 1 deletion Sources/AngouriMath/Core/Transformations/Transformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ public TransformationResult Apply(Entity input)
{
if (input is null)
throw new ArgumentNullException(nameof(input));
return new TransformationResult(this, input, ApplyCore(input));
var output = ApplyCore(input, out var refusal);
return new TransformationResult(this, input, output, output is null ? refusal : null);
}

/// <summary>
/// <see cref="ApplyCore(Entity)"/>, with why there is no answer where the transformation
/// can say: a bound it reached, or a part of the input outside what it reads.
/// https://github.com/asc-community/AngouriMath/issues/2721
/// </summary>
private protected virtual Entity? ApplyCore(Entity input, out string? refusal)
{
refusal = null;
return ApplyCore(input);
}

/// <summary>
Expand Down
15 changes: 12 additions & 3 deletions Sources/AngouriMath/Core/Transformations/TransformationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ namespace AngouriMath.Core.Transformations
/// </remarks>
public readonly struct TransformationResult
{
internal TransformationResult(Transformation transformation, Entity input, Entity? output)
=> (Transformation, Input, Output) = (transformation, input, output);
internal TransformationResult(Transformation transformation, Entity input, Entity? output, string? refusal = null)
=> (Transformation, Input, Output, Refusal) = (transformation, input, output, refusal);

/// <summary>
/// Why there is no answer, where the transformation says: a bound it reached and its value,
/// or the part of the input it does not read. <see langword="null"/> where there is an
/// answer, or where the transformation gives no reason.
/// </summary>
public string? Refusal { get; }

/// <summary>The transformation that was applied.</summary>
public Transformation Transformation { get; }
Expand Down Expand Up @@ -59,7 +66,9 @@ internal TransformationResult(Transformation transformation, Entity input, Entit
/// <inheritdoc/>
public override string ToString()
=> Output is null
? $"{Transformation.Name}: no answer for {Input.Stringize()}"
? Refusal is null
? $"{Transformation.Name}: no answer for {Input.Stringize()}"
: $"{Transformation.Name}: no answer for {Input.Stringize()}: {Refusal}"
: $"{Transformation.Name}: {Input.Stringize()} -> {Output.Stringize()}";
}
}
13 changes: 3 additions & 10 deletions Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,7 @@ static int ChooseNormalStrategy(
/// Buchberger's first criterion: leading monomials sharing no variable give an
/// S-polynomial that always reduces to zero, so it need not be built.
/// </summary>
static bool Coprime(ulong left, ulong right)
{
for (var variable = 0; variable < MultivariatePolynomial.MaxVariables; variable++)
if (MultivariatePolynomial.PowerOfMonomial(left, variable) > 0
&& MultivariatePolynomial.PowerOfMonomial(right, variable) > 0)
return false;
return true;
}
static bool Coprime(Monomial left, Monomial right) => Monomial.Coprime(left, right);

/// <summary>
/// The chain criterion: a third basis element whose leading monomial divides this
Expand All @@ -197,7 +190,7 @@ static bool Coprime(ulong left, ulong right)
/// </summary>
static bool IsRedundantByChain(
List<MultivariatePolynomial> basis, List<(int Left, int Right)> pairs,
MonomialOrder order, int left, int right, ulong lcm)
MonomialOrder order, int left, int right, Monomial lcm)
{
for (var k = 0; k < basis.Count; k++)
{
Expand All @@ -214,7 +207,7 @@ static bool IsRedundantByChain(
}

static MultivariatePolynomial? SPolynomial(
MultivariatePolynomial first, MultivariatePolynomial second, MonomialOrder order, ulong lcm)
MultivariatePolynomial first, MultivariatePolynomial second, MonomialOrder order, Monomial lcm)
{
var firstLeading = first.LeadingMonomial(order);
var secondLeading = second.LeadingMonomial(order);
Expand Down
22 changes: 11 additions & 11 deletions Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ internal static class Fglm
/// zero-dimensional — so running past the ceiling is how a system with infinitely
/// many solutions, or simply too many, announces itself.
/// </summary>
internal static List<ulong>? StandardMonomials(
internal static List<Monomial>? StandardMonomials(
IReadOnlyList<MultivariatePolynomial> basis, int variableCount, MonomialOrder order, int ceiling)
{
var leading = new List<ulong>(basis.Count);
var leading = new List<Monomial>(basis.Count);
foreach (var element in basis)
leading.Add(element.LeadingMonomial(order));

var standard = new List<ulong>();
var queue = new SortedSet<ulong> { 0UL };
var seen = new HashSet<ulong> { 0UL };
var standard = new List<Monomial>();
var queue = new SortedSet<Monomial> { Monomial.One };
var seen = new HashSet<Monomial> { Monomial.One };

while (queue.Count > 0)
{
Expand Down Expand Up @@ -104,7 +104,7 @@ internal static class Fglm
return null;
}

var position = new Dictionary<ulong, int>(standard.Count);
var position = new Dictionary<Monomial, int>(standard.Count);
for (var i = 0; i < standard.Count; i++)
position[standard[i]] = i;

Expand All @@ -114,15 +114,15 @@ internal static class Fglm
var rowVectors = new List<ERational[]>();
var rowCombinations = new List<ERational[]>();
var rowPivots = new List<int>();
var staircase = new List<ulong>();
var staircase = new List<Monomial>();

var lexicographic = new List<MultivariatePolynomial>();
var lexicographicLeading = new List<ulong>();
var lexicographicLeading = new List<Monomial>();

// Lexicographic order is integer order under this packing, so a sorted set of the
// Lexicographic order is the order Monomial compares in, so a sorted set of the
// packed monomials walks them smallest first, which is what FGLM wants.
var queue = new SortedSet<ulong> { 0UL };
var seen = new HashSet<ulong> { 0UL };
var queue = new SortedSet<Monomial> { Monomial.One };
var seen = new HashSet<Monomial> { Monomial.One };

while (queue.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,27 @@ internal enum MonomialOrder

internal sealed partial class MultivariatePolynomial
{
internal ERational CoefficientOf(ulong monomial)
internal ERational CoefficientOf(Monomial monomial)
=> terms.TryGetValue(monomial, out var value) ? value : ERational.Zero;

internal IEnumerable<ulong> Monomials => terms.Keys;
internal IEnumerable<Monomial> Monomials => terms.Keys;

internal static MultivariatePolynomial Term(int variableCount, ulong monomial, ERational coefficient)
/// <summary>The coefficient of the monomial with every exponent zero.</summary>
internal ERational ConstantCoefficient => CoefficientOf(Functions.Monomial.One);

internal static MultivariatePolynomial Term(int variableCount, Monomial monomial, ERational coefficient)
{
var built = new Dictionary<ulong, ERational>();
var built = new Dictionary<Monomial, ERational>();
if (!coefficient.IsZero)
built[monomial] = coefficient.InLowestTerms();
return new(variableCount, built);
}

/// <summary>The greatest monomial under <paramref name="order"/>; zero if there is none.</summary>
internal ulong LeadingMonomial(MonomialOrder order)
internal Monomial LeadingMonomial(MonomialOrder order)
{
var found = false;
ulong best = 0;
var best = Functions.Monomial.One;
foreach (var monomial in terms.Keys)
if (!found || Greater(order, monomial, best))
{
Expand All @@ -61,67 +64,37 @@ internal ulong LeadingMonomial(MonomialOrder order)

internal ERational LeadingCoefficient(MonomialOrder order) => terms[LeadingMonomial(order)];

internal static bool Greater(MonomialOrder order, ulong left, ulong right)
internal static bool Greater(MonomialOrder order, Monomial left, Monomial right)
{
if (order is MonomialOrder.Lexicographic)
return left > right;
int leftDegree = TotalDegree(left), rightDegree = TotalDegree(right);
int leftDegree = left.TotalDegree, rightDegree = right.TotalDegree;
if (leftDegree != rightDegree)
return leftDegree > rightDegree;
for (var variable = MaxVariables - 1; variable >= 0; variable--)
{
int here = PowerOf(left, variable), there = PowerOf(right, variable);
if (here != there)
return here < there;
}
return false;
return left.ReverseLexicographicallyAfter(right);
}

internal static int PowerOfMonomial(ulong monomial, int variable) => PowerOf(monomial, variable);
internal static int PowerOfMonomial(Monomial monomial, int variable) => PowerOf(monomial, variable);

internal static ulong PackMonomial(int variable, int power) => Pack(variable, power);
internal static Monomial PackMonomial(int variable, int power) => Pack(variable, power);

internal static int TotalDegree(ulong monomial)
{
var degree = 0;
for (var variable = 0; variable < MaxVariables; variable++)
degree += PowerOf(monomial, variable);
return degree;
}
internal static int TotalDegree(Monomial monomial) => monomial.TotalDegree;

internal static bool MonomialDivides(ulong divisor, ulong dividend)
{
for (var variable = 0; variable < MaxVariables; variable++)
if (PowerOf(divisor, variable) > PowerOf(dividend, variable))
return false;
return true;
}
internal static bool MonomialDivides(Monomial divisor, Monomial dividend) => divisor.Divides(dividend);

/// <summary>Only valid where <see cref="MonomialDivides"/> holds.</summary>
internal static ulong MonomialQuotient(ulong dividend, ulong divisor)
{
ulong quotient = 0;
for (var variable = 0; variable < MaxVariables; variable++)
quotient |= Pack(variable, PowerOf(dividend, variable) - PowerOf(divisor, variable));
return quotient;
}
internal static Monomial MonomialQuotient(Monomial dividend, Monomial divisor) => dividend - divisor;

internal static ulong MonomialLcm(ulong left, ulong right)
{
ulong lcm = 0;
for (var variable = 0; variable < MaxVariables; variable++)
lcm |= Pack(variable, Math.Max(PowerOf(left, variable), PowerOf(right, variable)));
return lcm;
}
internal static Monomial MonomialLcm(Monomial left, Monomial right) => Functions.Monomial.Lcm(left, right);

/// <summary>
/// Reaches the private multiplication by a single term, which already refuses rather
/// than wraps when an exponent would outgrow its byte.
/// </summary>
internal MultivariatePolynomial? TimesTerm(ulong monomial, ERational coefficient)
internal MultivariatePolynomial? TimesTerm(Monomial monomial, ERational coefficient)
=> MultiplyByTerm(monomial, coefficient);

internal static bool TryTimesMonomials(ulong left, ulong right, int variableCount, out ulong product)
internal static bool TryTimesMonomials(Monomial left, Monomial right, int variableCount, out Monomial product)
=> TryMultiplyMonomials(left, right, variableCount, out product);

internal MultivariatePolynomial MakeMonic(MonomialOrder order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ private static RationalPolynomial[] MultiplySeries(
continue;
if (!pair.Value.IsConstant)
return null; // a variable other than the two this works in
coefficients[pair.Key] = pair.Value.CoefficientOf(0);
coefficients[pair.Key] = pair.Value.ConstantCoefficient;
}
return RationalPolynomial.Create(coefficients);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static bool NextPoint(int[] point, int attempt, int count)
{
if (depth == others.Count)
return poly.IsZero ? ERational.Zero
: poly.IsConstant ? poly.CoefficientOf(0) : (ERational?)null;
: poly.IsConstant ? poly.ConstantCoefficient : (ERational?)null;

var total = ERational.Zero;
var at = EInteger.FromInt32(point[depth]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ bool Peel(MultivariatePolynomial rest, int depth, int at)
{
if (!rest.IsConstant)
return false;
coefficients[at] = coefficients[at].Add(rest.CoefficientOf(0));
coefficients[at] = coefficients[at].Add(rest.ConstantCoefficient);
return true;
}
foreach (var pair in rest.CoefficientsIn(order[depth]))
Expand Down
Loading
Loading