diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index baf38c55a..0b5838a6b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -381,6 +381,20 @@ one where both sides are real, and `NaN` where either is not |---|---|---| | `"x mod 3".ToEntity().Compile("x")(7)` | `UncompilableNodeException` | `(1, 0)` | +### A compiled product of real numbers is real at an infinity + +**Silent: a compiled product or quotient with a real infinity is that infinity, where it had a `NaN` imaginary part.** +Over `System.Numerics.Complex`, in FastExpression and in the LINQ compiler, the product's imaginary part was +`∞ · 0`, which is `NaN`, so `-x` at `+oo` was `(-∞, NaN)`: a value with no imaginary part at all, which a comparison +then had no truth value for. A product or a quotient of two real numbers is now taken as one of reals. A division by +`0` is unchanged, and has no value, as evaluation has none +([#2222](https://github.com/asc-community/AngouriMath/issues/2222)). + +| Input | Was (2.6.0) | 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 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 fb2115c35..acfbb3481 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -904,3 +904,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Convenience/EulerGammaTest.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 + +[AngouriMath/Functions/Compilation/RealProducts.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/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 diff --git a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs index f83b427a4..7e835d01b 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs @@ -188,11 +188,13 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value case InstructionType.CALL_MINUS: stack.Push(stack.Pop() - stack.Pop()); break; + // Real where both sides are, so that an infinity keeps an imaginary part of 0: + // https://github.com/asc-community/AngouriMath/issues/2222 case InstructionType.CALL_MUL: - stack.Push(stack.Pop() * stack.Pop()); + stack.Push(AngouriMath.Core.Compilation.RealProducts.Product(stack.Pop(), stack.Pop())); break; case InstructionType.CALL_DIV: - stack.Push(stack.Pop() / stack.Pop()); + stack.Push(AngouriMath.Core.Compilation.RealProducts.Quotient(stack.Pop(), stack.Pop())); break; case InstructionType.CALL_MOD: stack.Push(Core.Compilation.RealOnly.Mod(stack.Pop(), stack.Pop())); diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs b/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs index 0a57a5ea9..0a23b6da8 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs @@ -473,8 +473,10 @@ private static MethodInfo Conv(Expression> e) { { (ExpressionType.Add, typeof(System.Numerics.Complex)), Op((a, b) => a + b) }, { (ExpressionType.Subtract, typeof(System.Numerics.Complex)), Op((a, b) => a - b) }, - { (ExpressionType.Multiply, typeof(System.Numerics.Complex)), Op((a, b) => a * b) }, - { (ExpressionType.Divide, typeof(System.Numerics.Complex)), Op((a, b) => a / b) }, + // Real where both sides are, so that an infinity keeps an imaginary part of 0: + // https://github.com/asc-community/AngouriMath/issues/2222 + { (ExpressionType.Multiply, typeof(System.Numerics.Complex)), Call((a, b) => RealProducts.Product(a, b)) }, + { (ExpressionType.Divide, typeof(System.Numerics.Complex)), Call((a, b) => RealProducts.Quotient(a, b)) }, { (ExpressionType.Equal, typeof(System.Numerics.Complex)), Cmp((a, b) => a == b) }, { (ExpressionType.Add, typeof(BigInteger)), Op((a, b) => a + b) }, @@ -515,6 +517,8 @@ internal Expression ConvertMembership(Expression element, AngouriMath.Numerics.N [ConstantField] private static readonly MethodInfo complexRemainder = ((MethodCallExpression)((Expression>)((a, b) => RealOnly.Mod(a, b))).Body).Method; + private static MethodInfo Call(Expression> e) => ((MethodCallExpression)e.Body).Method; + private static MethodInfo Op(Expression> e) => ((BinaryExpression)e.Body).Method ?? throw new AngouriBugException($"{typeof(T)} defines this operator as an instruction, not a method"); diff --git a/Sources/AngouriMath/Functions/Compilation/RealProducts.cs b/Sources/AngouriMath/Functions/Compilation/RealProducts.cs new file mode 100644 index 000000000..5909c7d8d --- /dev/null +++ b/Sources/AngouriMath/Functions/Compilation/RealProducts.cs @@ -0,0 +1,31 @@ +// +// 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 NumericsComplex = System.Numerics.Complex; + +namespace AngouriMath.Core.Compilation +{ + /// + /// The product and the quotient 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. + /// https://github.com/asc-community/AngouriMath/issues/2222 + /// + internal static class RealProducts + { + public static NumericsComplex Product(NumericsComplex a, NumericsComplex b) + => a.Imaginary == 0 && b.Imaginary == 0 ? new NumericsComplex(a.Real * b.Real, 0) : a * b; + + /// + /// The quotient, real where both are real and the divisor is not 0. A division by 0 is left to + /// , whose NaN is evaluation's: 1/0 has no value. + /// + 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; + } +} diff --git a/Sources/Tests/UnitTests/Common/MembershipCompilationTest.cs b/Sources/Tests/UnitTests/Common/MembershipCompilationTest.cs index bc714c4c4..c1cb8060e 100644 --- a/Sources/Tests/UnitTests/Common/MembershipCompilationTest.cs +++ b/Sources/Tests/UnitTests/Common/MembershipCompilationTest.cs @@ -5,7 +5,6 @@ // Website: https://am.angouri.org. // -using System.Linq; using System.Numerics; using AngouriMath; using AngouriMath.Core.Exceptions; @@ -99,9 +98,7 @@ public void ADivisibilityIsWhatEvaluationSays() public void AMembershipInAnExpressionIsWhatEvaluationSays(string expression) { var fast = expression.ToEntity().Compile("x", "y"); - // Not at an infinity, where a compiled -x has a NaN imaginary part: - // https://github.com/asc-community/AngouriMath/issues/2222 - foreach (var a in points.Where(point => !point.Contains("oo"))) + foreach (var a in points) { var evaluated = expression.ToEntity().Substitute("x", a).Substitute("y", 0).Evaled; var value = fast.Call(Numeric(a), 0); diff --git a/Sources/Tests/UnitTests/Common/RealProductCompilationTest.cs b/Sources/Tests/UnitTests/Common/RealProductCompilationTest.cs new file mode 100644 index 000000000..939b61d21 --- /dev/null +++ b/Sources/Tests/UnitTests/Common/RealProductCompilationTest.cs @@ -0,0 +1,59 @@ +// +// 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 product or quotient of real numbers is real, so an infinity keeps an imaginary part of 0, where + /// 's own product made it ∞ · 0, NaN: -x at +oo was + /// (-∞, NaN). #2222 + /// + [Trait("Area", "Common")] + public sealed class RealProductCompilationTest + { + [Theory] + [InlineData("-x", "+oo")] + [InlineData("-x", "-oo")] + [InlineData("2 * x", "+oo")] + [InlineData("x / 2", "+oo")] + [InlineData("x / (-3)", "-oo")] + [InlineData("x * x", "-oo")] + [InlineData("-x + 1", "+oo")] + [InlineData("3 * x - 1", "-oo")] + public void AtAnInfinityTheValueIsEvaluations(string expression, string at) + { + var expected = ((Entity.Number.Complex)expression.ToEntity().Substitute("x", at).Evaled).ToNumerics(); + var x = ((Entity.Number.Complex)at.ToEntity().Evaled).ToNumerics(); + Assert.True(double.IsInfinity(expected.Real) && expected.Imaginary == 0, $"{expression} at {at} evaluates to {expected}"); + Assert.Equal(expected, expression.ToEntity().Compile("x").Call(x)); + Assert.Equal(expected, expression.ToEntity().Compile("x")(x)); + } + + /// A division by 0 still has no value, as evaluation has none, and a complex product is a complex product. + [Theory] + [InlineData("1 / x", "0", double.NaN, double.NaN)] + [InlineData("x / x", "0", double.NaN, double.NaN)] + [InlineData("(1 + i) * x", "1 - i", 2, 0)] + [InlineData("x * x", "i", -1, 0)] + [InlineData("x / (1 + i)", "2", 1, -1)] + [InlineData("x * 3", "-2", -6, 0)] + public void ElsewhereTheValueIsWhatItWas(string expression, string at, double real, double imaginary) + { + var x = ((Entity.Number.Complex)at.ToEntity().Evaled).ToNumerics(); + foreach (var value in new[] { expression.ToEntity().Compile("x").Call(x), expression.ToEntity().Compile("x")(x) }) + if (double.IsNaN(real)) + Assert.True(double.IsNaN(value.Real) || double.IsNaN(value.Imaginary), $"{expression} at {at}: {value}"); + else + Assert.True(Complex.Abs(value - new Complex(real, imaginary)) < 1e-15, $"{expression} at {at}: {value}"); + } + } +}