Skip to content

Commit f8e9734

Browse files
A piecewise is defined where the case it takes is (#1819)
Its domain condition asked every case's predicate to be defined, and an ordering is defined only between reals. Fixes #1817. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 77899a6 commit f8e9734

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ 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 is defined where the case it takes is
124+
125+
**Domain conditions that were false.** The domain condition of a piecewise asked every case's predicate
126+
to be defined, and an ordering is defined only between reals: `piecewise(1 provided not i in RR, 2 provided i < 0)`
127+
is `1`, and its domain condition was `false`. It is where some case's predicate holds, no earlier one does
128+
-- a predicate with no value passed over, as the simplification passes over it -- and that case's expression
129+
is defined. An antiderivative with an arm for each sign of a discriminant and one for its not being real
130+
carried the discriminant's being real in its condition, and held nowhere when it was not
131+
([#1817](https://github.com/asc-community/AngouriMath/issues/1817)).
132+
133+
| Input | Was (2.5.0) | Now |
134+
|---|---|---|
135+
| `"piecewise(1 provided not i in RR, 2 provided i < 0)".ToEntity().DomainCondition` | `false` | `true` |
136+
| `"piecewise(2 * x provided not x = 0, 3 * x ^ 2 provided i - 1 < 0)".ToEntity().DomainCondition` | `false` | `not x = 0` |
137+
| `"1/(sqrt(a + i*a*x)*(c + d*x)^(3/2)*(a - i*a*x))".ToEntity().Integrate("x")` | `integral(...)`; on the unreleased master, an answer with no value at any point | 3,267 characters, defined |
138+
123139
### A piecewise case whose condition is undefined is passed over
124140

125141
**A different answer.** A case whose predicate evaluates to NaN -- an order comparison of a number

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,45 @@ partial record Entity
4242
/// - Singularities and poles (points where a function is undefined)
4343
/// - Piecewise continuity (tracking where discontinuities occur)
4444
/// </remarks>
45-
public Entity DomainCondition => domainCondition.GetValue(static @this => ScopedToItsBinder(@this, @this.DirectChildren.Aggregate(@this.IntrinsicCondition, (accum, curr) =>
45+
public Entity DomainCondition => domainCondition.GetValue(static @this => (@this is Piecewise piecewise ? WhereTheCaseItTakesIsDefined(piecewise)
46+
: ScopedToItsBinder(@this, @this.DirectChildren.Aggregate(@this.IntrinsicCondition, (accum, curr) =>
4647
(accum, curr.DomainCondition) switch {
4748
(Boolean(true), Boolean(true)) => Boolean.True,
4849
(var l, Boolean(true)) => l,
4950
(Boolean(true), var r) => r,
5051
(var l, var r) => l & r,
51-
})), this).InnerSimplified;
52+
}))), this).InnerSimplified;
5253
private LazyPropertyA<Entity> domainCondition;
5354

55+
/// <summary>
56+
/// Where a piecewise is defined: where some case's predicate holds, no earlier one does,
57+
/// and that case's expression is defined. An earlier predicate that has no value -- <c>i &lt; 0</c>,
58+
/// the complex numbers not being ordered -- is passed over, as
59+
/// <see cref="Piecewise"/>'s simplification passes over it.
60+
/// </summary>
61+
/// <remarks>
62+
/// The domain condition of every child, conjoined, asked each predicate to be defined, and
63+
/// <c>c &lt; 0</c> is defined only where <c>c</c> is real: <c>piecewise(1 provided not i in RR, 2 provided i &lt; 0)</c>
64+
/// is 1 and its domain condition was false, and an antiderivative with an arm for each sign of
65+
/// a discriminant and one for its not being real held nowhere when the discriminant was not.
66+
/// https://github.com/asc-community/AngouriMath/issues/1817
67+
/// </remarks>
68+
private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise)
69+
{
70+
Entity defined = Boolean.False;
71+
Entity noEarlierCase = Boolean.True;
72+
foreach (var @case in piecewise.Cases)
73+
{
74+
// Never taken, and passed over by the cases after it.
75+
if (@case.Predicate.Evaled is var decided && (decided.IsNaN || decided == Boolean.False))
76+
continue;
77+
var predicateDomain = @case.Predicate.DomainCondition;
78+
defined |= @case.Predicate & @case.Expression.DomainCondition & noEarlierCase;
79+
noEarlierCase &= predicateDomain == Boolean.True ? !@case.Predicate : !@case.Predicate | !predicateDomain;
80+
}
81+
return defined;
82+
}
83+
5484
/// <summary>
5585
/// <paramref name="condition"/> with each conjunct that mentions a name
5686
/// <paramref name="binder"/> binds read the way the binder reads it: required at every

‎Sources/Tests/UnitTests/Core/UndefinedPiecewiseConditionTest.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,37 @@ public void TheDerivativeIsThatOfTheCaseThatHolds()
4949
var derivative = "piecewise(x ^ 2 provided not x = 0, x ^ 3 provided -13/10 * i - 3/5 < 0)".ToEntity().Differentiate("x");
5050
Near(0.6, derivative.Substitute("x", 0.3));
5151
}
52+
53+
// Where the piecewise is defined: where the case it takes is, an undefined predicate before
54+
// it passed over. https://github.com/asc-community/AngouriMath/issues/1817
55+
[Theory]
56+
[InlineData("piecewise(1 provided not i in RR, 2 provided i < 0)", "true")]
57+
[InlineData("piecewise(1 provided i < 0, 2 provided 1 > 0)", "true")]
58+
[InlineData("piecewise(2 * x provided not x = 0, 3 * x ^ 2 provided i - 1 < 0)", "not x = 0")]
59+
[InlineData("piecewise(1 provided i < 0, 2 provided i > 0)", "false")]
60+
public void TheDomainIsWhereTheCaseItTakesIsDefined(string piecewise, string domain)
61+
=> Assert.Equal(domain.ToEntity().InnerSimplified, piecewise.ToEntity().DomainCondition);
62+
63+
// Off the real line: the arm for the quantity not being real is taken, and the domain does
64+
// not ask the comparisons in the other arms to be defined.
65+
[Theory]
66+
[InlineData(0.7, 1.07)]
67+
[InlineData(-0.7, 2.0)]
68+
public void AnArmForANonRealQuantityKeepsTheDomain(double c, double d)
69+
{
70+
var domain = "piecewise(1 provided not (c + i*d) in RR, 2 provided c + i*d < 0, 3 provided c + i*d > 0)".ToEntity().DomainCondition;
71+
Assert.Equal(Entity.Boolean.True, domain.Substitute("c", c).Substitute("d", d).Evaled);
72+
}
73+
74+
// A case taken where its expression has no value leaves the piecewise undefined there,
75+
// whatever the cases after it say.
76+
[Fact]
77+
public void TheFirstCaseThatHoldsDecides()
78+
{
79+
var domain = "piecewise(1/x provided x > -1, 0 provided true)".ToEntity().DomainCondition;
80+
Assert.Equal(Entity.Boolean.False, domain.Substitute("x", 0).Evaled);
81+
Assert.Equal(Entity.Boolean.True, domain.Substitute("x", -2).Evaled);
82+
Assert.Equal(Entity.Boolean.True, domain.Substitute("x", 1).Evaled);
83+
}
5284
}
5385
}

0 commit comments

Comments
 (0)