Skip to content
Merged
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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question>();
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);
}
}

/// <summary>
/// An equation <see cref="SolveInverting"/> is asked: the residual, as it reaches it, the
/// variable, whether it is compensating, and the domain the roots are read over.
/// </summary>
private readonly record struct Question(Entity Expr, Variable X, bool Compensating, Core.Domain Codomain);

/// <summary>The questions this thread's solve is inside, outermost first.</summary>
[ThreadStatic] private static List<Question>? questionsBeingAsked;

private static Set? SolveInvertingAsked(Entity expr, Variable x, bool compensateSolving)
{
if (expr == x)
return new Entity[] { 0 }.ToSet();

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions Sources/Tests/UnitTests/Common/SolverRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,10 @@
var unknowns = variables.Split(',').Select(name => MathS.Var(name)).ToArray();
var solutions = MathS.SolveSystem(statement, unknowns);
Assert.NotNull(solutions);
Assert.Equal(rows, solutions.RowCount);

Check warning on line 517 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 517 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 517 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
var equations = AngouriMath.Core.EquationSystem.EquationsOf(statement, out _);
Assert.NotNull(equations);
Assert.Equal(MathS.Equations(equations).Solve(unknowns), solutions);

Check warning on line 520 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Possible null reference argument for parameter 'equations' in 'EquationSystem MathS.Equations(IEnumerable<Entity> equations)'.

Check warning on line 520 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Possible null reference argument for parameter 'equations' in 'EquationSystem MathS.Equations(IEnumerable<Entity> equations)'.

Check warning on line 520 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Possible null reference argument for parameter 'equations' in 'EquationSystem MathS.Equations(IEnumerable<Entity> equations)'.
for (var row = 0; row < solutions.RowCount; row++)
foreach (var equation in equations)
{
Expand All @@ -534,7 +534,7 @@
{
var solutions = MathS.SolveSystem("x + y = 3 and x - y = 1", "x", "y");
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);

Check warning on line 537 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 537 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 537 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
Assert.True(solutions[0, 0] == 2 && solutions[0, 1] == 1, solutions.ToString());
}

Expand Down Expand Up @@ -703,6 +703,22 @@
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<Entity.Set.FiniteSet>(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.
Expand Down
Loading