Skip to content

Commit 7a49ad0

Browse files
Cancel a sum the exponential substitution writes on both sides of the bar before simplifying the quotient (#1989)
x csch(x)^2/(a + b coth(x))^2 went through the exponential substitution as 4 (u^2 - 1)^2 u^2/((u^2 - 1)^2 (a (u^2 - 1) + b (u^2 + 1))^2 u), whose Simplify ran past 20 s with the symbols in it and stalled SolveAPolynomialTimesARationalFunctionOfAnExponential. Where the integrand holds symbols and a sum is written on both sides of the bar, that factor is now cancelled as written and the simplification skipped: 4 u (a (u^2 - 1) + b (u^2 + 1))^(-2), in 7 ms. Rubi's 6.4.2:203 is answered in 31 ms, checked at four pin sets on both sides and by quadrature; x/(sinh(x)^2 (a + b coth(x))^2) too. 158 solved symbolic hyperbolic and exponential rows answer as before, character for character; x sech(x)^2/(a + b tanh(x))^2 keeps its answer at 0.017 GB from 0.69. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8ccf7cc commit 7a49ad0

3 files changed

Lines changed: 116 additions & 13 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6111,6 +6111,22 @@ in front -- the identity `sqrt(a + a sin(y))` already had. Exact for any `a`, th
61116111
| `"cosh(x)/sqrt(a-a*cosh(x))".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(sinh(x/2))` in front |
61126112
| `"(A+B*cosh(x))/(a-a*cosh(x))^(5/2)".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(sinh(x/2))` in front |
61136113

6114+
### A sum the exponential substitution writes on both sides of the bar is cancelled before the quotient is simplified
6115+
6116+
**Answers where there were none.** `x csch(x)^2/(a + b coth(x))^2` ran past the time limit, and 2.5.0 left it
6117+
unevaluated. Under `u = e^x` its rational part is `4 (u^2 - 1)^2 u^2/((u^2 - 1)^2 (a (u^2 - 1) + b (u^2 + 1))^2 u)`,
6118+
and the simplification of that quotient, which with symbols in it searches for a factor to cancel, ran past twenty
6119+
seconds. The factor written on both sides is now cancelled as it is written, which leaves
6120+
`4 u (a (u^2 - 1) + b (u^2 + 1))^(-2)`, and one step of parts against it answers. Only where the integrand has a symbol
6121+
and a sum stands on both sides of the bar; otherwise the quotient is simplified as before. At `a = -b` the answer has
6122+
no value, as the answer to the hyperbolic tangent's `x sech(x)^2/(a + b tanh(x))^2` already had none there. Rubi's
6123+
6.4.2 ([#233](https://github.com/asc-community/AngouriMath/issues/233)).
6124+
6125+
| Input | Was (2.5.0) | Now |
6126+
|---|---|---|
6127+
| `"x*csch(x)^2/(a + b*coth(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 1,025 characters |
6128+
| `"x/(sinh(x)^2*(a + b*coth(x))^2)".ToEntity().Integrate("x")` | `integral(...)` | 1,025 characters |
6129+
61146130
### A rational function of `e^y` with a symbolic denominator is integrated by the hyperbolic half angle
61156131

61166132
`csch(x)^2/(a + b sinh(x))^2` was a search past eighty seconds: under `u = e^x` its denominator

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7030,7 +7030,20 @@ node is Powf(var xAgain, Number.Integer power) && xAgain == x && power.EInteger.
70307030
/// cancelled, power for power; the factors are kept as factors.
70317031
/// </summary>
70327032
private static Entity WithTheFactorsWrittenOnBothSidesCancelled(Entity.Variable u, Entity quotient)
7033+
=> WithTheFactorsWrittenOnBothSidesCancelled(u, quotient, keepTheWrittenBases: false, out _);
7034+
7035+
/// <summary>
7036+
/// <paramref name="quotient"/> with each factor written alike above and below the bar, read
7037+
/// as its polynomial in <paramref name="u"/>, cancelled, power for power. What is left is
7038+
/// written over one bar with its bases as polynomials, or, with
7039+
/// <paramref name="keepTheWrittenBases"/>, with its bases as they were written and the
7040+
/// factors below the bar as negative powers. <paramref name="anyCancelled"/> says whether a
7041+
/// sum was on both sides; a power of <paramref name="u"/> alone is not counted, since the
7042+
/// differential of an exponential's substitution writes one below every integrand.
7043+
/// </summary>
7044+
private static Entity WithTheFactorsWrittenOnBothSidesCancelled(Entity.Variable u, Entity quotient, bool keepTheWrittenBases, out bool anyCancelled)
70337045
{
7046+
anyCancelled = false;
70347047
var (top, bottom) = Functions.SingleQuotient.Of(quotient);
70357048
Entity AsItsPolynomial(Entity @base)
70367049
{
@@ -7049,19 +7062,19 @@ Entity AsItsPolynomial(Entity @base)
70497062
}
70507063
return written.InnerSimplified;
70517064
}
7052-
List<(Entity Base, EInteger Power)> Factors(Entity side)
7065+
List<(Entity Base, Entity Written, EInteger Power)> Factors(Entity side)
70537066
{
7054-
var factors = new List<(Entity Base, EInteger Power)>();
7067+
var factors = new List<(Entity Base, Entity Written, EInteger Power)>();
70557068
foreach (var factor in Mulf.LinearChildren(side))
70567069
{
7057-
var (@base, power) = factor is Powf(var raised, Number.Integer exponent) && exponent.EInteger.Sign > 0
7058-
? (raised, exponent.EInteger) : (factor, EInteger.One);
7059-
@base = AsItsPolynomial(@base);
7070+
var (raised, power) = factor is Powf(var raisedBase, Number.Integer exponent) && exponent.EInteger.Sign > 0
7071+
? (raisedBase, exponent.EInteger) : (factor, EInteger.One);
7072+
var @base = AsItsPolynomial(raised);
70607073
var at = factors.FindIndex(written => written.Base == @base);
70617074
if (at < 0)
7062-
factors.Add((@base, power));
7075+
factors.Add((@base, keepTheWrittenBases ? raised : @base, power));
70637076
else
7064-
factors[at] = (@base, factors[at].Power.Add(power));
7077+
factors[at] = (@base, factors[at].Written, factors[at].Power.Add(power));
70657078
}
70667079
return factors;
70677080
}
@@ -7075,12 +7088,17 @@ Entity AsItsPolynomial(Entity @base)
70757088
if (j < 0)
70767089
continue;
70777090
var common = EInteger.Min(above[i].Power, below[j].Power);
7078-
above[i] = (above[i].Base, above[i].Power.Subtract(common));
7079-
below[j] = (below[j].Base, below[j].Power.Subtract(common));
7091+
if (common.Sign > 0 && above[i].Base is Sumf or Minusf)
7092+
anyCancelled = true;
7093+
above[i] = (above[i].Base, above[i].Written, above[i].Power.Subtract(common));
7094+
below[j] = (below[j].Base, below[j].Written, below[j].Power.Subtract(common));
70807095
}
7081-
static Entity Product(List<(Entity Base, EInteger Power)> factors)
7096+
static Entity Product(List<(Entity Base, Entity Written, EInteger Power)> factors)
70827097
=> factors.Where(written => !written.Power.IsZero).Aggregate((Entity)Number.Integer.One,
7083-
(product, written) => product * (written.Power.Equals(EInteger.One) ? written.Base : MathS.Pow(written.Base, Number.Integer.Create(written.Power))));
7098+
(product, written) => product * (written.Power.Equals(EInteger.One) ? written.Written : MathS.Pow(written.Written, Number.Integer.Create(written.Power))));
7099+
if (keepTheWrittenBases)
7100+
return below.Where(written => !written.Power.IsZero).Aggregate(Product(above),
7101+
(product, written) => product * MathS.Pow(written.Written, Number.Integer.Create(written.Power.Negate()))).InnerSimplified;
70847102
return (Product(above) / Product(below)).InnerSimplified;
70857103
}
70867104

@@ -26609,8 +26627,22 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x)
2660926627
// `1/((a + b u)^2 ln F)` is a quadratic below the bar with `ln F` in every
2661026628
// coefficient, and the symbolic quadratic rule answers that as a piecewise on
2661126629
// whether `b^2 ln F` is zero, where `1/(a + b u)^2` is `-1/(b (a + b u))`.
26612-
var integrand = Functions.SingleQuotient.Combine(
26613-
rewritten / (Number.Rational.Create(k) * u)).Simplify();
26630+
// With symbols in it, a sum the substitution writes on both sides is cancelled as it is
26631+
// written, in place of the simplification: `csch(x)^2/(a + b coth(x))^2` is
26632+
// `4 (u^2 - 1)^2 u^2/((u^2 - 1)^2 (a (u^2 - 1) + b (u^2 + 1))^2 u)`, and the search for a
26633+
// simpler form, which with the factor in it grows with the symbols, ran past twenty
26634+
// seconds on it -- Rubi's 6.4.2 `x csch(x)^2/(a + b coth(x))^2` timed out on that, by
26635+
// parts against the whole rational function -- where cancelled it is
26636+
// `4 u (a (u^2 - 1) + b (u^2 + 1))^(-2)` in seven milliseconds. Left as the product the
26637+
// cancellation writes, which is what the search wrote for the tangent's
26638+
// `sech(x)^2/(a + b tanh(x))^2` after three seconds: simplified once more it is a
26639+
// quotient, which the symbolic quadratic rule answers with the case `a + b = 0` apart,
26640+
// and a piecewise antiderivative leaves parts a piecewise to integrate. With numbers
26641+
// alone, or no sum written twice, it is simplified as it was.
26642+
var combined = Functions.SingleQuotient.Combine(rewritten / (Number.Rational.Create(k) * u));
26643+
var integrand = combined.Vars.Any(symbol => symbol != u)
26644+
&& WithTheFactorsWrittenOnBothSidesCancelled(u, combined, keepTheWrittenBases: true, out var anyCancelled) is var cancelled && anyCancelled
26645+
? cancelled : combined.Simplify();
2661426646
if (integrand is Providedf(var inner, _))
2661526647
integrand = inner;
2661626648
integrand = WithLogarithmArgumentsOverOneBar(integrand, u);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// A sum the exponential substitution writes on both sides of the bar, cancelled as it is
16+
/// written in place of simplifying the quotient: <c>csch(x)^2/(a + b coth(x))^2</c> is
17+
/// <c>4 (u^2 - 1)^2 u^2/((u^2 - 1)^2 (a (u^2 - 1) + b (u^2 + 1))^2 u)</c> under <c>u = e^x</c>,
18+
/// and simplifying that with the symbols in it ran past twenty seconds, so that
19+
/// <c>x csch(x)^2/(a + b coth(x))^2</c>, one step of parts, timed out. Rubi's 6.4.2.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
21+
/// </summary>
22+
[Trait("Area", "Calculus")]
23+
public sealed class AFactorTheExponentialSubstitutionWritesOnBothSidesIntegralTest
24+
{
25+
[Theory]
26+
[InlineData("x*csch(x)^2/(a + b*coth(x))^2", 1.3, 0.7)]
27+
[InlineData("x*csch(x)^2/(a + b*coth(x))^2", 0.4, 1.1)]
28+
[InlineData("x/(sinh(x)^2*(a + b*coth(x))^2)", 1.3, 0.7)]
29+
[InlineData("x/(sinh(x)^2*(a + b*coth(x))^2)", 0.4, 1.1)]
30+
[InlineData("csch(x)^2/(a + b*coth(x))^2", 1.3, 0.7)]
31+
public void ByPartsAgainstTheCancelledQuotient(string integrand, double a, double b)
32+
{
33+
var integral = integrand.ToEntity().Integrate("x");
34+
var text = integral.Stringize();
35+
Assert.DoesNotContain("integral(", text);
36+
Assert.True(text.Length < 5000, $"{text.Length} characters of answer for {integrand}");
37+
Entity Pinned(Entity e) => e.Substitute("a", a).Substitute("b", b);
38+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
39+
var original = Pinned(integrand.ToEntity());
40+
var compared = 0;
41+
foreach (var at in new[] { -2.9, -1.3, 0.3, 0.8, 1.3, 2.9 })
42+
{
43+
var want = original.Substitute("x", at).EvalNumerical();
44+
var got = derivative.Substitute("x", at).EvalNumerical();
45+
if (want.IsNaN)
46+
continue;
47+
compared++;
48+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
49+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
50+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
51+
}
52+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)