diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 7f1a98f7d..31ee078bc 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -567,6 +567,35 @@ 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 power's domain condition: a negative base over the reals, a zero base off the real line + +**Silent: wrong conditions become right ones.** Over the reals (`DomainConditionIn(Domain.Real)`, `domain(..., RR)`) +a negative base has a real power only where the exponent is a whole number or a fraction of odd denominator below 10, +whose real root the library takes. The condition restricted the base only for a literal even root, so `x^pi`, `x^x` +and `(-1)^x` were taken as defined at points where their real reading is `NaN` +([#2739](https://github.com/asc-community/AngouriMath/issues/2739)). It now says `x >= 0` for any other number, and for +a symbolic exponent `e` names the fractions: `x >= 0 or 5 e in ZZ or 7 e in ZZ or 9 e in ZZ`. A literal odd denominator +from 11 up takes the principal root, which is not real, and `x^(1/11)` now needs `x >= 0` too. + +Off the real line, `0^w` is 0 where `Re(w) > 0`, and the condition of a power whose exponent is not a number said `w > 0`, +which has no value at a non-real `w`: at `0^(1 + i)`, which is 0, it was `NaN`. It now reads `Re(w) > 0`, and so does +the incomplete gamma function's at a lower limit of 0, and `0^x` simplifies to `0 provided Re(x) > 0` where it was +`0 provided x * (1 + 1 / sgn(x) ^ 2) > 0`, which had no value at 0 +([#2740](https://github.com/asc-community/AngouriMath/issues/2740)). Measured on `9b75a2678` and on this branch. + +| | Was (`9b75a2678`) | Now | +|---|---|---| +| `domain(x^pi, RR)` | `True` | `x >= 0` | +| `domain(x^sqrt(2), RR)` | `True` | `x >= 0` | +| `domain(x^(1/11), RR)` | `True` | `x >= 0` | +| `domain((-1)^x, RR)` | `True` | `5 * x in ZZ or 7 * x in ZZ or 9 * x in ZZ` | +| `domain(x^x, RR)` | `not x = 0 or x > 0` | `(x >= 0 or 5 * x in ZZ or 7 * x in ZZ or 9 * x in ZZ) and (not x = 0 or x > 0)` | +| `domain(x^(1/2), RR)`, `domain(x^(1/3), RR)`, `domain(e^x, RR)` | `x >= 0`, `True`, `True` | unchanged | +| `domain(0^x)` | `x > 0` | `Re(x) > 0` | +| `domain(x^y)` | `not x = 0 or y > 0` | `not x = 0 or Re(y) > 0` | +| `domain(gamma(x, 0))` | `x > 0` | `Re(x) > 0` | +| `(0^x).Simplify()` | `0 provided x * (1 + 1 / sgn(x) ^ 2) > 0` | `0 provided Re(x) > 0` | + ### 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 diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs index 2aa23ffbb..416a85025 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs @@ -412,18 +412,19 @@ public partial record Powf { // Power is undefined in two cases: // - 0^0 is indeterminate - // - 0^(negative) is undefined (division by zero) + // - 0^w with Re(w) <= 0 is undefined (division by zero, or no limit) // - // And over the reals there is a third, which is the whole of what `sqrt` is: an even - // root of a negative number is not real. `x ^ (1/2)` needs `x >= 0` and `x ^ (1/3)` - // needs nothing, so the exponent's *denominator* decides it and only a literal - // rational has one to read. A symbolic exponent is left with the condition above - // rather than given a guess: too strict is a wrong answer here, since this is what a - // rewrite consults before firing. - // https://github.com/asc-community/AngouriMath/issues/721 + // And over the reals there is a third, which is the whole of what `sqrt` is: a negative + // base to an exponent that is not a fraction of odd denominator below 10 is not real. + // `x ^ (1/2)` needs `x >= 0` and `x ^ (1/3)` needs nothing, the library taking the real + // root there. https://github.com/asc-community/AngouriMath/issues/721 + // + // A symbolic exponent asked nothing of the base, so x^pi and x^x were taken as real at a + // negative x; the condition says now which exponents take the real root. + // https://github.com/asc-community/AngouriMath/issues/2739 private protected override Entity IntrinsicCondition => - Codomain < Domain.Complex && IsEvenRoot - ? Base >= 0 & WhereZeroIsDefined + Codomain < Domain.Complex && WhereANegativeBaseIsReal is { } negativeBase + ? negativeBase & WhereZeroIsDefined : WhereZeroIsDefined; /// @@ -431,20 +432,45 @@ public partial record Powf /// w > 0 has no value, the complex numbers not being ordered, and made the /// whole condition NaN, so 0 b^i was NaN; its real part is read instead. /// #1865 + /// A symbolic exponent is asked for its real part too: w > 0 had no value at + /// 0^(1 + i), which is 0. Over the reals the exponent is real, and is its own real part. + /// #2740 + /// So is a real number, which is not wrapped: Re(-1/2) was 4% more allocation for a derivative. /// private Entity WhereZeroIsDefined => (Exponent is Complex ? Exponent : Exponent.Vars.Any() ? null : Exponent.Evaled) is Complex exponent and not Real ? exponent.RealPart.IsPositive ? Boolean.True : !Base.EqualTo(0) - : !Base.EqualTo(0) | Exponent > 0; + : !Base.EqualTo(0) | (Codomain < Domain.Complex || Exponent is Real ? Exponent > 0 : TheRealPartIsPositive(Exponent)); /// - /// Whether the exponent is a literal rational whose denominator is even — the case - /// where a negative base leaves the real line. + /// Re(w) > 0, simplified, kept per exponent. A simplification builds many powers + /// over one exponent and asks each for its condition, and a fresh Re is simplified + /// from the start every time: carried through a sum, each part asked whether it is real. + /// Unkept it was 9% more allocation for the benchmark's SimplifyHard. Kept under the + /// settings it was simplified in, as a node's own simplification is. /// - private bool IsEvenRoot - => Exponent is Rational and not Integer and Rational ratio - && ratio.ERational.Denominator.Remainder(2).IsZero; - + private static Entity TheRealPartIsPositive(Entity exponent) + => realPartIsPositive.GetValue(exponent, static _ => new()).Value + .GetValue(static e => (new Ref(e) > 0).InnerSimplified, exponent); + + [ConcurrentField] private static readonly System.Runtime.CompilerServices.ConditionalWeakTable>> realPartIsPositive = new(); + + /// + /// Over the reals, where a negative base has a real power: anywhere, for a whole exponent + /// or a fraction of odd denominator below 10, whose real root is taken; nowhere for any + /// other number; and for a symbolic exponent, where it is one of those fractions, which are + /// the numbers some multiple of 5, 7 or 9 makes whole. Null where the base is not restricted, + /// or is a number that is not negative. + /// + private Entity? WhereANegativeBaseIsReal + => !Base.Vars.Any() && Base.Evaled is Real { IsNegative: false } notNegative && !notNegative.IsNaN ? null : Exponent.Evaled switch + { + Integer => null, + Rational fraction when Numerics.IntervalEvaluation.TheRealRootIsTaken(fraction) is not null => null, + Complex => Base >= 0, + _ => Base >= 0 | (5 * Exponent).In(MathS.Sets.Z) | (7 * Exponent).In(MathS.Sets.Z) | (9 * Exponent).In(MathS.Sets.Z) + }; + /// /// Whether an exact to a decimal would be a power too /// large to build were the exponent the whole number just past it. @@ -494,9 +520,6 @@ private static bool TryPower(Matrix m, int exp, out Entity res) internal static bool MayHaveAnInverse(Matrix m) => m.IsSquare && !m.IsSingular; - // Re(x) = x/2 * (1 + 1/sgn(x)^2) - internal static Entity Re(Entity x) => x / 2 * (1 + 1 / new Signumf(x).Pow(2)); - /// /// The real odd root of a negative base, where the exponent is written as an exact fraction of odd denominator /// below 10 and evaluates to a decimal, as it does with downcasting off: 1/3 is then a quotient of the whole @@ -539,7 +562,9 @@ protected override Entity InnerSimplify(bool isExact) => ? c.RealPart.IsPositive ? 0 : MathS.NaN - : new Providedf(0, Re(x) > 0), + // The real part, as the condition reads it: x (1 + 1/sgn(x)^2) had no value at 0. + // https://github.com/asc-community/AngouriMath/issues/2740 + : new Providedf(0, new Ref(x) > 0), // An exact power too large to build stays a power. It is finite and not zero, and as // the decimal it was +oo or 0, which then fed exact arithmetic: 2^(10^9) - 2^(10^9) // was NaN, 0 * 2^(10^9) was NaN and 2^(10^9) > 2^(10^9 - 1) was False. Left written, @@ -2068,8 +2093,10 @@ public partial record IncompleteGammaf { // Defined but at z = 0 where Re a <= 0, where the integral diverges at its lower limit. // https://github.com/asc-community/AngouriMath/issues/2075 + // Re a > 0 off the real line, where a > 0 has no value. + // https://github.com/asc-community/AngouriMath/issues/2740 private protected override Entity IntrinsicCondition => - !LowerLimit.EqualTo(0) | Parameter > 0; + !LowerLimit.EqualTo(0) | (Codomain < Domain.Complex ? Parameter : new Ref(Parameter)) > 0; /// protected override Entity InnerSimplify(bool isExact) diff --git a/Sources/AngouriMath/Functions/PredicateEntailment.cs b/Sources/AngouriMath/Functions/PredicateEntailment.cs index 313df8db8..cd0ce2673 100644 --- a/Sources/AngouriMath/Functions/PredicateEntailment.cs +++ b/Sources/AngouriMath/Functions/PredicateEntailment.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -225,7 +225,7 @@ private static Entity Without(Entity expression, Entity predicate) /// (): the condition excludes the zeros of /// where a conjunct not d = 0 or d > 0 has d /// vanishing with e, or a disjunction has every side excluding them -- - /// x^x's not x = 0 or x > 0. + /// x^x's not x = 0 or Re(x) > 0. /// private static bool IsUndefinedAtTheZerosOf(Entity expression, Entity e) => ExcludesTheZerosOf(expression.DomainConditionIn(MathS.Settings.Codomain), e); @@ -244,7 +244,7 @@ private static bool ExcludesTheZerosOf(Entity condition, Entity e) /// /// Whether is zero wherever is: d is /// e, a positive power of it, a product with such a factor, a sine, tangent, - /// arcsine or arctangent of such a thing, or -- for a variable e -- a polynomial + /// arcsine, arctangent or real part of such a thing, or -- for a variable e -- a polynomial /// in it without a constant term. /// private static bool VanishesWith(Entity d, Entity e) @@ -255,6 +255,10 @@ private static bool VanishesWith(Entity d, Entity e) // at zero. if (d is Sinf or Tanf or Arcsinf or Arctanf) return VanishesWith(d.DirectChildren[0], e); + // And a real part, which a power's condition reads off the real line: `x^x`'s is + // `not x = 0 or Re(x) > 0`. https://github.com/asc-community/AngouriMath/issues/2740 + if (d is Ref(var argument)) + return VanishesWith(argument, e); if (d is Powf(var @base, var power) && power.Evaled is Real { IsPositive: true }) return VanishesWith(@base, e); if (d is Mulf) diff --git a/Sources/Tests/Harnesses/CanonCheck/canoncheck-baseline.tsv b/Sources/Tests/Harnesses/CanonCheck/canoncheck-baseline.tsv index 0e5bc545a..abbaec1c3 100644 --- a/Sources/Tests/Harnesses/CanonCheck/canoncheck-baseline.tsv +++ b/Sources/Tests/Harnesses/CanonCheck/canoncheck-baseline.tsv @@ -12,24 +12,12 @@ CanonicalOrderExact agreement x * x * x x ^ 3 CanonicalOrderExact agreement x + x 2 * x CanonicalOrderExact agreement x ^ 0 1 CanonicalOrderExact idempotence (-x) ^ (-1) (applied twice) -CanonicalOrderExact idempotence (0 ^ y) ^ 2 (applied twice) CanonicalOrderExact idempotence (1/2 / a) ^ (-1) (applied twice) CanonicalOrderExact idempotence (2 / y) ^ (-1) (applied twice) CanonicalOrderExact idempotence (a * (-1)) ^ (-1) (applied twice) CanonicalOrderExact idempotence (x * 1/2) ^ (-1) (applied twice) CanonicalOrderExact idempotence (y * a) ^ (-1) (applied twice) -CanonicalOrderExact idempotence -0 ^ y (applied twice) -CanonicalOrderExact idempotence 0 ^ a (applied twice) -CanonicalOrderExact idempotence 0 ^ x (applied twice) -CanonicalOrderExact idempotence 0 ^ y (applied twice) -CanonicalOrderExact idempotence abs(0 ^ y) (applied twice) -CanonicalOrderExact idempotence cos(0 ^ y) (applied twice) -CanonicalOrderExact idempotence e ^ 0 ^ y (applied twice) -CanonicalOrderExact idempotence ln(0 ^ y) (applied twice) CanonicalOrderExact idempotence ln(sqrt(-1)) (applied twice) -CanonicalOrderExact idempotence sgn(0 ^ y) (applied twice) -CanonicalOrderExact idempotence sin(0 ^ y) (applied twice) -CanonicalOrderExact idempotence sqrt(0 ^ y) (applied twice) InnerSimplified agreement (x * y) * a x * (y * a) InnerSimplified agreement (x + y) + a x + (y + a) InnerSimplified agreement (x ^ 2) ^ 3 x ^ 6 diff --git a/Sources/Tests/UnitTests/Core/DomainConditionInAReadingTest.cs b/Sources/Tests/UnitTests/Core/DomainConditionInAReadingTest.cs index ff82985ec..8f309545f 100644 --- a/Sources/Tests/UnitTests/Core/DomainConditionInAReadingTest.cs +++ b/Sources/Tests/UnitTests/Core/DomainConditionInAReadingTest.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2019-2026 Angouri. // AngouriMath is licensed under MIT. // Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. @@ -51,16 +51,89 @@ public void AReadingIsAnswered(string source, string overComplex, string overRea } /// - /// A symbolic exponent is left with the condition it had rather than guessed at: too - /// strict is a wrong answer here, since this is what a rewrite consults before firing. + /// Over the reals a negative base has a real power only for a whole exponent or a fraction + /// of odd denominator below 10, whose real root the library takes; a symbolic or irrational + /// exponent asked nothing of the base, so x^pi was taken as defined at -1. + /// https://github.com/asc-community/AngouriMath/issues/2739 + /// + [Theory] + // (-1)^pi = -0.9027 - 0.4303i, (-2)^sqrt(2) = -0.7096 - 2.5689i: not real. + [InlineData("x ^ pi", "-1", false)] + [InlineData("x ^ pi", "2", true)] + [InlineData("x ^ pi", "0", true)] + [InlineData("x ^ sqrt(2)", "-2", false)] + [InlineData("x ^ sqrt(2)", "2", true)] + // (-1/2)^(-1/2) = -1.4142i; (-1)^(-1) = -1; (-1/3)^(-1/3) = -3^(1/3), the real root. + [InlineData("x ^ x", "-1/2", false)] + [InlineData("x ^ x", "-1", true)] + [InlineData("x ^ x", "-1/3", true)] + [InlineData("x ^ x", "1/2", true)] + [InlineData("x ^ x", "0", false)] + [InlineData("(-1) ^ x", "1/2", false)] + [InlineData("(-1) ^ x", "1/3", true)] + [InlineData("(-1) ^ x", "2/7", true)] + [InlineData("(-1) ^ x", "4/9", true)] + [InlineData("(-1) ^ x", "2", true)] + [InlineData("(-1) ^ x", "1/11", false)] + [InlineData("(-1) ^ x", "1/15", false)] + [InlineData("(-1) ^ x", "pi", false)] + [InlineData("cos(x) ^ x", "pi", false)] + [InlineData("cos(x) ^ x", "2 * pi", true)] + // A literal odd denominator from 11 up takes the principal root, which is not real. + [InlineData("x ^ (1/11)", "-1", false)] + [InlineData("x ^ (1/11)", "1", true)] + [InlineData("x ^ (3/5)", "-1", true)] + [InlineData("e ^ x", "-1", true)] + public void Issue2739_ANegativeBaseIsRealOnlyForAnOddRoot(string source, string point, bool defined) + { + var condition = source.ToEntity().DomainConditionIn(Domain.Real); + Assert.Equal(defined, (bool)condition.Substitute("x", point.ToEntity()).EvalBoolean()); + // And the real reading's value agrees: a number where the condition holds, none where it does not. + var value = $"domain({source}, RR)".ToEntity().Substitute("x", point.ToEntity()).EvalNumerical(); + Assert.Equal(defined, !value.IsNaN); + } + + /// + /// A symbolic exponent is read in both: over the reals it restricts a negative base, and + /// over the complex numbers it does not. /// [Fact] - public void ASymbolicExponentIsNotGuessedAt() + public void ASymbolicExponentIsReadInEach() { var expr = "x ^ y".ToEntity(); - Assert.Equal( - expr.DomainConditionIn(Domain.Complex).Stringize(), - expr.DomainConditionIn(Domain.Real).Stringize()); + var overReal = expr.DomainConditionIn(Domain.Real); + var overComplex = expr.DomainConditionIn(Domain.Complex); + Entity At(Entity condition, string x, string y) => condition.Substitute("x", x.ToEntity()).Substitute("y", y.ToEntity()).Evaled; + Assert.Equal(Entity.Boolean.True, At(overReal, "-8", "1/3")); + Assert.Equal(Entity.Boolean.False, At(overReal, "-8", "1/2")); + Assert.Equal(Entity.Boolean.True, At(overComplex, "-8", "1/2")); + Assert.Equal(Entity.Boolean.True, At(overReal, "8", "1/2")); + } + + /// + /// 0^w is 0 where Re(w) > 0, and the condition of a power whose exponent is + /// not a number said w > 0, which has no value off the real line. + /// https://github.com/asc-community/AngouriMath/issues/2740 + /// + [Theory] + [InlineData("0 ^ x", "1 + i", true)] + [InlineData("0 ^ x", "2", true)] + [InlineData("0 ^ x", "i", false)] + [InlineData("0 ^ x", "-1 + i", false)] + [InlineData("0 ^ x", "0", false)] + [InlineData("0 ^ x", "-1", false)] + [InlineData("(x - 1) ^ (x * i + 1)", "1", true)] + [InlineData("x ^ (x + i)", "0", false)] + [InlineData("x ^ (x + 2 + i)", "0", true)] + [InlineData("gamma(x, 0)", "1 + i", true)] + [InlineData("gamma(x, 0)", "2", true)] + public void Issue2740_AZeroBaseIsDecidedForANonRealExponent(string source, string point, bool defined) + { + var expression = source.ToEntity(); + Assert.Equal(defined ? Entity.Boolean.True : Entity.Boolean.False, + expression.DomainCondition.Substitute("x", point.ToEntity()).Evaled); + var value = expression.Substitute("x", point.ToEntity()).EvalNumerical(); + Assert.Equal(defined, !value.IsNaN); } ///