diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 76adbf7d9..e3857e469 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -307,6 +307,19 @@ negative even integer or off the integers, since the extension through the gamma | `"n!!".ToEntity()` | throws `UnhandledParseException` | `n!!` | | `"n!!!".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` | +### The conjugate, the argument, the parts and the Iverson bracket compile + +**Not silent: these compile where they threw `UncompilableNodeException`.** `conj`, `arg`, `Re` and `Im` compile +through `Compile` and `Compile` alike, to the values the interpreter gives: `arg` is the principal argument +in `(-pi, pi]` with no value at `0`, and `-1 - 0i` is at `pi`. `iverson` compiles under `Compile` over the +reals, to `1` and `0` ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"iverson(x > 0)".ToEntity().Compile("x")(2)` | throws `UncompilableNodeException` | `1` | +| `"arg(x)".ToEntity().Compile("x").Call(-2)` | throws `UncompilableNodeException`, `arg` being a variable | `pi` | +| `"conj(x)".ToEntity().Compile("x")(3 + 4i)` | throws `UncompilableNodeException`, `conj` being a variable | `3 - 4i` | + ### `re` and `im` are the real and the imaginary part **Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 320603fd8..a60f119d9 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -814,3 +814,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Convenience/FactorialPowerTest.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/Numerics/ComplexParts.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/Convenience/ComplexPartsCompilationTest.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/Compiler.cs b/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs index 38db33971..3efcddd1d 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs @@ -432,6 +432,54 @@ private protected override void CompileNode(Compiler compiler) } } + // https://github.com/asc-community/AngouriMath/issues/2195 + public partial record Conjugatef + { + private protected override bool CompilesItself => true; + + private protected override void CompileNode(Compiler compiler) + { + Argument.InnerCompile(compiler); + compiler.Instructions.Add(new(InstructionType.CALL_CONJ)); + } + } + + // https://github.com/asc-community/AngouriMath/issues/2195 + public partial record Argf + { + private protected override bool CompilesItself => true; + + private protected override void CompileNode(Compiler compiler) + { + Argument.InnerCompile(compiler); + compiler.Instructions.Add(new(InstructionType.CALL_ARG)); + } + } + + // https://github.com/asc-community/AngouriMath/issues/2195 + public partial record Ref + { + private protected override bool CompilesItself => true; + + private protected override void CompileNode(Compiler compiler) + { + Argument.InnerCompile(compiler); + compiler.Instructions.Add(new(InstructionType.CALL_RE)); + } + } + + // https://github.com/asc-community/AngouriMath/issues/2195 + public partial record Imf + { + private protected override bool CompilesItself => true; + + private protected override void CompileNode(Compiler compiler) + { + Argument.InnerCompile(compiler); + compiler.Instructions.Add(new(InstructionType.CALL_IM)); + } + } + public partial record Phif { private protected override bool CompilesItself => true; diff --git a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs index 15c4b1ec8..36a301313 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs @@ -49,6 +49,10 @@ internal enum InstructionType CALL_CI, CALL_SHI, CALL_CHI, + CALL_CONJ, + CALL_ARG, + CALL_RE, + CALL_IM, // 2-arg functions CALL_SUM = 100, @@ -269,6 +273,18 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value case InstructionType.CALL_PHI: stack.Push(AngouriMath.Numerics.SpecialFunctions.Phi(stack.Pop())); break; + case InstructionType.CALL_CONJ: + stack.Push(AngouriMath.Numerics.ComplexParts.Conj(stack.Pop())); + break; + case InstructionType.CALL_ARG: + stack.Push(AngouriMath.Numerics.ComplexParts.Arg(stack.Pop())); + break; + case InstructionType.CALL_RE: + stack.Push(AngouriMath.Numerics.ComplexParts.Re(stack.Pop())); + break; + case InstructionType.CALL_IM: + stack.Push(AngouriMath.Numerics.ComplexParts.Im(stack.Pop())); + break; } if (stack.Count != 1) throw new AngouriBugException($"Unused values remain in the stack: {stack.Count} instead of 1"); diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs b/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs index adcded759..054cde6d3 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/CompilationProtocol.cs @@ -151,6 +151,15 @@ Shif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Shi", Chif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Chi", 1, newE.Type), newE), Factorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Factorial", 1, newE.Type), newE), Phif => Expression.Call(GetDef("Phi", 1, e.Type), e), + + // The conjugate, the argument and the parts, widened to double as the special + // functions are; an Iverson bracket is 1 where its statement holds and 0 where it + // does not. https://github.com/asc-community/AngouriMath/issues/2195 + Conjugatef when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Conj", 1, newE.Type), newE), + Argf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Arg", 1, newE.Type), newE), + Ref when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Re", 1, newE.Type), newE), + Imf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Im", 1, newE.Type), newE), + Iversonf when e.Type == typeof(bool) => Expression.Condition(e, Expression.Constant(1), Expression.Constant(0)), Notf => Expression.Not(e), diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs index dea2e704e..9cdcf64d8 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs @@ -320,6 +320,17 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a) public static System.Numerics.Complex Factorial(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a); public static double Factorial(double a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a); + // The conjugate, the principal argument and the real and the imaginary parts. + // https://github.com/asc-community/AngouriMath/issues/2195 + public static System.Numerics.Complex Conj(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Conj(a); + public static double Conj(double a) => AngouriMath.Numerics.ComplexParts.Conj(a); + public static System.Numerics.Complex Arg(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Arg(a); + public static double Arg(double a) => AngouriMath.Numerics.ComplexParts.Arg(a); + public static System.Numerics.Complex Re(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Re(a); + public static double Re(double a) => AngouriMath.Numerics.ComplexParts.Re(a); + public static System.Numerics.Complex Im(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Im(a); + public static double Im(double a) => AngouriMath.Numerics.ComplexParts.Im(a); + /// /// True where System.Numerics.Complex.IsNaN is, spelled out because that overload is /// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN @@ -496,6 +507,14 @@ public static bool IsNaN(System.Numerics.Complex a) { ("Chi", 1, typeof(double)), Def(a => Chi(a)) }, { ("Factorial", 1, typeof(System.Numerics.Complex)), Def(a => Factorial(a)) }, { ("Factorial", 1, typeof(double)), Def(a => Factorial(a)) }, + { ("Conj", 1, typeof(System.Numerics.Complex)), Def(a => Conj(a)) }, + { ("Conj", 1, typeof(double)), Def(a => Conj(a)) }, + { ("Arg", 1, typeof(System.Numerics.Complex)), Def(a => Arg(a)) }, + { ("Arg", 1, typeof(double)), Def(a => Arg(a)) }, + { ("Re", 1, typeof(System.Numerics.Complex)), Def(a => Re(a)) }, + { ("Re", 1, typeof(double)), Def(a => Re(a)) }, + { ("Im", 1, typeof(System.Numerics.Complex)), Def(a => Im(a)) }, + { ("Im", 1, typeof(double)), Def(a => Im(a)) }, }; // An expression tree is how C# spells "the MethodInfo of this method" without a string: diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt index 809048155..3cc3de524 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt @@ -12,6 +12,9 @@ // Defined for Complex and double alone: an integer or a float is widened to double first, as // for the trigonometry. var special = new[]{ "Erf", "Erfc", "Erfi", "Ei", "Li", "Si", "Ci", "Shi", "Chi", "Factorial" }; + // The conjugate, the argument and the parts, for Complex and double alone in the same way. + // https://github.com/asc-community/AngouriMath/issues/2195 + var parts = new[]{ "Conj", "Arg", "Re", "Im" }; var types = new[]{ "System.Numerics.Complex", "double", "float", "long", "int", "BigInteger" }; #> // @@ -207,6 +210,13 @@ namespace AngouriMath.Core.Compilation.IntoLinq public static double <#= name #>(double a) => AngouriMath.Numerics.SpecialFunctions.<#= name #>(a); <# } #> + // The conjugate, the principal argument and the real and the imaginary parts. + // https://github.com/asc-community/AngouriMath/issues/2195 +<# foreach (var name in parts) { #> + public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.<#= name #>(a); + public static double <#= name #>(double a) => AngouriMath.Numerics.ComplexParts.<#= name #>(a); +<# } #> + /// /// True where System.Numerics.Complex.IsNaN is, spelled out because that overload is /// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN @@ -245,6 +255,11 @@ namespace AngouriMath.Core.Compilation.IntoLinq <# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #> { ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) }, <# } #> +<# } #> +<# foreach (var name in parts) { #> +<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #> + { ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) }, +<# } #> <# } #> }; diff --git a/Sources/AngouriMath/Numerics/ComplexParts.cs b/Sources/AngouriMath/Numerics/ComplexParts.cs new file mode 100644 index 000000000..147884535 --- /dev/null +++ b/Sources/AngouriMath/Numerics/ComplexParts.cs @@ -0,0 +1,38 @@ +// +// 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; +using NumericsComplex = System.Numerics.Complex; + +namespace AngouriMath.Numerics +{ + /// + /// The conjugate, the principal argument and the real and the imaginary parts in double + /// precision, which both compilers call, with the values the interpreter gives. + /// https://github.com/asc-community/AngouriMath/issues/2195 + /// + internal static class ComplexParts + { + public static NumericsComplex Conj(NumericsComplex z) => NumericsComplex.Conjugate(z); + public static double Conj(double x) => x; + + /// + /// The principal argument, in (-pi, pi], with no value at 0, as arg is. A + /// negative zero is read as zero, so that -1 is at pi and not at -pi, + /// where would put -1 - 0i. + /// + public static NumericsComplex Arg(NumericsComplex z) => Arg(z.Real, z.Imaginary); + public static double Arg(double x) => Arg(x, 0); + private static double Arg(double re, double im) + => re == 0 && im == 0 ? double.NaN : Math.Atan2(im == 0 ? 0.0 : im, re == 0 ? 0.0 : re); + + public static NumericsComplex Re(NumericsComplex z) => z.Real; + public static double Re(double x) => x; + public static NumericsComplex Im(NumericsComplex z) => z.Imaginary; + public static double Im(double x) => double.IsNaN(x) ? double.NaN : 0; + } +} diff --git a/Sources/Tests/AotSmokeTest/Program.cs b/Sources/Tests/AotSmokeTest/Program.cs index 203c86a93..fcefc2674 100644 --- a/Sources/Tests/AotSmokeTest/Program.cs +++ b/Sources/Tests/AotSmokeTest/Program.cs @@ -230,6 +230,9 @@ private static readonly (string Expr, Complex Arg)[] complexCases = ("x / (x + 1)", 4.0), ("x + 2 * x - 3 * x ^ 2", 1.5), ("sin(x) ^ 2 + cos(x) ^ 2", 0.9), + // https://github.com/asc-community/AngouriMath/issues/2195 + ("conj(x) + arg(x)", new Complex(-1.5, 2)), + ("Re(x) * Im(x)", new Complex(3, -2)), }; private static void Compilation() @@ -260,6 +263,8 @@ private static void Compilation() // double, not Complex: a different set of MathAllMethods overloads. var d = "sin(x) + cos(x)".Compile(x); CheckClose("compiled double sin(x) + cos(x) at 0.4", d(0.4), Math.Sin(0.4) + Math.Cos(0.4), 1e-12); + var iverson = "iverson(x > 0) * x".Compile(x); + CheckClose("compiled iverson(x > 0) x at 2 and -2", iverson(2) + iverson(-2), 2, 1e-12); // Two arguments, and the floored remainder that takes the sign of the divisor. var mod = "x mod y".Compile(x, y); diff --git a/Sources/Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs b/Sources/Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs new file mode 100644 index 000000000..96ee8e09c --- /dev/null +++ b/Sources/Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs @@ -0,0 +1,75 @@ +// +// 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 AngouriMath.Extensions; +using Xunit; +using NumericsComplex = System.Numerics.Complex; + +namespace AngouriMath.Tests.Convenience +{ + /// + /// The conjugate, the principal argument and the real and the imaginary parts compile, through + /// Compile and Compile<TIn, TOut> alike, to the values the interpreter gives; + /// and an Iverson bracket compiles to a number over the reals. + /// #2195 + /// + [Trait("Area", "Convenience")] + public sealed class ComplexPartsCompilationTest + { + /// + /// Off the axes, on each half-axis, and at -1 with a negative zero, where the principal + /// argument is pi and Math.Atan2 would give -pi. + /// + public static readonly TheoryData Points = new() + { + { "conj(x)", 0.3, 0.2 }, { "conj(x)", -2.5, -1.5 }, { "conj(x)", 4, 0 }, + { "arg(x)", 0.3, 0.2 }, { "arg(x)", -2.5, -1.5 }, { "arg(x)", -1, 0 }, { "arg(x)", -1, -0.0 }, + { "arg(x)", 0, 3 }, { "arg(x)", 0, -3 }, { "arg(x)", 4, 0 }, + { "Re(x)", 0.3, 0.2 }, { "Re(x)", -2.5, -1.5 }, { "Im(x)", 0.3, 0.2 }, { "Im(x)", -2.5, -1.5 }, + { "Re(x) * Im(x) + conj(x) * arg(x)", 1.25, -0.75 }, + }; + + private static NumericsComplex Interpreted(string expression, double re, double im) + => ((Entity.Number.Complex)expression.ToEntity().Substitute("x", Entity.Number.Complex.Create(re, im)).EvalNumerical()).ToNumerics(); + + private static void Close(NumericsComplex expected, NumericsComplex actual) + => Assert.True((actual - expected).Magnitude <= 1e-12 * System.Math.Max(1, expected.Magnitude), $"expected {expected}, got {actual}"); + + [Theory] + [MemberData(nameof(Points))] + public void TheCompiledFormAgreesWithTheInterpreter(string expression, double re, double im) + => Close(Interpreted(expression, re, im), expression.ToEntity().Compile("x").Call(new NumericsComplex(re, im))); + + [Theory] + [MemberData(nameof(Points))] + public void TheExpressionTreeAgreesWithTheInterpreter(string expression, double re, double im) + => Close(Interpreted(expression, re, im), + expression.ToEntity().Compile("x")(new NumericsComplex(re, im))); + + /// The argument of 0 has no value, compiled or interpreted. + [Fact] + public void TheArgumentOfZeroHasNoValue() + { + Assert.True(double.IsNaN("arg(x)".ToEntity().Compile("x").Call(0).Real)); + Assert.True(double.IsNaN("arg(x)".ToEntity().Compile("x")(0))); + Assert.Equal(MathS.NaN, "arg(0)".ToEntity().Evaled); + } + + /// Over doubles, the argument of a negative number is pi, and the imaginary part is 0. + [Theory] + [InlineData("arg(x)", -2, System.Math.PI)] + [InlineData("arg(x)", 2, 0)] + [InlineData("Re(x)", -2, -2)] + [InlineData("Im(x)", -2, 0)] + [InlineData("conj(x)", -2, -2)] + [InlineData("iverson(x > 0)", 2, 1)] + [InlineData("iverson(x > 0)", -2, 0)] + [InlineData("iverson(x = 2) * x", 2, 2)] + public void OverTheRealsTheyAreReal(string expression, double at, double expected) + => Assert.Equal(expected, expression.ToEntity().Compile("x")(at), 12); + } +}