From d91296aa84dfe2b19118c7687cc7675487f4b432 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 27 Oct 2026 15:34:37 +0000 Subject: [PATCH] Simplify does not add a condition the input does not have PolynomialLongDivision divides by the divisor's leading coefficient. Its first attempt, on the pair as written, declines a symbolic one for the simplifier, since a quotient that loses a value of a parameter is not an equivalence. The second attempt, with the variables replaced by temporaries, had no such guard: it predates it, and #1269 left it as it was. (t x + y)/(1 - y x) divided there in y is -1/x + (t x + 1/x)/(1 - x y), undefined at x = 0, where the divisor is 1, and Simplify answered -(t x + y)/(1 - y x) + (t x + y)/(1 - y x) with 0 provided not 1 - x y = 0 and not x = 0. The input is -y + y at x = 0. The replaced attempt now declines too where the coefficient is symbolic and the divisor has more than one term in the variable. A divisor of one term, a x^k, is zero wherever its coefficient is, so dividing by it loses nothing the input had, and is still done. Guarding that case as well changed the limit of (tan(a x) - sin(b x))/(a x) at 0 from (a - b)/a to 1 - b/a, because Simplify does not put 1 - b/a over one denominator (#2137), so it is not guarded. On 2.6.0 and on this branch: -(t*x + y)/(1 - y*x) + (t*x + y)/(1 - y*x) 0 provided not 1 - x * y = 0 and not x = 0 -> 0 provided not 1 - x * y = 0 Unit suite 17,130 passed, 0 failed. SimpSweep 10,463/10,463 agree; CanonCheck matches its baseline; BoundCheck 0 disagreements; CasBench 120/121, unchanged. Part of https://github.com/asc-community/AngouriMath/issues/2134 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 13 +++++++ .../Functions/TreeAnalyzer/LongDivision.cs | 11 ++++++ .../SimplifiedConditionNotStricterTest.cs | 35 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 Sources/Tests/UnitTests/Common/SimplifiedConditionNotStricterTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 1a1ab7b56..d4430ed95 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -75,6 +75,19 @@ mentions answers it as a parameter, as a short system does, where it was `null` | `MathS.Equations("x - 1", "x^2 - 1").Solve("x", "y")` | `null` | `[[1, t_1]]` | | `MathS.Equations("a - 1", "b * 0").Solve("a", "b")` | `null` | `[[1, t_1]]` | +### `Simplify` does not add a condition the input does not have + +**Silent: an answer defined at fewer points is defined at all of the input's.** A polynomial division in a +variable divided by the divisor's leading coefficient, and with the variables replaced it did so even where that +coefficient is a symbol and the divisor has another term. `(t x + y)/(1 - y x)` divided in `y` has `1/x` in it, +and `Simplify` passed that on as `not x = 0`, though at `x = 0` the input is `-y + y`. A divisor of one term, +`a x^k`, is still divided, since the input is undefined wherever its coefficient is zero +([#2134](https://github.com/asc-community/AngouriMath/issues/2134)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"-(t*x + y)/(1 - y*x) + (t*x + y)/(1 - y*x)".Simplify()` | `0 provided not 1 - x * y = 0 and not x = 0` | `0 provided not 1 - x * y = 0` | + ### A matrix is divided, raised to a power and compared as a matrix **Silent: wrong answers about matrices become right, or stay written.** Where an operation had no answer of its own diff --git a/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs b/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs index 92b61c348..1e9b1d6b0 100644 --- a/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs +++ b/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs @@ -213,6 +213,17 @@ private static (Entity Divided, Entity Remainder)? DivideOnePolynomial( // https://github.com/asc-community/AngouriMath/issues/180 if (!replaceVars && ((maxvalQ.Vars.Any() && !genericCase) || maxvalQ.Evaled == Integer.Create(0))) return null; + // **And with the variables replaced, where the divisor has more than one term.** That + // pass divides by a leading coefficient as much as this one does, and it was left + // unguarded only because it predates the guard. `(t x + y)/(1 - y x)` divided in `y` + // is `-1/x + (t x + 1/x)/(1 - x y)`, undefined at `x = 0`, where the divisor is 1; and + // `Simplify` answered `-(t x + y)/(1 - y x) + (t x + y)/(1 - y x)` with + // `0 provided not x = 0`, a condition the input does not have. A divisor of one term, + // `a x^k`, is zero wherever its coefficient is, so dividing by that coefficient loses + // nothing the input had: `(a x - b x)/(a x)` is `(a - b)/a`. + // https://github.com/asc-community/AngouriMath/issues/2134 + if (replaceVars && maxvalQ.Vars.Any() && !genericCase && powersOfQ.Count > 1) + return null; // A leading coefficient with a symbol in it can be zero as a value without being zero // as written: `(-(a - a u^2) - (u^2 + 1) a)^3` is `-8 a^3`, and expanded it leaves diff --git a/Sources/Tests/UnitTests/Common/SimplifiedConditionNotStricterTest.cs b/Sources/Tests/UnitTests/Common/SimplifiedConditionNotStricterTest.cs new file mode 100644 index 000000000..502e03bf4 --- /dev/null +++ b/Sources/Tests/UnitTests/Common/SimplifiedConditionNotStricterTest.cs @@ -0,0 +1,35 @@ +// +// 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 AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Common +{ + /// + /// Simplify answered -(t x + y)/(1 - y x) + (t x + y)/(1 - y x) with + /// 0 provided not 1 - x y = 0 and not x = 0. At x = 0 the input is + /// -y + y, which is defined, and the answer was not: a polynomial division in + /// y had divided by the divisor's leading coefficient -x. + /// #2134 + /// + [Trait("Area", "Common")] + public sealed class SimplifiedConditionNotStricterTest + { + [Theory] + [InlineData("-(t*x + y)/(1 - y*x) + (t*x + y)/(1 - y*x)")] + [InlineData("(t*x + y)/(1 - y*x) - (t*x + y)/(1 - y*x)")] + public void TheAnswerIsDefinedWhereTheInputIs(string input) + { + var simplified = input.Simplify() + .Substitute("x", 0).Substitute("y", 2).Substitute("t", 3); + Assert.Equal(Integer.Zero, simplified.Evaled); + } + } +}