Skip to content

Commit 559b81f

Browse files
Two operands' conditions are joined once (#2193)
An operation on two operands that each carry a condition joined the two with a bare and, so (a provided b > 0) + (c provided b > 0) was a + c provided b > 0 and b > 0, and a second derivative of 1/sqrt(x^2 + 13) stated not x ^ 2 + 13 = 0 three times. #2186 made Providedf.Both state a conjunct once where another conjunct forces it, and Providedf's own simplification goes through it. The join of two operands now does too, in ExpandOnTwoArguments and ExpandOnTwoAndTArguments, so the condition comes out as Providedf would state it. Part of #212. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 7bb72a6 commit 559b81f

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ as before, and `|p| > 1` needs no condition, since every value is then a power.
8787
while it is solved, as a provided expression is differentiated: a value known to be real, or declared real, has the
8888
half-line for its sector, one known to lie on it needs no condition, and one known to lie off it has no root. A conjunct of a condition that
8989
another conjunct already forces is not stated, so `a > -5 and a >= 0` is `a >= 0`, and a condition stated twice is
90-
stated once, as a system's rows carried theirs once a substitution
90+
stated once, as a system's rows carried theirs once a substitution, and as two operands carrying one condition
91+
carried it once combined: a second derivative of `1/sqrt(x^2 + 13)` stated `not x ^ 2 + 13 = 0` three times
9192
([#212](https://github.com/asc-community/AngouriMath/issues/212)).
9293

9394
| Input | Was (2.6.0) | Now |
@@ -101,6 +102,7 @@ stated once, as a system's rows carried theirs once a substitution
101102
| `"(sqrt(x) = a) provided a > -5".ToEntity().Solve("x")` | `{ a ^ 2 provided a > -5 }` | `{ a ^ 2 provided a >= 0 }` |
102103
| `"x provided a > 1 and a > 2".ToEntity().InnerSimplified` | `x provided a > 1 and a > 2` | `x provided a > 2` |
103104
| `"(x provided a > 0) provided a >= 0".ToEntity().InnerSimplified` | `x provided a >= 0 and a > 0` | `x provided a > 0` |
105+
| `"(a provided b > 0) + (c provided b > 0)".ToEntity().InnerSimplified` | `a + c provided b > 0 and b > 0` | `a + c provided b > 0` |
104106
| `MathS.Equations("sqrt(y) - x", "y - a").Solve("x", "y")` | `[[sqrt(a), (sqrt(a)) ^ 2], [-sqrt(a), (-sqrt(a)) ^ 2]]` | `[[sqrt(a) provided C(sqrt(a)), (sqrt(a)) ^ 2 provided C(sqrt(a))], [-sqrt(a) provided C(-sqrt(a)), (-sqrt(a)) ^ 2 provided C(-sqrt(a))]]`, `C(v)` being `v = 0 or -1/2 * pi < arg(v) and arg(v) <= 1/2 * pi` |
105107

106108
### A system with a product equation is solved branch by branch

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ Entity ops(Entity a, Entity b)
158158

159159
return (left, right) switch
160160
{
161-
(Providedf a, Providedf b) => ops(a.Expression, b.Expression).Provided(a.Predicate & b.Predicate),
161+
// A condition both operands carry is stated once: (a provided b > 0) + (c provided b > 0)
162+
// is a + c provided b > 0. https://github.com/asc-community/AngouriMath/issues/212
163+
(Providedf a, Providedf b) => ops(a.Expression, b.Expression).Provided(Providedf.Both(a.Predicate, b.Predicate)),
162164
(Providedf a, var b) => ExpandOnTwoArguments(a.Expression, b, operation, defaultCtor, isExact).Provided(a.Predicate),
163165
(var a, Providedf b) => ExpandOnTwoArguments(a, b.Expression, operation, defaultCtor, isExact).Provided(b.Predicate),
164166
(Piecewise a, Piecewise b) => CombinedCaseByCase(a, b, operation, defaultCtor, isExact),
@@ -449,7 +451,7 @@ Entity ops(Entity a, Entity b)
449451

450452
return (left, right, third) switch
451453
{
452-
(Providedf a, Providedf b, _) => ops(a.Expression, b.Expression).Provided(a.Predicate & b.Predicate),
454+
(Providedf a, Providedf b, _) => ops(a.Expression, b.Expression).Provided(Providedf.Both(a.Predicate, b.Predicate)),
453455
(Providedf a, var b, _) => ExpandOnTwoAndTArguments(a.Expression, b, third, operation, defaultCtor, isExact).Provided(a.Predicate),
454456
(var a, Providedf b, _) => ExpandOnTwoAndTArguments(a, b.Expression, third, operation, defaultCtor, isExact).Provided(b.Predicate),
455457
(Piecewise a, Piecewise b, _) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ protected override Entity InnerSimplify(bool isExact) =>
470470
// the first is kept. The conjunction is rebuilt only where something goes, so every
471471
// other condition keeps its shape.
472472
// https://github.com/asc-community/AngouriMath/issues/212
473-
private static Entity Both(Entity first, Entity second)
473+
internal static Entity Both(Entity first, Entity second)
474474
{
475475
var conjuncts = new List<Entity>();
476476
void Gather(Entity condition)

‎Sources/Tests/UnitTests/Common/ForcedConditionTest.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ public void AConjunctAnotherForcesIsNotStated(string written, string expected)
3636
public void ConjunctsThatForceNothingAreKept(string written)
3737
=> Assert.Equal(written.ToEntity(), written.ToEntity().InnerSimplified);
3838

39+
/// <summary>
40+
/// Two operands carrying the same condition, or one forcing the other's, give a result that
41+
/// states it once. A second derivative of <c>1/sqrt(x^2 + 13)</c> stated
42+
/// <c>not x ^ 2 + 13 = 0</c> three times.
43+
/// </summary>
44+
[Theory]
45+
[InlineData("(a provided b > 0) + (c provided b > 0)", "a + c provided b > 0")]
46+
[InlineData("(a provided b > 0) * (c provided b > 1)", "a * c provided b > 1")]
47+
[InlineData("(a provided b > 0) + (c provided b > 0) + (d provided b > 0)", "a + c + d provided b > 0")]
48+
[InlineData("log(a provided b > 0, c provided b > 0)", "log(a, c) provided b > 0")]
49+
public void OperandsConditionsAreJoinedOnce(string written, string expected)
50+
=> Assert.Equal(expected.ToEntity().InnerSimplified, written.ToEntity().InnerSimplified);
51+
52+
[Fact]
53+
public void ASecondDerivativeStatesItsConditionOnce()
54+
{
55+
var derivative = "derivative(1 / sqrt(x^2 + 13), x, 2)".ToEntity().InnerSimplified;
56+
var condition = Assert.IsType<Entity.Providedf>(derivative).Predicate;
57+
Assert.Equal("not x ^ 2 + 13 = 0".ToEntity(), condition);
58+
}
59+
3960
[Fact]
4061
public void ASolvedRootStatesItOnce()
4162
=> Assert.Equal("{ a ^ 2 provided a >= 0 }".ToEntity(), "(sqrt(x) = a) provided a > -5".ToEntity().Solve("x"));

0 commit comments

Comments
 (0)