From b0ae78bbcad1ff3cc877c318bf2964e7fdeca606 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 5 Dec 2026 23:18:52 +0000 Subject: [PATCH] Skip simplifying an expansion that cannot win After its loop, Simplify expands the best form so far and simplifies the expansion as one more candidate. A power of a sum holding a quotient multiplies out into hundreds of quotients: 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 a letter in its place stays one atom and rates 708. The nested simplification of that expansion took 10 s of the 10.1 s call, nearly all of it in the common-denominator pass, which gathers the sum a pair of fractions at a time and simplifies each growing numerator in full -- and the candidate then lost. The same happens one level down, inside the nested runs, which is most of the double pendulum's time. The expansion is now simplified only where it rates at most eight times the best candidate so far. Measured over the unit suite, CasBench, SimpSweep and CanonCheck with every expansion simplified regardless, about 15,000 expansions won and none rated more than 3.6 times the best before it was simplified; the 45 above eight times all lost. No answer changes: CanonCheck, SimpSweep, RuleCheck, PropCheck and CasBench give identical findings and answers before and after. Fixes #2722 Co-Authored-By: Claude Opus 5.5 --- .../Functions/Simplification/Simplificator.cs | 26 ++++++++- ...pansionThatCannotWinIsNotSimplifiedTest.cs | 55 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Sources/Tests/UnitTests/Common/AnExpansionThatCannotWinIsNotSimplifiedTest.cs 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}"); + } + } +}