diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c36135e10..8b0fc1909 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -333,6 +333,21 @@ roots, scaled. 13 of the old ones lacked roots, and 3 were the same roots writte | `1 + (-2) (10^30 x) - 2 (10^30 x)^2 - 3 (10^30 x)^3 + 2 (10^30 x)^4 - 3 abs(3 (10^30 x))` | three roots | four: the one near `0.0893 10^-30` was missing | | `3 (x/10^30) - 2 (x/10^30)^2 + 3 (x/10^30)^3 + 2 abs(1 - 2 (x/10^30) - 3 (x/10^30)^2)` | `{ }` | `{ }` was wrong: its root near `-0.513 10^30` | +### An equation that respells itself back is declined rather than overflowing the stack + +**A crash becomes a decline.** Writing an equation in sine alone, where it has `cos(x)^2` beside `sin(x)`, and the +simplifier's spellings of what that wrote, which give `cos(x)^2` back, undid each other, and the solver asked the +same two questions inside each other until the stack overflowed and took the process down. `sin(3x)` is spelled +`3 sin(x) cos(x)^2 - sin(x)^3` among them, so `e^(-x) = sin(3x)` went that way. A question already being asked +further out is not asked again inside itself. Both equations have infinitely many real roots, one near each +multiple of `pi/3` for large x, so no finite set answers them and they are left unsolved +([#2680](https://github.com/asc-community/AngouriMath/issues/2680)). + +| Input | Was (2.10.0) | Now | +|---|---|---| +| `"e^(-x) = sin(3x)".Solve("x")` | stack overflow after about 2.5 min | `{ x : e ^ (-x) = sin(3 * x) }`, 0.6 s | +| `"e^(-x) + sin(x)^3 = 3 sin(x) cos(x)^2".Solve("x")` | did not finish in 60 s | `{ x : e ^ (-x) + sin(x) ^ 3 = 3 * sin(x) * cos(x) ^ 2 }`, 0.8 s | + ### 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/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs index e4ad795e3..5d4fdb265 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs @@ -104,6 +104,43 @@ internal static Set Solve(Entity expr, Variable x, bool compensateSolving = fals private static Set? SolveInverting(Entity expr, Variable x, bool compensateSolving) { if (!compensateSolving) expr = expr.InnerSimplified; // don't simplify away the 0 on the right hand side of the subtraction + + // A question this thread is already in the middle of asking is not asked again inside + // itself: the answer to it is whatever the call asking it first settles, and asking it + // again only asks it again. Two spellings that undo each other made that a cycle -- + // TryRewriteInOneFunction writes cos(x)^2 as 1 - sin(x)^2, the spellings of what it + // writes include the simplifier's cos(x)^2 again, and that spelling is handed back to + // the compensated pass, which rewrites it again. e^(-x) = sin(3x) went round it until + // the stack overflowed. Left unsolved here, at the inner asking, so the outer one goes + // on to the routes after the one that cycled. + // https://github.com/asc-community/AngouriMath/issues/2680 + var question = new Question(expr, x, compensateSolving, MathS.Settings.Codomain.Value); + var asked = questionsBeingAsked ??= new List(); + foreach (var earlier in asked) + if (earlier.Equals(question)) + return null; + asked.Add(question); + try + { + return SolveInvertingAsked(expr, x, compensateSolving); + } + finally + { + asked.RemoveAt(asked.Count - 1); + } + } + + /// + /// An equation is asked: the residual, as it reaches it, the + /// variable, whether it is compensating, and the domain the roots are read over. + /// + private readonly record struct Question(Entity Expr, Variable X, bool Compensating, Core.Domain Codomain); + + /// The questions this thread's solve is inside, outermost first. + [ThreadStatic] private static List? questionsBeingAsked; + + private static Set? SolveInvertingAsked(Entity expr, Variable x, bool compensateSolving) + { if (expr == x) return new Entity[] { 0 }.ToSet(); @@ -429,8 +466,11 @@ bool ProductIsDefinedAt(Entity root) // machinery, which is the only thing that can solve what the rewrite produces. // Passed `true`, this fired, produced `1 - sin(x)^2 + sin(x)` and then answered // it through the exponential solver anyway, which is the route it exists to - // avoid. It cannot recurse: what comes back mentions only one of the two - // functions, so the rewrite declines it. + // avoid. What comes back mentions only one of the two functions, but the + // simplifier's spellings of it can give the other back -- 1 - sin(x)^2 is cos(x)^2 + // -- and that spelling reaches this rewrite again: the same question, which + // SolveInverting does not ask again inside itself. + // https://github.com/asc-community/AngouriMath/issues/2680 if (TrigonometricSolver.TryRewriteInOneFunction(expr, x, out var inOneFunction) && Solve(inOneFunction, x) is FiniteSet { IsSetEmpty: false } elsPythagorean) return (Set)elsPythagorean.Select(ent => TryDowncast(expr, x, ent)).ToSet().InnerSimplified; diff --git a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs index 066555a37..196742623 100644 --- a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs @@ -703,6 +703,22 @@ private static void AssertTheRealRootsInTheBox(System.Collections.Generic.IEnume Assert.True(System.Math.Abs(value - root) <= 1e-12 * System.Math.Max(1, System.Math.Abs(value)), $"{root} for {value}"); } + // https://github.com/asc-community/AngouriMath/issues/2680 + // e^(-x) = sin(3x) overflowed the stack after about two minutes. The spellings of the equation + // include sin(3x) expanded to 3 sin(x) cos(x)^2 - sin(x)^3, which the compensated pass rewrote in + // sine alone; the spellings of that included cos(x)^2 again, and the same two questions were asked + // inside each other until the stack ran out. Each has infinitely many real roots, one near every + // multiple of pi/3 for large x, so no finite set answers it: a decline is honest, a finite set is not. + [Theory] + [InlineData("e^(-x) = sin(3x)")] + [InlineData("e^(-x) + sin(x)^3 = 3 sin(x) cos(x)^2")] + public void Issue2680_AQuestionIsNotAskedAgainInsideItself(string equation) + { + var task = System.Threading.Tasks.Task.Run(() => equation.Solve("x")); + Assert.True(task.Wait(System.TimeSpan.FromSeconds(60)), $"{equation} did not finish within 60 s"); + Assert.IsNotType(task.Result); + } + private static readonly PeterO.Numbers.EContext Unfloored = new(100, PeterO.Numbers.ERounding.HalfUp, -100000, 100000, false); // Real parts, evaluated without the exponent floor, of roots each checked to be real.