From 1c1b3288066b8a3800e6f256c1fde025197f63c7 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 14:15:28 +0000 Subject: [PATCH 1/2] Any power of a square written out is the power of its root, in any power of x A square written out, A + B w + C w^2 with B^2 = 4 A C, was read as the modulus of its root only under half an odd power and in a whole power of x. A symbolic power, (a^2 + 2 a b x + b^2 x^2)^p, a power such as 3/4, and the square in x^n or x^(1/3) were declined. The power of the square is now the power of its root L = w + B/(2 C) times F = (A + B w + C w^2)^p / L^(2p), which is constant wherever L is not zero and comes out of the integral: F above the bar, 1/F below it, and nothing where the square is not a factor of the integrand. After the rule for half an odd power, which keeps its answers, and at the top only. On Rubi's 1.2.3.2, 179 fair problems whose trinomial is a square, 156 are answered where master answers 103, none wrongly. Rubi's 1.2.1.2, 1.2.2.2 and 1.2.3.2. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 18 +++ .../Integration/IndefiniteIntegralSolver.cs | 135 ++++++++++++++++++ .../Integration/Integration.Definition.cs | 3 + .../PowerOfAPerfectSquareIntegralTest.cs | 93 ++++++++++++ 4 files changed, 249 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 8564b93e5..197620a4c 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -2192,6 +2192,24 @@ here that writes one does: left inside, a rule below differentiated it, and | `"sqrt(a^2+2*a*b*x+b^2*x^2)*sqrt(c+pe*x+d*x^2)".Integrate("x")` | left unevaluated | the antiderivative | | `"(a^2+2*a*b*x+b^2*x^2)^(5/2)".Integrate("x")` | the antiderivative, through the substitution search | `sgn(a + b x) (a + b x)^6 |b|^5/(6 b)` up to the form | +### Any power of a square written out is the power of its root, in any power of `x` + +**Answers where there were none.** A square written out, `A + B w + C w^2` with `B^2 = 4 A C`, +was read as the modulus of its root only under half an odd power, and only in a whole power of +`x`. A symbolic power, `(a^2 + 2 a b x + b^2 x^2)^p`, or a power such as `3/4`, was declined, and +so was the square in `x^n` or in `x^(1/3)`. Now the power of the square is the power of its root +`L = w + B/(2 C)` times `(A + B w + C w^2)^p / L^(2p)`. That factor is constant wherever `L` is +not zero, and it goes in front of the integral. These are Rubi's 1.2.1.2, 1.2.2.2 and 1.2.3.2 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2*(a^2 + 2*a*b*x^3 + b^2*x^6)^p".Integrate("x")` | left unevaluated | `(a^2 + 2 a b x^3 + b^2 x^6)^p (x^3 + a/b)/(3 (2 p + 1))`, as that factor times a power of `x^3 + a/b` | +| `"(a + b*x)*(d + e*x)^3*(a^2 + 2*a*b*x + b^2*x^2)^p".Integrate("x")` | left unevaluated | the factor times powers of `x + a/b` | +| `"(a^2 + 2*a*b*x^2 + b^2*x^4)^(3/4)".Integrate("x")` | left unevaluated | the factor times the antiderivative of `(x^2 + a/b)^(3/2)` | +| `"x*sqrt(a^2 + 2*a*b*x^n + b^2*x^(2*n))".Integrate("x")` | left unevaluated | `sqrt(a^2 + 2 a b x^n + b^2 x^(2n))/(x^n + a/b)` times `a x^2/(2 b) + x^(n + 2)/(n + 2)` | +| `"x*(a^2 + 2*a*b*x^(1/3) + b^2*x^(2/3))^p".Integrate("x")` | left unevaluated | the factor times powers of `x^(1/3) + a/b` | + ### The root of a quadratic that is a square is the modulus of a linear, and was read as any quadratic **Wrong answers, silent.** The table's entry for `k/sqrt(Q)` and `sqrt(Q)` reads a quadratic by the diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index a801299a2..85ddd49ca 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -13844,6 +13844,141 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x) return assumed == Entity.Boolean.True ? answer : answer.Provided(assumed); } + /// + /// A power of a square written out, (A + B w + C w^2)^p with B^2 = 4 A C and + /// w = x^k, as the power of its root, L = w + B/(2 C): the integral is + /// F times the integral with L^(2p) in its place, where + /// F = (A + B w + C w^2)^p / L^(2p). + /// + /// + /// + /// The square is C L^2, and F is constant on every interval where L is + /// not zero: its derivative is 2 p w' L^(2p-1) (C L^2 Q^(p-1) - Q^p) / L^(4p), and + /// Q^(p-1) Q = Q^p wherever Q is not zero. So F goes in front of the + /// integral, whatever p is: a symbol, 3/4, or half an odd number with a + /// leading coefficient of unknown sign. For a whole p it is C^p. The square + /// is a factor of the integrand or nothing is read: below the bar 1/F comes out, + /// and out of a sum nothing does. + /// + /// + /// After , which answers half an odd power + /// of a square in a whole power of x with the sign written out, and before which nothing + /// read k as anything but a whole number. This reads k as any exponent, a + /// fraction or a symbol: Rubi's 1.2.3.2 x^2 (a^2 + 2 a b x^3 + b^2 x^6)^p, + /// x sqrt(a^2 + 2 a b x^n + b^2 x^(2n)) and (a^2 + 2 a b x^2 + b^2 x^4)^(3/4) + /// were declined. At the top only, where the answer is the caller's, as for the sign the + /// rule above writes. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + /// + internal static Entity? SolveByWritingAPowerOfASquareAsAPowerOfItsRoot(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!Integration.AnsweringTheQuestionAsked) + return null; + // The square as a factor of the integrand, above the bar or below it, and nowhere else: + // the factor comes out of a product and not out of a sum, and below the bar it is its + // reciprocal that comes out. + var (above, below) = Functions.SingleQuotient.Of(expr); + (Entity Square, Entity Exponent, Entity Root, Entity Leading, bool Below)? found = null; + foreach (var (side, isBelow) in new[] { (above, false), (below, true) }) + foreach (var factor in Mulf.LinearChildren(side)) + { + if (factor is not Powf(var radicand, var power) || !radicand.ContainsNode(x) || power.ContainsNode(x) + || radicand is not (Sumf or Minusf) || radicand.Complexity > 60 + || TrinomialInAPowerOfX(radicand, x) is not var (a, b, c, w, k)) + continue; + var whole = power is Number.Integer; + var halfOdd = power is Number.Rational half && half.ERational.Denominator.Equals(EInteger.FromInt32(2)); + var kIsWhole = k is Number.Integer { EInteger.Sign: > 0 }; + // What the rule above and the rational rules answer: half an odd power, or a + // whole one, of a square in a whole power of x. + if (kIsWhole && (whole || halfOdd)) + continue; + if (!VanishesIdentically(b * b - 4 * a * c)) + continue; + // Two squares would want two factors; one is what Rubi writes. + if (found is { }) + return null; + var shift = Functions.PartialFractions.InLowestTermsOverTheSymbols(b / (2 * c)); + found = (radicand, power, shift == Number.Integer.Zero ? w : w + shift, c, isBelow); + } + if (found is not var (square, exponent, root, leading, squareIsBelow) + || above.Nodes.Concat(below.Nodes).Count(node => node == square) != 1) + return null; + var twice = (2 * exponent).InnerSimplified; + // The root's power above the bar either way, with a negative exponent for the square + // below it: `x (x + h)^(-2p)` is read below the top where `x/(x + h)^(2p)` was not. + Entity Without(Entity side) + { + Entity product = Number.Integer.One; + foreach (var factor in Mulf.LinearChildren(side)) + if (!(factor is Powf(var radicand, var power) && radicand == square && power == exponent)) + product = product == Number.Integer.One ? factor : product * factor; + return product; + } + var ofTheRoot = MathS.Pow(root, squareIsBelow ? (-twice).InnerSimplified : twice); + var written = squareIsBelow + ? ofTheRoot * above / Without(below) + : ofTheRoot * Without(above) / below; + if (Integration.ComputeIndefiniteIntegral(written, x, integrateByParts) is not { } integral) + return null; + // F: the leading coefficient's power where p is whole, and the quotient otherwise. + var factorOfTheSquare = exponent is Number.Integer + ? MathS.Pow(leading, exponent) + : MathS.Pow(square, exponent) / MathS.Pow(root, twice); + return (squareIsBelow ? integral / factorOfTheSquare : factorOfTheSquare * integral); + + // A + B x^k + C x^(2k), each term a coefficient times a power of x, with the three + // exponents 0, k and 2k. + static (Entity A, Entity B, Entity C, Entity W, Entity K)? TrinomialInAPowerOfX(Entity sum, Entity.Variable x) + { + var terms = new List<(Entity Coefficient, Entity Exponent)>(); + foreach (var term in Sumf.LinearChildren(sum)) + { + if (PowerOfX(term, x) is not { } read) + return null; + terms.Add(read); + } + if (terms.Count != 3) + return null; + var constant = terms.Where(term => term.Exponent == Number.Integer.Zero).ToList(); + if (constant.Count != 1) + return null; + var others = terms.Where(term => term.Exponent != Number.Integer.Zero).ToList(); + foreach (var (first, second) in new[] { (others[0], others[1]), (others[1], others[0]) }) + if (VanishesIdentically(second.Exponent - 2 * first.Exponent) && !VanishesIdentically(first.Exponent)) + { + var k = first.Exponent.InnerSimplified; + return (constant[0].Coefficient, first.Coefficient, second.Coefficient, MathS.Pow(x, k), k); + } + return null; + } + + // A term as a coefficient free of x and an exponent of x, through products and quotients. + static (Entity Coefficient, Entity Exponent)? PowerOfX(Entity term, Entity.Variable x) + { + if (!term.ContainsNode(x)) + return (term, Number.Integer.Zero); + switch (term) + { + case Entity.Variable v when v == x: + return (Number.Integer.One, Number.Integer.One); + case Powf(var @base, var power) when @base == x && !power.ContainsNode(x): + return (Number.Integer.One, power); + case Mulf(var left, var right): + if (PowerOfX(left, x) is not var (lc, le) || PowerOfX(right, x) is not var (rc, re)) + return null; + return (lc * rc, (le + re).InnerSimplified); + case Divf(var above, var below): + if (PowerOfX(above, x) is not var (ac, ae) || PowerOfX(below, x) is not var (bc, be)) + return null; + return (ac / bc, (ae - be).InnerSimplified); + default: + return null; + } + } + } + /// /// A whole power of a product in which the variable stands beside a constant, written /// as the product of the powers: (c x)^2 is c^2 x^2, exact for a whole diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index dec7b78a0..a35fb01cb 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -662,6 +662,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => if ((answer = IndefiniteIntegralSolver.SolveAReciprocalOfAnInverseTrigonometricFunction(expr, x, integrateByParts)) is { }) return answer; // A fractional power of a perfect square is the power of the modulus, sgn(P) P^(2r). if ((answer = IndefiniteIntegralSolver.SolveByTakingARootOfAPerfectSquare(expr, x, integrateByParts)) is { }) return answer; + // And any power of a square in any power of x, as the power of its root times a factor + // constant where the root is not zero: `x^2 (a^2 + 2 a b x^3 + b^2 x^6)^p`. + if ((answer = IndefiniteIntegralSolver.SolveByWritingAPowerOfASquareAsAPowerOfItsRoot(expr, x, integrateByParts)) is { }) return answer; // x^(n - 1) g(x^n) with a symbolic n is g(u)/n under u = x^n. if ((answer = IndefiniteIntegralSolver.SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(expr, x, integrateByParts)) is { }) return answer; // A whole power of a product of a constant and the variable, as the product of diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs new file mode 100644 index 000000000..dfd7fcb6f --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs @@ -0,0 +1,93 @@ +// +// 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 System.Linq; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A power of a square written out, (A + B w + C w^2)^p with B^2 = 4 A C and + /// w a power of x, integrated as the power of its root L = w + B/(2 C) times the + /// factor (A + B w + C w^2)^p / L^(2p), which is constant wherever L is not zero. + /// #718 + /// + /// + /// Differentiated back with a negative, so that L changes sign among the points + /// and the factor is checked on both sides of its zero, where it is not the same constant. + /// + [Trait("Area", "Calculus")] + public sealed class PowerOfAPerfectSquareIntegralTest + { + private static readonly double[] Points = { 0.3, 0.9, 1.7, 2.6 }; + + private static void DifferentiatesBack(string integrand, params (string, double)[] pins) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + + // The integration constant is C_1 where the integrand has a symbol named C. + Entity original = integrand.ToEntity(); + var derivative = (original.Vars.Any(v => v.Name == "C") ? integral : integral.Substitute("C", 0)).Differentiate("x"); + foreach (var (name, value) in pins) + { + derivative = derivative.Substitute(name, value); + original = original.Substitute(name, value); + } + var compared = 0; + foreach (var at in Points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + if (got.IsNaN || want.IsNaN) + continue; + compared++; + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); + Assert.True(difference / scale < 1e-9, + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 3, $"only {compared} points could be compared for {integrand}"); + } + + /// + /// A symbolic power, and a power that is neither whole nor half an odd number: Rubi's + /// 1.2.1.2, 1.2.2.2 and 1.2.3.2, which were declined. + /// + [Theory] + [InlineData("(a^2 + 2*a*b*x + b^2*x^2)^p")] + [InlineData("(a + b*x)*(d + e*x)^3*(a^2 + 2*a*b*x + b^2*x^2)^p")] + [InlineData("x^2*(a^2 + 2*a*b*x^3 + b^2*x^6)^p")] + [InlineData("(a^2 + 2*a*b*x^2 + b^2*x^4)^(3/4)")] + public void APowerThatIsNotHalfAnOddNumber(string integrand) + => DifferentiatesBack(integrand, ("a", -2.5), ("b", 1), ("d", 0.7), ("e", 1.3), ("p", 0.35)); + + /// + /// A square in a power of x that is not a whole number: a fraction, or a symbol, Rubi's + /// x sqrt(a^2 + 2 a b x^n + b^2 x^(2n)). + /// + [Theory] + [InlineData("x*(a^2 + 2*a*b*x^(1/3) + b^2*x^(2/3))^p")] + [InlineData("x*sqrt(a^2 + 2*a*b*x^n + b^2*x^(2*n))")] + public void ASquareInAPowerThatIsNotWhole(string integrand) + => DifferentiatesBack(integrand, ("a", -2.5), ("b", 1), ("p", -1.7), ("n", 1.5)); + + /// + /// The square below the bar, where it is 1/F that comes out of the integral. + /// + [Theory] + [InlineData("1/(a^2 + 2*a*b*x^(1/3) + b^2*x^(2/3))^(3/2)")] + [InlineData("1/(a^2 + b^2/x^(2/3) + 2*a*b/x^(1/3))^(1/2)")] + [InlineData("x^(2*n - 1)/(a^2 + 2*a*b*x^n + b^2*x^(2*n))^(3/2)")] + [InlineData("1/(x*sqrt(a^2 + 2*a*b*x^n + b^2*x^(2*n)))")] + [InlineData("x/(a^2 + 2*a*b*x + b^2*x^2)^p")] + public void TheSquareBelowTheBar(string integrand) + => DifferentiatesBack(integrand, ("a", -2.5), ("b", 1), ("p", 0.35), ("n", 1.5)); + } +} From 397f6f51a6bdd6edac582f0346be0a61637ba692 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 21:43:25 +0000 Subject: [PATCH 2/2] The square below the bar is tested where the root of a square does not answer it A half-odd power of a square in a fractional power of x is the root of a square's, by its modulus, since #1695. The two rows below the bar take a symbolic power and 3/4 instead, which only this rule answers. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs index dfd7fcb6f..dc2d6f4a9 100644 --- a/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/PowerOfAPerfectSquareIntegralTest.cs @@ -82,8 +82,8 @@ public void ASquareInAPowerThatIsNotWhole(string integrand) /// The square below the bar, where it is 1/F that comes out of the integral. /// [Theory] - [InlineData("1/(a^2 + 2*a*b*x^(1/3) + b^2*x^(2/3))^(3/2)")] - [InlineData("1/(a^2 + b^2/x^(2/3) + 2*a*b/x^(1/3))^(1/2)")] + [InlineData("1/(a^2 + 2*a*b*x^(1/3) + b^2*x^(2/3))^p")] + [InlineData("1/(a^2 + b^2/x^(2/3) + 2*a*b/x^(1/3))^(3/4)")] [InlineData("x^(2*n - 1)/(a^2 + 2*a*b*x^n + b^2*x^(2*n))^(3/2)")] [InlineData("1/(x*sqrt(a^2 + 2*a*b*x^n + b^2*x^(2*n)))")] [InlineData("x/(a^2 + 2*a*b*x + b^2*x^2)^p")]