|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System.Linq; |
| 9 | +using System.Numerics; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | +using static AngouriMath.Entity; |
| 13 | +using static AngouriMath.Entity.Set; |
| 14 | + |
| 15 | +namespace AngouriMath.Tests.Algebra |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// A polynomial whose roots have no closed form is solved by the Durand-Kerner iteration, |
| 19 | + /// which started its approximations on a circle of radius at least 1 and settled them to an |
| 20 | + /// absolute tolerance. So a polynomial with every root near <c>10^-12</c> did not settle, and |
| 21 | + /// one with every root near <c>10^-30</c> settled on NaN, its steps having divided by numbers |
| 22 | + /// below the decimal range. The iteration now runs on the roots scaled to about unit size. |
| 23 | + /// Found by solving generated equations with <c>x</c> written as <c>10^12 x</c>; no issue |
| 24 | + /// was filed. |
| 25 | + /// </summary> |
| 26 | + [Trait("Area", "Algebra")] |
| 27 | + public sealed class RootsOfAnySizeAreFoundNumericallyTest |
| 28 | + { |
| 29 | + /// <summary>The coefficients of an octic whose eight roots are near 1, highest power first.</summary> |
| 30 | + private static readonly (int Numerator, int Denominator)[] Octic = |
| 31 | + { |
| 32 | + (1, 1), (-7, 4), (-137, 216), (869, 432), (-12377, 23328), |
| 33 | + (-2557, 15552), (-6815, 93312), (961, 23328), (961, 93312), |
| 34 | + }; |
| 35 | + |
| 36 | + /// <summary>The coefficients of <c>x^5 - 3x + 1</c>, highest power first.</summary> |
| 37 | + private static readonly (int Numerator, int Denominator)[] Quintic = |
| 38 | + { (1, 1), (0, 1), (0, 1), (0, 1), (-3, 1), (1, 1) }; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The polynomial with the coefficients <paramref name="coefficients"/> and its roots |
| 42 | + /// times <c>10^shift</c>, with each coefficient written out as the rational it is. |
| 43 | + /// </summary> |
| 44 | + private static Entity Scaled((int Numerator, int Denominator)[] coefficients, int shift) |
| 45 | + { |
| 46 | + var degree = coefficients.Length - 1; |
| 47 | + var x = MathS.Var("x"); |
| 48 | + Entity? polynomial = null; |
| 49 | + for (var i = 0; i <= degree; i++) |
| 50 | + { |
| 51 | + var (numerator, denominator) = coefficients[i]; |
| 52 | + if (numerator == 0) |
| 53 | + continue; |
| 54 | + // The coefficient of x^k is c_k 10^(shift (n - k)). |
| 55 | + var power = shift * i; |
| 56 | + var scale = BigInteger.Pow(10, System.Math.Abs(power)); |
| 57 | + var top = power >= 0 ? numerator * scale : numerator; |
| 58 | + var bottom = power >= 0 ? denominator : denominator * scale; |
| 59 | + Entity coefficient = Number.Rational.Create( |
| 60 | + PeterO.Numbers.EInteger.FromString(top.ToString()), |
| 61 | + PeterO.Numbers.EInteger.FromString(bottom.ToString())); |
| 62 | + // Written as the polynomial is: x^0 is 1 only where x is not 0, which is a condition |
| 63 | + // on the answer, and the constant term is the coefficient alone. |
| 64 | + var k = degree - i; |
| 65 | + var term = k == 0 ? coefficient : k == 1 ? coefficient * x : coefficient * MathS.Pow(x, k); |
| 66 | + polynomial = polynomial is null ? term : polynomial + term; |
| 67 | + } |
| 68 | + return polynomial!; |
| 69 | + } |
| 70 | + |
| 71 | + [Theory] |
| 72 | + [InlineData(-12)] |
| 73 | + [InlineData(-30)] |
| 74 | + [InlineData(-45)] |
| 75 | + [InlineData(12)] |
| 76 | + [InlineData(0)] |
| 77 | + public void EveryRootOfTheOcticIsFound(int shift) => EveryRootIsFound(Octic, shift); |
| 78 | + |
| 79 | + [Theory] |
| 80 | + [InlineData(-30)] |
| 81 | + [InlineData(-45)] |
| 82 | + [InlineData(30)] |
| 83 | + [InlineData(0)] |
| 84 | + public void EveryRootOfTheQuinticIsFound(int shift) => EveryRootIsFound(Quintic, shift); |
| 85 | + |
| 86 | + private static void EveryRootIsFound((int Numerator, int Denominator)[] coefficients, int shift) |
| 87 | + { |
| 88 | + var answer = Assert.IsType<FiniteSet>(Scaled(coefficients, shift).SolveEquation("x")); |
| 89 | + Assert.Equal(coefficients.Length - 1, answer.Count); |
| 90 | + var unscaled = Scaled(coefficients, 0); |
| 91 | + var back = MathS.Pow(10, -shift); |
| 92 | + foreach (var root in answer) |
| 93 | + { |
| 94 | + // A root of the scaled polynomial, scaled back, is a root of the unscaled one. |
| 95 | + var residual = Assert.IsAssignableFrom<Number.Complex>(unscaled.Substitute("x", root * back).EvalNumerical()); |
| 96 | + Assert.True(residual.Abs() < 1e-40, $"{root} leaves {residual}"); |
| 97 | + } |
| 98 | + // And they are distinct, each to far more than the digits it is checked to. |
| 99 | + var values = answer.Select(root => (root * back).EvalNumerical()).Cast<Number.Complex>().ToList(); |
| 100 | + for (var i = 0; i < values.Count; i++) |
| 101 | + for (var j = i + 1; j < values.Count; j++) |
| 102 | + Assert.True((values[i] - values[j]).Abs() > 1e-3, $"{values[i]} and {values[j]}"); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// A root far smaller than the others is a root, not a residual of cancellation: the root |
| 107 | + /// near <c>1/(3 10^90)</c> beside four of size <c>1.3e-15</c> was rounded onto 0, which the |
| 108 | + /// polynomial leaves at <c>10^-150</c>, and 0 was answered among the roots. |
| 109 | + /// </summary> |
| 110 | + [Theory] |
| 111 | + [InlineData("x^5 - 3 * 10^(-60) * x + 10^(-150) = 0", 90)] |
| 112 | + [InlineData("x^5 - 3 * x + 10^(-60) = 0", 60)] |
| 113 | + public void ARootFarSmallerThanTheOthersIsNotZero(string equation, int digits) |
| 114 | + { |
| 115 | + var answer = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x")); |
| 116 | + Assert.Equal(5, answer.Count); |
| 117 | + Assert.DoesNotContain(answer, root => root.EvalNumerical() is Number.Complex { IsZero: true }); |
| 118 | + var small = MathS.Pow(3 * MathS.Pow(10, digits), -1); |
| 119 | + Assert.Contains(answer, root => ((root - small) / small).EvalNumerical() is Number.Complex relative && relative.Abs() < 1e-20); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Solved for the parts of x, a system whose last univariate equation is the octic above with |
| 124 | + /// its roots near <c>10^-12</c>: it kept one root of the two. Near <c>10^-30</c> the decimals |
| 125 | + /// of each root flushed every term but the constant, and the system read as having no |
| 126 | + /// solution answered <c>{ }</c>; it is declined then, and another route answers. |
| 127 | + /// </summary> |
| 128 | + [Theory] |
| 129 | + [InlineData(0)] |
| 130 | + [InlineData(12)] |
| 131 | + [InlineData(30)] |
| 132 | + public void BothRootsOfAnEquationInAModulusAreFound(int scale) |
| 133 | + { |
| 134 | + var x = $"(10^{scale} * x)"; |
| 135 | + var equation = $"(1 + i) + 2 * {x} - 3 * {x}^2 + {x} * abs(3 - {x}) = 0".ToEntity(); |
| 136 | + var answer = Assert.IsType<FiniteSet>(equation.Solve("x")); |
| 137 | + Assert.Equal(2, answer.Count); |
| 138 | + var unscaled = "(1 + i) + 2 * x - 3 * x^2 + x * abs(3 - x)".ToEntity(); |
| 139 | + foreach (var root in answer) |
| 140 | + { |
| 141 | + var residual = Assert.IsAssignableFrom<Number.Complex>(unscaled.Substitute("x", root * MathS.Pow(10, scale)).EvalNumerical()); |
| 142 | + Assert.True(residual.Abs() < 1e-30, $"{root} leaves {residual}"); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments