|
| 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; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using AngouriMath; |
| 12 | +using AngouriMath.Extensions; |
| 13 | +using Xunit; |
| 14 | +using NumericsComplex = System.Numerics.Complex; |
| 15 | + |
| 16 | +namespace AngouriMath.Tests.Common |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// A power of x to an exponent that is a number but not a real one was substituted by x itself, |
| 20 | + /// which renamed the variable and asked the same equation again until the stack overflowed: |
| 21 | + /// <c>(-2)^x + 4^x = 0</c> reaches that as <c>t^ln(-2) + t^ln(4)</c>. |
| 22 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2424">#2424</a> |
| 23 | + /// </summary> |
| 24 | + [Trait("Area", "Common")] |
| 25 | + public sealed class ANegativeBaseExponentialSolveTerminatesTest |
| 26 | + { |
| 27 | + private static readonly TimeSpan Budget = LimitTermination.Guard; |
| 28 | + |
| 29 | + private static Entity.Set Solved(string equation) |
| 30 | + { |
| 31 | + var task = Task.Run(() => equation.ToEntity().Solve("x")); |
| 32 | + Assert.True(task.Wait(Budget), $"{equation} did not finish within {Budget.TotalSeconds}s"); |
| 33 | + return task.Result; |
| 34 | + } |
| 35 | + |
| 36 | + private static NumericsComplex Value(Entity root) |
| 37 | + => Assert.IsAssignableFrom<Entity.Number.Complex>(root.EvalNumerical()).ToNumerics(); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// <c>a^x + s b^x = 0</c> with <c>s = 1</c> or <c>-1</c>. With principal powers it is |
| 41 | + /// <c>e^(x (ln a - ln b)) = -s</c>, so the roots are |
| 42 | + /// <c>x = (ln(-s) + 2 pi i k) / (ln a - ln b)</c> for every whole <c>k</c>: |
| 43 | + /// <c>(-2)^x + 4^x = 0</c> has <c>x = (2k + 1) pi i / (pi i - ln 2)</c>, the first of which is |
| 44 | + /// <c>0.95358 - 0.21039i</c>, and <c>(-2)^x + 2^x = 0</c> has the odd integers. Each root |
| 45 | + /// the solver answers is to be one of them; declining is honest, an empty set is not. |
| 46 | + /// </summary> |
| 47 | + [Theory] |
| 48 | + [InlineData("(-2)^x + 4^x = 0", -2, 4, 1)] |
| 49 | + [InlineData("(-3)^x + 9^x = 0", -3, 9, 1)] |
| 50 | + [InlineData("(-2)^x - 4^x = 0", -2, 4, -1)] |
| 51 | + [InlineData("(-2)^x + 2^x = 0", -2, 2, 1)] |
| 52 | + public void TheRootsAreOnTheFamily(string equation, int a, int b, int sign) |
| 53 | + { |
| 54 | + var solved = Solved(equation); |
| 55 | + if (solved is not Entity.Set.FiniteSet roots) |
| 56 | + return; |
| 57 | + Assert.NotEmpty(roots); |
| 58 | + var rate = NumericsComplex.Log(a) - NumericsComplex.Log(b); |
| 59 | + var shift = NumericsComplex.Log(-sign); |
| 60 | + foreach (var root in roots) |
| 61 | + { |
| 62 | + var x = Value(root); |
| 63 | + var left = NumericsComplex.Exp(x * NumericsComplex.Log(a)); |
| 64 | + var right = sign * NumericsComplex.Exp(x * NumericsComplex.Log(b)); |
| 65 | + Assert.True(NumericsComplex.Abs(left + right) <= 1e-9 * (NumericsComplex.Abs(left) + NumericsComplex.Abs(right)), |
| 66 | + $"{root} leaves {left + right} in {equation}"); |
| 67 | + var k = (x * rate - shift) / (2 * Math.PI * NumericsComplex.ImaginaryOne); |
| 68 | + Assert.True(Math.Abs(k.Real - Math.Round(k.Real)) < 1e-9 && Math.Abs(k.Imaginary) < 1e-9, |
| 69 | + $"{root} is not on the family of {equation}: k = {k}"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// The same substitution without a negative base. <c>x^(1 + i) + x</c> is |
| 75 | + /// <c>x (x^i + 1)</c>, whose roots are 0, where <c>0^(1 + i)</c> is 0, and <c>e^((2k + 1) pi)</c>. |
| 76 | + /// <c>x^i + x</c> and <c>x^i + x^(2i)</c> have no root at 0, where <c>0^i</c> is undefined, |
| 77 | + /// though the function Newton's method compiles is 0 there. |
| 78 | + /// </summary> |
| 79 | + [Theory] |
| 80 | + [InlineData("x^(1 + i) + x")] |
| 81 | + [InlineData("x^i + x")] |
| 82 | + [InlineData("x^i + x^(2*i)")] |
| 83 | + public void AComplexExponentOfXTerminates(string expression) |
| 84 | + { |
| 85 | + if (Solved(expression + " = 0") is not Entity.Set.FiniteSet roots) |
| 86 | + return; |
| 87 | + Assert.NotEmpty(roots); |
| 88 | + foreach (var root in roots) |
| 89 | + { |
| 90 | + var residual = expression.ToEntity().Substitute("x", root).EvalNumerical(); |
| 91 | + var value = Assert.IsAssignableFrom<Entity.Number.Complex>(residual); |
| 92 | + Assert.True(value.IsFinite && NumericsComplex.Abs(value.ToNumerics()) <= 1e-9 * Math.Max(1, NumericsComplex.Abs(Value(root))), |
| 93 | + $"{root} leaves {residual} in {expression} = 0"); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments