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

/// <summary>
/// An inequality with <c>x</c> 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; <see langword="null"/> for any other.
/// </summary>
/// <remarks>
/// <c>F(u) op F(v)</c> is <c>u op v</c> where both are defined, the comparison reversed for a base below 1:
/// <c>ln(u) &lt; ln(v)</c> is <c>u &gt; 0</c>, <c>v &gt; 0</c> and <c>u &lt; v</c>. A square root against
/// <c>w</c> is squared where both sides are nonnegative: <c>sqrt(u) &lt; w</c> is <c>u &gt;= 0</c>,
/// <c>w &gt; 0</c> and <c>u &lt; w^2</c>, and <c>sqrt(u) &gt; w</c> holds where <c>u &gt;= 0</c> and <c>w &lt; 0</c>, or
/// <c>w &gt;= 0</c> and <c>u &gt; w^2</c>. <c>sqrt(x + 5) &lt;= x</c> and <c>exp(x) &gt; exp(3x - 1)</c> were refused.
/// https://github.com/asc-community/AngouriMath/issues/2660
/// </remarks>
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;
}

/// <summary><c>sqrt(u) op w</c>: above it, or below it, strictly or not.</summary>
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);
}

/// <summary>The solution set, or <see langword="null"/> where a part of the statement is declined.</summary>
internal static Set? Solve(Entity expr, Variable x)
=> expr switch
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An inequality with <c>x</c> on both sides of a logarithm, an exponential or a square root threw
/// <c>NotSufficientlySupportedException</c>: <c>sqrt(x + 5) &lt;= x</c> and <c>exp(x) &gt; exp(3x - 1)</c> 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
/// </summary>
[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());

/// <summary>
/// Checked at points on both sides of each endpoint against the inequality evaluated there, rather than by how
/// the answer prints.
/// </summary>
[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}");
}
}
}
}
Loading