|
| 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<TIn, TOut></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