From 6ed6e393473fb4acac2d78de192c61f05b73d59a Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 6 Dec 2026 02:23:47 +0000 Subject: [PATCH] Keep ln and the inverse trigonometric functions in their range when inverting them, and (e^a)^c = e^(ac) for a real a only ln(x) = w has a root only where Im(w) is in (-pi, pi], and arctan(x) = w only where w is a value of arctan: Re in (-pi/2, pi/2), or pi/2 above the real line and -pi/2 below it, where the cuts land (arctan(2i) = pi/2 + 0.549i). The inversions answered e^w and tan(w) for every w, so e^(ln(x)^2) = 1 listed e^sqrt(10 pi i), where e^(ln(x)^2) - 1 is 27337 + 13573i, and abs(arctan(x)) = 2 listed tan(2 e^(i r)) at r = 0.3, where |arctan| is 1.365. A value that is not a number now carries the condition. On a line a n + b in a parameter the strip is decided once where it does not depend on n (sin(ln(x)) = 0 is unchanged); a line through an inverse trigonometric function is #2711's and is left as it was. arcsin, arccos and arccotan carry their ranges the same way, measured on the evaluator. (a^b)^c = a^(bc) holds for a whole c, or where ln(a^b) is b ln(a), which needs a positive and b real. It was applied for any b, so sqrt(e^(2x)) was e^x, the negative of the value at x = 2.1 - 1.7i. Both spellings of the rule and the factor reader ask one predicate now; over the reals it fires as before. Two tests pinned the old answers: log(b, x) = 2 was { b ^ 2 }, which is wrong at b = -1 (log(-1, 1) is 0), and the agreement test listed (2^x)^(1/2) as firing. Fixes #2713 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 21 +++ .../Transformations/Matching/MatchedRules.cs | 10 +- .../EquationSolver/InvertNode.Classes.cs | 120 ++++++++++++++++- .../Simplification/Patterns/Patterns.Power.cs | 19 ++- .../APrincipalRangeOffTheRealLineTest.cs | 127 ++++++++++++++++++ .../SolveTest/SolveEquationWithPiecewise.cs | 4 +- .../MatchedRulesAgreeWithTheSwitchTest.cs | 5 +- 7 files changed, 287 insertions(+), 19 deletions(-) create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/APrincipalRangeOffTheRealLineTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fad9d71b9..5ef1effbf 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -713,6 +713,27 @@ 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 | +### Inverting ln or an inverse trigonometric function keeps the value in its range, and (e^a)^c needs a real a + +**Silent: wrong complex roots and values go.** `ln(x) = w` has a root only where `Im(w)` is in `(-pi, pi]`, and +`arctan(x) = w` only where `Re(w)` is in `(-pi/2, pi/2)` (or on an edge, where the cuts land: `arctan(2i)` is +`pi/2 + 0.549i`); `arcsin`, `arccos` and `arccotan` likewise. A value that is not a number now carries that +condition; at `n = 5`, `e^(ln(x)^2) - 1` was `27337 + 13573i` at the old root. And `(a^b)^c = a^(bc)` for a positive +`a` and a fractional `c` now needs `b` real as well: `sqrt(e^(2x))` is `-e^x` at `x = 2.1 - 1.7i`. Over the reals +(`MathS.Settings.Codomain` set to `Domain.Real`) it is applied as before. Measured on master (409d68805) and on this +branch ([#2713](https://github.com/asc-community/AngouriMath/issues/2713)). + +| Input | Was (master) | Now | +|---|---|---| +| `e^(ln(x)^2) = 1` | `union({ e ^ sqrt(2 * pi * i * n), … }, n in ZZ)` | each member `provided -pi < Im(sqrt(2 * pi * i * n)) and Im(sqrt(2 * pi * i * n)) <= pi` | +| `abs(arctan(x)) = 2` | `{ tan(2 * e ^ (i * r_1)) provided r_1 in RR }` | `… and (-pi / 2 < 2 * Re(e ^ (i * r_1)) and 2 * Re(e ^ (i * r_1)) < pi / 2 or …)` | +| `ln(x) = a` | `{ e ^ a }` | `{ e ^ a provided -pi < Im(a) and Im(a) <= pi }` | +| `log(b, x) = 2` | `{ b ^ 2 }` | `{ b ^ 2 provided -pi < 2 * Im(ln(b)) and 2 * Im(ln(b)) <= pi }` | +| `arctan(x) = a` | `{ tan(a) }` | `{ tan(a) provided -pi / 2 < Re(a) and Re(a) < pi / 2 or Re(a) = pi / 2 and Im(a) > 0 or Re(a) = -pi / 2 and Im(a) < 0 }` | +| `sqrt(e^(2x)).Simplify()` | `e ^ x` | `sqrt(e ^ (2 * x))` | +| `(e^(2x))^(1/4)` | `e ^ (x / 2)` | `(e ^ (2 * x)) ^ (1/4)` | +| `sqrt(2^x)` | `2 ^ (x / 2)` | `sqrt(2 ^ x)` | + ### 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/Core/Transformations/Matching/MatchedRules.cs b/Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs index 4199d4066..f2ee47552 100644 --- a/Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs +++ b/Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs @@ -233,7 +233,7 @@ internal static class MatchedRules /// /// The third set expressed here, and the one that exercises the last piece of the /// design: a condition about the match as a whole rather than about one hole. - /// (a^b)^c = a^(b*c) is true for a positive base whatever the exponents, and for + /// (a^b)^c = a^(b*c) is true for a positive base and a real inner exponent, and for /// any base when the outer exponent is whole — and false outside those two, which is /// #752: applied /// unconditionally it turned sqrt(x^2) into x, which at -0.63 is -0.63 @@ -261,8 +261,7 @@ internal static class MatchedRules MatchPattern.Node(MatchPattern.Any("b"), MatchPattern.Any("c"))), Soundness.SoundUnderAssumptions, // Two bindings at once, which no predicate on a single hole can express. - when: bound => bound["c"] is Integer - || bound["a"].Evaled is Real { IsPositive: true })); + when: bound => Functions.Patterns.MayMultiplyExponents(bound["a"], bound["b"], bound["c"]))); /// /// k*p + k*q = k*(p + q), written once, where the switch writes it four @@ -3019,7 +3018,7 @@ is var (divided, remainder) // -2 for a base of one node and more for a larger one. growth: RewriteRuleGrowth.Collects), - // True for a positive base whatever the exponents, and for any base when the outer + // True for a positive base and a real inner exponent, and for any base when the outer // exponent is whole. Outside those two it moves the branch, which is what #752 // measured: sqrt(x^2) came back as x, and at -0.63 that is the negation. // https://github.com/asc-community/AngouriMath/issues/752 @@ -3030,8 +3029,7 @@ is var (divided, remainder) MatchPattern.Any("m")), bound => new Powf(bound["a"], bound["n"] * bound["m"]), Soundness.SoundUnderAssumptions, - when: bound => bound["m"] is Integer - || bound["a"].Evaled is Real { IsPositive: true }, + when: bound => Functions.Patterns.MayMultiplyExponents(bound["a"], bound["n"], bound["m"]), description: "(a ^ n) ^ m = a ^ (n * m)", // Two binary operators and three holes on each side, every hole used once: the // nesting moves from the base to the exponent and nothing is added or dropped. diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs index 44898e18d..1a80baceb 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs @@ -387,6 +387,30 @@ private protected static Entity Angle(Entity angle) }; } + partial record Function + { + /// + /// , each provided is a value of the + /// inverse function being inverted, which states of its real + /// and imaginary parts. abs(arctan(x)) = 2 is arctan(x) = 2 e ^ (i r), and + /// tan(2 e ^ (i r)) is no preimage where |Re(2 e ^ (i r))| > pi/2. A value + /// that is a number is decided by its bounds before this. + /// https://github.com/asc-community/AngouriMath/issues/2713 + /// + private protected static IEnumerable? WithinItsValues(IEnumerable? roots, Entity value, System.Func values) + { + // A line in a parameter of the solve, pi / 4 + pi n, is the family a periodic + // function gives, and which of its members are values is a question about a line, + // not this condition. https://github.com/asc-community/AngouriMath/issues/2711 + if (roots is null || value.Evaled is Complex + || value.Vars.Distinct().ToList() is { Count: 1 } vars && SolutionParameters.Current?.SetOf(vars[0]) is { } + && SolutionParameters.NumericLine(value, vars[0]) is not null) + return roots; + var condition = values(MathS.Re(value), MathS.Im(value)); + return roots.Select(root => root.Provided(condition)); + } + } + partial record Sinf { private protected override IEnumerable? InvertNode(Entity value, Entity x) => @@ -444,9 +468,63 @@ partial record Logf private protected override IEnumerable? InvertNode(Entity value, Entity x) => Base.ContainsNode(x) // log_x(a) = value => a = x ^ value => x = a ^ (1 / value) - ? Base.Invert(MathS.Pow(Antilogarithm, 1 / value), x) + ? Within(Base.Invert(MathS.Pow(Antilogarithm, 1 / value), x), InTheStrip(MathS.Ln(Antilogarithm) / value)) // log_a(x) = value => x = a ^ value - : Antilogarithm.Invert(MathS.Pow(Base, value), x); + : Within(Antilogarithm.Invert(MathS.Pow(Base, value), x), InTheStrip(Base == MathS.e ? value : value * MathS.Ln(Base))); + + private static IEnumerable? Within(IEnumerable? roots, Entity condition) + => condition is Boolean { Value: false } ? Enumerable.Empty() : roots?.Select(root => root.Provided(condition)); + + // ln(z) has its imaginary part in (-pi, pi], so e ^ w is a preimage of ln(x) = w only + // there: e ^ (ln(x) ^ 2) = 1 is ln(x) = sqrt(2 pi i n), and at n = 5 that is + // 3.96 + 3.96i, which ln never takes. A value with a number for its imaginary part is + // decided by the check of a finite answer, and a real one is in the strip. + // https://github.com/asc-community/AngouriMath/issues/2713 + private static Entity InTheStrip(Entity logarithm) + { + if (MathS.Settings.Codomain.Value == Core.Domain.Real || logarithm.Evaled is Complex + || Conjugatef.IsKnownReal(logarithm) || TreeAnalyzer.IsKnownToBeReal(logarithm)) + return Boolean.True; + if (OnALine(logarithm) is { } decided) + return decided; + var imaginary = MathS.Im(logarithm); + return new Lessf(-MathS.pi, imaginary) & new LessOrEqualf(imaginary, MathS.pi); + } + + /// + /// Whether a logarithm a n + b, for a parameter n of the solve, is in the + /// strip, where that does not depend on n; otherwise . With + /// a real its imaginary part is Im(b) throughout, as in sin(ln(x)) = 0. With + /// 2 pi i a whole multiple m a and n whole, the member at n is + /// the member at n - m, so each is the exponential of a logarithm in the strip: + /// e ^ ln(x) = 2 is ln(2) + 2 pi i n, and every member of it is 2. + /// + private static Boolean? OnALine(Entity logarithm) + { + if (SolutionParameters.Current is not { } scope + || logarithm.Vars.Distinct().ToList() is not { Count: 1 } vars + || scope.SetOf(vars[0]) is not { } set + || SolutionParameters.NumericLine(logarithm, vars[0]) is not var (slope, constant)) + return null; + var context = PeterO.Numbers.EContext.ForPrecision(50); + var margin = PeterO.Numbers.EDecimal.FromString("1E-30"); + if (slope.ImaginaryPart.IsZero) + { + var im = constant.ImaginaryPart.EDecimal; + var pi = MathS.DecimalConst.pi; + if (im.CompareTo(pi.Negate().Add(margin, context)) > 0 && im.CompareTo(pi.Subtract(margin, context)) < 0) + return Boolean.True; + if (im.CompareTo(pi.Negate().Subtract(margin, context)) < 0 || im.CompareTo(pi.Add(margin, context)) > 0) + return Boolean.False; + return null; + } + if (set != MathS.Sets.Z || !slope.RealPart.IsZero) + return null; + // 2 pi i / a for a = i t is 2 pi / t. + var turns = MathS.DecimalConst.pi.Multiply(PeterO.Numbers.EDecimal.FromInt32(2), context).Divide(slope.ImaginaryPart.EDecimal, context); + var whole = turns.RoundToIntegerNoRoundedFlag(context); + return !whole.IsZero && turns.Subtract(whole, context).Abs().CompareTo(margin) < 0 ? Boolean.True : null; + } } partial record Arcsinf @@ -455,7 +533,14 @@ partial record Arcsinf [ConstantField] private static readonly Complex To = Complex.Create(MathS.DecimalConst.pi / 2, Real.PositiveInfinity.EDecimal); // arcsin(x) = value => x = sin(value) private protected override IEnumerable? InvertNode(Entity value, Entity x) => - EntityInBounds(value, From, To) ? Argument.Invert(MathS.Sin(value), x) : Enumerable.Empty(); + EntityInBounds(value, From, To) ? WithinItsValues(Argument.Invert(MathS.Sin(value), x), value, Values) : Enumerable.Empty(); + + // Re in (-pi/2, pi/2), and on either edge the values below the real line, as + // evaluated: arcsin(2) = pi/2 - 1.317i and arcsin(-2) = -pi/2 - 1.317i. + // https://github.com/asc-community/AngouriMath/issues/2713 + private static Entity Values(Entity re, Entity im) + => new Lessf(-MathS.pi / 2, re) & new Lessf(re, MathS.pi / 2) + | (re.EqualTo(MathS.pi / 2) | re.EqualTo(-MathS.pi / 2)) & new LessOrEqualf(im, 0); } partial record Arccosf @@ -464,7 +549,14 @@ partial record Arccosf [ConstantField] private static readonly Complex To = Complex.Create(MathS.DecimalConst.pi, Real.PositiveInfinity.EDecimal); // arccos(x) = value => x = cos(value) private protected override IEnumerable? InvertNode(Entity value, Entity x) => - EntityInBounds(value, From, To) ? Argument.Invert(MathS.Cos(value), x) : Enumerable.Empty(); + EntityInBounds(value, From, To) ? WithinItsValues(Argument.Invert(MathS.Cos(value), x), value, Values) : Enumerable.Empty(); + + // Re in (0, pi), and on either edge the values above the real line, as evaluated: + // arccos(2) = 1.317i and arccos(-2) = pi + 1.317i. + // https://github.com/asc-community/AngouriMath/issues/2713 + private static Entity Values(Entity re, Entity im) + => new Lessf(0, re) & new Lessf(re, MathS.pi) + | (re.EqualTo(0) | re.EqualTo(MathS.pi)) & new GreaterOrEqualf(im, 0); } partial record Arctanf @@ -473,7 +565,16 @@ partial record Arctanf [ConstantField] private static readonly Complex To = Complex.Create(MathS.DecimalConst.pi / 2, Real.PositiveInfinity.EDecimal); // arctan(x) = value => x = tan(value) private protected override IEnumerable? InvertNode(Entity value, Entity x) => - EntityInBounds(value, From, To) ? Argument.Invert(MathS.Tan(value), x) : Enumerable.Empty(); + EntityInBounds(value, From, To) ? WithinItsValues(Argument.Invert(MathS.Tan(value), x), value, Values) : Enumerable.Empty(); + + // Re in (-pi/2, pi/2), with pi/2 above the real line and -pi/2 below it, the images + // of the cuts: arctan(2i) = pi/2 + 0.549i and arctan(-2i) = -pi/2 - 0.549i. Neither + // edge meets the real line, where tan has a pole. + // https://github.com/asc-community/AngouriMath/issues/2713 + internal static Entity Values(Entity re, Entity im) + => new Lessf(-MathS.pi / 2, re) & new Lessf(re, MathS.pi / 2) + | re.EqualTo(MathS.pi / 2) & new Greaterf(im, 0) + | re.EqualTo(-MathS.pi / 2) & new Lessf(im, 0); } partial record Arccotanf @@ -483,7 +584,14 @@ partial record Arccotanf [ConstantField] private static readonly Complex To = Complex.Create(MathS.DecimalConst.pi / 2, Real.PositiveInfinity.EDecimal); // arccotan(x) = value => x = cotan(value) private protected override IEnumerable? InvertNode(Entity value, Entity x) => - EntityInBounds(value, From, To) ? Argument.Invert(MathS.Cotan(value), x) : Enumerable.Empty(); + EntityInBounds(value, From, To) ? WithinItsValues(Argument.Invert(MathS.Cotan(value), x), value, Values) : Enumerable.Empty(); + + // arccotan(z) is arctan(1 / z), with arccotan(0) = pi/2: arctan's values and pi/2, + // without 0, where cotan has a pole. arccotan(0.5i) = -pi/2 - 0.549i. + // https://github.com/asc-community/AngouriMath/issues/2713 + private static Entity Values(Entity re, Entity im) + => Arctanf.Values(re, im) & !(re.EqualTo(0) & im.EqualTo(0)) + | re.EqualTo(MathS.pi / 2) & im.EqualTo(0); } partial record Arcsecantf diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs index 5094066d6..cc4d9e31b 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs @@ -59,6 +59,17 @@ expr is Powf(var @base, Integer { IsNegative: true } pow) internal static bool AReciprocalPowerSplits(Number c, Number d) => d is Integer || IsWholeOrAnOddRoot(d) && c is Real { IsPositive: true }; + /// + /// Whether (a ^ b) ^ c is a ^ (b c): where c is whole, since then + /// (a ^ b) ^ c is a product of a ^ bs however a is signed, or where + /// ln(a ^ b) is b ln(a), which needs a positive and b real. + /// A positive a alone is not enough: sqrt(e ^ (2x)) is not e ^ x at + /// x = 2.1 - 1.7i, where 2x leaves ln's strip. + /// https://github.com/asc-community/AngouriMath/issues/2713 + /// + internal static bool MayMultiplyExponents(Entity @base, Entity inner, Entity outer) + => outer is Integer || @base.Evaled is Real { IsPositive: true } && MayBeTakenAsReal(inner); + /// /// A whole number, or a rational whose denominator is odd: an exponent under which a /// negative base takes its real root, so that powers of it combine as they do on the @@ -91,16 +102,14 @@ internal static bool IsWholeOrAnOddRoot(Number n) => // ({} ^ {}) ^ {} = {} ^ ({} * {}) // - // True for a positive base whatever the exponents, and for any base when the - // outer exponent is a whole number -- (a^b)^3 is a^b * a^b * a^b however a is - // signed. Outside those two it is false, and it was applied unconditionally: + // Where MayMultiplyExponents says so. Applied unconditionally it was false: // // sqrt(x^2) came back as x, which at -0.63 is -0.63 where it is 0.63 // (x^2)^(3/2) came back as x^3, which at -2 is -8 where it is 8 // // https://github.com/asc-community/AngouriMath/issues/752 Powf(Powf(var any1, var any2), var any3) - when any3 is Integer || any1.Evaled is Real { IsPositive: true } + when MayMultiplyExponents(any1, any2, any3) => new Powf(any1, any2 * any3), // {1} ^ n * {2} ^ n = ({1} * {2}) ^ n @@ -513,7 +522,7 @@ private static (Entity Base, Entity Exponent) Decompose(Entity factor) if (factor is not Powf(var @base, var exponent)) return (factor, 1); while (@base is Powf(var inner, var innerExponent) - && (exponent is Integer || inner.Evaled is Real { IsPositive: true })) + && MayMultiplyExponents(inner, innerExponent, exponent)) { exponent = innerExponent * exponent; @base = inner; diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/APrincipalRangeOffTheRealLineTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/APrincipalRangeOffTheRealLineTest.cs new file mode 100644 index 000000000..c79b94a4c --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/APrincipalRangeOffTheRealLineTest.cs @@ -0,0 +1,127 @@ +// +// 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.Core; +using AngouriMath.Extensions; +using System.Linq; +using Xunit; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// ln takes its imaginary part in (-pi, pi] and arctan its real part in + /// (-pi/2, pi/2), so inverting either yields a preimage only for a value in that range; + /// and (e ^ a) ^ c is e ^ (a c) only where a stays in ln's strip. + /// https://github.com/asc-community/AngouriMath/issues/2713 + /// + [Trait("Area", "Algebra")] + public sealed class APrincipalRangeOffTheRealLineTest + { + private static Complex Value(Entity expression) + => Assert.IsAssignableFrom(expression.Evaled); + + private static bool IsNear(Entity left, Entity right) + { + var difference = Value(left) - Value(right); + return Value(((Entity)difference).Abs()) is Real { } modulus + && modulus.EDecimal.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-20")) < 0; + } + + /// The members of a family a condition does not rule out. + private static System.Collections.Generic.List Kept(Set solved, params int[] indices) + => solved.Members(indices).Select(member => member switch + { + Providedf(var root, var condition) => condition.Simplify() is Entity.Boolean(false) ? null : root, + _ => member + }).Where(member => member is not null).Select(member => member!).ToList(); + + [Theory] + [InlineData("e^(ln(x)^2)", "1")] + [InlineData("e^(ln(x)^2)", "e")] + [InlineData("e^(ln(x)^2)", "1/2")] + [InlineData("cos(ln(x)^2)", "1")] + [InlineData("sin(ln(x)^2)", "1/2")] + [InlineData("cosh(ln(x)^2)", "2")] + [InlineData("e^(ln(x + 1)^2)", "1")] + [InlineData("e^(ln(2x)^2)", "1")] + [InlineData("e^(log(2, x)^2)", "1")] + [InlineData("e^(arctan(x)^2)", "1")] + public void EveryMemberKeptIsARoot(string left, string right) + { + var solved = $"{left} = {right}".ToEntity().Solve("x"); + var indices = Enumerable.Range(-12, 25).ToArray(); + var kept = Kept(solved, indices); + Assert.NotEmpty(kept); + Assert.True(kept.Count < solved.Members(indices).Count, $"nothing ruled out of {solved.Stringize()}"); + foreach (var root in kept) + Assert.True(IsNear(left.ToEntity().Substitute("x", root), right.ToEntity()), + $"{root.Stringize()} is no root of {left} = {right}"); + } + + [Fact] + public void APreimageInsideTheStripIsKept() + { + // ln(x) = sqrt(2 pi i) is 1.77 + 1.77i, inside the strip, so e ^ sqrt(2 pi i) is a root. + var kept = Kept("e^(ln(x)^2) = 1".ToEntity().Solve("x"), -3, -2, -1, 0, 1, 2, 3); + Assert.Contains(kept, root => IsNear(root, "e^sqrt(2 * pi * i)".ToEntity())); + Assert.Contains(kept, root => IsNear(root, "1".ToEntity())); + } + + [Theory] + [InlineData("arctan", "2", "0.3", "1.2")] + [InlineData("arctan", "3", "0.2", "1.1")] + [InlineData("arcsin", "2", "0.3", "1.2")] + [InlineData("arccos", "4", "2.8", "1.2")] + [InlineData("arccotan", "2", "0.3", "1.2")] + public void AnInverseTrigonometricValueIsInItsRange(string function, string modulus, string outside, string inside) + { + var solved = Assert.IsType($"abs({function}(x)) = {modulus}".ToEntity().Solve("x")); + var member = Assert.Single(solved); + var (root, condition) = Assert.IsType(member) is var provided ? (provided.Expression, provided.Predicate) : default; + var parameter = root.Vars.Single(); + Assert.Equal(Entity.Boolean.False, condition.Substitute(parameter, outside.ToEntity()).Simplify()); + Assert.Equal(Entity.Boolean.True, condition.Substitute(parameter, inside.ToEntity()).Simplify()); + var at = root.Substitute(parameter, inside.ToEntity()); + Assert.True(IsNear($"abs({function}(x))".ToEntity().Substitute("x", at), modulus.ToEntity())); + } + + [Theory] + [InlineData("ln(x) = 2")] + [InlineData("ln(x)^2 = 4")] + [InlineData("arctan(x) = 1")] + [InlineData("arcsin(x) = 1/2")] + public void ANumericValueIsAnsweredAsBefore(string equation) + => Assert.All(Assert.IsType(equation.ToEntity().Solve("x")), root => Assert.IsNotType(root)); + + [Theory] + [InlineData("sqrt(e^(2x))")] + [InlineData("(e^(2x))^(1/4)")] + [InlineData("(e^(2x))^(-1/2)")] + [InlineData("(e^(i * x))^(1/2)")] + [InlineData("sqrt(e^(2x)) * e^x")] + [InlineData("e^x / sqrt(e^(2x))")] + [InlineData("sqrt(e^(2x)) - e^x")] + public void APowerOfAnExponentialKeepsItsValue(string expression) + { + var original = expression.ToEntity(); + var simplified = original.Simplify(); + foreach (var point in new[] { "2.1 - 1.7i", "4", "-0.3 + 2.5i" }) + Assert.True(IsNear(original.Substitute("x", point.ToEntity()), simplified.Substitute("x", point.ToEntity())), + $"{simplified.Stringize()} at {point}"); + } + + [Fact] + public void OverTheRealsAPowerOfAnExponentialStillMultipliesItsExponents() + { + using var _ = MathS.Settings.Codomain.Set(Domain.Real); + Assert.Equal("e^x".ToEntity().Simplify(), "sqrt(e^(2x))".ToEntity().Simplify()); + } + } +} diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/SolveEquationWithPiecewise.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/SolveEquationWithPiecewise.cs index c08f66721..4a77cd30f 100644 --- a/Sources/Tests/UnitTests/Algebra/SolveTest/SolveEquationWithPiecewise.cs +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/SolveEquationWithPiecewise.cs @@ -59,7 +59,9 @@ public void CheckIfRootWasObtained(string eq, //[InlineData("x + x / x = y", "{ -(1 + -y) provided not -(1 + -y) = 0}", Skip = "TODO")] // Partial function [InlineData("x ^ (a provided x > 0) = 8", "{ 8 ^ (1 / a) provided 8 ^ (1 / a) > 0 }")] // Power with possible domain restrictions [InlineData("(x ^ 2 provided x > 0) = 4", "{ 2 }")] // Quadratic with provided condition - [InlineData("log(b, x) = 2", "{ b ^ 2 }")] // TODO: Logarithm should preserve domain conditions + // ln(x) = 2 ln(b) has a root only where 2 ln(b) is in ln's strip: log(-1, 1) is 0, not 2. + // https://github.com/asc-community/AngouriMath/issues/2713 + [InlineData("log(b, x) = 2", "{ b ^ 2 provided -pi < 2 * Im(ln(b)) and 2 * Im(ln(b)) <= pi }")] // `(x! provided x > 7) = 6` answered `{ }` here, which was true only because the solver // claimed x! = 6 had no roots; it has 3. It is AnUnwrittenInverseIsNotTheEmptySetTest's now. public void SolveWithProvidedPreservesConditions(string equation, string result) diff --git a/Sources/Tests/UnitTests/Core/Transformations/MatchedRulesAgreeWithTheSwitchTest.cs b/Sources/Tests/UnitTests/Core/Transformations/MatchedRulesAgreeWithTheSwitchTest.cs index 3e92a7756..79e5028f1 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/MatchedRulesAgreeWithTheSwitchTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/MatchedRulesAgreeWithTheSwitchTest.cs @@ -696,7 +696,10 @@ public void AGuardOverTwoBindingsMatchesTheSwitch() [Theory] [InlineData("(x ^ 2) ^ 3", true)] // whole outer exponent, any base [InlineData("(x ^ 2) ^ (-1)", true)] // still whole - [InlineData("(2 ^ x) ^ (1/2)", true)] // base is a positive real + [InlineData("(2 ^ (1/3)) ^ x", true)] // a positive base to a real power + // A positive base alone is not enough: at x = 2.1 - 1.7i, sqrt(2 ^ x) is not 2 ^ (x/2). + // https://github.com/asc-community/AngouriMath/issues/2713 + [InlineData("(2 ^ x) ^ (1/2)", false)] [InlineData("(x ^ 2) ^ (1/2)", false)] // neither, and this one is #752 [InlineData("(x ^ 2) ^ (3/2)", false)] [InlineData("(x ^ y) ^ z", false)]