Skip to content

Commit 6d2a960

Browse files
A piecewise's condition under a reading asks only the case it takes (#2262)
DomainCondition reads a piecewise as defined where the case it takes is defined (#1817), but DomainConditionIn conjoined every case's condition. piecewise(sqrt(x) provided x > 0, 1) is 1 at -4, and its condition over the reals, x >= 0 and x in RR, failed there; so did tan(piecewise(pi / x provided x > 0, x / 2)) at 0, where it is 0. Over the complex numbers the two disagreed too. WhereTheCaseItTakesIsDefined now takes the condition it asks of a predicate and a case, and DomainConditionIn passes its own reading through it. PiecewiseConditionInAReadingTest takes four piecewise expressions at seven points over the reals, and three over the complex numbers, where the reading must agree with DomainCondition; four fail on master. Found by work/domaincheck. Part of #2256. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b90ae8f commit 6d2a960

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,17 @@ real roots, so those are now the kinks, and each piece is solved exactly
740740
| `"x^2/abs(1-x^2) = x".Solve("x")` | `{ 0, 0.618…, 1.618…, 1.618… + 1.7E-9i, … }` | `{ 0, (-1 + sqrt(5)) / 2, (1 + sqrt(5)) / 2 }` |
741741
| `"abs(x^2 - 4) = 3*x".Solve("x")` | `{ 1, 4, 4.0000000000000062… + 1.6E-7i }` | `{ 1, 4 }` |
742742

743+
### A piecewise's condition under a reading asks only the case it takes
744+
745+
**Silent: `DomainConditionIn` of a `piecewise` is where the case it takes is defined, as `DomainCondition` already
746+
reads it (#1817), where it asked every case to be defined.** `piecewise(sqrt(x) provided x > 0, 1)` is 1 at `-4`, and its
747+
condition over the reals failed there. The library asks for this condition under the ambient reading, so the change
748+
reaches whatever consults it there ([#2256](https://github.com/asc-community/AngouriMath/issues/2256)).
749+
750+
| Input | Was (2.6.0) | Now |
751+
|---|---|---|
752+
| `"piecewise(sqrt(x) provided x > 0, 1)".ToEntity().DomainConditionIn(Domain.Real)` | `x >= 0 and x in RR` | `x > 0 and x >= 0 or not x > 0 or not x in RR` |
753+
743754
### A system with more or fewer equations than unknowns is solved or declined
744755

745756
**Not silent: what was an exception is an answer or `null`.** `EquationSystem.Solve` threw

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
258258
[Tests/UnitTests/Convenience/IversonTest.cs]
259259
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
260260

261+
[Tests/UnitTests/Convenience/PiecewiseConditionInAReadingTest.cs]
262+
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
263+
261264
[Tests/UnitTests/Convenience/MatrixFunctionsInAnExpressionTest.cs]
262265
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
263266

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ partial record Entity
6666
/// https://github.com/asc-community/AngouriMath/issues/1817
6767
/// </remarks>
6868
private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise)
69+
=> WhereTheCaseItTakesIsDefined(piecewise, static entity => entity.DomainCondition);
70+
71+
/// <summary>
72+
/// As above, with <paramref name="conditionOf"/> for where a predicate and a case's expression are defined:
73+
/// <see cref="DomainCondition"/>, or <see cref="DomainConditionIn(Domain)"/> under a reading.
74+
/// </summary>
75+
private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise, System.Func<Entity, Entity> conditionOf)
6976
{
7077
Entity defined = Boolean.False;
7178
Entity noEarlierCase = Boolean.True;
@@ -74,8 +81,8 @@ private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise)
7481
// Never taken, and passed over by the cases after it.
7582
if (@case.Predicate.Evaled is var decided && (decided.IsNaN || decided == Boolean.False))
7683
continue;
77-
var predicateDomain = @case.Predicate.DomainCondition;
78-
defined |= @case.Predicate & @case.Expression.DomainCondition & noEarlierCase;
84+
var predicateDomain = conditionOf(@case.Predicate);
85+
defined |= @case.Predicate & conditionOf(@case.Expression) & noEarlierCase;
7986
noEarlierCase &= predicateDomain == Boolean.True ? !@case.Predicate : !@case.Predicate | !predicateDomain;
8087
}
8188
return defined;
@@ -371,6 +378,11 @@ private static IEnumerable<Entity> Conjuncts(Entity condition)
371378
/// </remarks>
372379
public Entity DomainConditionIn(Domain reading)
373380
{
381+
// A piecewise is defined where the case it takes is, as DomainCondition reads it, and not where every case
382+
// is: tan(piecewise(pi / x provided x > 0, x / 2)) is 0 at 0, and its condition over the reals asked the
383+
// first case to be defined there. https://github.com/asc-community/AngouriMath/issues/2256
384+
if (this is Piecewise piecewise)
385+
return WhereTheCaseItTakesIsDefined(piecewise, entity => entity.DomainConditionIn(reading)).InnerSimplified;
374386
var read = Codomain > reading ? WithCodomain(reading) : this;
375387
var condition = read.IntrinsicCondition;
376388
foreach (var child in DirectChildren)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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;
9+
using AngouriMath.Core;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Convenience
14+
{
15+
/// <summary>
16+
/// A piecewise is defined under a reading where the case it takes is, as <see cref="Entity.DomainCondition"/> already
17+
/// reads it, where <see cref="Entity.DomainConditionIn(Domain)"/> asked every case to be defined:
18+
/// <c>tan(piecewise(pi / x provided x &gt; 0, x / 2))</c> is 0 at 0, and its condition over the reals failed there.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2256">#2256</a>
20+
/// </summary>
21+
[Trait("Area", "Convenience")]
22+
public sealed class PiecewiseConditionInAReadingTest
23+
{
24+
[Theory]
25+
[InlineData("tan(piecewise(pi / x provided x > 0, x / 2))", "0", true)]
26+
[InlineData("tan(piecewise(pi / x provided x > 0, x / 2))", "-2", true)]
27+
[InlineData("tan(piecewise(pi / x provided x > 0, x / 2))", "2", false)]
28+
[InlineData("piecewise(arccos(1 / x) provided x > 0, sec(sgn(x)))", "-1/2", true)]
29+
[InlineData("piecewise(arccos(1 / x) provided x > 0, sec(sgn(x)))", "1/2", false)]
30+
[InlineData("piecewise(sqrt(x) provided x > 0, 1)", "-4", true)]
31+
[InlineData("piecewise(sqrt(x - 1) provided x > 0, 1)", "1/2", false)]
32+
public void OverTheRealsItIsWhereTheCaseItTakesIs(string expression, string at, bool defined)
33+
{
34+
var condition = expression.ToEntity().DomainConditionIn(Domain.Real).Substitute("x", at.ToEntity()).Evaled;
35+
Assert.Equal(defined ? Entity.Boolean.True : Entity.Boolean.False, condition);
36+
}
37+
38+
/// <summary>Over the complex numbers it is what <see cref="Entity.DomainCondition"/> says.</summary>
39+
[Theory]
40+
[InlineData("tan(piecewise(pi / x provided x > 0, x / 2))", "0")]
41+
[InlineData("tan(piecewise(pi / x provided x > 0, x / 2))", "2")]
42+
[InlineData("piecewise(sqrt(x - 1) provided x > 0, 1)", "1/2")]
43+
public void OverTheComplexNumbersItIsTheDomainCondition(string expression, string at)
44+
=> Assert.Equal(
45+
expression.ToEntity().DomainCondition.Substitute("x", at.ToEntity()).Evaled,
46+
expression.ToEntity().DomainConditionIn(Domain.Complex).Substitute("x", at.ToEntity()).Evaled);
47+
}
48+
}

0 commit comments

Comments
 (0)