Skip to content

Commit a88080e

Browse files
A piecewise case whose condition is undefined is passed over (#1764)
A piecewise case whose predicate evaluates to NaN -- an order comparison off the real line, i < 0 -- made the whole piecewise NaN, even where another case held: piecewise(1 provided i < 0, 2 provided 1 > 0) was NaN. Piecewise.InnerSimplify skipped a false case and kept an undefined one; it passes over both now, in the pass that decides the piecewise and in the one that keeps the undecided cases. The derivative of an antiderivative with an arm for each sign of a quantity off the real line was NaN at every point for this. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 763f7ba commit a88080e

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ the working digits, or `MathS.Settings.PrecisionErrorZeroRange` where a caller h
120120
still that integer, so `e^(-123.456)` is 0 as it was.
121121
[#1338](https://github.com/asc-community/AngouriMath/issues/1338).
122122

123+
### A piecewise case whose condition is undefined is passed over
124+
125+
**A different answer.** A case whose predicate evaluates to NaN -- an order comparison of a number
126+
off the real line, `i < 0`, the complex numbers not being ordered -- made the whole piecewise NaN,
127+
even where another case held. It holds no more than a false predicate does now: the case is never
128+
taken, and the cases after it are read as before. The derivative of an antiderivative written with
129+
an arm for each sign of a quantity that is not real, `x^2 provided not x = 0` beside
130+
`x^3 provided -13/10 i - 3/5 < 0`, came out NaN that way, where it is `2x`.
131+
132+
| Input | Was (2.5.0) | Now |
133+
|---|---|---|
134+
| `"piecewise(1 provided i < 0, 2 provided 1 > 0)".ToEntity().InnerSimplified` | `NaN` | `2` |
135+
| `"piecewise(2 * x provided not x = 0, 3 * x ^ 2 provided i - 1 < 0)".ToEntity().InnerSimplified` | `NaN` | `2 * x` where `x` is not 0 |
136+
| `"piecewise(1 provided i < 0, 2 provided i > 0)".ToEntity().InnerSimplified` | `NaN` | `NaN`, as before: no case holds |
137+
123138
### The factorial far from zero, and a quotient by a number whose modulus is nearly whole
124139

125140
The numerical factorial is Spouge's approximation, and it holds right of zero. Used left of zero it

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,23 @@ partial record Piecewise
470470
/// <inheritdoc/>
471471
protected override Entity InnerSimplify(bool isExact)
472472
{
473+
// A predicate that evaluates to NaN -- `i < 0`, the complex numbers not being
474+
// ordered -- holds no more than a false one does: the case is never taken, and the
475+
// cases after it are read as they would be. Kept, it made the whole piecewise NaN,
476+
// so that `piecewise(2x provided not x = 0, 3x^2 provided i - 1 < 0)` had no value
477+
// at any point, and the derivative of an antiderivative written with an arm for
478+
// each sign of a quantity that is not real came out NaN where it is `2x`.
473479
foreach (var oneCase in Cases)
474480
{
481+
if (oneCase.Predicate.Evaled.IsNaN) continue;
475482
if (oneCase.Predicate.Evaled is not Boolean) goto notYetDecidable;
476483
if (oneCase.Predicate.Evaled == Boolean.True) return oneCase.Expression.InnerSimplified(isExact);
477484
}
478485
return MathS.NaN;
479486
notYetDecidable:
480487
var res = new List<Providedf>();
481488
foreach (var (@case, srcCase) in (Cases, Cases.Select(c => c.New(c.Expression.InnerSimplified(isExact), c.Predicate.InnerSimplified(isExact)))).Zip()) {
482-
if (@case.Predicate.Evaled == Boolean.False) continue;
489+
if (@case.Predicate.Evaled == Boolean.False || @case.Predicate.Evaled.IsNaN) continue;
483490
var toAdd = srcCase.Expression is Providedf(var inner, var pred) ? new Providedf(inner, (srcCase.Predicate & pred).InnerSimplified(isExact)) : srcCase;
484491

485492
// A piecewise takes its first matching case, and both rules below follow
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
using static AngouriMath.Entity;
13+
14+
namespace AngouriMath.Tests.Core
15+
{
16+
/// <summary>
17+
/// A piecewise case whose predicate evaluates to NaN -- an order comparison of a number off
18+
/// the real line, <c>i - 1 &lt; 0</c> -- is never taken, as a false one is not, and the cases
19+
/// after it are read as they would be. Kept, it made the whole piecewise NaN.
20+
/// </summary>
21+
[Trait("Area", "Core")]
22+
public sealed class UndefinedPiecewiseConditionTest
23+
{
24+
private static void Near(double expected, Entity value)
25+
=> Assert.True(Math.Abs((double)((Number.Complex)value.EvalNumerical()).RealPart - expected) < 1e-12, $"{value} is not {expected}");
26+
27+
[Fact]
28+
public void TheCaseIsSkippedAndTheRestKept()
29+
{
30+
var simplified = "piecewise(2 * x provided not x = 0, 3 * x ^ 2 provided i - 1 < 0)".ToEntity().InnerSimplified;
31+
Assert.False(simplified.Evaled.IsNaN);
32+
Near(0.6, simplified.Substitute("x", 0.3));
33+
}
34+
35+
[Fact]
36+
public void AnUndefinedCaseBeforeADecidedOneIsPassedOver()
37+
=> Assert.Equal(Number.Integer.Create(2), "piecewise(1 provided i < 0, 2 provided 1 > 0)".ToEntity().InnerSimplified);
38+
39+
[Fact]
40+
public void WithNoCaseThatHoldsItIsStillNaN()
41+
=> Assert.True("piecewise(1 provided i < 0, 2 provided i > 0)".ToEntity().InnerSimplified.Evaled.IsNaN);
42+
43+
// How an antiderivative with an arm for each sign of a quantity off the real line is
44+
// differentiated back: the arm for the negative sign is undefined there, and the
45+
// derivative was NaN where it is that of the arm which holds.
46+
[Fact]
47+
public void TheDerivativeIsThatOfTheCaseThatHolds()
48+
{
49+
var derivative = "piecewise(x ^ 2 provided not x = 0, x ^ 3 provided -13/10 * i - 3/5 < 0)".ToEntity().Differentiate("x");
50+
Near(0.6, derivative.Substitute("x", 0.3));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)