diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4b50057c0..b05ac8721 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -395,6 +395,22 @@ then had no truth value for. A product or a quotient of two real numbers is now | `"-x".ToEntity().Compile("x").Call(double.PositiveInfinity)` | `(-Infinity, NaN)` | `(-Infinity, 0)` | | `"x / 2".ToEntity().Compile("x")(double.PositiveInfinity)` | `(Infinity, NaN)` | `(Infinity, 0)` | +### A compiled power of real numbers is the real power, at an infinity and at zero + +**Silent: a compiled power over `Complex` is what evaluation gives where it had no value or the wrong one.** In +FastExpression and in the LINQ compiler over `System.Numerics.Complex`, a power was `Complex.Pow`, which takes +`e^(b ln a)`. At an infinite base that brings in `∞ · 0`, so `x^2`, `sqrt(x)` and `exp(-x)` at `+oo` had no value, and +it gave `0` for `0^(-1)` and `1` for `0^0`, where evaluation has none. A power of two reals is now `Math.Pow` where +it is real, a positive base or a negative one to a whole power; `0` to a power that is not positive has no value; and +anything else is `Complex.Pow` as before. At finite positive points the two agree to the last bit +([#2225](https://github.com/asc-community/AngouriMath/issues/2225)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"x^2".ToEntity().Compile("x").Call(double.PositiveInfinity)` | `(NaN, NaN)` | `(Infinity, 0)` | +| `"x^(-1)".ToEntity().Compile("x").Call(0)` | `(0, 0)` | `(NaN, 0)` | +| `"x^0".ToEntity().Compile("x").Call(0)` | `(1, 0)` | `(NaN, 0)` | + ### A compiled statement with no truth value has none **Silent in part: a compiled comparison has the truth value evaluation gives it.** A comparison over `Complex` was refused diff --git a/Sources/.editorconfig b/Sources/.editorconfig index be1d1cfff..bea86d119 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -919,3 +919,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Common/RealProductCompilationTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[Tests/UnitTests/Common/RealPowerCompilationTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs index 7e835d01b..080c78b10 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs @@ -227,7 +227,7 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value break; } case InstructionType.CALL_POW: - stack.Push(System.Numerics.Complex.Pow(stack.Pop(), stack.Pop())); + stack.Push(AngouriMath.Core.Compilation.RealProducts.Power(stack.Pop(), stack.Pop())); break; case InstructionType.CALL_SIN: stack.Push(System.Numerics.Complex.Sin(stack.Pop())); diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs index 4af291eac..18f2c70e5 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs @@ -37,7 +37,7 @@ public static BigInteger Log(BigInteger a, BigInteger b) public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b) - => System.Numerics.Complex.Pow(a, b); + => AngouriMath.Core.Compilation.RealProducts.Power(a, b); public static double Pow(double a, double b) => Math.Pow(a, b); public static float Pow(float a, float b) diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt index d0022b23c..de6c9d098 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt @@ -62,7 +62,7 @@ namespace AngouriMath.Core.Compilation.IntoLinq public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b) - => System.Numerics.Complex.Pow(a, b); + => AngouriMath.Core.Compilation.RealProducts.Power(a, b); public static double Pow(double a, double b) => Math.Pow(a, b); public static float Pow(float a, float b) diff --git a/Sources/AngouriMath/Functions/Compilation/RealProducts.cs b/Sources/AngouriMath/Functions/Compilation/RealProducts.cs index 5909c7d8d..bf1342d0d 100644 --- a/Sources/AngouriMath/Functions/Compilation/RealProducts.cs +++ b/Sources/AngouriMath/Functions/Compilation/RealProducts.cs @@ -5,12 +5,13 @@ // Website: https://am.angouri.org. // +using System; using NumericsComplex = System.Numerics.Complex; namespace AngouriMath.Core.Compilation { /// - /// The product and the quotient of two numbers carried as , real where both + /// The product, the quotient and the power of two numbers carried as , real where both /// are real. forms the product's imaginary part as a.Re b.Im + a.Im b.Re, /// which is ∞ · 0, NaN, where a real factor is infinite, so a compiled -x at +oo was /// (-∞, NaN), a value with no imaginary part at all, where evaluation gives -oo. @@ -27,5 +28,29 @@ public static NumericsComplex Product(NumericsComplex a, NumericsComplex b) /// public static NumericsComplex Quotient(NumericsComplex a, NumericsComplex b) => a.Imaginary == 0 && b.Imaginary == 0 && b.Real != 0 ? new NumericsComplex(a.Real / b.Real, 0) : a / b; + + /// + /// The power, real where both are real and it is: a positive base, or a negative one to a whole power. + /// takes + /// e^(b ln a), which at an infinite base brings in ∞ · 0, so x^2 at +oo was + /// (NaN, NaN); and it gave 0 for 0^(-1) and 1 for 0^0, where evaluation has no value. + /// Anything else is left to it. https://github.com/asc-community/AngouriMath/issues/2225 + /// + public static NumericsComplex Power(NumericsComplex a, NumericsComplex b) + { + if (a.Imaginary != 0 || b.Imaginary != 0) + return NumericsComplex.Pow(a, b); + var (x, y) = (a.Real, b.Real); + if (double.IsNaN(x) || double.IsNaN(y)) + return new NumericsComplex(double.NaN, 0); + // 0^y is 0 for a positive y and has no value otherwise, 0^0 included, as evaluation has it. + if (x == 0) + return y > 0 ? NumericsComplex.Zero : new NumericsComplex(double.NaN, 0); + if (x > 0) + return new NumericsComplex(y == 0.5 ? Math.Sqrt(x) : Math.Pow(x, y), 0); + if (!double.IsInfinity(y) && Math.Floor(y) == y) + return new NumericsComplex(Math.Pow(x, y), 0); + return NumericsComplex.Pow(a, b); + } } } diff --git a/Sources/Tests/UnitTests/Common/RealPowerCompilationTest.cs b/Sources/Tests/UnitTests/Common/RealPowerCompilationTest.cs new file mode 100644 index 000000000..182ad6c81 --- /dev/null +++ b/Sources/Tests/UnitTests/Common/RealPowerCompilationTest.cs @@ -0,0 +1,85 @@ +// +// 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.Numerics; +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Common +{ + /// + /// A compiled power of real numbers is the real power where there is one, a positive base or a negative one to a + /// whole power, and what evaluation gives at an infinity and at a zero base. + /// made x^2 at +oo (NaN, NaN), 0^(-1) 0 and 0^0 1. + /// #2225 + /// + [Trait("Area", "Common")] + public sealed class RealPowerCompilationTest + { + private static readonly string[] bases = { "0", "1", "2", "1/2", "3", "7/3", "-2", "-1/2", "+oo", "-oo" }; + private static readonly string[] exponents = { "0", "1", "2", "3", "-1", "-2", "1/2", "5/2", "-1/3", "+oo", "-oo" }; + + private static Complex Numeric(string value) => ((Entity.Number.Complex)value.ToEntity().Evaled).ToNumerics(); + + private static bool Agree(Complex expected, Complex actual) + => double.IsNaN(expected.Real) || double.IsNaN(expected.Imaginary) + ? double.IsNaN(actual.Real) || double.IsNaN(actual.Imaginary) + : double.IsInfinity(expected.Real) ? expected == actual + : Complex.Abs(expected - actual) <= 1e-14 * System.Math.Max(1, Complex.Abs(expected)); + + [Fact] + public void APowerOfRealsIsWhatEvaluationSays() + { + var fast = "x^y".ToEntity().Compile("x", "y"); + var linq = "x^y".ToEntity().Compile("x", "y"); + var compared = 0; + foreach (var a in bases) + foreach (var b in exponents) + { + var (x, y) = (Numeric(a), Numeric(b)); + // A negative base to a power that is not whole: evaluation takes the real root for an odd + // denominator, which a double exponent cannot show, and the compilers the principal value. + if (x.Real < 0 && System.Math.Floor(y.Real) != y.Real) + continue; + if ($"({a})^({b})".ToEntity().Evaled is not Entity.Number.Complex evaluated) + continue; + var expected = evaluated.ToNumerics(); + Assert.True(Agree(expected, fast.Call(x, y)), $"({a})^({b}): {expected} evaluated, {fast.Call(x, y)} by FastExpression"); + Assert.True(Agree(expected, linq(x, y)), $"({a})^({b}): {expected} evaluated, {linq(x, y)} by LINQ"); + compared++; + } + Assert.True(compared > 80, $"only {compared} powers compared"); + } + + [Theory] + [InlineData("x^2", "+oo")] + [InlineData("x^2", "-oo")] + [InlineData("x^3", "-oo")] + [InlineData("sqrt(x)", "+oo")] + [InlineData("exp(-x)", "+oo")] + [InlineData("exp(x)", "-oo")] + public void AtAnInfinityTheValueIsEvaluations(string expression, string at) + { + var expected = ((Entity.Number.Complex)expression.ToEntity().Substitute("x", at).Evaled).ToNumerics(); + Assert.False(double.IsNaN(expected.Real), $"{expression} at {at} evaluates to {expected}"); + Assert.Equal(expected, expression.ToEntity().Compile("x").Call(Numeric(at))); + Assert.Equal(expected, expression.ToEntity().Compile("x")(Numeric(at))); + } + + /// A complex power is the principal value it was. + [Theory] + [InlineData("i", "2", -1, 0)] + [InlineData("-4", "1/2", 0, 2)] + [InlineData("2", "i", 0.7692389013639721, 0.6389612763136348)] + public void AComplexPowerIsThePrincipalValue(string a, string b, double real, double imaginary) + { + var value = "x^y".ToEntity().Compile("x", "y").Call(Numeric(a), Numeric(b)); + Assert.True(Complex.Abs(value - new Complex(real, imaginary)) < 1e-14, $"({a})^({b}): {value}"); + } + } +}