|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System.Numerics; |
| 9 | +using AngouriMath; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Common |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// A compiled power of real numbers is the real power where there is one, a positive base or a negative one to a |
| 17 | + /// whole power, and what evaluation gives at an infinity and at a zero base. <see cref="Complex.Pow(Complex, Complex)"/> |
| 18 | + /// made <c>x^2</c> at <c>+oo</c> <c>(NaN, NaN)</c>, <c>0^(-1)</c> <c>0</c> and <c>0^0</c> <c>1</c>. |
| 19 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2225">#2225</a> |
| 20 | + /// </summary> |
| 21 | + [Trait("Area", "Common")] |
| 22 | + public sealed class RealPowerCompilationTest |
| 23 | + { |
| 24 | + private static readonly string[] bases = { "0", "1", "2", "1/2", "3", "7/3", "-2", "-1/2", "+oo", "-oo" }; |
| 25 | + private static readonly string[] exponents = { "0", "1", "2", "3", "-1", "-2", "1/2", "5/2", "-1/3", "+oo", "-oo" }; |
| 26 | + |
| 27 | + private static Complex Numeric(string value) => ((Entity.Number.Complex)value.ToEntity().Evaled).ToNumerics(); |
| 28 | + |
| 29 | + private static bool Agree(Complex expected, Complex actual) |
| 30 | + => double.IsNaN(expected.Real) || double.IsNaN(expected.Imaginary) |
| 31 | + ? double.IsNaN(actual.Real) || double.IsNaN(actual.Imaginary) |
| 32 | + : double.IsInfinity(expected.Real) ? expected == actual |
| 33 | + : Complex.Abs(expected - actual) <= 1e-14 * System.Math.Max(1, Complex.Abs(expected)); |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void APowerOfRealsIsWhatEvaluationSays() |
| 37 | + { |
| 38 | + var fast = "x^y".ToEntity().Compile("x", "y"); |
| 39 | + var linq = "x^y".ToEntity().Compile<Complex, Complex, Complex>("x", "y"); |
| 40 | + var compared = 0; |
| 41 | + foreach (var a in bases) |
| 42 | + foreach (var b in exponents) |
| 43 | + { |
| 44 | + var (x, y) = (Numeric(a), Numeric(b)); |
| 45 | + // A negative base to a power that is not whole: evaluation takes the real root for an odd |
| 46 | + // denominator, which a double exponent cannot show, and the compilers the principal value. |
| 47 | + if (x.Real < 0 && System.Math.Floor(y.Real) != y.Real) |
| 48 | + continue; |
| 49 | + if ($"({a})^({b})".ToEntity().Evaled is not Entity.Number.Complex evaluated) |
| 50 | + continue; |
| 51 | + var expected = evaluated.ToNumerics(); |
| 52 | + Assert.True(Agree(expected, fast.Call(x, y)), $"({a})^({b}): {expected} evaluated, {fast.Call(x, y)} by FastExpression"); |
| 53 | + Assert.True(Agree(expected, linq(x, y)), $"({a})^({b}): {expected} evaluated, {linq(x, y)} by LINQ"); |
| 54 | + compared++; |
| 55 | + } |
| 56 | + Assert.True(compared > 80, $"only {compared} powers compared"); |
| 57 | + } |
| 58 | + |
| 59 | + [Theory] |
| 60 | + [InlineData("x^2", "+oo")] |
| 61 | + [InlineData("x^2", "-oo")] |
| 62 | + [InlineData("x^3", "-oo")] |
| 63 | + [InlineData("sqrt(x)", "+oo")] |
| 64 | + [InlineData("exp(-x)", "+oo")] |
| 65 | + [InlineData("exp(x)", "-oo")] |
| 66 | + public void AtAnInfinityTheValueIsEvaluations(string expression, string at) |
| 67 | + { |
| 68 | + var expected = ((Entity.Number.Complex)expression.ToEntity().Substitute("x", at).Evaled).ToNumerics(); |
| 69 | + Assert.False(double.IsNaN(expected.Real), $"{expression} at {at} evaluates to {expected}"); |
| 70 | + Assert.Equal(expected, expression.ToEntity().Compile("x").Call(Numeric(at))); |
| 71 | + Assert.Equal(expected, expression.ToEntity().Compile<Complex, Complex>("x")(Numeric(at))); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary>A complex power is the principal value it was.</summary> |
| 75 | + [Theory] |
| 76 | + [InlineData("i", "2", -1, 0)] |
| 77 | + [InlineData("-4", "1/2", 0, 2)] |
| 78 | + [InlineData("2", "i", 0.7692389013639721, 0.6389612763136348)] |
| 79 | + public void AComplexPowerIsThePrincipalValue(string a, string b, double real, double imaginary) |
| 80 | + { |
| 81 | + var value = "x^y".ToEntity().Compile("x", "y").Call(Numeric(a), Numeric(b)); |
| 82 | + Assert.True(Complex.Abs(value - new Complex(real, imaginary)) < 1e-14, $"({a})^({b}): {value}"); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments