diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index eca3201d1..f8cb47f26 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -59,6 +59,22 @@ only in decimals, is answered as before ([#2607](https://github.com/asc-communit | `2^x + 2^y - 8`, `x - y` | `null` | `[[2 + 2 * pi * i * n_1 / ln(2), …]]` | | `x - y`, `ln(x) + ln(y) - 1` | `[[sqrt(e), sqrt(e)]]` | unchanged | +### 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: +`(-8)^(1/3)` is `-2`. With downcasting off, the parser keeps `1/3` as a quotient of the decimals 1 and 3, which +evaluates to `0.333…`. The power then took the principal root, `1 + 1.732i`. A power whose exponent is written as an +exact fraction with an odd denominator below 10 now takes the real root under either setting, as the precise evaluator +already did. With downcasting on, nothing changes ([#2622](https://github.com/asc-community/AngouriMath/issues/2622)). + +| `.Evaled`, downcasting off | Was (2.10.0) | Now | +|---|---|---| +| `(-8)^(1/3)` | `1.000… + 1.732…i` | `-2.000…` | +| `(-8)^(2/3)` | `-2.000… + 3.464…i` | `4.000…` | +| `(-32)^(1/5)` | `1.618… + 1.175…i` | `-2.000…` | +| `(-30.5)^(1/3)` | `1.562… + 2.705…i` | `-3.124…` | +| `(-30.5)^(1/2)` | `5.522…i` | unchanged | + ### A singular matrix is not inverted because its determinant is not written as 0 **Not silent: a wrong answer is now no answer.** `Matrix.Inverse`, `Matrix.Pow` with a negative exponent, `M^(-1)` and 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 c186e8293..5a7466d86 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 @@ -482,8 +482,28 @@ internal static bool MayHaveAnInverse(Matrix m) // 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 + /// decimals 1 and 3, and is 0.333..., and (-8)^(1/3) was the principal 1 + 1.732i, where it is -2 with the + /// downcasting on. https://github.com/asc-community/AngouriMath/issues/2622 + /// + private static Entity? AnOddRootOfANegative(Entity @base, Entity exponent) + => exponent switch + { + Rational r => r, + Divf(Real { EDecimal: var above }, Real { EDecimal: var below }) when above.IsInteger() && below.IsInteger() && !below.IsZero + => Rational.Create(PeterO.Numbers.ERational.Create(above.ToEInteger(), below.ToEInteger())), + _ => null + } + is { } fraction && Numerics.IntervalEvaluation.TheRealRootIsTaken(fraction) is { } numerator + && exponent.Evaled is not Rational && @base.Evaled is Real { EDecimal.IsNegative: true } negative + && Number.Pow(-negative, fraction) is var modulus + ? (numerator.IsEven ? modulus : -modulus) : null; + /// protected override Entity InnerSimplify(bool isExact) => + !isExact && AnOddRootOfANegative(Base, Exponent) is { } oddRoot ? oddRoot : ExpandOnTwoArguments(Base, Exponent, (a, b) => (a, b) switch { diff --git a/Sources/Tests/UnitTests/Core/AnOddRootIsRealWhateverTheDowncastingTest.cs b/Sources/Tests/UnitTests/Core/AnOddRootIsRealWhateverTheDowncastingTest.cs new file mode 100644 index 000000000..c29b1e128 --- /dev/null +++ b/Sources/Tests/UnitTests/Core/AnOddRootIsRealWhateverTheDowncastingTest.cs @@ -0,0 +1,53 @@ +// +// 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.Core +{ + /// + /// An odd root of a negative number is the real one, (-8)^(1/3) = -2. With downcasting off the parser keeps + /// 1/3 as a quotient, which evaluates to the decimal 0.333…, and the power took the principal root: + /// 1 + 1.732i. The value no longer depends on the setting. + /// https://github.com/asc-community/AngouriMath/issues/2622 + /// + [Trait("Area", "Core")] + public sealed class AnOddRootIsRealWhateverTheDowncastingTest + { + [Theory] + [InlineData("(-8)^(1/3)", -2.0, 0.0)] + [InlineData("(-8)^(2/3)", 4.0, 0.0)] + [InlineData("(-8)^(-1/3)", -0.5, 0.0)] + [InlineData("(-32)^(1/5)", -2.0, 0.0)] + [InlineData("(-32)^(3/5)", -8.0, 0.0)] + [InlineData("(-30.5)^(1/3)", -3.1243998847631218, 0.0)] + [InlineData("(-2)^(1/7)", -1.1040895136738123, 0.0)] + // Halves are principal either way, a control. + [InlineData("(-30.5)^(1/2)", 0.0, 5.522680508593630)] + [InlineData("(-4)^(3/2)", 0.0, -8.0)] + // A positive base, and an even denominator, are unchanged. + [InlineData("8^(1/3)", 2.0, 0.0)] + [InlineData("(-16)^(1/4)", 1.4142135623730951, 1.4142135623730951)] + public void IsTheSameWithTheDowncastingOnAndOff(string power, double re, double im) + { + foreach (var downcasting in new[] { true, false }) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting); + var entity = power.ToEntity(); + foreach (var value in new[] { entity.Evaled, entity.EvalNumerical() }) + { + var number = Assert.IsAssignableFrom(value); + Assert.True(System.Math.Abs(number.RealPart.EDecimal.ToDouble() - re) < 1e-12 + && System.Math.Abs(number.ImaginaryPart.EDecimal.ToDouble() - im) < 1e-12, + $"{power} with downcasting {(downcasting ? "on" : "off")} is {value}"); + } + } + } + } +}