Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,23 @@ reached 3^12 cases and 8 GB inside integration by parts
| `(x < 0 and x = 0).Simplify()` | `False provided x in RR` | `False` — the condition was over-strong, one conjunct is false wherever `x` is |
| `(x > 0 and x > 0).Evaled` | `x > 0` | `x > 0` (unchanged) |

### Two piecewises split on one quantity combine sign by sign

A sum or product of two piecewises still joins every case's condition to every other's, and now
drops two more kinds of pair. A quantity is read up to a constant factor, so `f = 0 and not 2 f = 0`
is `False`. And `q > 0 and q < 0`, which is `NaN` off the real line rather than `False`, goes where
every case after it tests `q` too: off the real line each of those is false or undecided, so no case
it was keeping from being reached could be. Where a later case tests something else it stays. Sums
of the answers the radical substitution gives for `x sqrt(1 + d x) sqrt(1 + f x)` had reached
32,769 cases ([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0) + piecewise(10 provided q = 0, 20 provided q > 0, 30 provided q < 0)`, `.InnerSimplified` | nine cases | three: `11 provided q = 0`, `22 provided q > 0`, `33 provided q < 0` |
| `piecewise(1 provided f = 0, 2 provided not 2 * f = 0) + piecewise(10 provided 3 * f = 0, 20 provided not f = 0)`, `.InnerSimplified` | four cases | two |
| the same with a third case `provided p > 0` in each | nine cases | nine, unchanged |
| `"x*sqrt(1 + d*x)*sqrt(1 + f*x)".Integrate("x")`, and with `x^2`, or `A + B x + K x^2`, for `x` | left unevaluated | the antiderivative, a piecewise of three cases |

### A union with an empty interval is the other set

**Wrong answer fixed.** Two intervals were joined wherever one ended where the other began, before
Expand Down
90 changes: 88 additions & 2 deletions Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,103 @@ Entity ops(Entity a, Entity b)
private Entity CombinedCaseByCase(Piecewise a, Piecewise b,
Func<Entity, Entity, Entity?> operation, Func<Entity, Entity, Entity, Entity> defaultCtor, bool isExact)
{
var cases = new List<Providedf>();
// Every pair of cases, in order, and its conjunction; the expressions are combined only
// for the pairs that are kept.
var pairs = new List<(Providedf First, Providedf Second, Entity Predicate, Entity? Undecided)>();
foreach (var (c1, c2) in (a.Cases, b.Cases).EachForEach())
{
var predicate = (c1.Predicate & c2.Predicate).InnerSimplified;
if (predicate == Boolean.False)
continue;
cases.Add((ExpandOnTwoArguments(c1.Expression, c2.Expression, operation, defaultCtor, isExact), predicate).ToProvided());
switch (Contradiction(predicate))
{
case (true, _):
continue;
case (false, var quantity):
pairs.Add((c1, c2, predicate, quantity));
break;
}
}
// A pair that asks a quantity to be both positive and negative is false for a real
// one and undecided off the real line, where it would stop the cases after it from
// being reached. It goes only where every later pair tests that quantity too: those
// are each false or undecided off the real line, never true, so nothing it stopped
// is reached without it. A sum of piecewises split on the sign of `d/f` -- the
// integrator's answer for `x sqrt(1 + d x) sqrt(1 + f x)` -- otherwise doubled its
// cases with every term: 32,769 for six terms of `t^k/(d - f t^2)^4`.
// https://github.com/asc-community/AngouriMath/issues/718
var keep = new bool[pairs.Count];
for (var i = pairs.Count - 1; i >= 0; i--)
keep[i] = pairs[i].Undecided is not { } quantity
|| !Enumerable.Range(i + 1, pairs.Count - i - 1).All(later => !keep[later] || TestsTheSignOf(pairs[later].Predicate, quantity));
var cases = new List<Providedf>();
for (var i = 0; i < pairs.Count; i++)
if (keep[i])
cases.Add((ExpandOnTwoArguments(pairs[i].First.Expression, pairs[i].Second.Expression, operation, defaultCtor, isExact), pairs[i].Predicate).ToProvided());
return MathS.Piecewise(cases);
}

/// <summary>
/// Whether <paramref name="predicate"/>, a conjunction, asks one quantity <c>q</c> to be
/// zero and not zero, or zero and of a sign -- false for every value, complex or not -- in
/// <c>Always</c>; and otherwise the quantity it asks to be both positive and negative,
/// which is false for a real one and undecided off the real line, or null. A quantity is
/// read up to a constant factor, which only the direction of a sign depends on:
/// <c>f = 0</c> and <c>not 2 f = 0</c> contradict each other.
/// </summary>
private static (bool Always, Entity? Undecided) Contradiction(Entity predicate)
{
var tests = new List<(Entity Quantity, int Relation)>();
foreach (var conjunct in Conjuncts(predicate))
if (SignTest(conjunct) is { } test)
tests.Add(test);
Entity? undecided = null;
for (var i = 0; i < tests.Count; i++)
for (var j = i + 1; j < tests.Count; j++)
{
if (tests[i].Quantity != tests[j].Quantity)
continue;
var (r1, r2) = (tests[i].Relation, tests[j].Relation);
// 0 is "= 0", 2 is "not = 0", 1 and -1 the signs.
if (r1 == 0 && r2 != 0 || r2 == 0 && r1 != 0)
return (true, null);
if (r1 * r2 == -1)
undecided = tests[i].Quantity;
}
return (false, undecided);
}

/// <summary>Whether <paramref name="predicate"/> has a conjunct testing <paramref name="quantity"/> against zero.</summary>
private static bool TestsTheSignOf(Entity predicate, Entity quantity)
=> Conjuncts(predicate).Any(conjunct => SignTest(conjunct) is var (q, relation) && q == quantity && relation != 2);

/// <summary>
/// A comparison of a quantity with zero as the quantity, with any constant factor taken
/// out, and the relation: 0 for <c>= 0</c>, 2 for <c>not = 0</c>, and the sign for
/// <c>&gt; 0</c> and <c>&lt; 0</c>, turned by a negative factor. Null for anything else.
/// </summary>
private static (Entity Quantity, int Relation)? SignTest(Entity conjunct)
{
(Entity, int)? read = conjunct switch
{
Equalsf(var q, var zero) when zero == Integer.Zero => (q, 0),
Equalsf(var zero, var q) when zero == Integer.Zero => (q, 0),
Notf(Equalsf(var q, var zero)) when zero == Integer.Zero => (q, 2),
Notf(Equalsf(var zero, var q)) when zero == Integer.Zero => (q, 2),
Greaterf(var q, var zero) when zero == Integer.Zero => (q, 1),
Lessf(var zero, var q) when zero == Integer.Zero => (q, 1),
Lessf(var q, var zero) when zero == Integer.Zero => (q, -1),
Greaterf(var zero, var q) when zero == Integer.Zero => (q, -1),
_ => null,
};
if (read is not var (quantity, relation))
return null;
// The constant factor out: `4 f (-d)` is `f (-d)`, and `2 f` is `f`.
if (quantity is Mulf(Real factor, var rest) && !factor.IsZero)
(quantity, relation) = (rest, relation is 1 or -1 && factor.IsNegative ? -relation : relation);
return (quantity, relation);
}

private Entity ExpandOnOneArgument(Entity expr, Func<Entity, Entity?> operation, Func<Entity, Entity, Entity> defaultCtor, bool isExact,
bool propagateSet = true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public void TwoRootsOfTheSameOrder(string integrand)
[InlineData("sqrt(a + b*x)*sqrt(c + d*x)")]
[InlineData("x/(sqrt(a + b*x)*sqrt(c + d*x))")]
[InlineData("1/(sqrt(a + b*x)*sqrt(c + d*x))")]
// A power of x beside them: each term of the rational function in t is split on the
// sign of the same quantity, and their sum combines sign by sign rather than pair by pair
// (https://github.com/asc-community/AngouriMath/issues/718).
[InlineData("x*sqrt(a + b*x)*sqrt(c + d*x)")]
[InlineData("x^2*sqrt(a + b*x)*sqrt(c + d*x)")]
public void SymbolicCoefficients(string integrand)
=> DifferentiatesBackPinned(integrand, new[] { 0.3, 0.9, 1.7, 2.6 }, ("a", 1.7), ("b", 2.3), ("c", 0.6), ("d", 1.1));

Expand Down
45 changes: 38 additions & 7 deletions Sources/Tests/UnitTests/Core/ContradictoryConjunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,57 @@ private static int CaseCount(Entity piecewise)
[Fact]
public void TwoPiecewisesOnTheSameSplitCombineCaseByCase()
{
// Three matching pairs, and the two pairs of opposite strict signs, `q > 0 and q < 0`
// and `q < 0 and q > 0`, which are NaN rather than False off the real line and so stay
// (five cases, not nine). Everything else pairs an equality against a sign and is gone.
// The three matching pairs. A pair that pairs an equality against a sign is False and
// gone. The two pairs of opposite strict signs, `q > 0 and q < 0` and `q < 0 and q > 0`,
// are NaN rather than False off the real line, where they would stop the cases after
// them -- but every case after them tests q too, so none of those is true there either,
// and they go as well: three cases, not nine.
// https://github.com/asc-community/AngouriMath/issues/718
var sum = "piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0) + piecewise(10 provided q = 0, 20 provided q > 0, 30 provided q < 0)".ToEntity().InnerSimplified;
Assert.Equal(5, CaseCount(sum));
Assert.Equal(3, CaseCount(sum));
Assert.Equal(Number.Integer.Create(11), sum.Substitute("q", 0).Evaled);
Assert.Equal(Number.Integer.Create(22), sum.Substitute("q", 1).Evaled);
Assert.Equal(Number.Integer.Create(33), sum.Substitute("q", -1).Evaled);
// Off the real line it has no value, as with every pair: no case is true there.
Assert.IsNotType<Number.Integer>(sum.Substitute("q", "i").Evaled);

// And a chain of them stays at five cases rather than growing by a factor of three each
// time: the two contradictory cases absorb every further sign rather than multiplying.
// And a chain of them stays at three cases rather than growing by a factor of three each
// time.
var chain = sum;
for (var i = 0; i < 6; i++)
chain = (chain + "piecewise(1 provided q = 0, 2 provided q > 0, 3 provided q < 0)".ToEntity()).InnerSimplified;
Assert.Equal(5, CaseCount(chain));
Assert.Equal(3, CaseCount(chain));
Assert.Equal(Number.Integer.Create(22 + 12), chain.Substitute("q", 1).Evaled);
Assert.Equal(Number.Integer.Create(33 + 18), chain.Substitute("q", -1).Evaled);
Assert.Equal(Number.Integer.Create(11 + 6), chain.Substitute("q", 0).Evaled);
}

/// <summary>
/// A quantity is read up to a constant factor: <c>f = 0</c> and <c>not 2 f = 0</c>
/// contradict each other, which is how the integrator's case for a vanishing leading
/// coefficient meets the cases that divide by it.
/// </summary>
[Fact]
public void AConstantFactorIsTheSameQuantity()
{
var sum = "piecewise(1 provided f = 0, 2 provided not 2 * f = 0) + piecewise(10 provided 3 * f = 0, 20 provided not f = 0)".ToEntity().InnerSimplified;
Assert.Equal(2, CaseCount(sum));
Assert.Equal(Number.Integer.Create(11), sum.Substitute("f", 0).Evaled);
Assert.Equal(Number.Integer.Create(22), sum.Substitute("f", 5).Evaled);
}

/// <summary>
/// ...and two opposite strict signs stay where a later case does not test the quantity:
/// off the real line that case can be true, and the undecided pair is what keeps it from
/// being reached.
/// </summary>
[Fact]
public void OppositeSignsStayWhereALaterCaseDoesNotTestTheQuantity()
{
var sum = "piecewise(1 provided q > 0, 2 provided q < 0, 3 provided p > 0) + piecewise(10 provided q < 0, 20 provided q > 0, 30 provided p > 0)".ToEntity().InnerSimplified;
Assert.Contains(Assert.IsType<Piecewise>(sum).Cases, @case => @case.Predicate.ToString().Contains("q > 0") && @case.Predicate.ToString().Contains("q < 0"));
}

[Fact]
public void TwoPiecewisesOnDifferentSplitsStillCombineEveryPair()
{
Expand Down
Loading