From 47e47ece90d1547d6e5c36c36bbdc5ed6e97b766 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 11:26:06 +0000 Subject: [PATCH] Solve an inequality with x on both sides of ln, exp or sqrt (#2660) Solve threw NotSufficientlySupportedException for an inequality with x on both sides of a logarithm, an exponential or a square root. MonotoneInverted (#2586) read such a function only against a constant. F(u) op F(v), for the same strictly increasing F on both sides, is u op v where both are defined: ln(u) < ln(v) is u > 0, v > 0 and u < v. A base below 1 reverses the comparison. A square root against an expression without one is squared where both sides are nonnegative. sqrt(u) < w is u >= 0, w > 0 and u < w^2. sqrt(u) > w holds where u >= 0 and w < 0, or w >= 0 and u > w^2. Each is then solved as those polynomial or monotone inequalities. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 16 +++++ .../Solvers/EquationSolver/SolveStatement.cs | 55 +++++++++++++++- ...WithXOnBothSidesOfAMonotoneFunctionTest.cs | 65 +++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/AnInequalityWithXOnBothSidesOfAMonotoneFunctionTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 5abee0b6d..0c0d4a9fd 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -484,6 +484,22 @@ and the two share a factor free of variables. The value is the same | `"ln(2)^2 / (x * ln(2))".Simplify()` | `ln(2) ^ 2 / (x * ln(2))` | `ln(2) / x` | | `"ln(2) * x / ln(2)^2".Simplify()` | `x * ln(2) / ln(2) ^ 2` | `x / ln(2)` | +### An inequality with x on both sides of ln, exp or sqrt is solved + +**Not silent: an inequality that threw has an answer.** `Solve` threw `NotSufficientlySupportedException` for an +inequality with `x` on both sides of a logarithm, an exponential or a square root. The same increasing function on +both sides now compares its arguments where they are defined, with the comparison reversed for a base below 1. A +square root against an expression is squared where both sides are nonnegative: `sqrt(u) < w` is `u >= 0`, `w > 0` and +`u < w^2` ([#2660](https://github.com/asc-community/AngouriMath/issues/2660)). + +| `.Solve("x")` | Was (2.10.0) | Now | +|---|---|---| +| `sqrt(x + 5) <= x` | throws | `[(1 + sqrt(21))/2; +oo)` | +| `sqrt(4 - x) <= x - 1` | throws | `[(1 + sqrt(13))/2; 4]` | +| `sqrt(x + 3) > x` | throws | `[-3; 0) \/ [0; (sqrt(13) + 1) / 2)` | +| `exp(x) > exp(3x - 1)` | throws | `(-oo; 1/2)` | +| `ln(x) < ln(4 - x)` | throws | `(0; 2)` | + ### An inequality through ln, an exponential or sqrt is solved **Not silent: an inequality that threw has an answer.** `Solve` threw `NotSufficientlySupportedException` for an diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 39609de86..507e1df5a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -986,7 +986,7 @@ private static Entity Comparison(Entity u, bool above, bool strict, Entity bound if (!left.ContainsNode(x)) (left, right, above) = (right, left, !above); if (RealConstant(right, x) is not { } bound) - return null; + return BothSides(left, right, above, strict, x); var positive = bound.EDecimal.Sign > 0; switch (left) { @@ -1009,6 +1009,59 @@ private static Entity Comparison(Entity u, bool above, bool strict, Entity bound static bool IsAboveOne(Real b) => b.EDecimal.CompareTo(EDecimal.One) > 0; } + /// + /// An inequality with x on both sides, through the same strictly increasing function of each side -- a + /// logarithm or an exponential to one constant base, or a square root -- or a square root against an expression + /// without one, solved as the inequalities it is equivalent to; for any other. + /// + /// + /// F(u) op F(v) is u op v where both are defined, the comparison reversed for a base below 1: + /// ln(u) < ln(v) is u > 0, v > 0 and u < v. A square root against + /// w is squared where both sides are nonnegative: sqrt(u) < w is u >= 0, + /// w > 0 and u < w^2, and sqrt(u) > w holds where u >= 0 and w < 0, or + /// w >= 0 and u > w^2. sqrt(x + 5) <= x and exp(x) > exp(3x - 1) were refused. + /// https://github.com/asc-community/AngouriMath/issues/2660 + /// + private static Set? BothSides(Entity left, Entity right, bool above, bool strict, Variable x) + { + switch (left, right) + { + case (Logf(var leftBase, var u), Logf(var rightBase, var v)) + when leftBase == rightBase && RealConstant(leftBase, x) is { } b && IsAPositiveBaseOtherThanOne(b) + && IsRealAtRealX(u, x) && IsRealAtRealX(v, x): + return Solve(u > 0 & v > 0 & Comparison(u, above == IsAboveOne(b), strict, v), x); + case (Powf(var leftBase, var u), Powf(var rightBase, var v)) + when leftBase == rightBase && RealConstant(leftBase, x) is { } b && IsAPositiveBaseOtherThanOne(b) + && IsRealAtRealX(u, x) && IsRealAtRealX(v, x) && !MayHaveAPole(u) && !MayHaveAPole(v): + return Solve(Comparison(u, above == IsAboveOne(b), strict, v), x); + case (Powf(var u, Rational { ERational: var p }), Powf(var v, Rational { ERational: var q })) + when IsASquareRoot(p) && IsASquareRoot(q) && IsRealAtRealX(u, x) && IsRealAtRealX(v, x): + return Solve(u >= 0 & v >= 0 & Comparison(u, above, strict, v), x); + case (Powf(var u, Rational { ERational: var p }), var w) + when IsASquareRoot(p) && IsRealAtRealX(u, x) && IsRealAtRealX(w, x) && !HoldsARoot(w): + return SquareRootAgainst(u, w, above, strict, x); + case (var w, Powf(var u, Rational { ERational: var p })) + when IsASquareRoot(p) && IsRealAtRealX(u, x) && IsRealAtRealX(w, x) && !HoldsARoot(w): + return SquareRootAgainst(u, w, !above, strict, x); + default: + return null; + } + + static bool IsASquareRoot(ERational power) => power.Numerator.Equals(EInteger.One) && power.Denominator.Equals(EInteger.FromInt32(2)); + static bool HoldsARoot(Entity w) => w.Nodes.Any(node => node is Powf(_, Rational and not Integer)); + static bool IsAPositiveBaseOtherThanOne(Real b) => b.EDecimal.Sign > 0 && b.EDecimal.CompareTo(EDecimal.One) != 0; + static bool IsAboveOne(Real b) => b.EDecimal.CompareTo(EDecimal.One) > 0; + } + + /// sqrt(u) op w: above it, or below it, strictly or not. + private static Set? SquareRootAgainst(Entity u, Entity w, bool above, bool strict, Variable x) + { + var squared = MathS.Pow(w, 2); + return above + ? Solve(u >= 0 & w < 0 | w >= 0 & Comparison(u, true, strict, squared), x) + : Solve(u >= 0 & Comparison(w, true, strict, Integer.Zero) & Comparison(u, false, strict, squared), x); + } + /// The solution set, or where a part of the statement is declined. internal static Set? Solve(Entity expr, Variable x) => expr switch diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/AnInequalityWithXOnBothSidesOfAMonotoneFunctionTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/AnInequalityWithXOnBothSidesOfAMonotoneFunctionTest.cs new file mode 100644 index 000000000..bb6c4fa0c --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/AnInequalityWithXOnBothSidesOfAMonotoneFunctionTest.cs @@ -0,0 +1,65 @@ +// +// 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.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// An inequality with x on both sides of a logarithm, an exponential or a square root threw + /// NotSufficientlySupportedException: sqrt(x + 5) <= x and exp(x) > exp(3x - 1) had no answer. + /// The same increasing function on both sides compares its arguments, where they are defined, and a square root + /// against an expression is squared where both sides are nonnegative. + /// https://github.com/asc-community/AngouriMath/issues/2660 + /// + [Trait("Area", "Algebra")] + public sealed class AnInequalityWithXOnBothSidesOfAMonotoneFunctionTest + { + [Theory] + [InlineData("sqrt(x + 5) <= x", "[(1 + sqrt(21))/2; +oo)")] + [InlineData("sqrt(4 - x) <= x - 1", "[(1 + sqrt(13))/2; 4]")] + [InlineData("exp(x) > exp(3x - 1)", "(-oo; 1/2)")] + [InlineData("ln(x) < ln(4 - x)", "(0; 2)")] + [InlineData("sqrt(x) < sqrt(2 - x)", "[0; 1)")] + public void TheAnswerIsTheSetItHoldsOn(string inequality, string expected) + => Assert.Equal(expected.ToEntity().Simplify(), inequality.ToEntity().Solve("x").Simplify()); + + /// + /// Checked at points on both sides of each endpoint against the inequality evaluated there, rather than by how + /// the answer prints. + /// + [Theory] + [InlineData("sqrt(x + 5) <= x")] + [InlineData("sqrt(x + 5) >= x")] + [InlineData("sqrt(2 - x) < x")] + [InlineData("sqrt(2 - x) > x")] + [InlineData("x - 1 >= sqrt(x + 1)")] + [InlineData("exp(2x) <= exp(x + 1)")] + [InlineData("(1/2)^x < (1/2)^(3 - x)")] + [InlineData("ln(2x + 1) > ln(x + 3)")] + // Answered as two adjacent intervals, [-3; 0) and [0; (1 + sqrt(13))/2), and checked as a set. + [InlineData("sqrt(x + 3) > x")] + [InlineData("x < sqrt(x + 3)")] + public void TheAnswerHoldsExactlyWhereTheInequalityDoes(string inequality) + { + Variable x = "x"; + var solved = (Set)inequality.ToEntity().Solve(x).Simplify(); + foreach (var point in new Entity[] { -7, -5, -3, "-5/2".ToEntity(), -2, "-3/2".ToEntity(), -1, "-1/2".ToEntity(), 0, + "1/4".ToEntity(), "1/2".ToEntity(), 1, "3/2".ToEntity(), 2, "5/2".ToEntity(), + 3, "7/2".ToEntity(), 4, 5, 7, 11 }) + { + var holds = inequality.ToEntity().Substitute(x, point).Simplify(); + if (holds is not Boolean truth) + continue; // no value there: neither in nor out says anything + Assert.True(solved.Contains(point) == (bool)truth, + $"{inequality} solved to {solved}, which disagrees with it at x = {point}"); + } + } + } +}