Skip to content

Commit b71a26e

Browse files
A rational function of the hyperbolic functions odd in the sine or the cosine is integrated by the other (#1464)
`sinh(x)^3/(a + b cosh(x)^2)` was a search past the budget: under u = e^x it is a rational function of a symbolic palindromic quartic, and under u = tanh(x/2) of a symbolic polynomial of degree eight. It is (u^2 - 1)/(a + b u^2) under u = cosh(x) -- Bioche's first two rules, which the trigonometric functions already had: odd in the hyperbolic sine, u = cosh with sinh^2 = u^2 - 1; odd in the hyperbolic cosine, u = sinh with cosh^2 = u^2 + 1. The six hyperbolic functions are read back from their exponential spellings through the gathered exponentials, for any linear argument and either sign of it; a written power is handed on as the power it is, factor by factor, and not expanded into a symbolic quartic for the rational integrator to factor again; a root of a polynomial in the two even in the function is admitted, a Laurent one (`a + b sech(x)`) declined, since the search after `sqrt(a + b/u)` is longer than the exponential substitution's. ParityIn and InU are lifted out of SolveByBiochesOddSubstitution and shared. In front of the substitution search, which with a symbolic coefficient spends the budget on u = e^x. Family 6 of the Rubi suite: 344 -> 357 of 417, 0 wrong, timeouts 34 -> 24; family 7 215/270 and the 1774-problem suite 1707 unchanged. Suite 12610 passed; allocation gate passed on all 19 gated benchmarks. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 139423a commit b71a26e

4 files changed

Lines changed: 293 additions & 21 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,26 @@ is `sqrt(b + a u^2)/|u|`, and `|u|^k` for the bare square is `sgn(u) u^k` at eve
10301030
| `"coth(x)/(a+b*coth(x)^2)^(3/2)".Integrate("x")` | left unevaluated | the antiderivative |
10311031
| `"(a+b*coth(x)^2)^(3/2)*tanh(x)^2".Integrate("x")` | left unevaluated | the antiderivative |
10321032

1033+
### A rational function of the hyperbolic functions odd in the sine or the cosine is integrated by the other
1034+
1035+
`sinh(x)^3/(a + b cosh(x)^2)` was a search past the budget: under `u = e^x` it is a rational
1036+
function of a symbolic palindromic quartic, and under `u = tanh(x/2)` of a symbolic polynomial
1037+
of degree eight. It is `(u^2 - 1)/(a + b u^2)` under `u = cosh(x)` -- Bioche's first two rules,
1038+
which the trigonometric functions already had: odd in the hyperbolic sine, `u = cosh` with
1039+
`sinh^2 = u^2 - 1`; odd in the hyperbolic cosine, `u = sinh` with `cosh^2 = u^2 + 1`. The six
1040+
hyperbolic functions are read back from their exponential spellings for any linear argument,
1041+
a written power is handed on as the power it is, and a root of a polynomial in the two even
1042+
in the function is admitted. Rubi's 6.1.7, 6.2.7, 6.3.7, 6.5.7 and 6.6.3
1043+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
1044+
1045+
| Input | Was (2.5.0) | Now |
1046+
|---|---|---|
1047+
| `"sinh(x)^3/(a+b*cosh(x)^2)".Integrate("x")` | left unevaluated | `cosh(x)/b - (a + b) arctan(sqrt(b) cosh(x)/sqrt(a))/(b sqrt(a b))` up to the form, by cases on `a` and `b` |
1048+
| `"cosh(c+d*x)/(a+b*tanh(c+d*x)^2)".Integrate("x")` | left unevaluated | the antiderivative, in `sinh(c + d x)` |
1049+
| `"sech(c+d*x)/(a+b*sinh(c+d*x)^2)^(3/2)".Integrate("x")` | left unevaluated | the antiderivative |
1050+
| `"csch(x)^5/(a+b*cosh(x)^2)".Integrate("x")` | left unevaluated | the antiderivative |
1051+
| `"sinh(x)^3/(1+cosh(x)^2)".Integrate("x")` | left unevaluated | `cosh(x) - 2 arctan(cosh(x))`, in exponentials -- where `master` between the releases gave it through `u = e^x`, in `arctan(2 e^x/sqrt(12 - 8 sqrt(2)))` and its like |
1052+
10331053
### `binomial(n, k)` is a function
10341054

10351055
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

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

Lines changed: 246 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13946,37 +13946,262 @@ private static int Lcm(int a, int b)
1394613946
return sign == 1 ? answer : -answer;
1394713947
}
1394813948
return null;
13949+
}
13950+
13951+
/// <summary>1 for even in <paramref name="f"/>, -1 for odd, 0 for neither; a zero polynomial is both.</summary>
13952+
private static int ParityIn(Entity polynomial, Entity.Variable f)
13953+
{
13954+
if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read))
13955+
return 0;
13956+
var evenPowers = read.Keys.Any(k => k.IsEven);
13957+
var oddPowers = read.Keys.Any(k => !k.IsEven);
13958+
return evenPowers && oddPowers ? 0 : oddPowers ? -1 : 1;
13959+
}
1394913960

