diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index edd19a65c..b28096cd7 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -990,6 +990,21 @@ parameter, so their Ex 5.2.6 is answered with the condition the book asks about | `"sum(k/2^k, k, 1, +oo)".ToEntity().Evaled` | as written | `2` | | `"forall n in ZZ+ : sum(q^i, i, 0, n-1) = (q^n - 1)/(q - 1)".ToEntity().Evaled` | `UnhandledParseException` -- quantifiers are new since | `True provided not q = 1` | +### A product of powers of one base, and a polynomial times `k!`, in closed form + +`product(2^k, k, 0, n - 1)` was left as written; it is `2^(n (n - 1)/2)`, the base to the sum of +the exponents, `c^s c^t = c^(s + t)`, which holds on the principal branch as well, every power +being `e^(s ln c)` with the one `ln c`. And `sum(k k!, k, 1, n)` is `(n + 1)! - 1`: a polynomial +`P(k)` beside `k!` is summed where `P(k) k! = T(k + 1) - T(k)` for `T(k) = Q(k) k!` with a +polynomial `Q`, which one triangular solve finds or rules out. `sum(k!, k, 0, n)`, the left +factorial, has none and stays. Probs 1.3.12 and 2.7.17 of Sullivan and Mackey's +*An Introduction to Proofs* ([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). + +| 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 | +| `"sum(k*factorial(k), k, 1, n)".ToEntity().Evaled` | as written | `((n + 1)! - 1) provided n >= 0`, `0` otherwise | + ### `(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/.editorconfig b/Sources/.editorconfig index 80e2bd225..775c3215a 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -412,6 +412,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs] 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 + +[AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs] +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 + +[Tests/UnitTests/Core/FactorialSumTest.cs] +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 [Tests/UnitTests/Core/ClosedFormProductTest.cs] 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 diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs new file mode 100644 index 000000000..fc01e914f --- /dev/null +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs @@ -0,0 +1,115 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Collections.Generic; +using System.Linq; +using PeterO.Numbers; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Functions +{ + /// + /// A polynomial times the factorial of the index, summed in closed form where the sum + /// telescopes: sum(k k!, k, 1, n) is (n + 1)! - 1, Sullivan and Mackey's Prob 2.7.17. + /// + /// + /// + /// P(k) k! is T(k + 1) - T(k) for T(k) = Q(k) k! exactly where + /// (k + 1) Q(k + 1) - Q(k) = P(k), and then the sum from a to b is + /// T(b + 1) - T(a). The equation is triangular in Q's coefficients from the top + /// down, the coefficient of k^(m + 1) fixing q_m, and its constant term is one + /// condition more: q_1 + q_2 + ... = p_0. Where that fails, no polynomial Q exists + /// and the sum is declined: sum(k!, k, 0, n), the left factorial, has no closed form in + /// factorials. This is Gosper's algorithm for the one family, where the certificate is a + /// polynomial. + /// + /// + /// Over a >= 0, where a! is defined; a range that runs backwards is empty and + /// the sum zero, which is what the piecewise says, while the closed form at b = a - 1 is + /// zero already. + /// + /// https://github.com/asc-community/AngouriMath/issues/1409 + /// + internal static class FactorialSum + { + private const int MaxDegree = 12; + + internal static Entity? ClosedForm(Entity expression, Entity var, Entity from, Entity to) + { + if (var is not Variable index) + return null; + if (!PolynomialSummation.IsWholeOrSymbolic(from) || !PolynomialSummation.IsWholeOrSymbolic(to) + || from.ContainsNode(index) || to.ContainsNode(index) || from.Evaled is Integer { IsNegative: true }) + return null; + Entity? polynomial = null; + var sawFactorial = false; + foreach (var factor in Mulf.LinearChildren(expression.InnerSimplified)) + { + if (factor is Factorialf(var argument) && argument == index && !sawFactorial) + sawFactorial = true; + else + polynomial = polynomial is null ? factor : polynomial * factor; + } + if (!sawFactorial || polynomial is null || !polynomial.ContainsNode(index)) + return null; + if (!TreeAnalyzer.TryGetPolynomial(polynomial, index, out var monomials) + || monomials.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32())) + return null; + var degree = monomials.Keys.Max()!.ToInt32Checked(); + if (degree < 1 || degree > MaxDegree) + return null; + Entity P(int m) => monomials.TryGetValue(EInteger.FromInt32(m), out var c) ? c : Integer.Zero; + + // q_d .. q_0 from the coefficients of k^(d + 1) .. k^1 of (k + 1) Q(k + 1) - Q(k), + // which is sum_j q_j sum_i C(j + 1, i) k^i - sum_j q_j k^j. + var d = degree - 1; + var q = new Entity[d + 1]; + for (var m = d + 1; m >= 1; m--) + { + // The coefficient of k^m: q_(m - 1) + sum_(j >= m) q_j C(j + 1, m) - q_m. + Entity known = P(m); + for (var j = m; j <= d; j++) + known -= Integer.Create(Binomial(j + 1, m)) * q[j]; + if (m <= d) + known += q[m]; + q[m - 1] = known.InnerSimplified; + } + // The constant term: sum_(j >= 1) q_j = p_0, or there is no polynomial certificate. + Entity rest = Integer.Zero; + for (var j = 1; j <= d; j++) + rest += q[j]; + if (PartialFractions.Bare((rest - P(0)).Simplify()) is not Integer { IsZero: true }) + return null; + // The constant term written as itself: at a = 0 a power 0^0 would stand in for it. + Entity Q(Entity at) + { + Entity value = q[0]; + for (var j = 1; j <= d; j++) + value += q[j] * MathS.Pow(at, Integer.Create(j)); + return value; + } + var next = (to + Integer.One).InnerSimplified; + var closed = (Q(next) * MathS.Factorial(next) - Q(from) * MathS.Factorial(from)).InnerSimplified; + if (to.Evaled is Integer && from.Evaled is Integer) + return closed; + return MathS.Piecewise(new[] + { + new Providedf(closed, new GreaterOrEqualf(to, (from - Integer.One).InnerSimplified)), + new Providedf(Integer.Zero, Entity.Boolean.True), + }).InnerSimplified; + } + + private static EInteger Binomial(int n, int k) + { + var result = EInteger.One; + for (var i = 0; i < k; i++) + result = result.Multiply(EInteger.FromInt32(n - i)).Divide(EInteger.FromInt32(i + 1)); + return result; + } + } +} diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs index 2e65bd464..b69a42330 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/MonomialProduct.cs @@ -5,6 +5,7 @@ // Website: https://am.angouri.org. // +using System.Linq; using PeterO.Numbers; using static AngouriMath.Entity; using static AngouriMath.Entity.Number; @@ -44,6 +45,36 @@ namespace AngouriMath.Functions /// internal static class MonomialProduct { + /// + /// product(c^(p(k)), k, a, b) is c^(sum(p(k), k, a, b)) for a base free of the + /// index, where that sum closes: the factors are powers of one base, whose exponents add, + /// c^s c^t = c^(s + t) -- on the principal branch as well, each power being + /// e^(s ln c) with the one ln c. Prob 1.3.12 of Sullivan and Mackey: + /// product(2^k, k, 0, n - 1) is 2^(n (n - 1)/2). An empty range sums to zero, + /// and c^0 is the empty product's 1. + /// https://github.com/asc-community/AngouriMath/issues/1409 + /// + internal static Entity? OfAPower(Entity expression, Entity var, Entity from, Entity to) + { + if (var is not Variable index || expression is not Powf(var @base, var exponent) + || @base.ContainsNode(index) || !exponent.ContainsNode(index) || @base.Evaled is Integer { IsZero: true }) + return null; + var sum = MathS.Sum(exponent, index, from, to).InnerSimplified; + if (sum.Nodes.Any(node => node is Summationf)) + return null; + if (to.Evaled is Integer && from.Evaled is Integer) + return MathS.Pow(@base, sum).InnerSimplified; + // Over a range with a symbolic end, the sum is its closed form where the range is not + // empty; and an empty product is 1 for every base, where c^0 is not at c = 0. + var nonEmpty = sum is Piecewise { Cases: var cases } && cases.Count() == 2 && cases.Last().Predicate == Entity.Boolean.True + ? cases.First().Expression : sum; + return MathS.Piecewise(new[] + { + new Providedf(MathS.Pow(@base, nonEmpty).InnerSimplified, new GreaterOrEqualf(to, from)), + new Providedf(Integer.One, Entity.Boolean.True), + }).InnerSimplified; + } + /// /// The largest power of the index this will take. The answer carries the factorial to /// that power, so the ceiling is about how big an expression is worth returning. diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs index ab116b047..e78c84687 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Calculus.Classes.cs @@ -239,6 +239,7 @@ protected override Entity InnerSimplify(bool isExact) => ?? Functions.ExponentialSeries.ClosedForm(Expression, Var, From, To) ?? Functions.BinomialSum.ClosedForm(Expression, Var, From, To) ?? Functions.BinomialIdentities.ClosedForm(Expression, Var, From, To) + ?? Functions.FactorialSum.ClosedForm(Expression, Var, From, To) ?? Functions.GeometricSeries.ClosedForm(Expression, Var, From, To) ?? Functions.PolynomialGeometricSeries.ClosedForm(Expression, Var, From, To) // Last: a series that converges is summed above, and only what none of them @@ -290,6 +291,7 @@ public partial record Productf protected override Entity InnerSimplify(bool isExact) => Summationf.Expanded(this, Expression, Var, From, To, static (a, b) => a * b, 1, isExact) ?? Functions.MonomialProduct.ClosedForm(Expression, Var, From, To) + ?? Functions.MonomialProduct.OfAPower(Expression, Var, From, To) ?? this; } diff --git a/Sources/Tests/UnitTests/Core/ClosedFormProductTest.cs b/Sources/Tests/UnitTests/Core/ClosedFormProductTest.cs index 092fa1437..b888d76ec 100644 --- a/Sources/Tests/UnitTests/Core/ClosedFormProductTest.cs +++ b/Sources/Tests/UnitTests/Core/ClosedFormProductTest.cs @@ -46,6 +46,18 @@ private static void AgreesWithTheExpansion(string expression, params int[] bound public void ThePowerOfAFactorial(string expression) => AgreesWithTheExpansion(expression, -2, -1, 0, 1, 2, 3, 5, 8); + /// + /// A power of one base is the base to the sum of the exponents, where that sum closes: + /// Sullivan and Mackey's Prob 1.3.12, product(2^k, k, 0, n - 1) = 2^(n (n - 1)/2). + /// #1409 + /// + [Theory] + [InlineData("product(2^k, k, 0, n - 1)")] + [InlineData("product(3^(2 * k + 1), k, 1, n)")] + [InlineData("product(c^k, k, 1, n)")] + public void APowerOfOneBase(string expression) + => AgreesWithTheExpansion(expression, -2, -1, 0, 1, 2, 3, 5, 8); + /// A constant factor comes out as a power of itself, one per term. [Theory] [InlineData("product(2, k, 1, n)")] @@ -117,7 +129,7 @@ public void ALowerBoundThatIsNotAConcretePositiveIntegerIsDeclined(string expres [Theory] [InlineData("product(k + 1, k, 1, n)")] [InlineData("product(k ^ 2 + k, k, 1, n)")] - [InlineData("product(2 ^ k, k, 1, n)")] + [InlineData("product(2 ^ sin(k), k, 1, n)")] [InlineData("product(sin(k), k, 1, n)")] [InlineData("product(1 / k, k, 1, n)")] public void WhatIsNotAMonomialIsCarried(string expression) diff --git a/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs b/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs index cc03c973e..724031b2e 100644 --- a/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs +++ b/Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs @@ -164,7 +164,7 @@ public void WhatIsNotPolynomialInTheIndexIsCarried(string expression) /// [Theory] [InlineData("product(k + 1, k, 1, n)")] - [InlineData("product(2 ^ k, k, 1, n)")] + [InlineData("product(2 ^ sin(k), k, 1, n)")] public void TheProductIsCarried(string expression) => Assert.IsType(expression.ToEntity().Simplify()); diff --git a/Sources/Tests/UnitTests/Core/FactorialSumTest.cs b/Sources/Tests/UnitTests/Core/FactorialSumTest.cs new file mode 100644 index 000000000..5d273ac61 --- /dev/null +++ b/Sources/Tests/UnitTests/Core/FactorialSumTest.cs @@ -0,0 +1,50 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Core +{ + /// + /// A polynomial times the factorial of the index, summed where the sum telescopes: + /// Sullivan and Mackey's Prob 2.7.17, sum(k k!, k, 1, n) = (n + 1)! - 1. + /// #1409 + /// + [Trait("Area", "Core")] + public sealed class FactorialSumTest + { + [Theory] + [InlineData("sum(k * factorial(k), k, 1, n)", "factorial(n + 1) - 1")] + [InlineData("sum(k * factorial(k), k, 0, n)", "factorial(n + 1) - 1")] + [InlineData("sum((k^2 + k + 1) * factorial(k), k, 1, n)", "(n + 1) * factorial(n + 1) - 1")] + [InlineData("sum(c * k * factorial(k), k, 2, n)", "c * (factorial(n + 1) - 2)")] + public void ATelescopingSumIsClosed(string sum, string expected) + { + var closed = sum.ToEntity().Simplify(); + Assert.IsNotType(closed); + foreach (var n in new[] { 0, 1, 2, 3, 6, 11 }) + { + var want = sum.ToEntity().Substitute("n", n).Substitute("c", 3).Simplify().Evaled; + Assert.Equal(want, closed.Substitute("n", n).Substitute("c", 3).Simplify().Evaled); + if (n >= 1) + Assert.Equal(want, expected.ToEntity().Substitute("n", n).Substitute("c", 3).Simplify().Evaled); + } + } + + /// + /// Where no polynomial certificate exists the sum is left as written: the left factorial + /// sum(k!, k, 0, n) has no closed form in factorials, and neither has sum(k^2 k!). + /// + [Theory] + [InlineData("sum(factorial(k), k, 0, n)")] + [InlineData("sum(k^2 * factorial(k), k, 1, n)")] + public void ASumThatDoesNotTelescopeIsLeft(string sum) + => Assert.IsType(sum.ToEntity().Simplify()); + } +} diff --git a/Sources/Tests/UnitTests/Core/SummationProductTest.cs b/Sources/Tests/UnitTests/Core/SummationProductTest.cs index 0f2dc3bdc..14bf4a79d 100644 --- a/Sources/Tests/UnitTests/Core/SummationProductTest.cs +++ b/Sources/Tests/UnitTests/Core/SummationProductTest.cs @@ -65,12 +65,13 @@ public void ASymbolicBoundIsCarried(string expression) => Assert.IsType(expression.ToEntity().Simplify()); /// - /// A product is carried unless its body is a single term. Where the sum takes any - /// polynomial apart by linearity, a product has none to take it apart with. + /// A product is carried unless its body is a single term, or a power of one base whose + /// exponents sum in closed form. Where the sum takes any polynomial apart by linearity, a + /// product has none to take it apart with. /// [Theory] [InlineData("product(k + 1, k, 1, n)")] - [InlineData("product(2 ^ k, k, 1, n)")] + [InlineData("product(2 ^ sin(k), k, 1, n)")] public void AProductWithASymbolicBoundIsCarried(string expression) => Assert.IsType(expression.ToEntity().Simplify());