diff --git a/Sources/AngouriMath/Functions/Simplification/Simplificator.cs b/Sources/AngouriMath/Functions/Simplification/Simplificator.cs index f21dd7618..4b957445c 100644 --- a/Sources/AngouriMath/Functions/Simplification/Simplificator.cs +++ b/Sources/AngouriMath/Functions/Simplification/Simplificator.cs @@ -36,6 +36,26 @@ internal static class Simplificator /// private static int CandidateLevel(int level) => Math.Min(Math.Abs(level), 2); + /// + /// How many times the best candidate's rate an expansion may rate and still be simplified + /// as a candidate after the loop. + /// + /// + /// Simplifying an expansion is a nested search over it, and a long sum of quotients is + /// the costliest thing that search meets: the common-denominator pass gathers it a pair of + /// fractions at a time, simplifying each growing numerator in full. A letter stays one + /// atom when a power of a sum is multiplied out, a quotient does not: + /// tanh(-2) is (1/e^4 - 1)/(1/e^4 + 1), and ((x + tanh(-2) - 2y)^2)^3 + /// expanded rates 12488 against the input's 61, where ((x + c - 2y)^2)^3 rates 708. + /// Simplifying it took 10 s of the 10.1 s the call took, 98% of that in the + /// common-denominator pass, and it lost. + /// Over the unit suite, CasBench, SimpSweep and CanonCheck, 15,000 expansions won and + /// none of them rated more than 3.6 times the best candidate before it was simplified; the + /// 45 that rated above eight times it all lost, and cost 26 s of the suite's 737 s of + /// expansions. https://github.com/asc-community/AngouriMath/issues/2722 + /// + private const int MostAnExpansionOutgrowsTheBest = 8; + internal static Entity PickSimplest(Entity one, Entity another) => one.SimplifiedRate < another.SimplifiedRate ? one : another; @@ -589,7 +609,11 @@ not solve this issue completely and yet too slow to be accepted. if (level > 0) // if level < 0 we don't check whether expanded version is better { var expandMark = recording?.Mark() ?? 0; - AddHistory(Noted(recording, res, Expanded(res), nameof(Entity.Expand), expandMark).Simplify(-CandidateLevel(level))); + // Simplified only where it might win: see MostAnExpansionOutgrowsTheBest. + // https://github.com/asc-community/AngouriMath/issues/2722 + var expansion = Expanded(res); + if (expansion.SimplifiedRate <= MostAnExpansionOutgrowsTheBest * history.Keys.Min()) + AddHistory(Noted(recording, res, expansion, nameof(Entity.Expand), expandMark).Simplify(-CandidateLevel(level))); var factorizeMark = recording?.Mark() ?? 0; // The **rule-based** factorisation, not `Entity.Factorize` -- which now also asks // the polynomial layer, and whose answers must not become candidates here. The diff --git a/Sources/Tests/UnitTests/Common/AnExpansionThatCannotWinIsNotSimplifiedTest.cs b/Sources/Tests/UnitTests/Common/AnExpansionThatCannotWinIsNotSimplifiedTest.cs new file mode 100644 index 000000000..af23cdc3c --- /dev/null +++ b/Sources/Tests/UnitTests/Common/AnExpansionThatCannotWinIsNotSimplifiedTest.cs @@ -0,0 +1,55 @@ +// +// 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; +using System.Threading.Tasks; +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Common +{ + /// + /// A power of a sum holding a quotient multiplies out into a sum of hundreds of quotients, and + /// Simplify simplified that expansion as a candidate, gathering it a pair of fractions at + /// a time, before ranking it last: ((x + tanh(-2) - 2y)^2)^3 took 10 s and came back as it + /// went in. A letter in place of tanh(-2) stays one atom and took 0.01 s. + /// https://github.com/asc-community/AngouriMath/issues/2722 + /// + [Trait("Area", "Common")] + public sealed class AnExpansionThatCannotWinIsNotSimplifiedTest + { + /// + /// A backstop, not a measurement: these take under 0.6 s on a loaded desktop, and 14 s to + /// more than 20 s before the fix. + /// + private static readonly TimeSpan Budget = TimeSpan.FromSeconds(15); + + [Theory] + [InlineData("((x + tanh(-2) - 2 * y)^2)^3")] + [InlineData("(((x + tanh(-2) - 2 * y)^2)^2)^2")] + [InlineData("((x + tanh(-2) - 2 * y + z)^2)^3")] + [InlineData("((x + (b - 1) / (b + 1) - 2 * y)^2)^3")] + public void ItFinishesWithTheSameValue(string input) + { + var expression = input.ToEntity(); + var task = Task.Run(() => expression.Simplify()); + Assert.True(task.Wait(Budget), $"{input} did not simplify within {Budget.TotalSeconds} s"); + var at = new (string Name, double Value)[] { ("x", 0.7), ("y", -0.3), ("z", 1.1), ("b", 2.5) }; + Entity before = expression, after = task.Result; + foreach (var (name, value) in at) + { + before = before.Substitute(name, value); + after = after.Substitute(name, value); + } + var expected = before.EvalNumerical().RealPart.EDecimal.ToDouble(); + var actual = after.EvalNumerical().RealPart.EDecimal.ToDouble(); + Assert.True(Math.Abs(expected - actual) <= 1e-9 * Math.Max(1, Math.Abs(expected)), + $"{input} became {after.Stringize()}: {actual} where it was {expected}"); + } + } +}