13950-
// 1 for even in f, -1 for odd, 0 for neither; a zero polynomial is both.
13951-
static int ParityIn(Entity polynomial, Entity.Variable f)
13961+
/// <summary>
13962+
/// The polynomial with every <c>f^2</c> written as <paramref name="fSquared"/> -- <c>1 - u^2</c>
13963+
/// for a sine or cosine beside the other as <c>u</c>, <c>u^2 - 1</c> for a hyperbolic sine
13964+
/// beside the cosine, <c>u^2 + 1</c> the other way -- and the other function as <c>u</c>,
13965+
/// one <c>f</c> divided out first where asked.
13966+
/// </summary>
13967+
private static Entity? InU(Entity polynomial, Entity.Variable f, Entity.Variable other, Entity.Variable u, Entity fSquared, bool dividedByOdd)
13968+
{
13969+
if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read))
13970+
return null;
13971+
Entity sum = Number.Integer.Zero;
13972+
foreach (var pair in read)
1395213973
{
13953-
if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read))
13954-
return 0;
13955-
var evenPowers = read.Keys.Any(k => k.IsEven);
13956-
var oddPowers = read.Keys.Any(k => !k.IsEven);
13957-
return evenPowers && oddPowers ? 0 : oddPowers ? -1 : 1;
13974+
if (pair.Key.Sign < 0 || !pair.Key.CanFitInInt32())
13975+
return null;
13976+
var power = pair.Key.ToInt32Unchecked() - (dividedByOdd ? 1 : 0);
13977+
if (power < 0 || power % 2 != 0)
13978+
return null;
13979+
var half = power / 2;
13980+
var term = pair.Value.Substitute(other, u) * (half == 0 ? Number.Integer.One : half == 1 ? fSquared : MathS.Pow(fSquared, half));
13981+
sum = sum == Number.Integer.Zero ? term : sum + term;
1395813982
}
13983+
return sum;
13984+
}
1395913985

