diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 5abee0b6d..f6f397195 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs index ddf21c44b..5df09baff 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs @@ -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. @@ -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; } @@ -377,6 +388,13 @@ static bool Satisfies( IReadOnlyList equations, IReadOnlyList 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(variables.Count); for (var i = 0; i < variables.Count; i++) substitutions[variables[i]] = candidate[i]; @@ -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); diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs index 27b8d34e1..b9d85b892 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs @@ -287,6 +287,10 @@ private static bool Within(Real value, Entity left, Entity right) if (solutions is null) return roots; var ofSignums = new HashSet(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++) { @@ -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; } @@ -389,8 +398,8 @@ private static bool TryReadReal(Entity value, out Real? real) /// Whether is too large, beside , to be /// what rounding leaves of a zero. /// - 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; /// /// An expression as (Real + Imaginary i) / Denominator, each of the three real diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs index 31880d1e9..8c4a363ff 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs @@ -309,7 +309,7 @@ internal static IEnumerable 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 } @@ -320,6 +320,37 @@ internal static IEnumerable SolveCubic(Entity a, Entity b, Entity c, Ent shifted ? -(b + uk * C) / 3 / a : -(b + uk * C + D0 / C / uk) / 3 / a); } + /// + /// Whether , 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 it is the sum of; never where it is not a number. + /// + /// + /// The formulas asked value.Evaled == 0, and two numbers are equal within + /// , a millionth, so every quantity under a millionth was + /// taken for 0. In a quartic whose roots are near 10^-30, U is near 10^-30, and the branch for + /// U = 0 wrote the roots of another quartic, which every check then refused: 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, and the quartic + /// 10^120 x^4 + 10^90 x^3 - 7 10^60 x^2 + 2 10^30 x + 5 = 0 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 + /// + 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 }; /// Solves ax^4 + bx^3 + cx^2 + dx + e /// Coefficient of x^4 @@ -348,7 +379,7 @@ internal static IEnumerable 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)); @@ -357,10 +388,21 @@ internal static IEnumerable 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; diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 39609de86..43f47e2e3 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -513,6 +513,22 @@ internal enum ResidualWeight /// https://github.com/asc-community/AngouriMath/issues/2627 /// 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); + } + + /// in the decimal context in force. + 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 @@ -645,6 +661,17 @@ private static bool IsExactAndAlgebraic(Entity expression) /// https://github.com/asc-community/AngouriMath/issues/2645 /// 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; diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs index 8c2c6d658..ce39238e8 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs @@ -86,6 +86,10 @@ private static bool IsFiniteRoundedOntoAnInfinity(Entity part) /// private static bool CannotBeZero(Entity expr) => expr switch { + // A rational is zero only where it is 0: IsZero reads a number to within ten to the minus sixteen, and + // 1/10^180 was taken for 0, so (1/10^180) sqrt(17863/6912), whose decimals flush to 0, was simplified to 0. + // https://github.com/asc-community/AngouriMath/issues/2650 + Rational exactly => !exactly.ERational.IsZero, Complex number => !number.IsZero && IsFiniteExactly(number), Variable { IsConstant: true } constant => constant.Evaled is Complex { IsZero: false } value && IsFiniteExactly(value), Mulf(var left, var right) => CannotBeZero(left) && CannotBeZero(right), @@ -107,8 +111,19 @@ private static bool IsFiniteRoundedOntoAnInfinity(Entity part) /// zero, or a product, a quotient, a power or a sum of those whose sign the parts decide; /// where they do not. /// - private static int? SignOfANonzero(Entity expr) => expr switch + private static int? SignOfANonzero(Entity expr) + // A sum, product, quotient or power of rational numbers whose sign the parts do not decide has the sign of its + // value, which rational arithmetic keeps exact however small it is. The parts did not decide + // 5/10^200 - 4/10^200, so sqrt(5/10^200 - 4/10^200), whose decimals flush to 0, was not known to be a + // power that cannot be 0, and was taken for the 0 it evaluated to. The quartic formula takes such a square + // root of its coefficients for a quartic whose roots are near 10^-30, and wrote the roots of another quartic. + // https://github.com/asc-community/AngouriMath/issues/2650 + => SignByTheParts(expr) ?? (expr is Sumf or Minusf or Mulf or Divf or Powf && expr.Evaled is Rational { ERational: { IsZero: false } exactly } ? exactly.Sign : null); + + /// as the parts decide it. + private static int? SignByTheParts(Entity expr) => expr switch { + Rational exactly => exactly.ERational.IsZero ? null : exactly.ERational.Sign, Real number => !number.IsZero && number.IsFinite ? (number.IsPositive ? 1 : -1) : null, Variable { IsConstant: true } constant => constant.Evaled is Real { IsZero: false } value ? (value.IsPositive ? 1 : -1) : null, Mulf(var left, var right) => SignOfANonzero(left) * SignOfANonzero(right), @@ -120,6 +135,57 @@ private static bool IsFiniteRoundedOntoAnInfinity(Entity part) _ => null }; + /// + /// Whether has a product, a quotient or a power of exact numbers that evaluates to the exact 0 + /// though none of its parts can be 0: its value is past the decimals' exponent range, ten to the minus a hundred by default, and + /// flushed. Such a part is not 0, and an exact value of the whole that rests on it is not the whole's value: + /// -Q/2 + sqrt(Q^2/4 + P^3/27), the square root about 1.6e-180, evaluated to the rational -Q/2, and + /// the quartic formula wrote the roots of another quartic. Walked only where the whole evaluates to an exact + /// number. https://github.com/asc-community/AngouriMath/issues/2650 + /// + private static bool HasARootRoundedOntoZero(Entity node) + { + // Recursive and with the arithmetic nodes read directly, so that the walk allocates nothing: it runs on every + // exact shortcut, and a stack and the children's enumerators were 1.5 KB on each differentiation. + switch (node) + { + case Number or Variable: + return false; + case Mulf or Divf or Powf when node.Evaled is Integer { IsZero: true } && CannotBeZero(node) && IsOfExactNumbers(node): + return true; + case Sumf(var left, var right): + return HasARootRoundedOntoZero(left) || HasARootRoundedOntoZero(right); + case Minusf(var left, var right): + return HasARootRoundedOntoZero(left) || HasARootRoundedOntoZero(right); + case Mulf(var left, var right): + return HasARootRoundedOntoZero(left) || HasARootRoundedOntoZero(right); + case Divf(var left, var right): + return HasARootRoundedOntoZero(left) || HasARootRoundedOntoZero(right); + case Powf(var left, var right): + return HasARootRoundedOntoZero(left) || HasARootRoundedOntoZero(right); + default: + var children = node.DirectChildren; + for (var i = 0; i < children.Count; i++) + if (HasARootRoundedOntoZero(children[i])) + return true; + return false; + } + } + + /// + /// Whether every number in is exact. A part with a decimal in it, such as a root that + /// Newton's method found, is a decimal approximation already, and one far below the exponent range was folded to 0 + /// before as well: keeping it whole left 10^-220 times a Newton root standing in a residual, and the solver + /// gave no roots at all for an equation that has two. + /// + private static bool IsOfExactNumbers(Entity part) + { + foreach (var node in part.Nodes) + if (node is Number { IsExact: false }) + return false; + return true; + } + /// /// For two-argument nodes /// Used in InnerSimplify and InnerEval @@ -162,7 +228,7 @@ private Entity ExpandOnTwoArguments( bool settlesNaN = false) { if (isExact && this.Evaled is (Number { IsExact: true } or Boolean) and var n && !IsAZeroOnlyRoundingMade(this, n) - && !IsAnInfinityOnlyRoundingMade(this, n)) + && !IsAnInfinityOnlyRoundingMade(this, n) && !HasARootRoundedOntoZero(this)) return n; left = left.InnerSimplified(isExact); right = right.InnerSimplified(isExact); @@ -186,7 +252,7 @@ Entity ops(Entity a, Entity b) if (operation(a, b) is { } res) return res; if (isExact && defaultCtor(this, a, b) is var built && built.Evaled is Number { IsExact: true } n && !IsAZeroOnlyRoundingMade(built, n) - && !IsAnInfinityOnlyRoundingMade(built, n)) + && !IsAnInfinityOnlyRoundingMade(built, n) && !HasARootRoundedOntoZero(built)) return n; return defaultCtor(this, a, b); } diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootFarBelowOneIsVerifiedTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootFarBelowOneIsVerifiedTest.cs new file mode 100644 index 000000000..c3cce3e15 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootFarBelowOneIsVerifiedTest.cs @@ -0,0 +1,73 @@ +// +// 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.Linq; +using AngouriMath; +using AngouriMath.Extensions; +using PeterO.Numbers; +using Xunit; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Set; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// An equation solved exactly with its roots near 1 was declined with x written as 10^30 x: the decimals flush a value + /// under ten to the minus a hundred to 0, and an exact value resting on a flushed part was taken for exact, the quartic + /// formula branched on a value under a millionth as on 0, and a true root's residual, flushed term by term, was read as + /// evidence against it. https://github.com/asc-community/AngouriMath/issues/2650 + /// + [Trait("Area", "Algebra")] + public sealed class AnExactRootFarBelowOneIsVerifiedTest + { + // It was 0. Evaluated without the exponent floor, it is 10^-100. + [Fact] + public void AFlushedSquareRootIsNotZero() + { + var root = "sqrt(5/10^200 - 4/10^200)".ToEntity().InnerSimplified; + Assert.NotEqual(Number.Integer.Zero, root); + using var _ = MathS.Settings.DecimalPrecisionContext.Set(new EContext(100, ERounding.HalfUp, -100000, 100000, false)); + var value = ((Number.Complex)root.Evaled).RealPart.EDecimal; + Assert.True(Close(value, EDecimal.Create(1, -100).Multiply(EDecimal.One)) && !value.IsZero, $"{value}"); + } + + // The roots of the equation with x written as 10^30 x are those of the equation as written over 10^30. + [Theory] + [InlineData("x^4 + x^3 - 7 x^2 + 2 x + 5 = 0")] + [InlineData("(1 - i) + (1 - 2i) x + 2 x^2 - 3 Im(x) = 0")] + [InlineData("3 + (2 + i) x^2 + 3 Im(x) = 0")] + // Q = 0 in the quartic's resolvent: U - P/(3U) divided by a U that evaluated to 0 near 10^-60. + [InlineData("9 x^4 + 12 x^3 + 4 x - 1 = 0")] + // Answered { } on the way: the quartic its square gives had no root to check, and then a row of the + // system for the parts of x, put back in decimals with the exponent floor, was no solution. + [InlineData("1 - 2 x + x abs(-2 - 3 x) = 0")] + [InlineData("-2 - 3 x + (2 + 2i x) sgn(-3 + (-1 - i) x) = 0")] + public void TheRootsScaleWithX(string equation) + { + var asWritten = Values(equation.ToEntity().Solve("x")); + var scaled = Values(System.Text.RegularExpressions.Regex.Replace(equation, "(? + Close(root.Re.ScaleByPowerOfTen(30), re) && Close(root.Im.ScaleByPowerOfTen(30), im)); + } + + // Evaluated without the exponent floor, under which the parts of a root near 10^-30 flush. + private static System.Collections.Generic.List<(EDecimal Re, EDecimal Im)> Values(Set solved) + { + using var _ = MathS.Settings.DecimalPrecisionContext.Set(new EContext(100, ERounding.HalfUp, -100000, 100000, false)); + return Assert.IsType(solved).Select(root => (Number.Complex)root.Evaled) + .Select(root => (root.RealPart.EDecimal, root.ImaginaryPart.EDecimal)).ToList(); + } + + // Relative, as the roots are near 10^-30 and the root of the first test near 10^-100. + private static bool Close(EDecimal a, EDecimal b) + => b.IsZero ? a.IsZero || a.Abs().CompareTo(EDecimal.Create(1, -100)) <= 0 + : a.Subtract(b).Abs().CompareTo(EDecimal.Create(1, -40).Multiply(b.Abs())) <= 0; + } +}