Skip to content

Commit 1e2daa0

Browse files
The conjugate, the argument, the parts and the Iverson bracket compile (#2196)
conj, arg, Re and Im had no compiled form in either compiler, and iverson none in the expression trees. Each threw UncompilableNodeException, which #2195 counts among the gaps that work/compilecheck measures. The four are AngouriMath.Numerics.ComplexParts, in double precision, called by FastExpression's new CALL_CONJ, CALL_ARG, CALL_RE and CALL_IM instructions and by CompilationProtocol, which widens an integer to double first as it does for the special functions. Their methods are in MathAllMethods.tt, from which MathAllMethods.cs is regenerated. The argument is the interpreter's: in (-pi, pi], with no value at 0, and a negative zero read as zero, so that -1 - 0i is at pi where Math.Atan2 gives -pi. iverson compiles under Compile<TIn, TOut> to a conditional of 1 and 0. Over Complex it is still refused, since the expression trees have no comparison of complex numbers, and FastExpression has no booleans at all; both are #2195's other items. compilecheck on this branch: FastExpression 47 of 71 numeric nodes where it was 43, the expression trees over Complex 46 where 42 and over double 49 where 44, and no compiled value that disagrees with EvalNumerical. The native smoke test compiles all five. Part of #2195. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent ea08214 commit 1e2daa0

10 files changed

Lines changed: 244 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ negative even integer or off the integers, since the extension through the gamma
307307
| `"n!!".ToEntity()` | throws `UnhandledParseException` | `n!!` |
308308
| `"n!!!".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
309309

310+
### The conjugate, the argument, the parts and the Iverson bracket compile
311+
312+
**Not silent: these compile where they threw `UncompilableNodeException`.** `conj`, `arg`, `Re` and `Im` compile
313+
through `Compile` and `Compile<TIn, TOut>` alike, to the values the interpreter gives: `arg` is the principal argument
314+
in `(-pi, pi]` with no value at `0`, and `-1 - 0i` is at `pi`. `iverson` compiles under `Compile<TIn, TOut>` over the
315+
reals, to `1` and `0` ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
316+
317+
| Input | Was (2.6.0) | Now |
318+
|---|---|---|
319+
| `"iverson(x > 0)".ToEntity().Compile<double, double>("x")(2)` | throws `UncompilableNodeException` | `1` |
320+
| `"arg(x)".ToEntity().Compile("x").Call(-2)` | throws `UncompilableNodeException`, `arg` being a variable | `pi` |
321+
| `"conj(x)".ToEntity().Compile<Complex, Complex>("x")(3 + 4i)` | throws `UncompilableNodeException`, `conj` being a variable | `3 - 4i` |
322+
310323
### `re` and `im` are the real and the imaginary part
311324

312325
**Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were

‎Sources/.editorconfig‎

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

815815
[Tests/UnitTests/Convenience/FactorialPowerTest.cs]
816816
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
817+
818+
[AngouriMath/Numerics/ComplexParts.cs]
819+
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
820+
821+
[Tests/UnitTests/Convenience/ComplexPartsCompilationTest.cs]
822+
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/Compiler.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,54 @@ private protected override void CompileNode(Compiler compiler)
432432
}
433433
}
434434

435+
// https://github.com/asc-community/AngouriMath/issues/2195
436+
public partial record Conjugatef
437+
{
438+
private protected override bool CompilesItself => true;
439+
440+
private protected override void CompileNode(Compiler compiler)
441+
{
442+
Argument.InnerCompile(compiler);
443+
compiler.Instructions.Add(new(InstructionType.CALL_CONJ));
444+
}
445+
}
446+
447+
// https://github.com/asc-community/AngouriMath/issues/2195
448+
public partial record Argf
449+
{
450+
private protected override bool CompilesItself => true;
451+
452+
private protected override void CompileNode(Compiler compiler)
453+
{
454+
Argument.InnerCompile(compiler);
455+
compiler.Instructions.Add(new(InstructionType.CALL_ARG));
456+
}
457+
}
458+
459+
// https://github.com/asc-community/AngouriMath/issues/2195
460+
public partial record Ref
461+
{
462+
private protected override bool CompilesItself => true;
463+
464+
private protected override void CompileNode(Compiler compiler)
465+
{
466+
Argument.InnerCompile(compiler);
467+
compiler.Instructions.Add(new(InstructionType.CALL_RE));
468+
}
469+
}
470+
471+
// https://github.com/asc-community/AngouriMath/issues/2195
472+
public partial record Imf
473+
{
474+
private protected override bool CompilesItself => true;
475+
476+
private protected override void CompileNode(Compiler compiler)
477+
{
478+
Argument.InnerCompile(compiler);
479+
compiler.Instructions.Add(new(InstructionType.CALL_IM));
480+
}
481+
}
482+
435483
public partial record Phif
436484
{
437485
private protected override bool CompilesItself => true;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ internal enum InstructionType
4949
CALL_CI,
5050
CALL_SHI,
5151
CALL_CHI,
52+
CALL_CONJ,
53+
CALL_ARG,
54+
CALL_RE,
55+
CALL_IM,
5256

5357
// 2-arg functions
5458
CALL_SUM = 100,
@@ -269,6 +273,18 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
269273
case InstructionType.CALL_PHI:
270274
stack.Push(AngouriMath.Numerics.SpecialFunctions.Phi(stack.Pop()));
271275
break;
276+
case InstructionType.CALL_CONJ:
277+
stack.Push(AngouriMath.Numerics.ComplexParts.Conj(stack.Pop()));
278+
break;
279+
case InstructionType.CALL_ARG:
280+
stack.Push(AngouriMath.Numerics.ComplexParts.Arg(stack.Pop()));
281+
break;
282+
case InstructionType.CALL_RE:
283+
stack.Push(AngouriMath.Numerics.ComplexParts.Re(stack.Pop()));
284+
break;
285+
case InstructionType.CALL_IM:
286+
stack.Push(AngouriMath.Numerics.ComplexParts.Im(stack.Pop()));
287+
break;
272288
}
273289
if (stack.Count != 1)
274290
throw new AngouriBugException($"Unused values remain in the stack: {stack.Count} instead of 1");

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ Shif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Shi",
151151
Chif when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Chi", 1, newE.Type), newE),
152152
Factorialf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Factorial", 1, newE.Type), newE),
153153
Phif => Expression.Call(GetDef("Phi", 1, e.Type), e),
154+
155+
// The conjugate, the argument and the parts, widened to double as the special
156+
// functions are; an Iverson bracket is 1 where its statement holds and 0 where it
157+
// does not. https://github.com/asc-community/AngouriMath/issues/2195
158+
Conjugatef when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Conj", 1, newE.Type), newE),
159+
Argf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Arg", 1, newE.Type), newE),
160+
Ref when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Re", 1, newE.Type), newE),
161+
Imf when ShouldBeAtLeastDouble(e) is var newE => Expression.Call(GetDef("Im", 1, newE.Type), newE),
162+
Iversonf when e.Type == typeof(bool) => Expression.Condition(e, Expression.Constant(1), Expression.Constant(0)),
154163

155164
Notf => Expression.Not(e),
156165

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
320320
public static System.Numerics.Complex Factorial(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a);
321321
public static double Factorial(double a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a);
322322

323+
// The conjugate, the principal argument and the real and the imaginary parts.
324+
// https://github.com/asc-community/AngouriMath/issues/2195
325+
public static System.Numerics.Complex Conj(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Conj(a);
326+
public static double Conj(double a) => AngouriMath.Numerics.ComplexParts.Conj(a);
327+
public static System.Numerics.Complex Arg(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Arg(a);
328+
public static double Arg(double a) => AngouriMath.Numerics.ComplexParts.Arg(a);
329+
public static System.Numerics.Complex Re(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Re(a);
330+
public static double Re(double a) => AngouriMath.Numerics.ComplexParts.Re(a);
331+
public static System.Numerics.Complex Im(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Im(a);
332+
public static double Im(double a) => AngouriMath.Numerics.ComplexParts.Im(a);
333+
323334
/// <summary>
324335
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
325336
/// .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)
496507
{ ("Chi", 1, typeof(double)), Def<double>(a => Chi(a)) },
497508
{ ("Factorial", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Factorial(a)) },
498509
{ ("Factorial", 1, typeof(double)), Def<double>(a => Factorial(a)) },
510+
{ ("Conj", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Conj(a)) },
511+
{ ("Conj", 1, typeof(double)), Def<double>(a => Conj(a)) },
512+
{ ("Arg", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Arg(a)) },
513+
{ ("Arg", 1, typeof(double)), Def<double>(a => Arg(a)) },
514+
{ ("Re", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Re(a)) },
515+
{ ("Re", 1, typeof(double)), Def<double>(a => Re(a)) },
516+
{ ("Im", 1, typeof(System.Numerics.Complex)), Def<System.Numerics.Complex>(a => Im(a)) },
517+
{ ("Im", 1, typeof(double)), Def<double>(a => Im(a)) },
499518
};
500519

501520
// An expression tree is how C# spells "the MethodInfo of this method" without a string:

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// Defined for Complex and double alone: an integer or a float is widened to double first, as
1313
// for the trigonometry.
1414
var special = new[]{ "Erf", "Erfc", "Erfi", "Ei", "Li", "Si", "Ci", "Shi", "Chi", "Factorial" };
15+
// The conjugate, the argument and the parts, for Complex and double alone in the same way.
16+
// https://github.com/asc-community/AngouriMath/issues/2195
17+
var parts = new[]{ "Conj", "Arg", "Re", "Im" };
1518
var types = new[]{ "System.Numerics.Complex", "double", "float", "long", "int", "BigInteger" };
1619
#>
1720
//
@@ -207,6 +210,13 @@ namespace AngouriMath.Core.Compilation.IntoLinq
207210
public static double <#= name #>(double a) => AngouriMath.Numerics.SpecialFunctions.<#= name #>(a);
208211
<# } #>
209212

213+
// The conjugate, the principal argument and the real and the imaginary parts.
214+
// https://github.com/asc-community/AngouriMath/issues/2195
215+
<# foreach (var name in parts) { #>
216+
public static System.Numerics.Complex <#= name #>(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.<#= name #>(a);
217+
public static double <#= name #>(double a) => AngouriMath.Numerics.ComplexParts.<#= name #>(a);
218+
<# } #>
219+
210220
/// <summary>
211221
/// True where <c>System.Numerics.Complex.IsNaN</c> is, spelled out because that overload is
212222
/// .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
245255
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
246256
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },
247257
<# } #>
258+
<# } #>
259+
<# foreach (var name in parts) { #>
260+
<# foreach (var type in new[]{ "System.Numerics.Complex", "double" }) { #>
261+
{ ("<#= name #>", 1, typeof(<#= type #>)), Def<<#= type #>>(a => <#= name #>(a)) },
262+
<# } #>
248263
<# } #>
249264
};
250265

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
9+
using NumericsComplex = System.Numerics.Complex;
10+
11+
namespace AngouriMath.Numerics
12+
{
13+
/// <summary>
14+
/// The conjugate, the principal argument and the real and the imaginary parts in double
15+
/// precision, which both compilers call, with the values the interpreter gives.
16+
/// https://github.com/asc-community/AngouriMath/issues/2195
17+
/// </summary>
18+
internal static class ComplexParts
19+
{
20+
public static NumericsComplex Conj(NumericsComplex z) => NumericsComplex.Conjugate(z);
21+
public static double Conj(double x) => x;
22+
23+
/// <summary>
24+
/// The principal argument, in <c>(-pi, pi]</c>, with no value at 0, as <c>arg</c> is. A
25+
/// negative zero is read as zero, so that <c>-1</c> is at <c>pi</c> and not at <c>-pi</c>,
26+
/// where <see cref="Math.Atan2"/> would put <c>-1 - 0i</c>.
27+
/// </summary>
28+
public static NumericsComplex Arg(NumericsComplex z) => Arg(z.Real, z.Imaginary);
29+
public static double Arg(double x) => Arg(x, 0);
30+
private static double Arg(double re, double im)
31+
=> re == 0 && im == 0 ? double.NaN : Math.Atan2(im == 0 ? 0.0 : im, re == 0 ? 0.0 : re);
32+
33+
public static NumericsComplex Re(NumericsComplex z) => z.Real;
34+
public static double Re(double x) => x;
35+
public static NumericsComplex Im(NumericsComplex z) => z.Imaginary;
36+
public static double Im(double x) => double.IsNaN(x) ? double.NaN : 0;
37+
}
38+
}

‎Sources/Tests/AotSmokeTest/Program.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ private static readonly (string Expr, Complex Arg)[] complexCases =
230230
("x / (x + 1)", 4.0),
231231
("x + 2 * x - 3 * x ^ 2", 1.5),
232232
("sin(x) ^ 2 + cos(x) ^ 2", 0.9),
233+
// https://github.com/asc-community/AngouriMath/issues/2195
234+
("conj(x) + arg(x)", new Complex(-1.5, 2)),
235+
("Re(x) * Im(x)", new Complex(3, -2)),
233236
};
234237

235238
private static void Compilation()
@@ -260,6 +263,8 @@ private static void Compilation()
260263
// double, not Complex: a different set of MathAllMethods overloads.
261264
var d = "sin(x) + cos(x)".Compile<double, double>(x);
262265
CheckClose("compiled double sin(x) + cos(x) at 0.4", d(0.4), Math.Sin(0.4) + Math.Cos(0.4), 1e-12);
266+
var iverson = "iverson(x > 0) * x".Compile<double, double>(x);
267+
CheckClose("compiled iverson(x > 0) x at 2 and -2", iverson(2) + iverson(-2), 2, 1e-12);
263268

264269
// Two arguments, and the floored remainder that takes the sign of the divisor.
265270
var mod = "x mod y".Compile<long, long, long>(x, y);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 AngouriMath.Extensions;
9+
using Xunit;
10+
using NumericsComplex = System.Numerics.Complex;
11+
12+
namespace AngouriMath.Tests.Convenience
13+
{
14+
/// <summary>
15+
/// The conjugate, the principal argument and the real and the imaginary parts compile, through
16+
/// <c>Compile</c> and <c>Compile&lt;TIn, TOut&gt;</c> alike, to the values the interpreter gives;
17+
/// and an Iverson bracket compiles to a number over the reals.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2195">#2195</a>
19+
/// </summary>
20+
[Trait("Area", "Convenience")]
21+
public sealed class ComplexPartsCompilationTest
22+
{
23+
/// <summary>
24+
/// Off the axes, on each half-axis, and at -1 with a negative zero, where the principal
25+
/// argument is pi and Math.Atan2 would give -pi.
26+
/// </summary>
27+
public static readonly TheoryData<string, double, double> Points = new()
28+
{
29+
{ "conj(x)", 0.3, 0.2 }, { "conj(x)", -2.5, -1.5 }, { "conj(x)", 4, 0 },
30+
{ "arg(x)", 0.3, 0.2 }, { "arg(x)", -2.5, -1.5 }, { "arg(x)", -1, 0 }, { "arg(x)", -1, -0.0 },
31+
{ "arg(x)", 0, 3 }, { "arg(x)", 0, -3 }, { "arg(x)", 4, 0 },
32+
{ "Re(x)", 0.3, 0.2 }, { "Re(x)", -2.5, -1.5 }, { "Im(x)", 0.3, 0.2 }, { "Im(x)", -2.5, -1.5 },
33+
{ "Re(x) * Im(x) + conj(x) * arg(x)", 1.25, -0.75 },
34+
};
35+
36+
private static NumericsComplex Interpreted(string expression, double re, double im)
37+
=> ((Entity.Number.Complex)expression.ToEntity().Substitute("x", Entity.Number.Complex.Create(re, im)).EvalNumerical()).ToNumerics();
38+
39+
private static void Close(NumericsComplex expected, NumericsComplex actual)
40+
=> Assert.True((actual - expected).Magnitude <= 1e-12 * System.Math.Max(1, expected.Magnitude), $"expected {expected}, got {actual}");
41+
42+
[Theory]
43+
[MemberData(nameof(Points))]
44+
public void TheCompiledFormAgreesWithTheInterpreter(string expression, double re, double im)
45+
=> Close(Interpreted(expression, re, im), expression.ToEntity().Compile("x").Call(new NumericsComplex(re, im)));
46+
47+
[Theory]
48+
[MemberData(nameof(Points))]
49+
public void TheExpressionTreeAgreesWithTheInterpreter(string expression, double re, double im)
50+
=> Close(Interpreted(expression, re, im),
51+
expression.ToEntity().Compile<NumericsComplex, NumericsComplex>("x")(new NumericsComplex(re, im)));
52+
53+
/// <summary>The argument of 0 has no value, compiled or interpreted.</summary>
54+
[Fact]
55+
public void TheArgumentOfZeroHasNoValue()
56+
{
57+
Assert.True(double.IsNaN("arg(x)".ToEntity().Compile("x").Call(0).Real));
58+
Assert.True(double.IsNaN("arg(x)".ToEntity().Compile<double, double>("x")(0)));
59+
Assert.Equal(MathS.NaN, "arg(0)".ToEntity().Evaled);
60+
}
61+
62+
/// <summary>Over doubles, the argument of a negative number is pi, and the imaginary part is 0.</summary>
63+
[Theory]
64+
[InlineData("arg(x)", -2, System.Math.PI)]
65+
[InlineData("arg(x)", 2, 0)]
66+
[InlineData("Re(x)", -2, -2)]
67+
[InlineData("Im(x)", -2, 0)]
68+
[InlineData("conj(x)", -2, -2)]
69+
[InlineData("iverson(x > 0)", 2, 1)]
70+
[InlineData("iverson(x > 0)", -2, 0)]
71+
[InlineData("iverson(x = 2) * x", 2, 2)]
72+
public void OverTheRealsTheyAreReal(string expression, double at, double expected)
73+
=> Assert.Equal(expected, expression.ToEntity().Compile<double, double>("x")(at), 12);
74+
}
75+
}

0 commit comments

Comments
 (0)