Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14477,6 +14477,141 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x)
return assumed == Entity.Boolean.True ? answer : answer.Provided(assumed);
}

/// <summary>
/// A power of a square written out, <c>(A + B w + C w^2)^p</c> with <c>B^2 = 4 A C</c> and
/// <c>w = x^k</c>, as the power of its root, <c>L = w + B/(2 C)</c>: the integral is
/// <c>F</c> times the integral with <c>L^(2p)</c> in its place, where
/// <c>F = (A + B w + C w^2)^p / L^(2p)</c>.
/// </summary>
/// <remarks>
/// <para>
/// The square is <c>C L^2</c>, and <c>F</c> is constant on every interval where <c>L</c> is
/// not zero: its derivative is <c>2 p w' L^(2p-1) (C L^2 Q^(p-1) - Q^p) / L^(4p)</c>, and
/// <c>Q^(p-1) Q = Q^p</c> wherever <c>Q</c> is not zero. So <c>F</c> goes in front of the
/// integral, whatever <c>p</c> is: a symbol, <c>3/4</c>, or half an odd number with a
/// leading coefficient of unknown sign. For a whole <c>p</c> it is <c>C^p</c>. The square
/// is a factor of the integrand or nothing is read: below the bar <c>1/F</c> comes out,
/// and out of a sum nothing does.
/// </para>
/// <para>
/// After <see cref="SolveByTakingARootOfAPerfectSquare"/>, 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 <c>k</c> as anything but a whole number. This reads <c>k</c> as any exponent, a
/// fraction or a symbol: Rubi's 1.2.3.2 <c>x^2 (a^2 + 2 a b x^3 + b^2 x^6)^p</c>,
/// <c>x sqrt(a^2 + 2 a b x^n + b^2 x^(2n))</c> and <c>(a^2 + 2 a b x^2 + b^2 x^4)^(3/4)</c>
/// 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
/// </para>
/// </remarks>
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;
}
}
}

/// <summary>
/// <paramref name="sum"/> as a polynomial in <c>w = x^k</c> for a rational <c>k</c> that is
/// not whole, each of its terms a constant times a power of <paramref name="x"/> that is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// A power of a multiple of a quadratic's derivative beside a power of the quadratic is
// a binomial in the derivative: `(b d + 2 c d x)^m (a + b x + c x^2)^p`.
if ((answer = IndefiniteIntegralSolver.SolveByTheDerivativeOfAQuadraticAsTheVariable(expr, x, integrateByParts)) is { }) return answer;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A power of a square written out, <c>(A + B w + C w^2)^p</c> with <c>B^2 = 4 A C</c> and
/// <c>w</c> a power of x, integrated as the power of its root <c>L = w + B/(2 C)</c> times the
/// factor <c>(A + B w + C w^2)^p / L^(2p)</c>, which is constant wherever <c>L</c> is not zero.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Differentiated back with <c>a</c> negative, so that <c>L</c> changes sign among the points
/// and the factor is checked on both sides of its zero, where it is not the same constant.
/// </remarks>
[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}");
}

/// <summary>
/// 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.
/// </summary>
[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));

/// <summary>
/// A square in a power of x that is not a whole number: a fraction, or a symbol, Rubi's
/// <c>x sqrt(a^2 + 2 a b x^n + b^2 x^(2n))</c>.
/// </summary>
[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));

/// <summary>
/// The square below the bar, where it is <c>1/F</c> that comes out of the integral.
/// </summary>
[Theory]
[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")]
public void TheSquareBelowTheBar(string integrand)
=> DifferentiatesBack(integrand, ("a", -2.5), ("b", 1), ("p", 0.35), ("n", 1.5));
}
}
Loading