13960-
// The polynomial with every f^2 written as 1 - u^2 and the other function as u,
13961-
// one f divided out first where asked.
13962-
static Entity? InU(Entity polynomial, Entity.Variable f, Entity.Variable other, Entity.Variable u, Entity oneMinusUSquared, bool dividedByOdd)
13986+
/// <summary>
13987+
/// Bioche's first two rules for the hyperbolic functions: a rational function of
13988+
/// <c>sinh(y)</c> and <c>cosh(y)</c> that is odd in the hyperbolic sine is a rational
13989+
/// function of <c>u = cosh(y)</c> times <c>sinh(y) dy = du</c>, with <c>sinh^2</c> as
13990+
/// <c>u^2 - 1</c>; one odd in the hyperbolic cosine is one of <c>u = sinh(y)</c> times
13991+
/// <c>cosh(y) dy = du</c>, with <c>cosh^2</c> as <c>u^2 + 1</c>. Radicals of polynomials
13992+
/// in the two even in the function are admitted as coefficients, as in
13993+
/// <see cref="SolveByBiochesOddSubstitution"/>.
13994+
/// </summary>
13995+
/// <remarks>
13996+
/// The hyperbolic functions are not nodes: the library writes <c>sinh(y)</c> as
13997+
/// <c>(e^y - e^-y)/2</c> and <c>tanh(y)</c> as <c>(e^(2y) - 1)/(e^(2y) + 1)</c>, so the
13998+
/// six are read back by their spellings, for the argument each exponential names, and
13999+
/// nothing else in <c>x</c> may remain. Rubi's <c>sinh(x)^3/(a + b cosh(x)^2)</c> is
14000+
/// <c>(u^2 - 1)/(a + b u^2)</c> here, where under <c>u = e^x</c> it is a rational
14001+
/// function of a symbolic palindromic quartic and under <c>u = tanh(x/2)</c> of one of
14002+
/// degree eight, and both were searches past the budget; <c>cosh(x)/(a + b tanh(x)^2)</c>,
14003+
/// <c>sech(x)/(a + b sinh(x)^2)^(3/2)</c> and <c>csch(x)^5/(a + b cosh(x)^2)</c> likewise.
14004+
/// In front of the substitution search, which with a symbolic coefficient spends the
14005+
/// budget on <c>u = e^x</c>. Exact: <c>cosh</c> is positive, so <c>u = sinh(y)</c> is a
14006+
/// bijection of the line, and <c>u = cosh(y)</c> one on each side of zero.
14007+
/// https://github.com/asc-community/AngouriMath/issues/718
14008+
/// </remarks>
14009+
internal static Entity? SolveByBiochesOddHyperbolicSubstitution(Entity expr, Entity.Variable x, bool integrateByParts)
14010+
{
14011+
if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
14012+
return null;
14013+
if (!TryGatherExponentialsOfOneLinearForm(expr, x, unitX: true, out var exponentials, out var y, out _))
14014+
return null;
14015+
var s = Variable.CreateUnique(expr, "s_hyp");
14016+
var c = Variable.CreateUnique(expr, "c_hyp");
14017+
// The six read by their spellings, each exponential by the multiple of y it is:
14018+
// sinh(y) is (e^y - e^-y)/2, cosh(y) (e^y + e^-y)/2, tanh(y) (e^(2y) - 1)/(e^(2y) + 1),
14019+
// coth(y) the reciprocal, sech(y) 1/cosh(y) and csch(y) 1/sinh(y). The argument is y,
14020+
// or its half where `tanh(c + d x)` alone is written with e^(2(c + d x)) and the
14021+
// gathering names that y: then the tangent's spelling carries e^y.
14022+
Entity? argument = null;
14023+
Entity overTheTwo = expr;
14024+
foreach (var halved in new[] { false, true })
1396314025
{
13964-
if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read))
14026+
var one = halved ? null : (EInteger?)EInteger.One;
14027+
var two = halved ? EInteger.One : EInteger.FromInt32(2);
14028+
// With y named from an exponential of the other sign, e^-y is the k = 1 one:
14029+
// sinh and tanh are odd, cosh even.
14030+
Entity? ReadTheTwo(Entity node)
14031+
{
14032+
switch (node)
14033+
{
14034+
case Divf(Minusf(var a, var b), Number.Integer { EInteger: var d }) when d.Equals(EInteger.FromInt32(2)) && Is(a, one) && Is(b, one?.Negate()):
14035+
return s;
14036+
case Divf(Minusf(var a, var b), Number.Integer { EInteger: var d }) when d.Equals(EInteger.FromInt32(2)) && Is(a, one?.Negate()) && Is(b, one):
14037+
return -s;
14038+
case Divf(Sumf(var a, var b), Number.Integer { EInteger: var d }) when d.Equals(EInteger.FromInt32(2)) && (Is(a, one) && Is(b, one?.Negate()) || Is(a, one?.Negate()) && Is(b, one)):
14039+
return c;
14040+
case Divf(Minusf(var a, Number.Integer { EInteger: var m }), Sumf(var b, Number.Integer { EInteger: var n })) when m.Equals(EInteger.One) && n.Equals(EInteger.One) && Is(a, two) && Is(b, two):
14041+
return s / c;
14042+
case Divf(Minusf(var a, Number.Integer { EInteger: var m }), Sumf(var b, Number.Integer { EInteger: var n })) when m.Equals(EInteger.One) && n.Equals(EInteger.One) && Is(a, two.Negate()) && Is(b, two.Negate()):
14043+
return -s / c;
14044+
case Divf(Sumf(var a, Number.Integer { EInteger: var m }), Minusf(var b, Number.Integer { EInteger: var n })) when m.Equals(EInteger.One) && n.Equals(EInteger.One) && Is(a, two) && Is(b, two):
14045+
return c / s;
14046+
case Divf(Sumf(var a, Number.Integer { EInteger: var m }), Minusf(var b, Number.Integer { EInteger: var n })) when m.Equals(EInteger.One) && n.Equals(EInteger.One) && Is(a, two.Negate()) && Is(b, two.Negate()):
14047+
return -c / s;
14048+
case Divf(Number.Integer { EInteger: var m }, var inner) when m.Equals(EInteger.One) && ReadTheTwo(inner) is { } read:
14049+
return 1 / read;
14050+
}
1396514051
return null;
13966-
Entity sum = Number.Integer.Zero;
13967-
foreach (var pair in read)
14052+
}
14053+
bool Is(Entity node, EInteger? k) => k is { } wanted && exponentials.TryGetValue(node, out var found) && found.Equals(wanted);
14054+
var read = expr.Replace(node => node.ContainsNode(x) && ReadTheTwo(node) is { } inTheTwo ? inTheTwo : node);
14055+
if (read.ContainsNode(s) || read.ContainsNode(c))
1396814056
{
13969-
if (pair.Key.Sign < 0 || !pair.Key.CanFitInInt32())
13970-
return null;
13971-
var power = pair.Key.ToInt32Unchecked() - (dividedByOdd ? 1 : 0);
13972-
if (power < 0 || power % 2 != 0)
14057+
argument = halved ? (y / 2).InnerSimplified : y;
14058+
overTheTwo = read;
14059+
break;
14060+
}
14061+
}
14062+
if (argument is null)
14063+
return null;
14064+
if (!overTheTwo.ContainsNode(s) && !overTheTwo.ContainsNode(c))
14065+
return null;
14066+
var radicals = new List<(Variable Symbol, Entity Base, Number.Rational Exponent)>();
14067+
overTheTwo = overTheTwo.Replace(node =>
14068+
{
14069+
if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(s) && !radicand.ContainsNode(c))
14070+
return node;
14071+
var symbol = Variable.CreateUnique(expr, "r_hyp" + radicals.Count);
14072+
var combined = Functions.SingleQuotient.Combine(radicand);
14073+
radicals.Add((symbol, combined, exponent));
14074+
return symbol;
14075+
});
14076+
if (overTheTwo.ContainsNode(x))
14077+
return null;
14078+
// A radicand that is a polynomial in the two, and not a Laurent one: `a + b sech(x)`
14079+
// is `a + b/c`, whose root under u = cosh(x) is a root of `a + b/u`, and the search
14080+
// that follows it is longer than the exponential substitution's.
14081+
if (radicals.Any(radical => radical.Base.ContainsNode(x) || !IsAPolynomialInBoth(radical.Base)))
14082+
return null;
14083+
// The quotient factor by factor, each factor a polynomial in the two, so that a
14084+
// written power -- `(a + b sech(x)^2)^2` -- is handed on as the power it is and not
14085+
// expanded into a symbolic quartic for the rational integrator to factor again.
14086+
var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(overTheTwo));
14087+
var factors = new List<(Entity Base, int Power)>();
14088+
foreach (var (side, sign) in new[] { (above, 1), (below, -1) })
14089+
foreach (var factor in Mulf.LinearChildren(side))
14090+
{
14091+
var (factorBase, power) = factor is Powf(var inner, Number.Integer whole) && whole.EInteger.CanFitInInt32() ? (inner, whole.EInteger.ToInt32Unchecked()) : (factor, 1);
14092+
factorBase = factorBase.Expand();
14093+
if (!IsAPolynomialInBoth(factorBase))
1397314094
return null;
13974-
var half = power / 2;
13975-
var term = pair.Value.Substitute(other, u) * (half == 0 ? Number.Integer.One : half == 1 ? oneMinusUSquared : MathS.Pow(oneMinusUSquared, half));
13976-
sum = sum == Number.Integer.Zero ? term : sum + term;
14095+
factors.Add((factorBase, sign * power));
1397714096
}
13978-
return sum;
14097+
bool IsAPolynomialInBoth(Entity polynomial)
14098+
=> TreeAnalyzer.TryGetPolynomial(polynomial, s, out var inS) && inS.Keys.All(k => k.Sign >= 0)
14099+
&& TreeAnalyzer.TryGetPolynomial(polynomial, c, out var inC) && inC.Keys.All(k => k.Sign >= 0);
14100+
14101+
var u = Variable.CreateUnique(expr, "u_hyp");
14102+
foreach (var (odd, even, fSquared, back) in new[]
14103+
{
14104+
(s, c, MathS.Sqr(u) - 1, MathS.Hyperbolic.Cosh(argument)),
14105+
(c, s, MathS.Sqr(u) + 1, MathS.Hyperbolic.Sinh(argument)),
14106+
})
14107+
{
14108+
if (radicals.Any(radical => ParityIn(radical.Base, odd) != 1))
14109+
continue;
14110+
// An odd factor is f times an even one, and the powers of f are counted, one
14111+
// of them being dx's. A factor neither even nor odd in f -- `1 + sinh(x)` --
14112+
// sends the two sides expanded and cleared against the mirror, as
14113+
// SolveByBiochesOddSubstitution does, since its mirror is in u only beside it.
14114+
Entity numerator = Number.Integer.One;
14115+
Entity denominator = Number.Integer.One;
14116+
var powerOfF = -1;
14117+
var readable = true;
14118+
if (factors.All(factor => ParityIn(factor.Base, odd) != 0))
14119+
{
14120+
foreach (var (factorBase, power) in factors)
14121+
{
14122+
var parity = ParityIn(factorBase, odd);
14123+
if (InU(factorBase, odd, even, u, fSquared, dividedByOdd: parity == -1) is not { } inU)
14124+
{
14125+
readable = false;
14126+
break;
14127+
}
14128+
if (parity == -1)
14129+
powerOfF += power;
14130+
var powered = MathS.Pow(inU, Number.Integer.Create(System.Math.Abs(power)));
14131+
if (power > 0) numerator *= powered; else denominator *= powered;
14132+
}
14133+
if (!readable || powerOfF % 2 != 0)
14134+
continue;
14135+
var half = powerOfF / 2;
14136+
if (half > 0)
14137+
numerator *= MathS.Pow(fSquared, Number.Integer.Create(half));
14138+
else if (half < 0)
14139+
denominator *= MathS.Pow(fSquared, Number.Integer.Create(-half));
14140+
}
14141+
else
14142+
{
14143+
var aboveExpanded = above.Expand();
14144+
var belowExpanded = below.Expand();
14145+
var parityOfBelow = ParityIn(belowExpanded, odd);
14146+
Entity top;
14147+
Entity bottom;
14148+
bool oneFOutOfTheNumerator;
14149+
if (parityOfBelow == 1)
14150+
{
14151+
top = aboveExpanded;
14152+
bottom = belowExpanded;
14153+
oneFOutOfTheNumerator = true;
14154+
}
14155+
else if (parityOfBelow == -1)
14156+
{
14157+
top = aboveExpanded;
14158+
bottom = (belowExpanded * odd).Expand();
14159+
oneFOutOfTheNumerator = false;
14160+
}
14161+
else
14162+
{
14163+
var mirrored = belowExpanded.Substitute(odd, -odd).Expand();
14164+
top = (aboveExpanded * mirrored).Expand();
14165+
bottom = (belowExpanded * mirrored).Expand();
14166+
oneFOutOfTheNumerator = true;
14167+
}
14168+
if (ParityIn(top, odd) != (oneFOutOfTheNumerator ? -1 : 1) || ParityIn(bottom, odd) != 1)
14169+
continue;
14170+
if (InU(top, odd, even, u, fSquared, oneFOutOfTheNumerator) is not { } topInU
14171+
|| InU(bottom, odd, even, u, fSquared, dividedByOdd: false) is not { } bottomInU)
14172+
continue;
14173+
numerator = topInU;
14174+
denominator = bottomInU;
14175+
}
14176+
var radicalsInU = new List<(Variable Symbol, Entity InU)>();
14177+
foreach (var (symbol, radicand, exponent) in radicals)
14178+
{
14179+
if (InU(radicand, odd, even, u, fSquared, dividedByOdd: false) is not { } radicandInU)
14180+
break;
14181+
radicalsInU.Add((symbol, MathS.Pow(radicandInU, exponent)));
14182+
}
14183+
if (radicalsInU.Count != radicals.Count)
14184+
continue;
14185+
foreach (var (symbol, radical) in radicalsInU)
14186+
{
14187+
numerator = numerator.Substitute(symbol, radical);
14188+
denominator = denominator.Substitute(symbol, radical);
14189+
}
14190+
var integrand = Functions.PartialFractions.Bare((numerator / denominator).InnerSimplified);
14191+
if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result
14192+
|| result.Nodes.Any(node => node == MathS.NaN))
14193+
continue;
14194+
var answer = result.Substitute(u, back);
14195+
// dx is dy/d for the argument y = c + d x.
14196+
if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) || TreeAnalyzer.IsZero(slope))
14197+
return null;
14198+
var scaled = slope == Number.Integer.One ? answer : answer / slope;
14199+
// Checked where a symbol is involved, as every answer assembled from pieces is.
14200+
if (expr.Vars.Any(symbol => symbol != x) && !Functions.PartialFractions.DerivativeHoldsAtSampledPoints(scaled, expr, x))
14201+
return null;
14202+
return scaled;
1397914203
}
14204+
return null;
1398014205
}
1398114206

1398214207
/// <summary>

0 commit comments

Comments
 (0)