Skip to content

Commit a4b7b74

Browse files
A compiled power of real numbers is the real power, at an infinity and at zero (#2231)
In FastExpression and in the LINQ compiler over Complex, a power was Complex.Pow, which takes e^(b ln a). At an infinite base that brings in inf * 0, so x^2, x^3, sqrt(x) and exp(-x) at an infinity were (NaN, NaN), where evaluation gives +oo, -oo or 0. And it gave 0 for 0^(-1) and 1 for 0^0, where evaluation has no value for either. RealProducts.Power is now the power in both compilers, through FastExpression's CALL_POW and MathAllMethods.Pow over Complex: - a real power where there is one, a positive base or a negative one to a whole power, by Math.Pow, and by Math.Sqrt for an exponent of 1/2; - 0 to a positive power is 0, and 0 to any other power has no value; - NaN where either side is NaN; - Complex.Pow for anything else, as before. That includes a negative base to a power that is not whole, whose principal value differs from evaluation's real root for an odd denominator. A double exponent cannot show that denominator. At finite positive points nothing moves. Complex.Pow already equals Math.Pow at all 100000 random points of (0, 100) x (-5, 5) measured. RealPowerCompilationTest compares both compilers with evaluation over 10 real bases by 11 exponents, among them 0, the infinities and fractions. It also covers six expressions at an infinity and three complex powers that stay principal. 7 of its 10 cases fail on master. Fixes #2225. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1c9dbce commit a4b7b74

7 files changed

Lines changed: 133 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,22 @@ then had no truth value for. A product or a quotient of two real numbers is now
395395
| `"-x".ToEntity().Compile("x").Call(double.PositiveInfinity)` | `(-Infinity, NaN)` | `(-Infinity, 0)` |
396396
| `"x / 2".ToEntity().Compile<Complex, Complex>("x")(double.PositiveInfinity)` | `(Infinity, NaN)` | `(Infinity, 0)` |
397397

398+
### A compiled power of real numbers is the real power, at an infinity and at zero
399+
400+
**Silent: a compiled power over `Complex` is what evaluation gives where it had no value or the wrong one.** In
401+
FastExpression and in the LINQ compiler over `System.Numerics.Complex`, a power was `Complex.Pow`, which takes
402+
`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
403+
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
404+
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
405+
anything else is `Complex.Pow` as before. At finite positive points the two agree to the last bit
406+
([#2225](https://github.com/asc-community/AngouriMath/issues/2225)).
407+
408+
| Input | Was (2.6.0) | Now |
409+
|---|---|---|
410+
| `"x^2".ToEntity().Compile("x").Call(double.PositiveInfinity)` | `(NaN, NaN)` | `(Infinity, 0)` |
411+
| `"x^(-1)".ToEntity().Compile("x").Call(0)` | `(0, 0)` | `(NaN, 0)` |
412+
| `"x^0".ToEntity().Compile("x").Call(0)` | `(1, 0)` | `(NaN, 0)` |
413+
398414
### A compiled statement with no truth value has none
399415

400416
**Silent in part: a compiled comparison has the truth value evaluation gives it.** A comparison over `Complex` was refused

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
919919

920920
[Tests/UnitTests/Common/RealProductCompilationTest.cs]
921921
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
922+
923+
[Tests/UnitTests/Common/RealPowerCompilationTest.cs]
924+
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

‎Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
227227
break;
228228
}
229229
case InstructionType.CALL_POW:
230-
stack.Push(System.Numerics.Complex.Pow(stack.Pop(), stack.Pop()));
230+
stack.Push(AngouriMath.Core.Compilation.RealProducts.Power(stack.Pop(), stack.Pop()));
231231
break;
232232
case InstructionType.CALL_SIN:
233233
stack.Push(System.Numerics.Complex.Sin(stack.Pop()));

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static BigInteger Log(BigInteger a, BigInteger b)
3737

3838

3939
public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b)
40-
=> System.Numerics.Complex.Pow(a, b);
40+
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
4141
public static double Pow(double a, double b)
4242
=> Math.Pow(a, b);
4343
public static float Pow(float a, float b)

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace AngouriMath.Core.Compilation.IntoLinq
6262

6363

6464
public static System.Numerics.Complex Pow(System.Numerics.Complex a, System.Numerics.Complex b)
65-
=> System.Numerics.Complex.Pow(a, b);
65+
=> AngouriMath.Core.Compilation.RealProducts.Power(a, b);
6666
public static double Pow(double a, double b)
6767
=> Math.Pow(a, b);
6868
public static float Pow(float a, float b)

‎Sources/AngouriMath/Functions/Compilation/RealProducts.cs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
// Website: https://am.angouri.org.
66
//
77

8+
using System;
89
using NumericsComplex = System.Numerics.Complex;
910

1011
namespace AngouriMath.Core.Compilation
1112
{
1213
/// <summary>
13-
/// The product and the quotient of two numbers carried as <see cref="System.Numerics.Complex"/>, real where both
14+
/// The product, the quotient and the power of two numbers carried as <see cref="System.Numerics.Complex"/>, real where both
1415
/// are real. <see cref="System.Numerics.Complex"/> forms the product's imaginary part as <c>a.Re b.Im + a.Im b.Re</c>,
1516
/// 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
1617
/// <c>(-∞, NaN)</c>, a value with no imaginary part at all, where evaluation gives <c>-oo</c>.
@@ -27,5 +28,29 @@ public static NumericsComplex Product(NumericsComplex a, NumericsComplex b)
2728
/// </summary>
2829
public static NumericsComplex Quotient(NumericsComplex a, NumericsComplex b)
2930
=> a.Imaginary == 0 && b.Imaginary == 0 && b.Real != 0 ? new NumericsComplex(a.Real / b.Real, 0) : a / b;
31+
32+
/// <summary>
33+
/// The power, real where both are real and it is: a positive base, or a negative one to a whole power.
34+
/// <see cref="System.Numerics.Complex.Pow(System.Numerics.Complex, System.Numerics.Complex)"/> takes
35+
/// <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
36+
/// <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.
37+
/// Anything else is left to it. https://github.com/asc-community/AngouriMath/issues/2225
38+
/// </summary>
39+
public static NumericsComplex Power(NumericsComplex a, NumericsComplex b)
40+
{
41+
if (a.Imaginary != 0 || b.Imaginary != 0)
42+
return NumericsComplex.Pow(a, b);
43+
var (x, y) = (a.Real, b.Real);
44+
if (double.IsNaN(x) || double.IsNaN(y))
45+
return new NumericsComplex(double.NaN, 0);
46+
// 0^y is 0 for a positive y and has no value otherwise, 0^0 included, as evaluation has it.
47+
if (x == 0)
48+
return y > 0 ? NumericsComplex.Zero : new NumericsComplex(double.NaN, 0);
49+
if (x > 0)
50+
return new NumericsComplex(y == 0.5 ? Math.Sqrt(x) : Math.Pow(x, y), 0);
51+
if (!double.IsInfinity(y) && Math.Floor(y) == y)
52+
return new NumericsComplex(Math.Pow(x, y), 0);
53+
return NumericsComplex.Pow(a, b);
54+
}
3055
}
3156
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)