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
16 changes: 15 additions & 1 deletion BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -1488,9 +1488,23 @@ factorial, has none and stays. Probs 1.3.12 and 2.7.17 of Sullivan and Mackey's

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"product(2^k, k, 0, n - 1)".ToEntity().Evaled` | as written | `2^((n - 1)/2 + (n - 1)^2/2) provided n - 1 >= 0`, `1` otherwise |
| `"product(2^k, k, 0, n - 1)".ToEntity().Evaled` | as written | `2^((n - 1)/2 + (n - 1)^2/2) provided n >= 1`, `1` otherwise |
| `"sum(k*factorial(k), k, 1, n)".ToEntity().Evaled` | as written | `((n + 1)! - 1) provided n >= 0`, `0` otherwise |

### A closed form reads as the book writes it: multiplied out where simpler, over a bound on its name

The sum of the first `n` odd numbers, `sum(2 k + 1, k, 0, n - 1)`, came out as
`2 n + (n - 1)^2 - 1 provided n - 1 >= -1`, and Sullivan and Mackey write it `n^2`. A polynomial
sum's closed form is now multiplied out where that rates simpler, so it is `n^2`, and Prob 5.7.16's
`sum(2 k + 1, k, n, 2 n - 1)` is `3 n^2`, while `sum(k, k, 1, n)` keeps `(n + n^2)/2`. And the
condition that the range is not empty, written as the ends left it, is now a bound on the one name
the ends are about, `n >= 0`. With two names it is left as it was, `n >= m - 1`
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"sum(2 k + 1, k, 0, n - 1)".Simplify()` | `piecewise((2 * n + (n - 1) ^ 2 - 1) provided (n - 1 >= -1), 0 provided True)` | `piecewise((n ^ 2) provided (n >= 0), 0 provided True)` |

### `(a + b asech(c x))/(d + e x)^2` is integrated, and a root written apart with `|x|` no longer needs a parity

Rubi's 7.5.1 with a symbolic linear below the bar ran for ten minutes without an answer. By
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private static Entity Ranged(Entity closed, Entity upper)
? closed
: MathS.Piecewise(new[]
{
new Providedf(closed, new GreaterOrEqualf(upper, Integer.Zero)),
new Providedf(closed, PolynomialSummation.AtLeast(upper, Integer.Zero)),
new Providedf(Integer.Zero, Entity.Boolean.True),
}).InnerSimplified;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal static class BinomialSum
return closed;
return MathS.Piecewise(new[]
{
new Providedf(closed, new GreaterOrEqualf(upper, Integer.Zero)),
new Providedf(closed, PolynomialSummation.AtLeast(upper, Integer.Zero)),
new Providedf(Integer.Zero, Entity.Boolean.True),
}).InnerSimplified;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Entity Q(Entity at)
return closed;
return MathS.Piecewise(new[]
{
new Providedf(closed, new GreaterOrEqualf(to, (from - Integer.One).InnerSimplified)),
new Providedf(closed, PolynomialSummation.NonEmpty(from, to)),
new Providedf(Integer.Zero, Entity.Boolean.True),
}).InnerSimplified;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal static class GeometricSeries

