diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fad9d71b9..bed060193 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -713,6 +713,26 @@ multiple of `pi/3` for large x, so no finite set answers them and they are left | `"e^(-x) = sin(3x)".Solve("x")` | stack overflow after about 2.5 min | `{ x : e ^ (-x) = sin(3 * x) }`, 0.6 s | | `"e^(-x) + sin(x)^3 = 3 sin(x) cos(x)^2".Solve("x")` | did not finish in 60 s | `{ x : e ^ (-x) + sin(x) ^ 3 = 3 * sin(x) * cos(x) ^ 2 }`, 0.8 s | +### A root of a modulus raised to a negative number has no roots + +**Silent: a circle of non-roots becomes `{ }`.** A root of `abs(u)` is real and not negative, but raising +`sqrt(abs(x)) = -1` gives `abs(x) = 1`, and its roots `e^(i r)` were answered. Each was checked by substituting it, and a +family with a real parameter was not, so it was kept. Where `x` is only inside one modulus, the family's members at a +few angles are now checked, and it is dropped where none of them is a root +([#2712](https://github.com/asc-community/AngouriMath/issues/2712)). + +Measured over 90 equations of an even or fractional root of a modulus, on `409d68805` and on this branch: 27 answers +changed, each to `{ }` or to the old answer without its non-roots. Every root left was checked with mpmath. + +| `.Solve("x")` | Was (`409d68805`) | Now | +|---|---|---| +| `sqrt(abs(x)) = -1` | `{ e ^ (i * r_1) provided r_1 in RR }` | `{ }` | +| `sqrt(abs(x - 4) - 3) = -5` | `{ 28 * e ^ (i * r_1) + 4 provided r_1 in RR }` | `{ }` | +| `sqrt(abs(x) + 1) = -1` | `{ 0 provided r_1 in RR }` | `{ }` | +| `abs(x)^(3/2) = -8` | `{ 4 * e ^ (i * r_1) provided r_1 in RR }` | `{ }` | +| `sqrt(abs(x)) = abs(x) - 2` | `{ e ^ (i * r_1) provided r_1 in RR, 4 * e ^ (i * r_1) provided r_1 in RR }` | `{ 4 * e ^ (i * r_1) provided r_1 in RR }` | +| `sqrt(abs(x)) = -abs(x)` | `{ 0 provided r_1 in RR, e ^ (i * r_1) provided r_1 in RR }` | `{ 0 provided r_1 in RR }` | + ### The whole part of a large rational power is taken out **Silent, in the printed form only.** A rational raised to a rational power was written as a whole part times a diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolutionParameters.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolutionParameters.cs index a5e158d07..75b5ef881 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolutionParameters.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolutionParameters.cs @@ -247,14 +247,21 @@ private Set Bind(FiniteSet listed) /// /// at one member of its family: each symbol in it a whole /// parameter minted in the scope open, put at the whole number nearest 0 in its set; or - /// where a symbol is anything else. + /// where a symbol is anything else. With + /// , a real parameter, the angle a modulus is inverted with, is + /// put at . /// - internal static Entity? AtAMember(Entity root) + internal static Entity? AtAMember(Entity root, Entity? angle = null) { if (current is not { } scope) return null; foreach (var parameter in root.Vars.Distinct().ToList()) { + if (angle is not null && scope.SetOf(parameter) is { } real && real == MathS.Sets.R) + { + root = root.Substitute(parameter, angle); + continue; + } if (scope.SetOf(parameter) is not { } set || !IsWhole(set)) return null; Entity? value = null; @@ -273,6 +280,10 @@ private Set Bind(FiniteSet listed) [ConstantField] private static readonly Integer[] Nearest = { 0, 1, -1, 2, -2 }; + /// Whether a parameter of is real, an angle a modulus is inverted with. + internal static bool HasAnAngle(Entity root) => + current is { } scope && root.Vars.Any(parameter => scope.SetOf(parameter) is { } set && set == MathS.Sets.R); + /// /// Each root in , with an index of an indexed union in it minted as /// a parameter over the union's index set, or where diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index df9698c17..b042135d5 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -264,10 +264,36 @@ private static RootVerdict Verdict(Entity equation, Variable x, Entity root) // members, as the principal root alone was before the family was written: a member // that fails is evidence against the family, and one that passes proves nothing // about the rest, so the family is kept. https://github.com/asc-community/AngouriMath/issues/2319 + // So is a family the modulus is inverted into, |u| = c being u = c e^(i r) for every real r, + // where the equation has x only inside that one modulus: there it is a function of |u|, + // which is c wherever u is. Raising sqrt(abs(x)) = -1 gives abs(x) = 1, and e^(i r) was + // answered, whose members all have sqrt(abs(x)) = 1. https://github.com/asc-community/AngouriMath/issues/2712 + // But the inverse of what is inside the modulus can be one branch of many, so the family is + // dropped only where no member at a few angles is a root: abs(arctan(x)) = 2 is answered + // tan(2 e^(i r)), whose member tan(2) at r = 0 fails, as arctan(tan(2)) = 2 - pi, and whose + // member i tanh(2) at r = pi/2 is a root. A member where the root has no value, or its own + // condition excludes it, is no evidence either way: abs((x + 1) / (x - 1)) = 1 is answered + // -(e^(i r) + 1) / (1 - e^(i r)), the imaginary axis, which at r = 0 is -2 / 0. if (root.Vars.Any()) - return SolutionParameters.AtAMember(root) is { } member && !member.Vars.Any() && Verdict(equation, x, member) is var verdict and not RootVerdict.Kept - ? verdict - : RootVerdict.Kept; + { + if (!HasXOnlyInsideOneModulus(equation, x) || !SolutionParameters.HasAnAngle(root)) + return SolutionParameters.AtAMember(root) is { } member && !member.Vars.Any() && Verdict(equation, x, member) is var verdict and not RootVerdict.Kept + ? verdict + : RootVerdict.Kept; + RootVerdict? against = null; + foreach (var angle in Angles) + { + if (SolutionParameters.AtAMember(root, angle) is not { } member || member.Vars.Any()) + return RootVerdict.Kept; + if (member.Evaled is not Complex { IsFinite: true }) + continue; + var verdict = Verdict(equation, x, member); + if (verdict is RootVerdict.Kept) + return RootVerdict.Kept; + against ??= verdict; + } + return against ?? RootVerdict.Kept; + } var substituted = equation.Substitute(x, root); if (IsUndefinedExactly(substituted)) return RootVerdict.Spurious; @@ -396,6 +422,34 @@ private static bool IsAWrittenInteger(Real part) return part.EDecimal.IsFinite && part.EDecimal.Exponent.IsZero; } + /// The angles a family a modulus is inverted into is judged at: it is dropped where no member at them with a value is a root. + [ConstantField] private static readonly Entity[] Angles = { 0, MathS.pi / 2, MathS.pi, -MathS.pi / 2, MathS.pi / 4, 3 * MathS.pi / 4, 1, 2 }; + + /// + /// Whether every in is inside a modulus, and + /// every modulus with in it, outermost, is the same one. + /// + private static bool HasXOnlyInsideOneModulus(Entity equation, Variable x) + { + Entity? modulus = null; + bool Walk(Entity node) + { + if (node is Absf && node.ContainsNode(x)) + { + modulus ??= node; + return modulus == node; + } + if (node == x) + return false; + var children = node.DirectChildren; + for (var i = 0; i < children.Count; i++) + if (!Walk(children[i])) + return false; + return true; + } + return Walk(equation) && modulus is not null; + } + /// What does with a root. private enum RootVerdict { diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalOfAModulusIsNeverNegativeTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalOfAModulusIsNeverNegativeTest.cs new file mode 100644 index 000000000..12e3de100 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalOfAModulusIsNeverNegativeTest.cs @@ -0,0 +1,96 @@ +// +// 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.Linq; +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// A root of a modulus is real and not negative, so raising sqrt(abs(x)) = -1 to + /// abs(x) = 1 adds the circle e^(i r), none of whose members is a root. The equation + /// has x only inside the modulus, so the family is judged by its members at a few angles, and + /// dropped where none of those with a value is a root. + /// https://github.com/asc-community/AngouriMath/issues/2712 + /// + [Trait("Area", "Algebra")] + public sealed class ARadicalOfAModulusIsNeverNegativeTest + { + [Theory] + [InlineData("sqrt(abs(x)) = -1")] + [InlineData("sqrt(abs(x - 4) - 3) = -5")] + [InlineData("sqrt(abs(x)) + 1 = 0")] + [InlineData("sqrt(abs(x) + 1) = -1")] + [InlineData("2 sqrt(abs(x)) + 3 = 0")] + [InlineData("abs(x) ^ (1/4) = -1")] + [InlineData("abs(x) ^ (3/4) = -1")] + [InlineData("abs(x) ^ (3/2) = -8")] + [InlineData("sqrt(abs(x)) * abs(x) = -8")] + public void HasNoRoot(string equation) + => Assert.Equal(Set.Empty, equation.ToEntity().Solve("x")); + + // Each answer is one family, every member of which is a root: |x| = 4 and |x| = 0, and not + // the |x| = 1 and |x| = 1/4 the squares add. + [Theory] + [InlineData("sqrt(abs(x)) - abs(x) + 2", 4)] + [InlineData("sqrt(abs(x)) + 2 abs(x)", 0)] + [InlineData("sqrt(abs(x)) + abs(x)", 0)] + [InlineData("sqrt(abs(x)) - 2", 4)] + [InlineData("abs(x) ^ (3/2) - 8", 4)] + public void HasOneCircleOfRoots(string residual, int modulus) + { + var root = Assert.Single(Assert.IsType(residual.ToEntity().Equalizes(0).Solve("x"))); + foreach (var angle in new[] { "0", "1/3", "2", "-5/2" }) + { + var member = root.Vars.Aggregate(root, (at, parameter) => at.Substitute(parameter, angle.ToEntity())); + Assert.Equal(0, ((Entity.Number.Complex)(MathS.Abs(member) - modulus).Evaled).Abs().EDecimal.ToDouble(), 12); + Assert.Equal(0, ((Entity.Number.Complex)residual.ToEntity().Substitute("x", member).Evaled).Abs().EDecimal.ToDouble(), 12); + } + } + + // Every x on the imaginary axis is as far from 1 as from -1, so these are roots, 0 and i among + // them. The family the modulus is inverted into, -(e^(i r) + 1) / (1 - e^(i r)), has no value at + // r = 0, which is no evidence against it, and it is judged at a member that has one. + [Theory] + [InlineData("abs((x + 1) / (x - 1)) - 1")] + [InlineData("abs((x + 2) / (x - 2)) - 1")] + public void KeepsACircleWithNoValueAtAngleZero(string residual) + { + foreach (var x in new[] { "0", "i" }) + Assert.Equal(0, ((Entity.Number.Complex)residual.ToEntity().Substitute("x", x.ToEntity()).Evaled).Abs().EDecimal.ToDouble(), 12); + var answer = residual.ToEntity().Equalizes(0).Solve("x"); + Assert.NotEqual(Set.Empty, answer); + var root = Assert.Single(Assert.IsType(answer)); + foreach (var angle in new[] { "pi / 2", "1/3", "2", "-5/2" }) + { + var member = (Entity.Number.Complex)root.Vars.Aggregate(root, (at, parameter) => at.Substitute(parameter, angle.ToEntity())).Evaled; + Assert.Equal(0, member.RealPart.EDecimal.ToDouble(), 12); + Assert.Equal(0, ((Entity.Number.Complex)residual.ToEntity().Substitute("x", member).Evaled).Abs().EDecimal.ToDouble(), 12); + } + } + + // The inverse of what is inside the modulus is one branch of many, so only some members of the + // family are roots: tan(2 e^(i r)) at r = 0 is tan(2), and arctan(tan(2)) = 2 - pi, while at + // r = pi/2 it is i tanh(2), and arctan(i tanh(2)) = 2i. sin(3 e^(i r)) is the same at r = 0 + // and r = pi/2, with arcsin(sin(3)) = pi - 3 and arcsin(i sinh(3)) = 3i. + [Theory] + [InlineData("abs(arctan(x)) - 2")] + [InlineData("abs(arcsin(x)) - 3")] + public void KeepsACircleWithSomeMembersRoots(string residual) + { + var answer = residual.ToEntity().Equalizes(0).Solve("x"); + Assert.NotEqual(Set.Empty, answer); + var root = Assert.Single(Assert.IsType(answer)); + var member = (Entity.Number.Complex)root.Vars.Aggregate(root, (at, parameter) => at.Substitute(parameter, "pi / 2".ToEntity())).Evaled; + Assert.Equal(0, member.RealPart.EDecimal.ToDouble(), 12); + Assert.Equal(0, ((Entity.Number.Complex)residual.ToEntity().Substitute("x", member).Evaled).Abs().EDecimal.ToDouble(), 12); + } + } +}