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 @@ -1294,6 +1294,21 @@ arctangent, or an inverse hyperbolic tangent, of the tangent of the argument or
| `"1/(a+b*cosh(x)^5)".ToEntity().Integrate("x")` | `integral(...)` | 3,607 characters |
| `"1/(a-b*cosh(x)^8)".ToEntity().Integrate("x")` | `integral(...)` | 1,441 characters |

### A sum split into its terms is given up at the first term declined

**Answers where there were none.** `1/(a sech(x) + b tanh(x))^2`, `1/(a coth(x) + b csch(x))^2` and
`1/(1 - cosh(x)^8)`, Rubi's 6.7.1 and 6.2.7, were declined, and before this change ran past two minutes. Integrating
a sum term by term asked every term after one had been declined, and declining those came before the rules that
answer these integrands; the split now ends at the first term declined. More slowly, in ten to twenty seconds,
`1/(1 - cosh(x)^6)` and `e^(a + b x + c x^2) (b + 2 c x)/(a + b x + c x^2)^2` are answered too
([#233](https://github.com/asc-community/AngouriMath/issues/233)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"1/(a*sech(x)+b*tanh(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 1,836 characters |
| `"1/(a*coth(x)+b*csch(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 1,831 characters |
| `"1/(1-cosh(x)^8)".ToEntity().Integrate("x")` | `integral(...)` | 1,232 characters |

### A whole power of an inverse function of `c + d x^2` is integrated by its recurrence

**Answers where there were none.** `(a + b acos(1 + d x^2))^4` was declined, and so were twenty-five more of Rubi's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,21 @@ internal static class IndefiniteIntegralSolver
return ((top * group.Radicals).InnerSimplified / bottom).InnerSimplified;
}).ToList());

// Term by term, and over at the first term declined: the sum is answered only if every
// term is, and the terms after one declined were each asked to no purpose. Rubi's 6.1.7
// `csch(c + d x)^6 (a + b sinh(c + d x)^3)^3` is expanded into terms one of which is
// declined, and the terms after it cost 4.2 GB of the 5.5 the integral took.
Entity? Integrated(List<Entity> terms)
=> terms.Select(e => Integration.ComputeAsAQuestionOfItsOwn(e, x, integrateByParts)).Aggregate((e1, e2) => (e1, e2) switch {
(null, _) or (_, null) => null,
(var int1, var int2) => int1 + int2
});
{
Entity? sum = null;
foreach (var term in terms)
{
if (Integration.ComputeAsAQuestionOfItsOwn(term, x, integrateByParts) is not { } integrated)
return null;
sum = sum is null ? integrated : sum + integrated;
}
return sum;
}
}

/// <summary>
Expand Down Expand Up @@ -28609,11 +28619,13 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x)
// `sech(x)^2/(a + b tanh(x))^2` after three seconds: simplified once more it is a
// quotient, which the symbolic quadratic rule answers with the case `a + b = 0` apart,
// and a piecewise antiderivative leaves parts a piecewise to integrate. With numbers
// alone, or no sum written twice, it is simplified as it was.
// alone, or no sum written twice, it is simplified as it was -- but for a power of a
// symbolic polynomial of the third degree or more, without the expanded and factored
// forms the simplifier offers after its search: see HasAPowerOfASymbolicCubicOrMore.
var combined = Functions.SingleQuotient.Combine(rewritten / (Number.Rational.Create(k) * u));
var integrand = combined.Vars.Any(symbol => symbol != u)
&& WithTheFactorsWrittenOnBothSidesCancelled(u, combined, keepTheWrittenBases: true, out var anyCancelled) is var cancelled && anyCancelled
? cancelled : combined.Simplify();
? cancelled : combined.Simplify(HasAPowerOfASymbolicCubicOrMore(combined, u) ? -2 : 2);
if (integrand is Providedf(var inner, _))
integrand = inner;
integrand = WithLogarithmArgumentsOverOneBar(integrand, u);
Expand Down Expand Up @@ -28661,6 +28673,64 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x)
}
}

/// <summary>
/// Whether <paramref name="quotient"/> holds a whole power past the first of a sum that is a
/// polynomial in <paramref name="u"/> of the third degree or more with a symbol in one of
/// its coefficients: <c>(a (2u)^3 + b (u^2 - 1)^3)^2</c>, which is what the exponential
/// substitution makes of <c>(a + b sinh(x)^3)^2</c>.
/// </summary>
/// <remarks>
/// <para>
/// Such a quotient is simplified without the two forms the simplifier offers after its
/// search, the quotient multiplied out and the quotient factored, each simplified again
/// before it is ranked; the search itself runs as before. With the symbols in it both are
/// polynomials of a high degree with a page of coefficients: Rubi's 6.1.7
/// <c>csch(c + d x)^m (a + b sinh(c + d x)^3)^p</c> spent 2.2 to 7.7 GB of its 5.2 to 10.5
/// simplifying them -- the common denominator of the expanded terms and the long division
/// of the factored quotient, each coefficient simplified on its own -- to rank both behind
/// what the search had found, where the search alone takes 12 to 130 MB.
/// </para>
/// <para>
/// Those fourteen rows, <c>m</c> from 1 to 7 and <c>p</c> 2 or 3, are the corpus's 6.1.7
/// quotients with such a power. On thirteen the search alone gives what the whole
/// simplification gives. On <c>csch^7 (a + b sinh^3)^2</c> the factored form wins: the powers
/// of <c>u</c> written apart above the bar and below it are of one degree, seven, and in the
/// factored form they cancel to a number. So where they are of one degree the quotient is
/// simplified in full; and so it is where the sum has no constant term, so that <c>u</c>
/// divides it, as in 6.7.1's <c>(A + B cosh(x) + C sinh(x))/(b cosh(x) + c sinh(x))^2</c>:
/// the factored form wins there too, and from the search's form the antiderivative was
/// 178,682 characters where it is 3,485. A power of a polynomial with numbers for its
/// coefficients is simplified in full as well, as is one of the second degree: there the
/// forms after the search win often and cost little.
/// https://github.com/asc-community/AngouriMath/issues/233
/// </para>
/// </remarks>
private static bool HasAPowerOfASymbolicCubicOrMore(Entity quotient, Entity.Variable u)
{
// With a constant term: a sum that u divides has a factor the factored form takes out.
if (!quotient.Nodes.Any(node => node is Powf(var @base, Number.Integer { EInteger: var power })
&& @base is Sumf or Minusf && power.CompareTo(EInteger.One) > 0
&& TreeAnalyzer.TryGetPolynomial(@base, u, out var terms)
&& terms.Keys.All(degree => degree.Sign >= 0) && terms.Keys.Any(degree => degree.CompareTo(EInteger.FromInt32(3)) >= 0)
&& terms.Values.All(coefficient => !coefficient.ContainsNode(u)) && terms.Values.Any(coefficient => coefficient.Vars.Any())
&& terms.TryGetValue(EInteger.Zero, out var constant) && !Functions.PartialFractions.IsZeroAsAValue(constant)))
return false;
// Where the powers of u written apart above the bar and below it are of one degree the
// factored form cancels them to a number and wins: kept to the whole simplification.
var (top, bottom) = Functions.SingleQuotient.Of(quotient);
return !DegreeOfTheMonomials(top).Equals(DegreeOfTheMonomials(bottom));

EInteger DegreeOfTheMonomials(Entity side)
{
var degree = EInteger.Zero;
foreach (var factor in Mulf.LinearChildren(side))
if (factor.ContainsNode(u) && TreeAnalyzer.TryGetPolynomial(factor, u, out var monomial) && monomial.Count == 1
&& monomial.Keys.First() is { Sign: > 0 } power && !monomial.Values.First().ContainsNode(u))
degree += power;
return degree;
}
}

/// <summary>
/// <paramref name="expr"/> with every logarithm whose argument is a rational function
/// of <paramref name="x"/> with a quotient nested inside it written over one bar in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// 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;
using AngouriMath.Extensions;
using Xunit;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// Rubi's 6.1.7 <c>csch(c + d x)^m (a + b sinh(c + d x)^3)^p</c>. Under <c>u = e^(c + d x)</c> it is a
/// quotient with a power of a symbolic polynomial of the sixth degree in it, which is simplified
/// without the expanded and factored forms the simplifier offers after its search; and the split
/// of the expanded integrand into terms, which declines, stops at the first term declined. Each
/// row cost 5.2 to 10.5 GB, and the answers are the ones they were.
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
/// </summary>
/// <remarks>
/// Each row has symbols of its own, so that no answer the thread remembers from another test
/// makes its bound hold without the work it measures. The bounds fail with either change
/// undone: with the quotient simplified in full every row, at 4.9 to 8.8 GB, and with every
/// term of the split asked the second and the fourth, at 5.2 and 2.8 GB.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class AHyperbolicCosecantBesideAPowerOfABinomialInTheCubedSineIntegralTest
{
[Theory]
[InlineData("csch(v + w*x)^5*(p + q*sinh(v + w*x)^3)^2", "v,w,p,q", 2_000_000_000L)]
[InlineData("csch(r + s*x)^6*(k + m*sinh(r + s*x)^3)^3", "r,s,k,m", 3_000_000_000L)]
[InlineData("csch(g + h*x)*(n + t*sinh(g + h*x)^3)^3", "g,h,n,t", 2_000_000_000L)]
[InlineData("csch(y + z*x)^7*(f + u*sinh(y + z*x)^3)^3", "y,z,f,u", 2_000_000_000L)]
public void AnsweredWithinItsAllocation(string integrand, string symbols, long mostBytes)
{
var before = GC.GetAllocatedBytesForCurrentThread();
var integral = integrand.ToEntity().Integrate("x");
var allocated = GC.GetAllocatedBytesForCurrentThread() - before;
Assert.True(allocated < mostBytes, $"{integrand} allocated {allocated / 1_000_000} MB, past {mostBytes / 1_000_000} MB");
DifferentiatesBack(integrand, integral, symbols);
}

/// <summary>
/// Where the powers of <c>u</c> above the bar and below it are of one degree, seven here, the
/// factored form the simplifier offers after its search wins and is kept: from the search's
/// form alone the antiderivative was 1,938 characters where it is 571.
/// </summary>
[Fact]
public void WherePowersOfTheExponentialCancelTheFactoredFormIsKept()
{
const string integrand = "csch(c + d*x)^7*(a + b*sinh(c + d*x)^3)^2";
var integral = integrand.ToEntity().Integrate("x");
Assert.True(integral.ToString().Length < 1000, $"the antiderivative of {integrand} is {integral.ToString().Length} characters");
DifferentiatesBack(integrand, integral, "c,d,a,b");
}

/// <summary>
/// And where the sum to a power has no constant term in <c>u</c>, so that <c>u</c> divides it:
/// under <c>u = e^x</c> Rubi's 6.7.1 <c>(A + B cosh(x) + C sinh(x))/(b cosh(x) + c sinh(x))^2</c>
/// is a quotient with such a sum squared, the factored form wins there, and from the search's
/// form alone the antiderivative was 178,682 characters where it is 3,485.
/// </summary>
[Fact]
public void WhereTheSumHasNoConstantTermTheFactoredFormIsKept()
{
const string integrand = "(A + B*cosh(x) + C*sinh(x))/(b*cosh(x) + c*sinh(x))^2";
var integral = integrand.ToEntity().Integrate("x");
Assert.True(integral.ToString().Length < 10000, $"the antiderivative of {integrand} is {integral.ToString().Length} characters");
DifferentiatesBack(integrand, integral, "A,B,C,b,c");
}

private static void DifferentiatesBack(string integrand, Entity integral, string symbols)
{
Assert.DoesNotContain("integral(", integral.Stringize());
var names = symbols.Split(',');
var values = new[] { 0.3, 1.1, 1.3, 0.7, 0.4 };
Entity Pinned(Entity e)
{
for (var k = 0; k < names.Length; k++)
e = e.Substitute(names[k], values[k]);
return e;
}
// A constant of integration differentiates away; C is a coefficient of one integrand here.
var derivative = Pinned(integral).Differentiate("x");
var original = Pinned(integrand.ToEntity());
var compared = 0;
foreach (var at in new[] { 0.2, 0.45, 0.7, 0.95, 1.2, 1.5 })
{
var want = original.Substitute("x", at).EvalNumerical();
var got = derivative.Substitute("x", at).EvalNumerical();
if (want.IsNaN)
continue;
compared++;
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
< 1e-8 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
}
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
}
}
}
Loading