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
24 changes: 24 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ large beside them ([#2645](https://github.com/asc-community/AngouriMath/issues/2
| `10^6 sinh(x / 10^6) = 2 x` | `{ -4.9027…E-13, 0, 4.9027…E-13 }` | `{ 0 }` |
| `tanh(x) = x / 2` | its roots | unchanged |

### An exact root far below 1 is verified exactly

**Not silent: an unsolved equation is now solved, and a wrong zero is gone.** The decimals that evaluation uses stop
at about `10^-100`. Below that a value flushed to `0`, and three things read the flushed value as exact. First, a
square root or power of an exact rational that flushed was put in its place as `0`, so `sqrt(5/10^200 - 4/10^200)`
simplified to `0`. Second, the quartic and cubic formulas chose their branch by whether a part was within `10^-6`
of `0`. Third, the residual that accepts a candidate root was evaluated with the same floor, as was the imaginary
part that `ModulusSolver` asks to be clear of `0`. So the equations of `x` with roots near `10^-30` stayed unsolved
while the same equations in `10^30 x` were solved.

A root, power or quotient of exact rationals is no longer replaced by a `0` that only the floor made. The branch
tests compare a part with the terms it is made of: exactly when it is a rational, and otherwise relative to their
size. A quartic whose resolvent has the root 0 takes it rather than dividing by a part that evaluates to 0. Residuals,
the imaginary-part test and the decimal entries of a system's solution are evaluated without the floor, and a decimal
residual is measured against its terms alone, not against 1. A residual that is not shown to be zero still declines
the candidate ([#2650](https://github.com/asc-community/AngouriMath/issues/2650)).

| | Was (2.10.0) | Now |
|---|---|---|
| `"sqrt(5/10^200 - 4/10^200)".Simplify()` | `0` | `sqrt(1/10^200)`, written out |
| `"10^120 x^4 + 10^90 x^3 - 7 * 10^60 x^2 + 2 * 10^30 x + 5 = 0".Solve("x")` | the equation, unsolved | its four roots, each `10^-30` times a root of `x^4 + x^3 - 7 x^2 + 2 x + 5` |
| `"(1 - i) + (1 - 2 * i) * (10^30 x) + 2 (10^30 x)^2 - 3 Im(10^30 x) = 0".Solve("x")` | the equation, unsolved | its roots, `10^-30` times those of the equation in `x` |
| `"3 + (2 + i) (10^30 x)^2 + 3 Im(10^30 x) = 0".Solve("x")` | the equation, unsolved | its roots, `10^-30` times those of the equation in `x` |

### The whole part of a large rational power is taken out

**Silent, in the printed form only.** A rational raised to a rational power was written as a whole part times a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
Expand Down Expand Up @@ -153,10 +153,21 @@ static bool Attempt(
}

var builder = new MatrixBuilder(variables.Count);
foreach (var candidate in found)
// An entry already in decimals is the number it evaluates to, rather than the
// polynomial in decimals that back-substitution wrote it as.
builder.Add(candidate.Select(entry => entry.Nodes.Any(IsADecimal) && entry.Evaled is Number.Complex value ? value : entry).ToArray());
// Evaluated without the exponent floor, as Satisfies checked them: under ten to the minus a hundred a value
// flushes to 0, and an entry near 10^-31 written as a polynomial in decimals came out of terms that had
// flushed, as the imaginary part 1.4e-30 of a root whose part is 6e-31, and the row was no solution.
// https://github.com/asc-community/AngouriMath/issues/2650
if (found.Any(candidate => candidate.Any(entry => entry.Nodes.Any(IsADecimal))))
{
using var unbounded = MathS.Settings.DecimalPrecisionContext.Set(InternalAMExtensions.WithUnboundedExponents(MathS.Settings.DecimalPrecisionContext.Value));
foreach (var candidate in found)
// An entry already in decimals is the number it evaluates to, rather than the
// polynomial in decimals that back-substitution wrote it as.
builder.Add(candidate.Select(entry => entry.Nodes.Any(IsADecimal) && entry.Evaled is Number.Complex value ? value : entry).ToArray());
}
else
foreach (var candidate in found)
builder.Add(candidate);
solutions = builder.ToMatrix();
return true;
}
Expand Down Expand Up @@ -377,6 +388,13 @@ static bool Satisfies(
IReadOnlyList<Entity> equations, IReadOnlyList<Variable> variables,
Entity[] candidate, GroebnerBudget budget)
{
// The zero is decided as it is at unit size, without the decimals' exponent floor: under ten to the minus a
// hundred a value flushes to 0, so the residual of a candidate near 10^-30, whose terms are products of
// values near 10^-60 and below, came out of cancellations that never happened, and not 0. Every candidate,
// not only one that fails with the floor: a flushed residual also passes a row that is no solution, and the
// system for the parts of x in (1 - i) + (1 - 2i) 10^30 x + 2 (10^30 x)^2 - 3 Im(10^30 x) = 0 lost its roots.
// https://github.com/asc-community/AngouriMath/issues/2650
using var _ = MathS.Settings.DecimalPrecisionContext.Set(InternalAMExtensions.WithUnboundedExponents(MathS.Settings.DecimalPrecisionContext.Value));
var substitutions = new Dictionary<Variable, Entity>(variables.Count);
for (var i = 0; i < variables.Count; i++)
substitutions[variables[i]] = candidate[i];
Expand Down Expand Up @@ -418,7 +436,10 @@ private static bool IsNegligible(Entity substituted)
{
if (substituted.Evaled is not Number.Complex { IsFinite: true } residual)
return false;
var largest = EDecimal.One;
// Beside the terms alone, not beside 1 as well, which made the share absolute below 1: the parts of a root
// near 10^-30 make terms near 10^-60, and any row at all came under the 10^-10 that 1 set.
// https://github.com/asc-community/AngouriMath/issues/2650
var largest = EDecimal.Zero;
foreach (var term in Sumf.LinearChildren(substituted))
if (term.Evaled is Number.Complex { IsFinite: true } value)
largest = EDecimal.Max(largest, value.Abs().EDecimal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ private static bool Within(Real value, Entity left, Entity right)
if (solutions is null)
return roots;
var ofSignums = new HashSet<Variable>(splitter.Signums.Select(argument => splitter.Moduli[argument]));
// The rows are read, and the roots put back, without the decimals' exponent floor, under which a value
// flushes to 0: for x written 10^30 x the parts of a root are near 10^-31, and the products the residual is
// made of fall under ten to the minus a hundred. https://github.com/asc-community/AngouriMath/issues/2650
using var _ = MathS.Settings.DecimalPrecisionContext.Set(InternalAMExtensions.WithUnboundedExponents(MathS.Settings.DecimalPrecisionContext.Value));

for (var row = 0; row < solutions.RowCount; row++)
{
Expand Down Expand Up @@ -378,8 +382,13 @@ private static bool TryReadReal(Entity value, out Real? real)
case Complex { IsFinite: true } complex when complex.ImaginaryPart.IsZero:
real = complex.RealPart;
return true;
// Beside the value's own size, not beside 1: a root near 10^-31, as the system for the parts of x has
// where x is written 10^30 x, has parts of that size, and its imaginary part 7e-31 against the floor
// of 1e-30 that max(1, |re|) set was not told from rounding, and declined the equation.
// https://github.com/asc-community/AngouriMath/issues/2650
case Complex { IsFinite: true } complex:
return IsClearOfZero(complex.ImaginaryPart.EDecimal, complex.RealPart.EDecimal);
return IsClearOfZero(complex.ImaginaryPart.EDecimal,
PeterO.Numbers.EDecimal.Max(complex.RealPart.EDecimal.Abs(), complex.ImaginaryPart.EDecimal.Abs()), besideOne: false);
default:
return false;
}
Expand All @@ -389,8 +398,8 @@ private static bool TryReadReal(Entity value, out Real? real)
/// Whether <paramref name="part"/> is too large, beside <paramref name="scale"/>, to be
/// what rounding leaves of a zero.
/// </summary>
private static bool IsClearOfZero(PeterO.Numbers.EDecimal part, PeterO.Numbers.EDecimal scale)
=> part.Abs().CompareTo(PeterO.Numbers.EDecimal.FromString("1E-30").Multiply(PeterO.Numbers.EDecimal.Max(PeterO.Numbers.EDecimal.One, scale.Abs()))) > 0;
private static bool IsClearOfZero(PeterO.Numbers.EDecimal part, PeterO.Numbers.EDecimal scale, bool besideOne = true)
=> part.Abs().CompareTo(PeterO.Numbers.EDecimal.FromString("1E-30").Multiply(besideOne ? PeterO.Numbers.EDecimal.Max(PeterO.Numbers.EDecimal.One, scale.Abs()) : scale.Abs())) > 0;

/// <summary>
/// An expression as <c>(Real + Imaginary i) / Denominator</c>, each of the three real
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ internal static IEnumerable<Entity> SolveCubic(Entity a, Entity b, Entity c, Ent
// D0/C being 0; that is -b/(3a) three times where D1 is 0 as well. For a negative D1 the
// cube root is written as the negative of the cube root of -D1, which is a cube root of
// D1 whichever root of a negative number a power is read as.
var shifted = D0.Evaled == 0;
var shifted = IsZeroBesideItsTerms(D0, MathS.Sqr(b), 3 * a * c);
var C = !shifted
? MathS.Pow((D1 + MathS.Sqrt(MathS.Sqr(D1) - 4 * MathS.Pow(D0, 3))) / 2, Rational.Create(1, 3))
: D1.Evaled is Real { IsNegative: true }
Expand All @@ -320,6 +320,37 @@ internal static IEnumerable<Entity> SolveCubic(Entity a, Entity b, Entity c, Ent
shifted ? -(b + uk * C) / 3 / a : -(b + uk * C + D0 / C / uk) / 3 / a);
}

/// <summary>
/// Whether <paramref name="value"/>, which a formula below divides by or branches on, is zero: exactly where
/// it is a rational number, and otherwise where its value is no more than a part in 10^30 of the largest of
/// the <paramref name="terms"/> it is the sum of; never where it is not a number.
/// </summary>
/// <remarks>
/// The formulas asked <c>value.Evaled == 0</c>, and two numbers are equal within
/// <see cref="MathS.Settings.PrecisionErrorCommon"/>, a millionth, so every quantity under a millionth was
/// taken for 0. In a quartic whose roots are near <c>10^-30</c>, <c>U</c> is near <c>10^-30</c>, and the branch for
/// <c>U = 0</c> wrote the roots of another quartic, which every check then refused: the system for the parts of x
/// in <c>(1 - i) + (1 - 2i) 10^30 x + 2 (10^30 x)^2 - 3 Im(10^30 x) = 0</c>, and the quartic
/// <c>10^120 x^4 + 10^90 x^3 - 7 10^60 x^2 + 2 10^30 x + 5 = 0</c> itself, were left unsolved. A branch taken on a
/// value that is not zero gives roots that fail their check and are not answered, so this decides zero only
/// where it is.
/// https://github.com/asc-community/AngouriMath/issues/2650
/// </remarks>
private static bool IsZeroBesideItsTerms(Entity value, params Entity[] terms)
{
var simplified = value.InnerSimplified;
if (simplified is Rational exactly)
return exactly.IsZero;
if (simplified.Evaled is not Complex { IsFinite: true } number)
return false;
var largest = EDecimal.Zero;
foreach (var term in terms)
if (term.Evaled is Complex { IsFinite: true } part)
largest = EDecimal.Max(largest, part.Abs().EDecimal);
var size = number.Abs().EDecimal;
return size.IsZero || !largest.IsZero && size.CompareTo(largest.Multiply(EDecimal.Create(1, -30))) <= 0;
}

[ConstantField] private static readonly int[] sqrtsOf1 = { -1, 1 };
/// <summary>Solves ax^4 + bx^3 + cx^2 + dx + e</summary>
/// <param name="a">Coefficient of x^4</param>
Expand Down Expand Up @@ -348,7 +379,7 @@ internal static IEnumerable<Entity> SolveQuartic(Entity a, Entity b, Entity c, E
// -b / 4 * a, which is -b a / 4, and for a leading coefficient other than 1 or -1 every
// candidate was off and failed its check: 31x^4 - 62x^3 + 33x^2 - 2x - 1 = 0, with four
// real roots, was answered { }.
if (beta.Evaled == 0)
if (IsZeroBesideItsTerms(beta, MathS.Pow(b, 3) / (8 * MathS.Pow(a, 3)), (b * c) / (2 * MathS.Sqr(a)), d / a))
return sqrtsOf1.SelectMany(_ => sqrtsOf1,
(s, t) => -b / (4 * a) + s * MathS.Sqrt((-alpha + t * MathS.Sqrt(MathS.Sqr(alpha) - 4 * gamma)) / 2));

Expand All @@ -357,10 +388,21 @@ internal static IEnumerable<Entity> SolveQuartic(Entity a, Entity b, Entity c, E
.InnerSimplified;
var Q = (-MathS.Pow(alpha, 3) / 108 + alpha * gamma / 3 - MathS.Sqr(beta) / 8)
.InnerSimplified;
var R = -Q / 2 + MathS.Sqrt(MathS.Sqr(Q) / 4 + MathS.Pow(P, 3) / 27);
var S = MathS.Sqrt(MathS.Sqr(Q) / 4 + MathS.Pow(P, 3) / 27);
var R = -Q / 2 + S;
var U = MathS.Pow(R, oneThird)
.InnerSimplified;
var y = (Rational.Create(-5, 6) * alpha + U + (U.Evaled == 0 ? -MathS.Pow(Q, oneThird) : -P / (3 * U)))
// R is 0 exactly where P is 0 and the square root of Q^2/4 is Q/2, Q not negative: decided on the rationals
// where P and Q are rational, since R itself can be past the decimal range.
var rIsZero = P is Rational exactP && Q is Rational exactQ
? exactP.IsZero && !exactQ.ERational.IsNegative
: IsZeroBesideItsTerms(R, Q / 2, S);
// y is -5/6 alpha plus a root t of t^3 + P t + Q, and any root serves. Where Q is 0, t = 0 is one, and is
// taken rather than U - P/(3U), which is 0 too but divides by U: for 9 10^120 x^4 + 12 10^90 x^3 +
// 4 10^30 x - 1 = 0, U is near 10^-60, evaluated to 0, and no root could be checked.
// https://github.com/asc-community/AngouriMath/issues/2650
var y = (Rational.Create(-5, 6) * alpha
+ (Q is Rational { ERational.IsZero: true } ? Integer.Zero : U + (rIsZero ? -MathS.Pow(Q, oneThird) : -P / (3 * U))))
.InnerSimplified;
var W = MathS.Sqrt(alpha + 2 * y)
.InnerSimplified;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,22 @@ internal enum ResidualWeight
/// https://github.com/asc-community/AngouriMath/issues/2627
/// </remarks>
internal static ResidualWeight Weigh(Entity substituted)
{
// A residual that weighs against the root is weighed again without the exponent floor of the decimals, ten
// to the minus a hundred by default: under it a value flushes to 0, and that is no rounding twice the digits
// take away. A root near 10^-30 of a quartic has a fourth power near 10^-120, and the residual of a true
// root, flushed term by term, came to 0.087 at a hundred digits and at two hundred, and the root was dropped
// as none. Only then: a setting set is a new state that every cached value is computed again under, and
// set on every call it was a third more bytes on solving x^2 + x + 1.
// https://github.com/asc-community/AngouriMath/issues/2650
if (WeighWithin(substituted) is var weight && weight is ResidualWeight.None)
return weight;
using var _ = MathS.Settings.DecimalPrecisionContext.Set(InternalAMExtensions.WithUnboundedExponents(MathS.Settings.DecimalPrecisionContext.Value));
return WeighWithin(substituted);
}

/// <summary><see cref="Weigh"/> in the decimal context in force.</summary>
private static ResidualWeight WeighWithin(Entity substituted)
{
// A residual of rational numbers alone is decided exactly. Evaluated, a residual of 3e-217 is zero to the
// working digits, and an enclosure to them holds zero: y^2 - 3 = 0, x^2 + (P - Q y) x - 1 = 0 at the 190th
Expand Down Expand Up @@ -645,6 +661,17 @@ private static bool IsExactAndAlgebraic(Entity expression)
/// https://github.com/asc-community/AngouriMath/issues/2645
/// </remarks>
private static bool IsLargeBesideTheTermsAsWritten(Entity equation, Variable x, Entity root, Entity substituted)
{
// A residual that reads as large is read again without the exponent floor, as Weigh reads one: a root near
// 10^-30 has terms whose products fall under ten to the minus a hundred, and flushed, a true root's residual
// is no residual of it. https://github.com/asc-community/AngouriMath/issues/2650
if (!IsLargeBesideTheTermsAsWrittenWithin(equation, x, root, substituted))
return false;
using var _ = MathS.Settings.DecimalPrecisionContext.Set(InternalAMExtensions.WithUnboundedExponents(MathS.Settings.DecimalPrecisionContext.Value));
return IsLargeBesideTheTermsAsWrittenWithin(equation, x, root, substituted);
}

private static bool IsLargeBesideTheTermsAsWrittenWithin(Entity equation, Variable x, Entity root, Entity substituted)
{
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
return false;
Expand Down
Loading
Loading