diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index e4ccd9325..888cc83ef 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -517,6 +517,30 @@ master (409d68805) and on this branch | `cos(x^(1/3)) = 1` | `{ }` | `union({ (2 * pi * n) ^ 3 provided 2 * pi * n = 0 or -1/3 * pi < arg(2 * pi * n) and arg(2 * pi * n) <= 1/3 * pi or arg(2 * pi * n) = pi }, n in ZZ)` | | `cos(x^(2/3)) = 1` | `union({ -(2 * pi * n) ^ (3/2) provided 2 * pi * n > 0 }, n in ZZ)` | `union({ (2 * pi * n) ^ (3/2) provided 2 * pi * n = 0 or -2/3 * pi < arg(2 * pi * n) and arg(2 * pi * n) <= 2/3 * pi, -(2 * pi * n) ^ (3/2) provided 2 * pi * n > 0 }, n in ZZ)` | +### A non-strict inequality keeps its irrational ends, and leaves out the equation's complex roots + +**Silent: lost endpoints come back and complex "solutions" go.** `f <= c` is solved as `f < c` together with the +roots of `f = c`. A root and the interval's end were the same number written two ways, `(-1 - sqrt(5)) / 2` and +`-1/2 + -1/2 * sqrt(5)`, whose decimals differ in the last digit, so the open interval was taken to hold the root and +the end stayed open. Interval membership, union and intersection now compare ends by an enclosure of their +difference, and as equal only where it simplifies to 0; a comparison neither decides leaves the set as written. The +equation's roots were also kept when they were not real. Only real roots are kept now; a root whose realness is not +decided, such as `sqrt(a)`, is kept intersected with `RR`. Measured on master (2a7247653) and on this branch +([#2732](https://github.com/asc-community/AngouriMath/issues/2732), +[#2733](https://github.com/asc-community/AngouriMath/issues/2733)). + +| Expression | Was (master) | Now | +|---|---|---| +| `"(1 + sqrt(5)) / 2 in (0; 1/2 + sqrt(5) / 2)".EvalBoolean()` | `True` | `False` | +| `"sqrt(2) - 10^(-120) in (0; sqrt(2))".EvalBoolean()` | `False` | `True` | +| `"[0; (1 + sqrt(5)) / 2] /\ [1/2 + sqrt(5) / 2; 3]".Simplify()` | `{ }` | `{ (sqrt(5) + 1) / 2 }` | +| `"x^2 + x <= 1".Solve("x")` | `{ (-1 + sqrt(5)) / 2 } \/ (-1/2 + -1/2 * sqrt(5); (sqrt(5) - 1) / 2)` | `[-1/2 + -1/2 * sqrt(5); (sqrt(5) - 1) / 2]` | +| `"6*x^2 + 22*x - 9 >= 0".Solve("x")` | `(-oo; -5/6 * sqrt(7) - 11/6) \/ (5/6 * sqrt(7) - 11/6; +oo)` | `(-oo; -5/6 * sqrt(7) - 11/6] \/ [(-22 + 10 * sqrt(7)) / 12; +oo)` | +| `"x^2 + 1 <= 0".Solve("x")` | `{ i, -i }` | `{ }` | +| `"x^2 + 1 >= 0".Solve("x")` | `{ i, -i } \/ RR` | `RR` | +| `"x^3 - 2 <= 0".Solve("x")` | `{ (-1/2 + i * 1/2 * sqrt(3)) * 2 ^ (1/3), (-1/2 + i * -1/2 * sqrt(3)) * 2 ^ (1/3) } \/ (-oo; 2 ^ (1/3)]` | `(-oo; 2 ^ (1/3)]` | +| `"x^2 <= a".Solve("x")` | `{ sqrt(a), -sqrt(a) } \/ { x : -(-4) * a >= 0 and x in (-1/2 * abs(2 * sqrt(a)); abs(2 * sqrt(a)) / 2) }` | `{ x : -(-4) * a >= 0 and x in (-1/2 * abs(2 * sqrt(a)); abs(2 * sqrt(a)) / 2) } \/ { sqrt(a), -sqrt(a) } /\ RR` | + ### An odd root of a negative number is real with downcasting off as well **Silent, with `MathS.Settings.DowncastingEnabled` off only.** An odd root of a negative number is the real one: diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs index a7b847f9b..56e805241 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs @@ -247,9 +247,6 @@ public sealed partial record Interval(Entity Left, bool LeftClosed, Entity Right private Real RightReal => fRight.GetValue(static @this => @this.Right.EvaluableNumerical && @this.Right.Evaled is Real re ? re : Real.NaN, this); private Convenience.SettingsDependent fRight; - private static bool IsALessThanB(Real A, Real B, bool closed) - => A < B || closed && A == B; - internal Interval New(Entity left, Entity right) => ReferenceEquals(Left, left) && ReferenceEquals(Right, right) @@ -268,6 +265,7 @@ public override Entity Replace(Func func) public override bool TryContains(Entity entity, out bool contains) { contains = false; + var element = entity; entity = entity.Evaled; if (entity is Complex and not Real) { @@ -296,7 +294,12 @@ public override bool TryContains(Entity entity, out bool contains) return false; if (!IsNumeric) return false; - contains = IsALessThanB(LeftReal, re, LeftClosed) && IsALessThanB(re, RightReal, RightClosed); + // By value, and at an end by whether it is that number, however it is written: + // https://github.com/asc-community/AngouriMath/issues/2732 + if (SetOperators.Order(Left, LeftReal, element, re) is not { } fromLeft + || SetOperators.Order(element, re, Right, RightReal) is not { } toRight) + return false; + contains = (fromLeft < 0 || LeftClosed && fromLeft == 0) && (toRight < 0 || RightClosed && toRight == 0); return true; } diff --git a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Definition.cs b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Definition.cs index 73877c7be..2da5cef24 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Definition.cs @@ -5,6 +5,7 @@ // Website: https://am.angouri.org. // +using PeterO.Numbers; using static AngouriMath.Entity.Set; namespace AngouriMath.Core.Sets @@ -17,5 +18,53 @@ internal static (ConditionalSet one, ConditionalSet another) MergeToOneVariable var stat1 = another.Predicate.Substitute(another.Var, one.Var); return (one, (ConditionalSet)another.New(one.Var, stat1)); } + + /// + /// Negative, zero or positive as the real constant is below, at or above + /// , each given with its value; where that is not decided. + /// + /// + /// Two spellings of one number evaluate to decimals that can differ in the last digit: + /// (1 + sqrt(5)) / 2 and 1/2 + sqrt(5) / 2 do, so comparing the decimals put + /// the first strictly inside (0; 1/2 + sqrt(5) / 2), and x^2 + x <= 1 lost its + /// root. Decimals far enough apart that rounding cannot have put them there are compared as + /// they are; nearer ones by an interval enclosing the difference, and as equal only where + /// the difference simplifies to zero. Two decimals that agree are not taken for one number. + /// https://github.com/asc-community/AngouriMath/issues/2732 Reading the decimals at all goes with + /// downcasting in v3: https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + /// + internal static int? Order(Entity a, Entity.Number.Real aValue, Entity b, Entity.Number.Real bValue) + { + if (a == b) + return 0; + if (aValue.IsNaN || bValue.IsNaN) + return null; + var (x, y) = (aValue.EDecimal, bValue.EDecimal); + if (a is Entity.Number.Real && b is Entity.Number.Real || !x.IsFinite || !y.IsFinite) + return x.CompareTo(y); + var difference = x.Subtract(y); + var scale = EDecimal.Max(EDecimal.One, EDecimal.Max(x.Abs(), y.Abs())); + if (difference.Abs().CompareTo(scale.Multiply(ApartShare)) > 0) + return difference.Sign; + var gap = a - b; + foreach (var digits in OrderDigits) + if (Numerics.PreciseEvaluation.Of(gap, digits) is { Re: var re } && (re.Low.Sign > 0 || re.High.Sign < 0)) + return re.Low.Sign > 0 ? 1 : -1; + return gap.Simplify() is Entity.Number.Complex { IsZero: true } ? 0 : null; + } + + /// of two constants that evaluate to real numbers. + internal static int? Order(Entity a, Entity b) + => a == b ? 0 + : a.Evaled is Entity.Number.Real aValue && b.Evaled is Entity.Number.Real bValue ? Order(a, aValue, b, bValue) : null; + + /// + /// How far apart, relative to their size, two hundred-digit decimals are told apart as they + /// are: half the digits, which no rounding of an evaluation reaches. + /// + [ConstantField] private static readonly EDecimal ApartShare = EDecimal.FromString("1E-50"); + + /// The digits a difference nearer than is enclosed in, in turn. + [ConstantField] private static readonly int[] OrderDigits = { 60, 150 }; } } diff --git a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs index a5e968e8d..974617144 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs @@ -168,29 +168,39 @@ A.Right.Evaled is not Real aRight || B.Left.Evaled is not Real bLeft || B.Right.Evaled is not Real bRight) return Rays(A, B) ?? Rays(B, A) ?? A.Intersect(B); - if (aLeft == bRight) + // The ends are ordered as numbers, and an end written two ways is one end: the decimals + // of two spellings can differ in the last digit, and [a; r] /\ [r'; b] was empty. + // https://github.com/asc-community/AngouriMath/issues/2732 + if (Order(A.Left, aLeft, B.Right, bRight) is not { } aLeftToBRight + || Order(B.Left, bLeft, A.Right, aRight) is not { } bLeftToARight + || Order(A.Left, aLeft, A.Right, aRight) is not { } aWidth + || Order(B.Left, bLeft, B.Right, bRight) is not { } bWidth + || Order(A.Left, aLeft, B.Left, bLeft) is not { } lefts + || Order(A.Right, aRight, B.Right, bRight) is not { } rights) + return A.Intersect(B); + if (aLeftToBRight == 0) return A.LeftClosed && B.RightClosed ? new FiniteSet(A.Left) : Empty; - if (bLeft == aRight) + if (bLeftToARight == 0) return A.RightClosed && B.LeftClosed ? new FiniteSet(B.Left) : Empty; // An interval whose ends are reversed holds nothing, and one closed at a single // point holds that point, so that is what it meets the other in. Returning the other // interval is what the union does, and is right there: (3; 1) /\ [0; 5] was [0; 5]. - if (aLeft >= aRight) - return aLeft == aRight && A.LeftClosed && A.RightClosed ? IntersectFiniteSetAndSet(new FiniteSet(A.Left), B) : Empty; - if (bLeft >= bRight) - return bLeft == bRight && B.LeftClosed && B.RightClosed ? IntersectFiniteSetAndSet(new FiniteSet(B.Left), A) : Empty; - if (aLeft > bRight) + if (aWidth >= 0) + return aWidth == 0 && A.LeftClosed && A.RightClosed ? IntersectFiniteSetAndSet(new FiniteSet(A.Left), B) : Empty; + if (bWidth >= 0) + return bWidth == 0 && B.LeftClosed && B.RightClosed ? IntersectFiniteSetAndSet(new FiniteSet(B.Left), A) : Empty; + if (aLeftToBRight > 0) return Empty; - if (bLeft > aRight) + if (bLeftToARight > 0) return Empty; var (left, leftClosed) = - aLeft == bLeft ? + lefts == 0 ? (A.Left, A.LeftClosed && B.LeftClosed) : - (bLeft < aLeft ? (A.Left, A.LeftClosed) : (B.Left, B.LeftClosed)); + (lefts > 0 ? (A.Left, A.LeftClosed) : (B.Left, B.LeftClosed)); var (right, rightClosed) = - aRight == bRight ? + rights == 0 ? (A.Right, A.RightClosed && B.RightClosed) : - (bRight > aRight ? (A.Right, A.RightClosed) : (B.Right, B.RightClosed)); + (rights < 0 ? (A.Right, A.RightClosed) : (B.Right, B.RightClosed)); return new Interval(left, leftClosed, right, rightClosed); } diff --git a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs index b5039d442..a7e4a1424 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Union.cs @@ -23,6 +23,9 @@ internal static partial class SetOperators /// internal static Set? CanonicalUnion(Set set) { + // Ends are compared as numbers, not as their decimals, and a comparison that is not + // decided leaves the union as written. https://github.com/asc-community/AngouriMath/issues/2732 + var undecided = false; var pieces = new List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>(); bool Collect(Entity piece) { @@ -31,7 +34,7 @@ bool Collect(Entity piece) case Unionf(var left, var right): return Collect(left) && Collect(right); case Interval(var left, var leftClosed, var right, var rightClosed) when left.Evaled is Number.Real && right.Evaled is Number.Real: - var order = At(left).CompareTo(At(right)); + var order = Compare(left, right, ref undecided); if (order < 0 || order == 0 && leftClosed && rightClosed) pieces.Add((left, leftClosed, right, rightClosed)); return true; @@ -43,10 +46,23 @@ bool Collect(Entity piece) return false; } } - if (!Collect(set)) + if (!Collect(set) || undecided) return null; // By the left end, a closed end before an open one at the same point. - pieces.Sort(static (a, b) => At(a.Left).CompareTo(At(b.Left)) is var byLeft and not 0 ? byLeft : b.LeftClosed.CompareTo(a.LeftClosed)); + // An insertion sort: the pieces are few, and a comparer passed as a delegate would have to capture. + for (var i = 1; i < pieces.Count; i++) + { + var moved = pieces[i]; + var j = i - 1; + for (; j >= 0; j--) + { + var byLeft = Compare(pieces[j].Left, moved.Left, ref undecided); + if (byLeft < 0 || byLeft == 0 && (pieces[j].LeftClosed || !moved.LeftClosed)) + break; + pieces[j + 1] = pieces[j]; + } + pieces[j + 1] = moved; + } var merged = new List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>(); foreach (var piece in pieces) { @@ -56,20 +72,26 @@ bool Collect(Entity piece) continue; } var last = merged[^1]; - var gap = At(piece.Left).CompareTo(At(last.Right)); + var gap = Compare(piece.Left, last.Right, ref undecided); if (gap > 0 || gap == 0 && !last.RightClosed && !piece.LeftClosed) { merged.Add(piece); continue; } - var further = At(piece.Right).CompareTo(At(last.Right)); + var further = Compare(piece.Right, last.Right, ref undecided); merged[^1] = further > 0 ? (last.Left, last.LeftClosed, piece.Right, piece.RightClosed) : further == 0 ? (last.Left, last.LeftClosed, last.Right, last.RightClosed || piece.RightClosed) : last; } - var points = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) == 0).Select(static piece => piece.Left).ToList(); - var intervals = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) != 0) - .Select(static piece => (Set)new Interval(piece.Left, piece.LeftClosed, piece.Right, piece.RightClosed)).ToList(); + var points = new List(); + var intervals = new List(); + foreach (var piece in merged) + if (Compare(piece.Left, piece.Right, ref undecided) == 0) + points.Add(piece.Left); + else + intervals.Add(new Interval(piece.Left, piece.LeftClosed, piece.Right, piece.RightClosed)); + if (undecided) + return null; Set canonical = intervals.Count == 0 ? new FiniteSet(points) : (points.Count == 0 ? intervals : intervals.Prepend(new FiniteSet(points))) .Aggregate(static (left, right) => new Unionf(left, right)); @@ -78,6 +100,15 @@ bool Collect(Entity piece) private static PeterO.Numbers.EDecimal At(Entity end) => ((Number.Real)end.Evaled).EDecimal; + /// The order of two ends by , and by their decimals where that is not decided, which it records. + private static int Compare(Entity a, Entity b, ref bool undecided) + { + if (Order(a, b) is { } order) + return order; + undecided = true; + return At(a).CompareTo(At(b)); + } + internal static Set UniteFiniteSetAndSet(FiniteSet finite, Set set) { if (set is FiniteSet another) @@ -90,9 +121,10 @@ internal static Set UniteFiniteSetAndSet(FiniteSet finite, Set set) foreach (var el in finite) if (!set.TryContains(el, out var contains) || !contains) { - if (el == left) + // An end written another way is that end: https://github.com/asc-community/AngouriMath/issues/2732 + if (Order(el, left) == 0) newLeftClosed = true; - else if (el == right) + else if (Order(el, right) == 0) newRightClosed = true; else sb.Add(el); diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 9a3976181..19c411fef 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -1403,6 +1403,43 @@ when IsASquareRoot(p) && IsRealAtRealX(u, x) && IsRealAtRealX(w, x) && !HoldsARo : Solve(u >= 0 & Comparison(w, true, strict, Integer.Zero) & Comparison(u, false, strict, squared), x); } + /// + /// The real members of an equation's roots: a listed root kept where it is a real number, left + /// out where an enclosure shows its imaginary part is not zero, and left intersected with the + /// reals where neither is decided, as sqrt(a) is. Roots that are not listed are + /// intersected with the reals as a whole. + /// https://github.com/asc-community/AngouriMath/issues/2733 + /// + private static Set RealRoots(Set roots) + { + switch (roots) + { + case FiniteSet listed: + var real = new List(); + List? undecided = null; + foreach (var root in listed) + if (root.Evaled is Real) + real.Add(root); + else if (!IsShownNotReal(root)) + (undecided ??= new()).Add(root); + if (real.Count == listed.Count) + return listed; + Set kept = new FiniteSet(real); + return undecided is null ? kept : MathS.Union(kept, MathS.Intersection(new FiniteSet(undecided), MathS.Sets.R)); + case Unionf(Set left, Set right): + return MathS.Union(RealRoots(left), RealRoots(right)); + default: + return roots.IsSetEmpty ? roots : MathS.Intersection(roots, MathS.Sets.R); + } + + static bool IsShownNotReal(Entity root) + => !root.Vars.Any() && root.Evaled is Complex + && Numerics.PreciseEvaluation.Of(root, RealnessDigits) is { Im: var im } && (im.Low.Sign > 0 || im.High.Sign < 0); + } + + /// The digits a root's imaginary part is enclosed in by . + private const int RealnessDigits = 60; + /// The solution set, or where a part of the statement is declined. internal static Set? Solve(Entity expr, Variable x) => expr switch @@ -1451,12 +1488,14 @@ ComparisonSign comparison when MonotoneInverted(comparison, x) is { } inverted = Greaterf(var left, var right) => WithoutPoles(AnalyticalInequalitySolver.Solve(Minus(left, right), x), Minus(left, right), x), + // An inequality is over the reals, so of the equation's roots only the real ones + // are solutions: x^2 + 1 <= 0 was { i, -i }. https://github.com/asc-community/AngouriMath/issues/2733 LessOrEqualf(var left, var right) => WithoutPoles(AnalyticalInequalitySolver.Solve(Minus(right, left), x) - ?.Unite(AnalyticalEquationSolver.Solve(Minus(left, right), x)), Minus(left, right), x), + ?.Unite(RealRoots(AnalyticalEquationSolver.Solve(Minus(left, right), x))), Minus(left, right), x), GreaterOrEqualf(var left, var right) => WithoutPoles(AnalyticalInequalitySolver.Solve(Minus(left, right), x) is { } above - ? (Set)MathS.Union(above, AnalyticalEquationSolver.Solve(Minus(left, right), x)) : null, Minus(left, right), x), + ? (Set)MathS.Union(above, RealRoots(AnalyticalEquationSolver.Solve(Minus(left, right), x))) : null, Minus(left, right), x), Lessf(var left, var right) => WithoutPoles(AnalyticalInequalitySolver.Solve(Minus(right, left), x), Minus(left, right), x), diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs index 07ea2739a..b20182b1f 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs @@ -612,7 +612,12 @@ partial record Inf private protected override Entity IntrinsicCondition => True; /// protected override Entity InnerSimplify(bool isExact) - => ExpandOnTwoArguments(Element, SupSet, + // A constant in an interval is decided as written, before its parts are evaluated: + // two spellings of an end evaluate to decimals that can differ in the last digit. + // https://github.com/asc-community/AngouriMath/issues/2732 + => SupSet is Interval interval && !Element.Vars.Any() && interval.TryContains(Element, out var inside) + ? (inside ? True : False) + : ExpandOnTwoArguments(Element, SupSet, (a, b) => (a, b) switch { (var el, Set set) when set.TryContains(el, out var contains) => contains, diff --git a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs index 4d64c3c95..6b75c2aa8 100644 --- a/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs @@ -305,7 +305,9 @@ private static void ValueIs(object expected, object actual) "Determinant" => (MathS.FromString("[[0.333333333333333333333333, 1], [1, 3]]", useCache: false), e => ((Entity.Matrix)e).Determinant!), "Rank" => (MathS.FromString("[[0.333333333333333333333333, 1], [1, 3]]", useCache: false), e => ((Entity.Matrix)e).Rank), "Inverse" => (MathS.FromString("[[0.333333333333333333333333, 1], [1, 3]]", useCache: false), e => (object?)((Entity.Matrix)e).Inverse ?? "none"), - "Interval ends" => (MathS.FromString("[1/3 - 0.33333333333333333333; 1]", useCache: false), e => ((Entity.Set)e).Contains(0)), + // An end the zero range reads as 0, against a member 10^-15 from it: an element at the end + // itself is decided exactly, whatever the decimals say (https://github.com/asc-community/AngouriMath/issues/2732). + "Interval ends" => (MathS.FromString("[1/3 - 0.333333333333; 1]", useCache: false), e => ((Entity.Set)e).Contains(MathS.FromString("1/10^15"))), _ => throw new System.ArgumentException(cache), }; diff --git a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs index cdfb0fcc6..8dcdcf725 100644 --- a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs @@ -869,6 +869,57 @@ public void Issue2690_ALogarithmTimesACosineLessTheLogarithmIsZero(string expres public void Issue2690_TwoSidedTheConventionForALogarithmAtZeroStands() => Assert.IsType("ln(x) * cos(x) - ln(x)".ToEntity().Limit("x", 0)); + // https://github.com/asc-community/AngouriMath/issues/2732 + // An open interval decided its own endpoint was a member when the element was the same number + // written another way, because the two spellings evaluate to decimals that differ in the last digit. + [Theory] + [InlineData("(1 + sqrt(5)) / 2", "(0; 1/2 + sqrt(5) / 2)", false)] + [InlineData("(-1 - sqrt(5)) / 2", "(-1/2 + -1/2 * sqrt(5); 0)", false)] + [InlineData("(3 + sqrt(21)) / 2", "(0; 3/2 + sqrt(21) / 2)", false)] + [InlineData("(1 + sqrt(5)) / 2", "(0; 1/2 + sqrt(5) / 2]", true)] + [InlineData("(-1 - sqrt(5)) / 2", "[-1/2 + -1/2 * sqrt(5); 0)", true)] + [InlineData("sqrt(2)", "(sqrt(2); 3)", false)] + [InlineData("sqrt(2) + 10^(-120)", "(0; sqrt(2))", false)] + [InlineData("sqrt(2) - 10^(-120)", "(0; sqrt(2))", true)] + public void Issue2732_AnEndWrittenAnotherWayIsThatEnd(string element, string interval, bool expected) + { + var set = Assert.IsAssignableFrom(interval.ToEntity()); + Assert.True(set.TryContains(element.ToEntity(), out var contains), $"{element} in {interval} undecided"); + Assert.Equal(expected, contains); + Assert.Equal(expected, (bool)$"{element} in {interval}".ToEntity().EvalBoolean()); + } + + // Each answer checked at its ends, written as the roots are, and just inside and outside them. + [Theory] + [InlineData("x^2 + x <= 1", new[] { "(-1 - sqrt(5)) / 2", "(-1 + sqrt(5)) / 2" })] + [InlineData("x^2 - 3*x <= 3", new[] { "(3 - sqrt(21)) / 2", "(3 + sqrt(21)) / 2" })] + [InlineData("6*x^2 + 22*x - 9 >= 0", new[] { "(-11 - 5 sqrt(7)) / 6", "(-11 + 5 sqrt(7)) / 6" })] + [InlineData("sqrt(x^2 - 4) >= 1/2", new[] { "-sqrt(17) / 2", "sqrt(17) / 2" })] + public void Issue2732_ANonStrictInequalityHoldsItsIrrationalRoots(string inequality, string[] roots) + { + var solved = Assert.IsAssignableFrom(inequality.ToEntity().Solve("x").Simplify()); + foreach (var root in roots) + foreach (var offset in new[] { "0", "10^(-6)", "-10^(-6)" }) + { + var point = (root.ToEntity() + offset.ToEntity()); + var holds = (bool)inequality.ToEntity().Substitute("x", point).EvalBoolean(); + Assert.True(solved.TryContains(point, out var contains), $"{point} in {solved} undecided"); + Assert.True(holds == contains, $"{inequality} solved to {solved}, which disagrees with it at x = {point}"); + } + } + + // https://github.com/asc-community/AngouriMath/issues/2733 + // An inequality is over the reals, and its non-strict form answered with the equation's complex roots. + [Theory] + [InlineData("x^2 + 1 <= 0", "{ }")] + [InlineData("-x^2 - 1 >= 0", "{ }")] + [InlineData("x^2 - 2*x + 1 <= -1", "{ }")] + [InlineData("exp(x^2 + 3/2) <= 4", "{ }")] + [InlineData("x^2 + 1 >= 0", "RR")] + [InlineData("x^2 + 1 >= 2", "(-oo; -1] \\/ [1; +oo)")] + public void Issue2733_ANonStrictInequalityHasOnlyRealSolutions(string inequality, string expected) + => Assert.Equal(expected.ToEntity().Simplify(), inequality.ToEntity().Solve("x").Simplify()); + private static readonly PeterO.Numbers.EContext Unfloored = new(100, PeterO.Numbers.ERounding.HalfUp, -100000, 100000, false); // Real parts, evaluated without the exponent floor, of roots each checked to be real.