From 879a0b318c2c3f6c7374a7cf7d84f0df0ac55419 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 29 Sep 2026 17:36:51 +0000 Subject: [PATCH] A closed form reads as the book writes it A polynomial sum's closed form is multiplied out where that rates simpler, where it is made: the sum of the first n odd numbers is n^2, where it was 2 n + (n - 1)^2 - 1, and Sullivan and Mackey's Prob 5.7.16 is 3 n^2, while sum(k, k, 1, n) keeps (n + n^2)/2. And the condition that a range is not empty, written as the ends left it (n - 1 >= -1), is a bound on the one name the ends are about, n >= 0, written by one helper the seven closed forms share; with two names it is left as it was. Part of #1409. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 16 +++++++- .../Algebra/Polynomials/BinomialIdentities.cs | 2 +- .../Algebra/Polynomials/BinomialSum.cs | 2 +- .../Algebra/Polynomials/FactorialSum.cs | 2 +- .../Algebra/Polynomials/GeometricSeries.cs | 2 +- .../Algebra/Polynomials/MonomialProduct.cs | 4 +- .../Polynomials/PolynomialGeometricSeries.cs | 4 +- .../Polynomials/PolynomialSummation.cs | 41 ++++++++++++++++++- .../UnitTests/Core/ClosedFormSummationTest.cs | 28 +++++++++++++ 9 files changed, 90 insertions(+), 11 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 529a33eea..c336d1889 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs index c9edc88f2..40606b6c1 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialIdentities.cs @@ -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; diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialSum.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialSum.cs index 40333282d..d782a8246 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialSum.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/BinomialSum.cs @@ -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; } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs index fc01e914f..997603cfc 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs @@ -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; } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs index 5adcc8b40..a69dbbabf 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/GeometricSeries.cs @@ -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[] { diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs index b69a42330..8bb5991a2 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs @@ -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; } @@ -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; } diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs index 7e704bcdf..14f32dcdd 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGeometricSeries.cs @@ -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[] { diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialSummation.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialSummation.cs index eaf84894a..59fa7939f 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialSummation.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialSummation.cs @@ -101,8 +101,8 @@ 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), @@ -110,6 +110,43 @@ Entity AtBound(Entity bound) }).InnerSimplified; } + /// + /// A closed form as it reads most simply: multiplied out where that rates simpler, as the + /// sums of odd numbers do -- 2 n + (n - 1)^2 - 1 is n^2, and Sullivan and + /// Mackey's Prob 5.7.16 is 3 n^2 -- and as it came otherwise, so (n + n^2)/2 + /// stays. Here, where the closed form is made, rather than under Simplify, which + /// would multiply out every conditional answer it meets. + /// + internal static Entity Simplest(Entity closed) + => closed.Expand().InnerSimplified is var expanded && expanded.SimplifiedRate < closed.SimplifiedRate ? expanded : closed; + + /// + /// That the range from to is not empty, + /// to >= from - 1, as writes it: n - 1 >= -1 is + /// n >= 0. + /// + internal static Entity NonEmpty(Entity from, Entity to) => AtLeast(to, from - Integer.One); + + /// + /// value >= bound, written as a bound on the one name the two are about where their + /// difference is linear in it with a rational slope: 2 n - 1 >= n - 1 is + /// n >= 0, which is how a closed form's range reads. As written otherwise, so + /// n >= a - 1 keeps both of its names. + /// + 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); + } + /// /// 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 diff --git a/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs b/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs index 724031b2e..4d332ef01 100644 --- a/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs +++ b/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs @@ -7,6 +7,7 @@ using AngouriMath; using AngouriMath.Extensions; +using System.Linq; using Xunit; namespace AngouriMath.Tests.Core @@ -48,6 +49,33 @@ private static void AgreesWithTheExpansion(string expression, params int[] bound } } + /// + /// 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. + /// + [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(sum.ToEntity().Simplify()).Cases.First().Predicate); + + /// + /// 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. + /// + [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(sum.ToEntity().Simplify()).Cases.First().Expression.Stringize()); + /// The sums whose closed forms have names. [Theory] [InlineData("sum(1, k, 1, n)")]