diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 581be2d37..118f33948 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 | `"coth(x)/(a+b*coth(x)^2)^(3/2)".Integrate("x")` | left unevaluated | the antiderivative | | `"(a+b*coth(x)^2)^(3/2)*tanh(x)^2".Integrate("x")` | left unevaluated | the antiderivative | +### A rational function of the hyperbolic functions odd in the sine or the cosine is integrated by the other + +`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 for any linear argument, +a written power is handed on as the power it is, and a root of a polynomial in the two even +in the function is admitted. Rubi's 6.1.7, 6.2.7, 6.3.7, 6.5.7 and 6.6.3 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"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` | +| `"cosh(c+d*x)/(a+b*tanh(c+d*x)^2)".Integrate("x")` | left unevaluated | the antiderivative, in `sinh(c + d x)` | +| `"sech(c+d*x)/(a+b*sinh(c+d*x)^2)^(3/2)".Integrate("x")` | left unevaluated | the antiderivative | +| `"csch(x)^5/(a+b*cosh(x)^2)".Integrate("x")` | left unevaluated | the antiderivative | +| `"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 | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 45d9b3973..e472dfec9 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -13946,37 +13946,262 @@ private static int Lcm(int a, int b) return sign == 1 ? answer : -answer; } return null; + } + + /// 1 for even in , -1 for odd, 0 for neither; a zero polynomial is both. + private static int ParityIn(Entity polynomial, Entity.Variable f) + { + if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read)) + return 0; + var evenPowers = read.Keys.Any(k => k.IsEven); + var oddPowers = read.Keys.Any(k => !k.IsEven); + return evenPowers && oddPowers ? 0 : oddPowers ? -1 : 1; + } - // 1 for even in f, -1 for odd, 0 for neither; a zero polynomial is both. - static int ParityIn(Entity polynomial, Entity.Variable f) + /// + /// The polynomial with every f^2 written as -- 1 - u^2 + /// for a sine or cosine beside the other as u, u^2 - 1 for a hyperbolic sine + /// beside the cosine, u^2 + 1 the other way -- and the other function as u, + /// one f divided out first where asked. + /// + private static Entity? InU(Entity polynomial, Entity.Variable f, Entity.Variable other, Entity.Variable u, Entity fSquared, bool dividedByOdd) + { + if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read)) + return null; + Entity sum = Number.Integer.Zero; + foreach (var pair in read) { - if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read)) - return 0; - var evenPowers = read.Keys.Any(k => k.IsEven); - var oddPowers = read.Keys.Any(k => !k.IsEven); - return evenPowers && oddPowers ? 0 : oddPowers ? -1 : 1; + if (pair.Key.Sign < 0 || !pair.Key.CanFitInInt32()) + return null; + var power = pair.Key.ToInt32Unchecked() - (dividedByOdd ? 1 : 0); + if (power < 0 || power % 2 != 0) + return null; + var half = power / 2; + var term = pair.Value.Substitute(other, u) * (half == 0 ? Number.Integer.One : half == 1 ? fSquared : MathS.Pow(fSquared, half)); + sum = sum == Number.Integer.Zero ? term : sum + term; } + return sum; + } - // The polynomial with every f^2 written as 1 - u^2 and the other function as u, - // one f divided out first where asked. - static Entity? InU(Entity polynomial, Entity.Variable f, Entity.Variable other, Entity.Variable u, Entity oneMinusUSquared, bool dividedByOdd) + /// + /// Bioche's first two rules for the hyperbolic functions: a rational function of + /// sinh(y) and cosh(y) that is odd in the hyperbolic sine is a rational + /// function of u = cosh(y) times sinh(y) dy = du, with sinh^2 as + /// u^2 - 1; one odd in the hyperbolic cosine is one of u = sinh(y) times + /// cosh(y) dy = du, with cosh^2 as u^2 + 1. Radicals of polynomials + /// in the two even in the function are admitted as coefficients, as in + /// . + /// + /// + /// The hyperbolic functions are not nodes: the library writes sinh(y) as + /// (e^y - e^-y)/2 and tanh(y) as (e^(2y) - 1)/(e^(2y) + 1), so the + /// six are read back by their spellings, for the argument each exponential names, and + /// nothing else in x may remain. Rubi's sinh(x)^3/(a + b cosh(x)^2) is + /// (u^2 - 1)/(a + b u^2) here, where under u = e^x it is a rational + /// function of a symbolic palindromic quartic and under u = tanh(x/2) of one of + /// degree eight, and both were searches past the budget; cosh(x)/(a + b tanh(x)^2), + /// sech(x)/(a + b sinh(x)^2)^(3/2) and csch(x)^5/(a + b cosh(x)^2) likewise. + /// In front of the substitution search, which with a symbolic coefficient spends the + /// budget on u = e^x. Exact: cosh is positive, so u = sinh(y) is a + /// bijection of the line, and u = cosh(y) one on each side of zero. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByBiochesOddHyperbolicSubstitution(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!Integration.AnsweringTheQuestionAskedOrOneBelow) + return null; + if (!TryGatherExponentialsOfOneLinearForm(expr, x, unitX: true, out var exponentials, out var y, out _)) + return null; + var s = Variable.CreateUnique(expr, "s_hyp"); + var c = Variable.CreateUnique(expr, "c_hyp"); + // The six read by their spellings, each exponential by the multiple of y it is: + // sinh(y) is (e^y - e^-y)/2, cosh(y) (e^y + e^-y)/2, tanh(y) (e^(2y) - 1)/(e^(2y) + 1), + // coth(y) the reciprocal, sech(y) 1/cosh(y) and csch(y) 1/sinh(y). The argument is y, + // or its half where `tanh(c + d x)` alone is written with e^(2(c + d x)) and the + // gathering names that y: then the tangent's spelling carries e^y. + Entity? argument = null; + Entity overTheTwo = expr; + foreach (var halved in new[] { false, true }) { - if (!TreeAnalyzer.TryGetPolynomial(polynomial, f, out var read)) + var one = halved ? null : (EInteger?)EInteger.One; + var two = halved ? EInteger.One : EInteger.FromInt32(2); + // With y named from an exponential of the other sign, e^-y is the k = 1 one: + // sinh and tanh are odd, cosh even. + Entity? ReadTheTwo(Entity node) + { + switch (node) + { + 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()): + return s; + 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): + return -s; + 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)): + return c; + 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): + return s / c; + 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()): + return -s / c; + 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): + return c / s; + 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()): + return -c / s; + case Divf(Number.Integer { EInteger: var m }, var inner) when m.Equals(EInteger.One) && ReadTheTwo(inner) is { } read: + return 1 / read; + } return null; - Entity sum = Number.Integer.Zero; - foreach (var pair in read) + } + bool Is(Entity node, EInteger? k) => k is { } wanted && exponentials.TryGetValue(node, out var found) && found.Equals(wanted); + var read = expr.Replace(node => node.ContainsNode(x) && ReadTheTwo(node) is { } inTheTwo ? inTheTwo : node); + if (read.ContainsNode(s) || read.ContainsNode(c)) { - if (pair.Key.Sign < 0 || !pair.Key.CanFitInInt32()) - return null; - var power = pair.Key.ToInt32Unchecked() - (dividedByOdd ? 1 : 0); - if (power < 0 || power % 2 != 0) + argument = halved ? (y / 2).InnerSimplified : y; + overTheTwo = read; + break; + } + } + if (argument is null) + return null; + if (!overTheTwo.ContainsNode(s) && !overTheTwo.ContainsNode(c)) + return null; + var radicals = new List<(Variable Symbol, Entity Base, Number.Rational Exponent)>(); + overTheTwo = overTheTwo.Replace(node => + { + if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(s) && !radicand.ContainsNode(c)) + return node; + var symbol = Variable.CreateUnique(expr, "r_hyp" + radicals.Count); + var combined = Functions.SingleQuotient.Combine(radicand); + radicals.Add((symbol, combined, exponent)); + return symbol; + }); + if (overTheTwo.ContainsNode(x)) + return null; + // A radicand that is a polynomial in the two, and not a Laurent one: `a + b sech(x)` + // is `a + b/c`, whose root under u = cosh(x) is a root of `a + b/u`, and the search + // that follows it is longer than the exponential substitution's. + if (radicals.Any(radical => radical.Base.ContainsNode(x) || !IsAPolynomialInBoth(radical.Base))) + return null; + // The quotient factor by factor, each factor a polynomial in the two, so that a + // written power -- `(a + b sech(x)^2)^2` -- is handed on as the power it is and not + // expanded into a symbolic quartic for the rational integrator to factor again. + var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(overTheTwo)); + var factors = new List<(Entity Base, int Power)>(); + foreach (var (side, sign) in new[] { (above, 1), (below, -1) }) + foreach (var factor in Mulf.LinearChildren(side)) + { + var (factorBase, power) = factor is Powf(var inner, Number.Integer whole) && whole.EInteger.CanFitInInt32() ? (inner, whole.EInteger.ToInt32Unchecked()) : (factor, 1); + factorBase = factorBase.Expand(); + if (!IsAPolynomialInBoth(factorBase)) return null; - var half = power / 2; - var term = pair.Value.Substitute(other, u) * (half == 0 ? Number.Integer.One : half == 1 ? oneMinusUSquared : MathS.Pow(oneMinusUSquared, half)); - sum = sum == Number.Integer.Zero ? term : sum + term; + factors.Add((factorBase, sign * power)); } - return sum; + bool IsAPolynomialInBoth(Entity polynomial) + => TreeAnalyzer.TryGetPolynomial(polynomial, s, out var inS) && inS.Keys.All(k => k.Sign >= 0) + && TreeAnalyzer.TryGetPolynomial(polynomial, c, out var inC) && inC.Keys.All(k => k.Sign >= 0); + + var u = Variable.CreateUnique(expr, "u_hyp"); + foreach (var (odd, even, fSquared, back) in new[] + { + (s, c, MathS.Sqr(u) - 1, MathS.Hyperbolic.Cosh(argument)), + (c, s, MathS.Sqr(u) + 1, MathS.Hyperbolic.Sinh(argument)), + }) + { + if (radicals.Any(radical => ParityIn(radical.Base, odd) != 1)) + continue; + // An odd factor is f times an even one, and the powers of f are counted, one + // of them being dx's. A factor neither even nor odd in f -- `1 + sinh(x)` -- + // sends the two sides expanded and cleared against the mirror, as + // SolveByBiochesOddSubstitution does, since its mirror is in u only beside it. + Entity numerator = Number.Integer.One; + Entity denominator = Number.Integer.One; + var powerOfF = -1; + var readable = true; + if (factors.All(factor => ParityIn(factor.Base, odd) != 0)) + { + foreach (var (factorBase, power) in factors) + { + var parity = ParityIn(factorBase, odd); + if (InU(factorBase, odd, even, u, fSquared, dividedByOdd: parity == -1) is not { } inU) + { + readable = false; + break; + } + if (parity == -1) + powerOfF += power; + var powered = MathS.Pow(inU, Number.Integer.Create(System.Math.Abs(power))); + if (power > 0) numerator *= powered; else denominator *= powered; + } + if (!readable || powerOfF % 2 != 0) + continue; + var half = powerOfF / 2; + if (half > 0) + numerator *= MathS.Pow(fSquared, Number.Integer.Create(half)); + else if (half < 0) + denominator *= MathS.Pow(fSquared, Number.Integer.Create(-half)); + } + else + { + var aboveExpanded = above.Expand(); + var belowExpanded = below.Expand(); + var parityOfBelow = ParityIn(belowExpanded, odd); + Entity top; + Entity bottom; + bool oneFOutOfTheNumerator; + if (parityOfBelow == 1) + { + top = aboveExpanded; + bottom = belowExpanded; + oneFOutOfTheNumerator = true; + } + else if (parityOfBelow == -1) + { + top = aboveExpanded; + bottom = (belowExpanded * odd).Expand(); + oneFOutOfTheNumerator = false; + } + else + { + var mirrored = belowExpanded.Substitute(odd, -odd).Expand(); + top = (aboveExpanded * mirrored).Expand(); + bottom = (belowExpanded * mirrored).Expand(); + oneFOutOfTheNumerator = true; + } + if (ParityIn(top, odd) != (oneFOutOfTheNumerator ? -1 : 1) || ParityIn(bottom, odd) != 1) + continue; + if (InU(top, odd, even, u, fSquared, oneFOutOfTheNumerator) is not { } topInU + || InU(bottom, odd, even, u, fSquared, dividedByOdd: false) is not { } bottomInU) + continue; + numerator = topInU; + denominator = bottomInU; + } + var radicalsInU = new List<(Variable Symbol, Entity InU)>(); + foreach (var (symbol, radicand, exponent) in radicals) + { + if (InU(radicand, odd, even, u, fSquared, dividedByOdd: false) is not { } radicandInU) + break; + radicalsInU.Add((symbol, MathS.Pow(radicandInU, exponent))); + } + if (radicalsInU.Count != radicals.Count) + continue; + foreach (var (symbol, radical) in radicalsInU) + { + numerator = numerator.Substitute(symbol, radical); + denominator = denominator.Substitute(symbol, radical); + } + var integrand = Functions.PartialFractions.Bare((numerator / denominator).InnerSimplified); + if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result + || result.Nodes.Any(node => node == MathS.NaN)) + continue; + var answer = result.Substitute(u, back); + // dx is dy/d for the argument y = c + d x. + if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) || TreeAnalyzer.IsZero(slope)) + return null; + var scaled = slope == Number.Integer.One ? answer : answer / slope; + // Checked where a symbol is involved, as every answer assembled from pieces is. + if (expr.Vars.Any(symbol => symbol != x) && !Functions.PartialFractions.DerivativeHoldsAtSampledPoints(scaled, expr, x)) + return null; + return scaled; } + return null; } /// diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index aa8d60cd5..c6fbcb26d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -602,6 +602,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // budget. Before the substitution search, which with a symbolic slope spends the // whole of it on `u = c + d x` and never comes back to the chain. if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionOfTheHyperbolicTangent(expr, x, integrateByParts)) is { }) return answer; + // Bioche's first two rules for the hyperbolic functions, read back by their + // exponential spellings: `sinh(x)^3/(a + b cosh(x)^2)` is `(u^2 - 1)/(a + b u^2)` + // under u = cosh(x), where the substitution search spends the budget on u = e^x. + if ((answer = IndefiniteIntegralSolver.SolveByBiochesOddHyperbolicSubstitution(expr, x, integrateByParts)) is { }) return answer; // A rational function of a sine or a cosine over a quadratic in it with a symbol // among the coefficients, split over the quadratic's two roots: under the // half-angle it is a quartic in t with a symbol in every coefficient. diff --git a/Sources/Tests/UnitTests/Calculus/ExponentialSubstitutionIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ExponentialSubstitutionIntegralTest.cs index 4a90ac908..8b92c3944 100644 --- a/Sources/Tests/UnitTests/Calculus/ExponentialSubstitutionIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ExponentialSubstitutionIntegralTest.cs @@ -468,6 +468,29 @@ public void ARationalFunctionOfTheHyperbolicTangent(string integrand) public void ARootOfARationalFunctionOfTheHyperbolicCotangent(string integrand) => DifferentiatesBackWithParametersPinned(integrand, new[] { -0.5, 0.23, 0.61, 1.05, 1.7 }); + /// + /// Bioche's first two rules for the hyperbolic functions: 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 are read back by their exponential + /// spellings, for any linear argument, a written power handed on as the power it is, + /// and a root of a polynomial in the two even in the function admitted. Rubi's 6.1.7, + /// 6.2.7, 6.3.7, 6.5.7 and 6.6.3, each a search past the budget under u = e^x. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + [Theory] + [InlineData("sinh(x)^3/(a + b*cosh(x)^2)")] + [InlineData("sinh(-x)^3/(a + b*cosh(x)^2)")] + [InlineData("cosh(c + d*x)/(a + b*tanh(c + d*x)^2)")] + [InlineData("sinh(c + d*x)^3/(a + b*tanh(c + d*x)^2)")] + [InlineData("sech(c + d*x)^3*(a + b*tanh(c + d*x)^2)^2")] + [InlineData("sinh(c + d*x)^3/(a + b*sech(c + d*x)^2)^2")] + [InlineData("csch(x)^5/(a + b*cosh(x)^2)")] + [InlineData("csch(c + d*x)/(a + b*sech(c + d*x)^2)^3")] + [InlineData("sech(c + d*x)/(a + b*sinh(c + d*x)^2)^(3/2)")] + [InlineData("cosh(x)^3/(2 + sinh(x))")] + public void ARationalFunctionOddInTheHyperbolicSineOrCosine(string integrand) + => DifferentiatesBackWithParametersPinned(integrand, new[] { -0.5, 0.23, 0.61, 1.05, 1.7 }); + /// /// A cubed symbolic quadratic beside a quadratic with real roots was integrated to a /// wrong answer -- the Hermite ansatz's logarithmic part, with coefficients that are