diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 176583a6a..823354acb 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1091,6 +1091,21 @@ but `sqrt(q)^2 = q` ([#1598](https://github.com/asc-community/AngouriMath/issues | `"1/sqrt(-x^2 + i)".Integrate("x")` | `-arcsin(-2x/sqrt(4i)) + C` | `ln(-2x + 2i sqrt(-x^2 + i))/i + C`, an antiderivative as before, in the logarithm | | `"1/(sqrt(x)*sqrt(1 + i*x))".Integrate("x")`, and Rubi 4.3.2.1's `sqrt(tan(c + d x))/(a + i a tan(c + d x))^(5/2)` | `integral(...)` | the antiderivative | +### A power of a constant times a whole power of a polynomial is integrated as that power + +**Answers where there were none.** `(c S^k)^p`, a constant times a whole power past the first of a +polynomial in `x` under a power that is not whole, was declined where it was not a function of a +linear: `x^2 (c (a + b x^2)^2)^(3/2)` is a polynomial times a factor that is constant wherever +`a + b x^2` is not zero. It is integrated as `K S^(k p)` now, with `K = c^q (c S^k)^r/S^(k r)` for the +whole part `q` and the rest `r` of `p`, where the substitution search, asked first, declines it. Rubi's +1.3.2 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2*(c*(a + b*x^2)^2)^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | `c (c (a + b x^2)^2)^(1/2)/(a + b x^2)` times `a^3 x^3/3 + 3 a^2 b x^5/5 + 3 a b^2 x^7/7 + b^3 x^9/9` | +| `"(c*(a + b*x^2)^3)^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | `c (c (a + b x^2)^3)^(1/2)/(a + b x^2)^(3/2)` times an antiderivative of `(a + b x^2)^(9/2)` | + ### A quotient by the square of a base is integrated by parts against the base's reciprocal **Answers where there were none.** `x^2/(a x cos(a x) - sin(a x))^2` was declined, with the rest of diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 7daa2a0e6..f486161f1 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -25177,6 +25177,94 @@ private static bool HasTheSameRatioAtTwoPoints(Entity expr, Entity reference, En return lambda; } + /// + /// A power that is not whole of a constant times a whole power of something of x, + /// (c S^k)^p for a whole k past 1, as K S^(k p), with + /// K = c^q (c S^k)^r/S^(k r) for the whole part q of p and the rest + /// r: Rubi's 1.3.2 x^m (c (a + b x^2)^2)^(3/2) is K x^m (a + b x^2)^3, + /// a polynomial, and was declined. + /// + /// + /// K is constant on every interval where S is not zero: its logarithmic + /// derivative is r (k S'/S - k S'/S). So it goes in front of the integral, and over it + /// for a factor below the bar, as the factor of + /// does. A whole power of the + /// constant times the power distributes exactly, which is why only the rest of p is + /// in K. Only factors of the integrand, where K is a factor of the whole; only + /// something of x by arithmetic, since a power of a function is the rules' for that function, + /// which write its sign; and at the top only, where the answer is the caller's. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByTakingAWholePowerOutOfAPowerOfAProduct(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!Integration.AnsweringTheQuestionAsked) + return null; + var (above, below) = Functions.SingleQuotient.Of(expr); + Entity constant = Number.Integer.One; + var changed = false; + var top = Rewritten(above, Number.Integer.One); + var bottom = Rewritten(below, Number.Integer.MinusOne); + if (!changed) + return null; + var question = (bottom == Number.Integer.One ? top : top / bottom).InnerSimplified; + if (Integration.ComputeIndefiniteIntegral(question, x, integrateByParts) is not { } integral) + return null; + var answer = constant * integral; + return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer; + + Entity Rewritten(Entity side, Number.Integer sign) + { + Entity product = Number.Integer.One; + foreach (var factor in Mulf.LinearChildren(side)) + { + var written = factor; + if (factor is Powf(var @base, var power) && power is not Number.Integer && !power.ContainsNode(x) + && @base is Mulf or Divf && ConstantTimesAWholePower(@base, x) is var (c, inner, k)) + { + written = MathS.Pow(inner, (k * power).InnerSimplified); + var signed = (sign * power).InnerSimplified; + Entity whole = Number.Integer.Zero; + var fraction = signed; + if (signed is Number.Rational rational) + { + whole = Number.Integer.Create(rational.ERational.Numerator.Divide(rational.ERational.Denominator)); + fraction = (signed - whole).InnerSimplified; + } + var turned = MathS.Pow(@base, fraction) / MathS.Pow(inner, (k * fraction).InnerSimplified); + var turn = whole == Number.Integer.Zero || c == Number.Integer.One ? turned : MathS.Pow(c, whole) * turned; + constant = constant == Number.Integer.One ? turn : constant * turn; + changed = true; + } + product = product == Number.Integer.One ? written : product * written; + } + return product; + } + + // A product of constants and one whole power past 1 of something of x: the constant, + // the something and the power. + static (Entity Constant, Entity Inner, Number.Integer Power)? ConstantTimesAWholePower(Entity @base, Entity.Variable x) + { + Entity c = Number.Integer.One; + (Entity Inner, Number.Integer Power)? found = null; + foreach (var part in Mulf.LinearChildren(@base)) + { + if (!part.ContainsNode(x)) + { + c = c == Number.Integer.One ? part : c * part; + continue; + } + if (found is not null || part is not Powf(var inner, Number.Integer { EInteger: var k } whole) || k.CompareTo(2) < 0) + return null; + // Of x by arithmetic alone: a power of a function of x is the rules' for that + // function, which write its sign. + if (inner.Nodes.Any(node => node.ContainsNode(x) && node is not (Sumf or Minusf or Mulf or Divf or Powf or Variable))) + return null; + found = (inner, whole); + } + return found is var (i, w) ? (c, i, w) : null; + } + } + /// /// A fractional or symbolic power of a monomial, distributed: (c x^n)^b is /// c^b x^(n b) for a positive c and a n that is not whole. diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 3e9474a31..aa9cee8f4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -909,6 +909,12 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // substitution search, which reads each root on its own. if ((answer = IndefiniteIntegralSolver.SolveBySubstitutingTheSumOfTwoRootsOfLinears(expr, x, integrateByParts)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer; + // A constant times a whole power of something of x under a power that is not whole, the + // whole power taken out with the factor constant where it is not zero: + // `x^2 (c (a + b x^2)^2)^(3/2)` is that factor times a polynomial. After the substitution + // search, which answers a power of a linear and an odd power of x beside a function of + // x^2 more shortly: `1/(c (a + b x)^2)^(5/2)` in 64 characters, where this writes 98. + if ((answer = IndefiniteIntegralSolver.SolveByTakingAWholePowerOutOfAPowerOfAProduct(expr, x, integrateByParts)) is { }) return answer; // A logarithmic derivative the substitution above could not read for a symbol in // an exponent: `(x^(n-1) - 1)/(x^n - n x)`. if ((answer = IndefiniteIntegralSolver.SolveALogarithmicDerivativeWithASymbolicExponent(expr, x)) is { }) return answer; diff --git a/Sources/Tests/UnitTests/Calculus/WholePowerOutOfAPowerOfAProductIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/WholePowerOutOfAPowerOfAProductIntegralTest.cs new file mode 100644 index 000000000..e60d6ac2a --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/WholePowerOutOfAPowerOfAProductIntegralTest.cs @@ -0,0 +1,59 @@ +// +// 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 +{ + /// + /// A power that is not whole of a constant times a whole power of something of x, + /// (c S^k)^p, as K S^(k p) with K constant where S is not zero. + /// Rubi's 1.3.2:338-352, which the substitution search, asked first, declines. + /// #718 + /// + /// + /// Checked by differentiating back with a = -1.3, b = 0.7 and c = 0.4, on + /// both sides of 0 and of the roots of a + b x^2, where K changes sign, wherever + /// the integrand is real. + /// + [Trait("Area", "Calculus")] + public sealed class WholePowerOutOfAPowerOfAProductIntegralTest + { + private static readonly double[] Points = { -2.3, -1.9, -0.8, -0.3, 0.4, 0.9, 1.9, 2.3 }; + + [Theory] + [InlineData("x^2*(c*(a + b*x^2)^2)^(3/2)")] + [InlineData("x^4*(c*(a + b*x^2)^2)^(3/2)")] + [InlineData("(c*(a + b*x^2)^2)^(3/2)")] + [InlineData("(c*(a + b*x^2)^2)^(3/2)/x^2")] + [InlineData("(c*(a + b*x^2)^3)^(3/2)")] + [InlineData("(c*(a + b*x^2)^3)^(3/2)/x^2")] + public void IsIntegrated(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Entity Pinned(Entity e) => e.Substitute("a", -1.3).Substitute("b", 0.7).Substitute("c", 0.4); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + var compared = 0; + foreach (var at in Points) + { + var want = original.Substitute("x", at).EvalNumerical(); + if (want.IsNaN || Math.Abs((double)want.ImaginaryPart) > 1e-12 * Math.Max(1, Math.Abs((double)want.RealPart))) + continue; + var got = derivative.Substitute("x", at).EvalNumerical(); + compared++; + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 4, $"only {compared} points were real for {integrand}"); + } + } +}