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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
115 changes: 115 additions & 0 deletions Sources/AngouriMath/Functions/Algebra/Polynomials/FactorialSum.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A polynomial times the factorial of the index, summed in closed form where the sum
/// telescopes: <c>sum(k k!, k, 1, n)</c> is <c>(n + 1)! - 1</c>, Sullivan and Mackey's Prob 2.7.17.
/// </summary>
/// <remarks>
/// <para>
/// <c>P(k) k!</c> is <c>T(k + 1) - T(k)</c> for <c>T(k) = Q(k) k!</c> exactly where
/// <c>(k + 1) Q(k + 1) - Q(k) = P(k)</c>, and then the sum from <c>a</c> to <c>b</c> is
/// <c>T(b + 1) - T(a)</c>. The equation is triangular in <c>Q</c>'s coefficients from the top
/// down, the coefficient of <c>k^(m + 1)</c> fixing <c>q_m</c>, and its constant term is one
/// condition more: <c>q_1 + q_2 + ... = p_0</c>. Where that fails, no polynomial <c>Q</c> exists
/// and the sum is declined: <c>sum(k!, k, 0, n)</c>, the left factorial, has no closed form in
/// factorials. This is Gosper's algorithm for the one family, where the certificate is a
/// polynomial.
/// </para>
/// <para>
/// Over <c>a &gt;= 0</c>, where <c>a!</c> is defined; a range that runs backwards is empty and
/// the sum zero, which is what the piecewise says, while the closed form at <c>b = a - 1</c> is
/// zero already.
/// </para>
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </remarks>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Website: https://am.angouri.org.
//

using System.Linq;
using PeterO.Numbers;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;
Expand Down Expand Up @@ -44,6 +45,36 @@ namespace AngouriMath.Functions
/// </remarks>
internal static class MonomialProduct
{
/// <summary>
/// <c>product(c^(p(k)), k, a, b)</c> is <c>c^(sum(p(k), k, a, b))</c> for a base free of the
/// index, where that sum closes: the factors are powers of one base, whose exponents add,
/// <c>c^s c^t = c^(s + t)</c> -- on the principal branch as well, each power being
/// <c>e^(s ln c)</c> with the one <c>ln c</c>. Prob 1.3.12 of Sullivan and Mackey:
/// <c>product(2^k, k, 0, n - 1)</c> is <c>2^(n (n - 1)/2)</c>. An empty range sums to zero,
/// and <c>c^0</c> is the empty product's <c>1</c>.
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
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;
}

/// <summary>
/// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 13 additions & 1 deletion Sources/Tests/UnitTests/Core/ClosedFormProductTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// 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, <c>product(2^k, k, 0, n - 1) = 2^(n (n - 1)/2)</c>.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>
/// </summary>
[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);

/// <summary>A constant factor comes out as a power of itself, one per term.</summary>
[Theory]
[InlineData("product(2, k, 1, n)")]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Tests/UnitTests/Core/ClosedFormSummationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void WhatIsNotPolynomialInTheIndexIsCarried(string expression)
/// </remarks>
[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<Entity.Productf>(expression.ToEntity().Simplify());

Expand Down
50 changes: 50 additions & 0 deletions Sources/Tests/UnitTests/Core/FactorialSumTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A polynomial times the factorial of the index, summed where the sum telescopes:
/// Sullivan and Mackey's Prob 2.7.17, <c>sum(k k!, k, 1, n) = (n + 1)! - 1</c>.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>
/// </summary>
[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<Entity.Summationf>(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);
}
}

/// <summary>
/// Where no polynomial certificate exists the sum is left as written: the left factorial
/// <c>sum(k!, k, 0, n)</c> has no closed form in factorials, and neither has <c>sum(k^2 k!)</c>.
/// </summary>
[Theory]
[InlineData("sum(factorial(k), k, 0, n)")]
[InlineData("sum(k^2 * factorial(k), k, 1, n)")]
public void ASumThatDoesNotTelescopeIsLeft(string sum)
=> Assert.IsType<Entity.Summationf>(sum.ToEntity().Simplify());
}
}
7 changes: 4 additions & 3 deletions Sources/Tests/UnitTests/Core/SummationProductTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ public void ASymbolicBoundIsCarried(string expression)
=> Assert.IsType<Entity.Summationf>(expression.ToEntity().Simplify());

/// <summary>
/// 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.
/// </summary>
[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<Entity.Productf>(expression.ToEntity().Simplify());

Expand Down
Loading