Skip to content

Commit 5d4a300

Browse files
A compiled function takes the side of its branch cut evaluation takes (#2240)
System.Numerics.Complex computes -0 as a part of many real values. cos(2) is (cos 2, -sin 2 * sinh 0), which is (-0.416, -0), and conj(-2) is (-2, -0). Complex.Log, Complex.Sqrt and the inverse functions read a -0 as the lower side of the negative real axis. The library has no signed zero, so a real number is on the upper side. So in both compilers, ln(cos(x)) at 2 was -0.8767 - pi i where evaluation gives -0.8767 + pi i, and sqrt(conj(x)) at -4 was -2i. ComplexBranches.Unsigned reads each zero part as +0, by adding +0.0, which turns -0 into +0 and changes no other number. It is taken at the entry of every compiled function with a cut, in FastExpression and in MathAllMethods: - the logarithm; - a power that is not whole, in RealProducts.Power; - arcsin, arccos, arctan, arccotan, arcsec and arccosec; - arg; - the exponential and logarithmic integrals; - lambertw. ComplexBranches.Arcsin already conjugated on its cut, and now does so from the +0 side, which is what it was written for. FastExpression's logarithm was Complex.Log(value, base.Real), whose base parameter is a double, so a complex base lost its imaginary part: log(x, 2) at 1 + i was NaN. It is ComplexBranches.Log now, ln(value) / ln(base), as the LINQ compiler already took it (#2239). BranchCutCompilationTest takes each of the 19 functions at 11 real points, with its argument written three ways that land on a cut: conj(x), cos(x) and i * x. It compares both compilers with evaluation, and adds three logarithms to complex bases. 9 of its 22 cases fail on master. A pole is left out, since there the infinity has a NaN part, which is not what this is about. So is an exponent with an odd denominator, where evaluation takes the real root, which a double exponent cannot show. Fixes #2236. Fixes #2239. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b8a6a6f commit 5d4a300

8 files changed

Lines changed: 186 additions & 39 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,23 @@ then had no truth value for. A product or a quotient of two real numbers is now
445445
| `"-x".ToEntity().Compile("x").Call(double.PositiveInfinity)` | `(-Infinity, NaN)` | `(-Infinity, 0)` |
446446
| `"x / 2".ToEntity().Compile<Complex, Complex>("x")(double.PositiveInfinity)` | `(Infinity, NaN)` | `(Infinity, 0)` |
447447

448+
### A compiled function takes the side of its branch cut evaluation takes
449+
450+
**Silent: a compiled logarithm, root or inverse function of a number on its cut is evaluation's value, where it was
451+
the conjugate.** `System.Numerics.Complex` computes `-0` as a part of many real values, `cos(2)` being `(-0.416, -0)`,
452+
and reads it as the lower side of a cut. The library has no signed zero, so a real number is on the upper side. Every
453+
compiled function with a cut now reads a zero part as `+0`: the logarithm, a power that is not whole, the inverse
454+
trigonometric functions, `arg`, the exponential and logarithmic integrals and `lambertw`, in FastExpression and in the
455+
LINQ compiler. A `conj` of a real number, new to compile since 2.6.0, took the same wrong side, as `ln(conj(x))` at
456+
`-2` did ([#2236](https://github.com/asc-community/AngouriMath/issues/2236)). FastExpression's logarithm also kept only
457+
the real part of a complex base, and takes `ln(value) / ln(base)` now
458+
([#2239](https://github.com/asc-community/AngouriMath/issues/2239)).
459+
460+
| Input | Was (2.6.0) | Now |
461+
|---|---|---|
462+
| `"ln(cos(x))".ToEntity().Compile("x").Call(2)` | `(-0.8767171085319084, -3.141592653589793)` | `(-0.8767171085319084, 3.141592653589793)` |
463+
| `"log(x, 2)".ToEntity().Compile("x").Call(new Complex(1, 1))` | `(NaN, NaN)` | `(0.32596797216345985, -0.7387021222729507)` |
464+
448465
### A compiled power of real numbers is the real power, at an infinity and at zero
449466

450467
**Silent: a compiled power over `Complex` is what evaluation gives where it had no value or the wrong one.** In

‎Sources/.editorconfig‎

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

932932
[Tests/UnitTests/Common/RealPowerCompilationTest.cs]
933933
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
934+
935+
[Tests/UnitTests/Common/BranchCutCompilationTest.cs]
936+
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/ComplexBranches.cs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,36 @@ internal static class ComplexBranches
3939
/// see https://github.com/asc-community/AngouriMath/issues/115.
4040
/// </remarks>
4141
public static NumericsComplex Arcsin(NumericsComplex a)
42-
=> a.Imaginary == 0 && Math.Abs(a.Real) > 1
42+
=> (a = Unsigned(a)).Imaginary == 0 && Math.Abs(a.Real) > 1
4343
? NumericsComplex.Conjugate(NumericsComplex.Asin(a))
4444
: NumericsComplex.Asin(a);
4545

46+
/// <summary>
47+
/// <paramref name="z"/> with each zero part read as <c>+0</c>. The library has no signed zero, so a real
48+
/// number is on the upper side of every branch cut it lies on; <see cref="System.Numerics.Complex"/>
49+
/// computes <c>-0</c> as a part of many real values, <c>cos(2)</c> and <c>conj(-2)</c> among them, and reads
50+
/// it as the lower side, which made <c>ln(cos(x))</c> at 2 the conjugate of the logarithm. Adding <c>+0</c>
51+
/// turns <c>-0</c> into <c>+0</c> and changes no other number. Taken at the entry of every function with a cut.
52+
/// https://github.com/asc-community/AngouriMath/issues/2236
53+
/// </summary>
54+
public static NumericsComplex Unsigned(NumericsComplex z) => new(z.Real + 0.0, z.Imaginary + 0.0);
55+
56+
/// <summary>
57+
/// The logarithm of <paramref name="value"/> to <paramref name="base"/>, <c>ln(value) / ln(base)</c> for any
58+
/// base, where FastExpression's <c>Complex.Log(value, base.Real)</c> dropped a complex base's imaginary part.
59+
/// https://github.com/asc-community/AngouriMath/issues/2239
60+
/// </summary>
61+
public static NumericsComplex Log(NumericsComplex @base, NumericsComplex value)
62+
=> RealProducts.Quotient(NumericsComplex.Log(Unsigned(value)), NumericsComplex.Log(Unsigned(@base)));
63+
64+
public static NumericsComplex Arccos(NumericsComplex a) => NumericsComplex.Acos(Unsigned(a));
65+
66+
public static NumericsComplex Arctan(NumericsComplex a) => NumericsComplex.Atan(Unsigned(a));
67+
68+
public static NumericsComplex Arccotan(NumericsComplex a) => NumericsComplex.Atan(Unsigned(1 / a));
69+
70+
public static NumericsComplex Arcsecant(NumericsComplex a) => NumericsComplex.Acos(Unsigned(1 / a));
71+
4672
/// <summary>
4773
/// The arccosecant the library evaluates to, which is <see cref="Arcsin"/> of the
4874
/// reciprocal and so lands on the same cuts -- for arccosecant, at the real arguments

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,22 +250,25 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
250250
stack.Push(1 / System.Numerics.Complex.Tan(stack.Pop()));
251251
break;
252252
case InstructionType.CALL_LOG:
253-
stack.Push(System.Numerics.Complex.Log(stack.Pop(), stack.Pop().Real));
253+
{
254+
var value = stack.Pop();
255+
stack.Push(Core.Compilation.ComplexBranches.Log(stack.Pop(), value));
256+
}
254257
break;
255258
case InstructionType.CALL_ARCSIN:
256259
stack.Push(Core.Compilation.ComplexBranches.Arcsin(stack.Pop()));
257260
break;
258261
case InstructionType.CALL_ARCCOS:
259-
stack.Push(System.Numerics.Complex.Acos(stack.Pop()));
262+
stack.Push(Core.Compilation.ComplexBranches.Arccos(stack.Pop()));
260263
break;
261264
case InstructionType.CALL_ARCTAN:
262-
stack.Push(System.Numerics.Complex.Atan(stack.Pop()));
265+
stack.Push(Core.Compilation.ComplexBranches.Arctan(stack.Pop()));
263266
break;
264267
case InstructionType.CALL_ARCCOTAN:
265-
stack.Push(System.Numerics.Complex.Atan(1 / stack.Pop()));
268+
stack.Push(Core.Compilation.ComplexBranches.Arccotan(stack.Pop()));
266269
break;
267270
case InstructionType.CALL_ARCSECANT:
268-
stack.Push(System.Numerics.Complex.Acos(1 / stack.Pop()));
271+
stack.Push(Core.Compilation.ComplexBranches.Arcsecant(stack.Pop()));
269272
break;
270273
case InstructionType.CALL_ARCCOSECANT:
271274
stack.Push(Core.Compilation.ComplexBranches.Arccosecant(stack.Pop()));
@@ -282,22 +285,22 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
282285
stack.Push(AngouriMath.Numerics.SpecialFunctions.Erfi(stack.Pop()));
283286
break;
284287
case InstructionType.CALL_EI:
285-
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ei(stack.Pop()));
288+
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ei(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
286289
break;
287290
case InstructionType.CALL_LI:
288-
stack.Push(AngouriMath.Numerics.SpecialFunctions.Li(stack.Pop()));
291+
stack.Push(AngouriMath.Numerics.SpecialFunctions.Li(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
289292
break;
290293
case InstructionType.CALL_SI:
291294
stack.Push(AngouriMath.Numerics.SpecialFunctions.Si(stack.Pop()));
292295
break;
293296
case InstructionType.CALL_CI:
294-
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ci(stack.Pop()));
297+
stack.Push(AngouriMath.Numerics.SpecialFunctions.Ci(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
295298
break;
296299
case InstructionType.CALL_SHI:
297300
stack.Push(AngouriMath.Numerics.SpecialFunctions.Shi(stack.Pop()));
298301
break;
299302
case InstructionType.CALL_CHI:
300-
stack.Push(AngouriMath.Numerics.SpecialFunctions.Chi(stack.Pop()));
303+
stack.Push(AngouriMath.Numerics.SpecialFunctions.Chi(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
301304
break;
302305
case InstructionType.CALL_FACTORIAL:
303306
stack.Push(AngouriMath.Numerics.SpecialFunctions.Factorial(stack.Pop()));
@@ -315,7 +318,7 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
315318
stack.Push(AngouriMath.Numerics.ComplexParts.Conj(stack.Pop()));
316319
break;
317320
case InstructionType.CALL_ARG:
318-
stack.Push(AngouriMath.Numerics.ComplexParts.Arg(stack.Pop()));
321+
stack.Push(AngouriMath.Numerics.ComplexParts.Arg(Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
319322
break;
320323
case InstructionType.CALL_RE:
321324
stack.Push(AngouriMath.Numerics.ComplexParts.Re(stack.Pop()));
@@ -374,7 +377,7 @@ public System.Numerics.Complex Substitute(params System.Numerics.Complex[] value
374377
stack.Push(AngouriMath.Numerics.CombinatorialFunctions.RisingFactorial(stack.Pop(), stack.Pop()));
375378
break;
376379
case InstructionType.CALL_LAMBERTW:
377-
stack.Push(AngouriMath.Numerics.LambertWFunction.LambertW(stack.Pop(), stack.Pop()));
380+
stack.Push(AngouriMath.Numerics.LambertWFunction.LambertW(stack.Pop(), Core.Compilation.ComplexBranches.Unsigned(stack.Pop())));
378381
break;
379382
case InstructionType.CALL_POLYGAMMA:
380383
stack.Push(AngouriMath.Numerics.PolygammaFunction.Polygamma(stack.Pop(), stack.Pop()));

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static class MathAllMethods
2323
/* POWERS */
2424

2525
public static System.Numerics.Complex Log(System.Numerics.Complex a, System.Numerics.Complex b)
26-
=> System.Numerics.Complex.Log(b) / System.Numerics.Complex.Log(a);
26+
=> AngouriMath.Core.Compilation.ComplexBranches.Log(a, b);
2727
public static double Log(double a, double b)
2828
=> Math.Log(b, a);
2929
public static float Log(float a, float b)
@@ -109,7 +109,7 @@ public static BigInteger Asin(BigInteger a)
109109

110110

111111
public static System.Numerics.Complex Acos(System.Numerics.Complex a)
112-
=> System.Numerics.Complex.Acos(a);
112+
=> AngouriMath.Core.Compilation.ComplexBranches.Arccos(a);
113113
public static double Acos(double a)
114114
=> Math.Acos(a);
115115
public static float Acos(float a)
@@ -123,7 +123,7 @@ public static BigInteger Acos(BigInteger a)
123123

124124

125125
public static System.Numerics.Complex Atan(System.Numerics.Complex a)
126-
=> System.Numerics.Complex.Atan(a);
126+
=> AngouriMath.Core.Compilation.ComplexBranches.Arctan(a);
127127
public static double Atan(double a)
128128
=> Math.Atan(a);
129129
public static float Atan(float a)
@@ -185,7 +185,7 @@ public static BigInteger Csc(BigInteger a)
185185

186186

187187
public static System.Numerics.Complex Acot(System.Numerics.Complex a)
188-
=> System.Numerics.Complex.Atan(1 / a);
188+
=> AngouriMath.Core.Compilation.ComplexBranches.Arccotan(a);
189189
public static double Acot(double a)
190190
=> Math.Atan(1 / a);
191191
public static float Acot(float a)
@@ -199,7 +199,7 @@ public static BigInteger Acot(BigInteger a)
199199

200200

201201
public static System.Numerics.Complex Asec(System.Numerics.Complex a)
202-
=> System.Numerics.Complex.Acos(1 / a);
202+
=> AngouriMath.Core.Compilation.ComplexBranches.Arcsecant(a);
203203
public static double Asec(double a)
204204
=> Math.Acos(1 / a);
205205
public static float Asec(float a)
@@ -299,36 +299,36 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
299299

300300
// The special functions, in double precision as the kernels define them, and the factorial,
301301
// the gamma function one along. https://github.com/asc-community/AngouriMath/issues/1607
302-
public static System.Numerics.Complex Erf(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erf(a);
302+
public static System.Numerics.Complex Erf(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erf(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
303303
public static double Erf(double a) => AngouriMath.Numerics.SpecialFunctions.Erf(a);
304-
public static System.Numerics.Complex Erfc(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erfc(a);
304+
public static System.Numerics.Complex Erfc(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erfc(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
305305
public static double Erfc(double a) => AngouriMath.Numerics.SpecialFunctions.Erfc(a);
306-
public static System.Numerics.Complex Erfi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erfi(a);
306+
public static System.Numerics.Complex Erfi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Erfi(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
307307
public static double Erfi(double a) => AngouriMath.Numerics.SpecialFunctions.Erfi(a);
308-
public static System.Numerics.Complex Ei(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Ei(a);
308+
public static System.Numerics.Complex Ei(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Ei(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
309309
public static double Ei(double a) => AngouriMath.Numerics.SpecialFunctions.Ei(a);
310-
public static System.Numerics.Complex Li(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Li(a);
310+
public static System.Numerics.Complex Li(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Li(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
311311
public static double Li(double a) => AngouriMath.Numerics.SpecialFunctions.Li(a);
312-
public static System.Numerics.Complex Si(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Si(a);
312+
public static System.Numerics.Complex Si(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Si(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
313313
public static double Si(double a) => AngouriMath.Numerics.SpecialFunctions.Si(a);
314-
public static System.Numerics.Complex Ci(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Ci(a);
314+
public static System.Numerics.Complex Ci(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Ci(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
315315
public static double Ci(double a) => AngouriMath.Numerics.SpecialFunctions.Ci(a);
316-
public static System.Numerics.Complex Shi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Shi(a);
316+
public static System.Numerics.Complex Shi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Shi(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
317317
public static double Shi(double a) => AngouriMath.Numerics.SpecialFunctions.Shi(a);
318-
public static System.Numerics.Complex Chi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Chi(a);
318+
public static System.Numerics.Complex Chi(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Chi(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
319319
public static double Chi(double a) => AngouriMath.Numerics.SpecialFunctions.Chi(a);
320-
public static System.Numerics.Complex Factorial(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a);
320+
public static System.Numerics.Complex Factorial(System.Numerics.Complex a) => AngouriMath.Numerics.SpecialFunctions.Factorial(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
321321
public static double Factorial(double a) => AngouriMath.Numerics.SpecialFunctions.Factorial(a);
322322

323323
// The conjugate, the principal argument and the real and the imaginary parts.
324324
// https://github.com/asc-community/AngouriMath/issues/2195
325-
public static System.Numerics.Complex Conj(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Conj(a);
325+
public static System.Numerics.Complex Conj(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Conj(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
326326
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);
327+
public static System.Numerics.Complex Arg(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Arg(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
328328
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);
329+
public static System.Numerics.Complex Re(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Re(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
330330
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);
331+
public static System.Numerics.Complex Im(System.Numerics.Complex a) => AngouriMath.Numerics.ComplexParts.Im(AngouriMath.Core.Compilation.ComplexBranches.Unsigned(a));
332332
public static double Im(double a) => AngouriMath.Numerics.ComplexParts.Im(a);
333333

334334
// The binomial coefficient, the factorial powers and the double factorial, on the interpreter's
@@ -346,7 +346,7 @@ public static System.Numerics.Complex Round(System.Numerics.Complex a)
346346

347347
// The Lambert W function on the branch the first argument names.
348348
// https://github.com/asc-community/AngouriMath/issues/2195
349-
public static System.Numerics.Complex LambertW(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
349+
public static System.Numerics.Complex LambertW(System.Numerics.Complex a, System.Numerics.Complex b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, AngouriMath.Core.Compilation.ComplexBranches.Unsigned(b));
350350
public static double LambertW(double a, double b) => AngouriMath.Numerics.LambertWFunction.LambertW(a, b);
351351

352352
// The polygamma functions of a whole order, and the harmonic numbers.

0 commit comments

Comments
 (0)