// The terms from a to b, as the difference of two tails of the same series.
Entity closed = constant * (PowerAt(ratio, from) - PowerAt(ratio, to + Integer.One)) / (Integer.One - ratio);
var nonEmpty = new GreaterOrEqualf(to, from - Integer.One);
var nonEmpty = PolynomialSummation.NonEmpty(from, to);
if (ratioIsANumber)
return MathS.Piecewise(new[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal static class MonomialProduct
? cases.First().Expression : sum;
return MathS.Piecewise(new[]
{
new Providedf(MathS.Pow(@base, nonEmpty).InnerSimplified, new GreaterOrEqualf(to, from)),
new Providedf(MathS.Pow(@base, nonEmpty).InnerSimplified, PolynomialSummation.AtLeast(to, from)),
new Providedf(Integer.One, Entity.Boolean.True),
}).InnerSimplified;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ internal static class MonomialProduct
// both branches say 1 there.
return MathS.Piecewise(new[]
{
new Providedf(closed.InnerSimplified, new GreaterOrEqualf(to, from)),
new Providedf(closed.InnerSimplified, PolynomialSummation.AtLeast(to, from)),
new Providedf(Integer.Create(1), Entity.Boolean.True),
}).InnerSimplified;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ Entity Q(Entity at)
}

var after = (to + Integer.One).InnerSimplified;
Entity closed = constant * (Q(after) * PowerAt(ratio, after) - Q(from) * PowerAt(ratio, from));
var nonEmpty = new GreaterOrEqualf(to, from - Integer.One);
var closed = PolynomialSummation.Simplest((constant * (Q(after) * PowerAt(ratio, after) - Q(from) * PowerAt(ratio, from))).InnerSimplified);
var nonEmpty = PolynomialSummation.NonEmpty(from, to);
if (ratioIsANumber)
return MathS.Piecewise(new[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,52 @@ Entity AtBound(Entity bound)
// sum from a to b is S(b) - S(a - 1), which holds for every pair of integers with
// b >= a - 1 -- at b = a - 1 both sides are zero, and below it the range is empty
// while the polynomial is not.
var closed = (AtBound(to) - AtBound(from - 1)).InnerSimplified;
var nonEmpty = new GreaterOrEqualf(to, from - 1);
var closed = Simplest((AtBound(to) - AtBound(from - 1)).InnerSimplified);
var nonEmpty = NonEmpty(from, to);
return MathS.Piecewise(new[]
{
new Providedf(closed, nonEmpty),
new Providedf(Integer.Create(0), Entity.Boolean.True),
}).InnerSimplified;
}

/// <summary>
/// A closed form as it reads most simply: multiplied out where that rates simpler, as the
/// sums of odd numbers do -- <c>2 n + (n - 1)^2 - 1</c> is <c>n^2</c>, and Sullivan and
/// Mackey's Prob 5.7.16 is <c>3 n^2</c> -- and as it came otherwise, so <c>(n + n^2)/2</c>
/// stays. Here, where the closed form is made, rather than under <c>Simplify</c>, which
/// would multiply out every conditional answer it meets.
/// </summary>
internal static Entity Simplest(Entity closed)
=> closed.Expand().InnerSimplified is var expanded && expanded.SimplifiedRate < closed.SimplifiedRate ? expanded : closed;

/// <summary>
/// That the range from <paramref name="from"/> to <paramref name="to"/> is not empty,
/// <c>to &gt;= from - 1</c>, as <see cref="AtLeast"/> writes it: <c>n - 1 &gt;= -1</c> is
/// <c>n &gt;= 0</c>.
/// </summary>
internal static Entity NonEmpty(Entity from, Entity to) => AtLeast(to, from - Integer.One);

/// <summary>
/// <c>value &gt;= bound</c>, written as a bound on the one name the two are about where their
/// difference is linear in it with a rational slope: <c>2 n - 1 &gt;= n - 1</c> is
/// <c>n &gt;= 0</c>, which is how a closed form's range reads. As written otherwise, so
/// <c>n &gt;= a - 1</c> keeps both of its names.
/// </summary>
internal static Entity AtLeast(Entity value, Entity bound)
{
var gap = (value - bound).InnerSimplified;
var names = gap.Vars.Distinct().Take(2).ToList();
if (names.Count == 1 && names[0] is Variable name
&& TreeAnalyzer.TryGetPolyLinear(gap, name, out var slope, out var rest) && !rest.ContainsNode(name)
&& slope.InnerSimplified is Rational { IsZero: false } a && rest.InnerSimplified is Rational b)
{
var root = (-b / a).InnerSimplified;
return a.IsPositive ? new GreaterOrEqualf(name, root) : new LessOrEqualf(name, root);
}
return new GreaterOrEqualf(value, bound.InnerSimplified);
}

/// <summary>
/// Whether a bound is one this identity can speak about: a whole number, or a name, of
/// which the second is read as standing for a whole number because that is what the
Expand Down
28 changes: 28 additions & 0 deletions Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using AngouriMath;
using AngouriMath.Extensions;
using System.Linq;
using Xunit;

namespace AngouriMath.Tests.Core
Expand Down Expand Up @@ -48,6 +49,33 @@ private static void AgreesWithTheExpansion(string expression, params int[] bound
}
}

/// <summary>
/// The range condition is a bound on the one name the range is about, which is the one
/// place the printed form is the point: the sum of the first n odd numbers holds from
/// n = 0 and says so, where it said n - 1 >= -1. With two names it is left as it was.
/// </summary>
[Theory]
[InlineData("sum(2 * k + 1, k, 0, n - 1)", "n >= 0")]
[InlineData("sum(2 * k + 1, k, n, 2 * n - 1)", "n >= 0")]
[InlineData("sum(k, k, 1, n)", "n >= 0")]
[InlineData("sum(2 ^ k, k, 0, n - 1)", "n >= 0")]
[InlineData("product(2 ^ k, k, 0, n - 1)", "n >= 1")]
[InlineData("sum(k, k, m, n)", "n >= m - 1")]
public void TheRangeConditionIsABoundOnItsName(string sum, string condition)
=> Assert.Equal(condition.ToEntity().InnerSimplified, Assert.IsType<Entity.Piecewise>(sum.ToEntity().Simplify()).Cases.First().Predicate);

/// <summary>
/// A closed form is given its simplest shape where it is made: the sums of odd numbers are
/// powers of n, as Sullivan and Mackey write them (Prob 5.7.16 is 3 n^2), and the
/// triangular numbers keep their quotient.
/// </summary>
[Theory]
[InlineData("sum(2 * k + 1, k, 0, n - 1)", "n ^ 2")]
[InlineData("sum(2 * k + 1, k, n, 2 * n - 1)", "3 * n ^ 2")]
[InlineData("sum(k, k, 1, n)", "(n + n ^ 2) / 2")]
public void AClosedFormReadsAsTheBookWritesIt(string sum, string closedForm)
=> Assert.Equal(closedForm, Assert.IsType<Entity.Piecewise>(sum.ToEntity().Simplify()).Cases.First().Expression.Stringize());

/// <summary>The sums whose closed forms have names.</summary>
[Theory]
[InlineData("sum(1, k, 1, n)")]
Expand Down
Loading