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
13 changes: 13 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TIn, TOut>` 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<TIn, TOut>` 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<double, double>("x")(2)` | throws `UncompilableNodeException` | `1` |
| `"arg(x)".ToEntity().Compile("x").Call(-2)` | throws `UncompilableNodeException`, `arg` being a variable | `pi` |
| `"conj(x)".ToEntity().Compile<Complex, Complex>("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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions Sources/AngouriMath/Functions/Compilation/IntoFE/FastExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
Expand Down Expand Up @@ -496,6 +507,14 @@ public static bool IsNaN(System.Numerics.Complex a)
{ ("Chi", 1, typeof(double)), Def<double>(a => Chi(a)) },
{ ("Factorial", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Factorial(a)) },
{ ("Factorial", 1, typeof(double)), Def<double>(a => Factorial(a)) },
{ ("Conj", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Conj(a)) },
{ ("Conj", 1, typeof(double)), Def<double>(a => Conj(a)) },
{ ("Arg", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Arg(a)) },
{ ("Arg", 1, typeof(double)), Def<double>(a => Arg(a)) },
{ ("Re", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Re(a)) },
{ ("Re", 1, typeof(double)), Def<double>(a => Re(a)) },
{ ("Im", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Im(a)) },
{ ("Im", 1, typeof(double)), Def<double>(a => Im(a)) },
};

// An expression tree is how C# spells "the MethodInfo of this method" without a string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
#>
//
Expand Down Expand Up @@ -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);
<# } #>

/// <summary>
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
/// .NET 7 and later and this assembly also targets netstandard2.0. A complex number is NaN
Expand Down Expand Up @@ -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)) },
<# } #>
<# } #>
};

Expand Down
38 changes: 38 additions & 0 deletions Sources/AngouriMath/Numerics/ComplexParts.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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
/// </summary>
internal static class ComplexParts
{
public static NumericsComplex Conj(NumericsComplex z) => NumericsComplex.Conjugate(z);
public static double Conj(double x) => x;

/// <summary>
/// The principal argument, in <c>(-pi, pi]</c>, with no value at 0, as <c>arg</c> is. A
/// negative zero is read as zero, so that <c>-1</c> is at <c>pi</c> and not at <c>-pi</c>,
/// where <see cref="Math.Atan2"/> would put <c>-1 - 0i</c>.
/// </summary>
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;
}
}
5 changes: 5 additions & 0 deletions Sources/Tests/AotSmokeTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -260,6 +263,8 @@ private static void Compilation()
// double, not Complex: a different set of MathAllMethods overloads.
var d = "sin(x) + cos(x)".Compile<double, double>(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<double, double>(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<long, long, long>(x, y);
Expand Down
75 changes: 75 additions & 0 deletions Sources/Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The conjugate, the principal argument and the real and the imaginary parts compile, through
/// <c>Compile</c> and <c>Compile&lt;TIn, TOut&gt;</c> alike, to the values the interpreter gives;
/// and an Iverson bracket compiles to a number over the reals.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2195">#2195</a>
/// </summary>
[Trait("Area", "Convenience")]
public sealed class ComplexPartsCompilationTest
{
/// <summary>
/// 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.
/// </summary>
public static readonly TheoryData<string, double, double> 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<NumericsComplex, NumericsComplex>("x")(new NumericsComplex(re, im)));

/// <summary>The argument of 0 has no value, compiled or interpreted.</summary>
[Fact]
public void TheArgumentOfZeroHasNoValue()
{
Assert.True(double.IsNaN("arg(x)".ToEntity().Compile("x").Call(0).Real));
Assert.True(double.IsNaN("arg(x)".ToEntity().Compile<double, double>("x")(0)));
Assert.Equal(MathS.NaN, "arg(0)".ToEntity().Evaled);
}

/// <summary>Over doubles, the argument of a negative number is pi, and the imaginary part is 0.</summary>
[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<double, double>("x")(at), 12);
}
}
Loading