Skip to content

Commit 4abf311

Browse files
Rafael-SOWNetclaude
andcommitted
A piecewise of Iverson brackets and provided integers compiles in LINQ
#2242 widened an Iverson bracket, an int?, and a provided integer, a long?, to double before a function took it. A piecewise's cases did not pass through that widening, so a piecewise with such a case still threw KeyNotFoundException, alone, under a function or nested in another piecewise: MaxType, which picks the type the cases are taken in, has no int? or long?. sin(piecewise(iverson(x > 0) provided x > 0, sin(2))) was one. HandlePiecewise now widens each numeric case as a function's operand is widened. On 2.6.0 an Iverson bracket did not compile at all, and a provided integer in a piecewise over double threw KeyNotFoundException too: piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x at 1. The #2237 line in BREAKING-CHANGES.md now says which of the two threw, and takes in the piecewise. PiecewiseOfNullableCompilationTest compares seven such expressions, under both compilers, with evaluation. All seven fail on master. Part of #2237. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 5f1e85a commit 4abf311

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,9 @@ one where both sides are real, and `NaN` where either is not
437437
- A constant is taken over `Complex` where the expression is compiled over it. As a `long`, a constant widened only
438438
to `double`, so `sqrt(-1)`, `ln(-1)` and `arcsin(2)` were `NaN` and `1/0` was `+oo`
439439
([#2235](https://github.com/asc-community/AngouriMath/issues/2235)).
440-
- An Iverson bracket, an `int?`, and a `provided` integer, a `long?`, are widened to `double` before a function takes
441-
them. They threw `KeyNotFoundException` or `AngouriBugException`
440+
- A `provided` integer, a `long?`, and an Iverson bracket, an `int?`, are widened to `double` before a function or a
441+
`piecewise` takes them. A `provided` integer threw `KeyNotFoundException` or `AngouriBugException` there, and an
442+
Iverson bracket did not compile at all in 2.6.0
442443
([#2237](https://github.com/asc-community/AngouriMath/issues/2237)).
443444
- `sgn(0)` is `0`, where `z / |z|` made it `NaN` ([#2238](https://github.com/asc-community/AngouriMath/issues/2238)).
444445

@@ -447,6 +448,7 @@ one where both sides are real, and `NaN` where either is not
447448
| `"x + sqrt(-1)".ToEntity().Compile<Complex, Complex>("x")(0)` | `(NaN, 0)` | `(6.123233995736766E-17, 1)` |
448449
| `"x + 1/0".ToEntity().Compile<Complex, Complex>("x")(0)` | `(Infinity, 0)` | `(NaN, NaN)` |
449450
| `"x + sgn(-1 provided pi > 3)".ToEntity().Compile<Complex, Complex>("x")(1)` | throws `AngouriBugException` | `(0, 0)` |
451+
| `"piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x".ToEntity().Compile<double, double>("x")(1)` | throws `KeyNotFoundException` | `0` |
450452
| `"sgn(x)".ToEntity().Compile<Complex, Complex>("x")(0)` | `(NaN, NaN)` | `(0, 0)` |
451453

452454
### A compiled product of real numbers is real at an infinity

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
396396
[Tests/UnitTests/Common/LinqCompilationValueTest.cs]
397397
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
398398

399+
[Tests/UnitTests/Common/PiecewiseOfNullableCompilationTest.cs]
400+
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
401+
399402
[Tests/UnitTests/Common/CompileMatrixTest.cs]
400403
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
401404

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ private Expression HandlePiecewise(IEnumerable<Expression> en)
386386
children[i] = ConvertType(children[i], typeof(bool?));
387387
statements++;
388388
}
389+
// A case that is an Iverson bracket or a provided integer is widened to a double, as a function's
390+
// operand is: MaxType has no int? or long?. https://github.com/asc-community/AngouriMath/issues/2237
391+
else
392+
children[i] = NumericOperand(children[i]);
389393
if (statements > 0 && statements * 2 != children.Length)
390394
throw new UncompilableNodeException("A piecewise whose cases are statements and numbers at once has no compiled form");
391395

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Numerics;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Common
14+
{
15+
/// <summary>
16+
/// A piecewise whose case is an Iverson bracket or a provided integer compiles in the LINQ compiler, where it threw
17+
/// <c>KeyNotFoundException</c>: its case types, an <c>int?</c> and a <c>long?</c>, were not in the widening table.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2237">#2237</a>
19+
/// </summary>
20+
[Trait("Area", "Common")]
21+
public sealed class PiecewiseOfNullableCompilationTest
22+
{
23+
[Theory]
24+
[InlineData("sin(piecewise(iverson(x > 0) provided x > 0, sin(2)))", 2)]
25+
[InlineData("sin(piecewise(iverson(x > 0) provided x > 0, sin(2)))", -2)]
26+
[InlineData("exp(piecewise(2^x provided x > 0, iverson(x > 0)))", -1)]
27+
[InlineData("abs(piecewise((-1)^2 provided x > 0, iverson(-1 > 0)))", 1)]
28+
[InlineData("piecewise(iverson(sinh(3) > 0) provided x > 0, tan(-1))", 1)]
29+
[InlineData("piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x", 1)]
30+
[InlineData("piecewise(piecewise(-1 provided x > 0, 3) provided x > 0, x) + x", -1)]
31+
public void ItIsWhatEvaluationGives(string expression, double at)
32+
{
33+
var expected = ((Entity.Number.Complex)expression.ToEntity().Substitute("x", at).Evaled).ToNumerics();
34+
var linq = expression.ToEntity().Compile<Complex, Complex>("x")(at);
35+
var real = expression.ToEntity().Compile<double, double>("x")(at);
36+
Assert.True(Complex.Abs(expected - linq) < 1e-12, $"{expression} at {at}: {expected} evaluated, {linq} by LINQ");
37+
Assert.True(System.Math.Abs(expected.Real - real) < 1e-12, $"{expression} at {at}: {expected} evaluated, {real} over double");
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)