Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Complex, Complex>("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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion Sources/AngouriMath/Functions/Compilation/RealProducts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// Website: https://am.angouri.org.
//

using System;
using NumericsComplex = System.Numerics.Complex;

namespace AngouriMath.Core.Compilation
{
/// <summary>
/// The product and the quotient of two numbers carried as <see cref="System.Numerics.Complex"/>, real where both
/// The product, the quotient and the power of two numbers carried as <see cref="System.Numerics.Complex"/>, real where both
/// are real. <see cref="System.Numerics.Complex"/> forms the product's imaginary part as <c>a.Re b.Im + a.Im b.Re</c>,
/// which is <c>∞ · 0</c>, <c>NaN</c>, where a real factor is infinite, so a compiled <c>-x</c> at <c>+oo</c> was
/// <c>(-∞, NaN)</c>, a value with no imaginary part at all, where evaluation gives <c>-oo</c>.
Expand All @@ -27,5 +28,29 @@ public static NumericsComplex Product(NumericsComplex a, NumericsComplex b)
/// </summary>
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;

/// <summary>
/// The power, real where both are real and it is: a positive base, or a negative one to a whole power.
/// <see cref="System.Numerics.Complex.Pow(System.Numerics.Complex, System.Numerics.Complex)"/> takes
/// <c>e^(b ln a)</c>, which at an infinite base brings in <c>∞ · 0</c>, so <c>x^2</c> at <c>+oo</c> was
/// <c>(NaN, NaN)</c>; and it gave <c>0</c> for <c>0^(-1)</c> and <c>1</c> for <c>0^0</c>, where evaluation has no value.
/// Anything else is left to it. https://github.com/asc-community/AngouriMath/issues/2225
/// </summary>
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);
}
}
}
85 changes: 85 additions & 0 deletions Sources/Tests/UnitTests/Common/RealPowerCompilationTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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. <see cref="Complex.Pow(Complex, Complex)"/>
/// 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>.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2225">#2225</a>
/// </summary>
[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<Complex, Complex, Complex>("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<Complex, Complex>("x")(Numeric(at)));
}

/// <summary>A complex power is the principal value it was.</summary>
[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}");
}
}
}
Loading