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
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,22 @@ Rubi's `x^m (a + b x^n)^p` and `(a + b x^n)^p (c + d x^n)^q`
| `"1/(2+3/x^2)^3".Integrate("x")`, `1/(a + b/x^3)` | left unevaluated | an antiderivative |
| `"(a+b*x^n)*(c+d*x^n)^3".Integrate("x")` | left unevaluated | written out, eight powers of `x` integrated: `a c^3 x + ... + b d^3 x^(4 n + 1)/(4 n + 1)` |

### The derivative of a power of `x` over a binomial is integrated as an arctangent over one plus a multiple of its square

**Answers where there were none.** `(k - 4 f x^3)/(k^2 + 4 d f x^2 + 4 k f x^3 + 4 f^2 x^6)` is
`w'/(2 f (1 + (d/f) w^2))` for `w = x/(x^3 + k/(2 f))`, and was declined: the partial fractions
over a sextic in `x`, or over a sum of symbolic powers of it, are out of reach, and nothing read the
quotient. A denominator `a + b x^p + c x^(2p) + d x^(2j)` with `b^2 = 4 a c` is read as
`c L^2 + d x^(2j)` for `L = x^p + b/(2c)`, and a numerator that is a constant multiple of
`(x^j/L)' L^2` is integrated as an arctangent of `x^j/L`, or a logarithm, by the sign of `d/c`.
Rubi's 1.3.2 ([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"(k - 4*f*x^3)/(k^2 + 4*d*f*x^2 + 4*k*f*x^3 + 4*f^2*x^6)".ToEntity().Integrate("x")` | `integral(...)` | `arctan(sqrt(d/f) x/(x^3 + k/(2 f)))/(2 sqrt(d f))` for `d/f > 0`, a logarithm for `d/f < 0` |
| `"(k - 2*f*(n - 1)*x^n)/(k^2 + 4*d*f*x^2 + 4*k*f*x^n + 4*f^2*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | the same in `x/(x^n + k/(2 f))` |
| `"x^m*(k*(1 + m) + 2*f*(1 + m - n)*x^n)/(k^2 + 4*d*f*x^(2 + 2*m) + 4*k*f*x^n + 4*f^2*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | the same in `x^(m + 1)/(x^n + k/(2 f))` |

### A half-odd power of a constant over a polynomial of either sign is integrated with its sign

**Answers where there were none.** `sqrt(1/(1 - x^2))`, the arcsine's derivative where it is real,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18913,6 +18913,114 @@ static Entity Shifted(Entity exponent, Entity by, bool subtract)
}
}

/// <summary>
/// The derivative of <c>w = x^k/L</c>, for a binomial <c>L = x^p + l</c>, over a multiple of
/// <c>L^2</c> plus a power of x, <c>c L^2 + d x^(2k)</c>: the integrand is
/// <c>(m/c) w'/(1 + (d/c) w^2)</c>, an arctangent of <c>w</c> or a hyperbolic one. Rubi's
/// 1.3.2 <c>(e - 4 f x^3)/(e^2 + 4 d f x^2 + 4 e f x^3 + 4 f^2 x^6)</c> is
/// <c>arctan(2 sqrt(d f) x/(e + 2 f x^3))/(2 sqrt(d f))</c>, and its rows in symbolic
/// powers of x are the same in <c>w = x^(m + 1)/(e + 2 f x^n)</c>.
/// </summary>
/// <remarks>
/// The denominator is read as four terms, each a constant times a power of x: one is
/// <c>d x^(2k)</c>, and the other three are <c>a + b x^p + c x^(2p)</c> with
/// <c>b^2 = 4 a c</c>, which is <c>c L^2</c> for <c>L = x^p + b/(2c)</c>. The numerator is
/// a constant multiple of <c>k x^(k - 1) L - p x^(k + p - 1)</c>, which is <c>w' L^2</c>;
/// the constant is read at <c>x = 1</c>, and the answer is differentiated back. Past a
/// quartic these were declined: partial fractions over a sextic in x, or over a sum of
/// symbolic powers of it, are out of reach, and nothing read the quotient.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </remarks>
internal static Entity? SolveAsTheArctangentOfAPowerOfXOverABinomial(Entity expr, Entity.Variable x, bool integrateByParts)
{
var (above, below) = Functions.SingleQuotient.Of(expr);
if (!below.ContainsNode(x))
return null;
var terms = Sumf.LinearChildren(below);
if (terms.Count != 4)
return null;
var read = new (Entity Coefficient, Entity Exponent)[4];
for (var i = 0; i < 4; i++)
if (ConstantTimesAPowerOfX(terms[i], x) is { } term)
read[i] = term;
else
return null;
for (var i = 0; i < 4; i++)
{
// The i-th as d x^(2k), and the other three as a + b x^p + c x^(2p).
Entity? a = null;
var powers = new List<(Entity Coefficient, Entity Exponent)>();
for (var j = 0; j < 4; j++)
if (j != i)
{
if (Functions.PartialFractions.IsZeroAsAValue(read[j].Exponent))
a = a is null ? read[j].Coefficient : null;
else
powers.Add(read[j]);
}
if (a is null || powers.Count != 2)
continue;
if (!Functions.PartialFractions.IsZeroAsAValue(powers[1].Exponent - 2 * powers[0].Exponent))
powers.Reverse();
var ((b, p), (c, twiceP)) = (powers[0], powers[1]);
if (!Functions.PartialFractions.IsZeroAsAValue(twiceP - 2 * p)
|| !Functions.PartialFractions.IsZeroAsAValue(b * b - 4 * a * c))
continue;
var (d, twiceK) = read[i];
var k = Exponent(twiceK / 2);
var binomial = MathS.Pow(x, p) + Exponent(b / (2 * c));
// w' L^2, and the constant the numerator is of it.
var derivative = k * MathS.Pow(x, Exponent(k - 1)) * binomial - p * MathS.Pow(x, Exponent(k + p - 1));
var atOne = derivative.Substitute(x, 1).InnerSimplified;
if (Functions.PartialFractions.IsZeroAsAValue(atOne))
continue;
// The constants in lowest terms: `(e - 4 f)/(1 + e/(2 f) - 3)` is `2 f`.
var multiple = Functions.PartialFractions.InLowestTermsOverTheSymbols(above.Substitute(x, 1) / (atOne * c));
var ratio = Functions.PartialFractions.InLowestTermsOverTheSymbols(d / c);
// The ratio named while it is integrated: as a quotient of symbols it is read as
// the coefficient of a general quadratic, with an arm for a double root that 1 + q u^2
// never has.
var u = Variable.CreateUnique(expr, "u_quotient");
var q = Variable.CreateUnique(expr, "q_ratio");
if (Integration.ComputeIndefiniteIntegral(1 / (1 + q * MathS.Pow(u, 2)), u, integrateByParts) is not { } inU)
return null;
var answer = multiple * inU.Substitute(u, MathS.Pow(x, k) / binomial).Substitute(q, ratio);
return Functions.PartialFractions.DerivativeHoldsAtSampledPoints(answer, expr, x) ? answer : null;
}
return null;

// An exponent as it simplifies: `(2 + 2 m)/2` is `1 + m`. Exponents are small.
static Entity Exponent(Entity exponent)
=> exponent.InnerSimplified is var inner && (inner is Number || inner.Complexity > 40) ? inner : inner.Simplify();

// A constant times a power of x, `c x^e` with `e` free of x and not 0 unless the term
// is free of x; null otherwise.
static (Entity Coefficient, Entity Exponent)? ConstantTimesAPowerOfX(Entity term, Entity.Variable x)
{
Entity coefficient = Number.Integer.One;
Entity? exponent = null;
foreach (var part in Mulf.LinearChildren(term))
{
if (!part.ContainsNode(x))
{
coefficient = coefficient == Number.Integer.One ? part : coefficient * part;
continue;
}
if (exponent is not null)
return null;
exponent = part switch
{
Variable v when v == x => Number.Integer.One,
Powf(var @base, var e) when @base == x && !e.ContainsNode(x) => e,
_ => null
};
if (exponent is null)
return null;
}
return (coefficient, exponent ?? Number.Integer.Zero);
}
}

/// <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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// And of quotients, which a substitution leaves where it has simplified a sum over
// the bar: `(c/(a + c u^2))^2` is read by nothing that reads `c^2/(a + c u^2)^2`.
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfQuotients(expr, x, integrateByParts)) is { }) return answer;
// The derivative of a power of x over a binomial, over a multiple of the binomial's
// square plus a power of x: an arctangent of the quotient, which nothing else reads
// past a quartic -- `(e - 4 f x^3)/(e^2 + 4 d f x^2 + 4 e f x^3 + 4 f^2 x^6)`.
if ((answer = IndefiniteIntegralSolver.SolveAsTheArctangentOfAPowerOfXOverABinomial(expr, x, integrateByParts)) is { }) return answer;
// And a fractional or symbolic power of a monomial: `(c x^n)^b` is `c^b x^(n b)`
// for a positive `c`, on the `x > 0` where a symbolic `n` leaves the integrand real.
if ((answer = IndefiniteIntegralSolver.SolveByDistributingAPowerOfAMonomial(expr, x, integrateByParts)) is { }) return answer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// 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
{
/// <summary>
/// The derivative of <c>w = x^j/(x^p + l)</c> over a multiple of <c>(x^p + l)^2</c> plus a
/// power of x: an arctangent of <c>w</c>, or a hyperbolic one. Rubi's 1.3.2.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Checked by differentiating back with <c>d = 0.7</c>, <c>f = 0.4</c>, <c>k = 1.3</c>,
/// <c>m = 0.6</c> and <c>n = 1.7</c>: on both sides of 0 where the powers of x are whole, and
/// at positive x where they are symbols.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class ArctangentOfAPowerOfXOverABinomialIntegralTest
{
[Theory]
[InlineData("(k - 4*f*x^3)/(k^2 + 4*d*f*x^2 + 4*k*f*x^3 + 4*f^2*x^6)")]
[InlineData("(k - 4*f*x^3)/(k^2 - 4*d*f*x^2 + 4*k*f*x^3 + 4*f^2*x^6)")]
[InlineData("x^2*(3*k + 2*f*x^2)/(k^2 + 4*k*f*x^2 + 4*f^2*x^4 + 4*d*f*x^6)")]
[InlineData("x*(2*k - 2*f*x^3)/(k^2 + 4*k*f*x^3 - 4*d*f*x^4 + 4*f^2*x^6)")]
public void IsIntegratedOnBothSidesOfZero(string integrand)
=> DifferentiatesBack(integrand, new[] { -1.6, -0.7, 0.3, 0.9, 1.7 });

[Theory]
[InlineData("(k - 2*f*(n - 1)*x^n)/(k^2 + 4*d*f*x^2 + 4*k*f*x^n + 4*f^2*x^(2*n))")]
[InlineData("x^m*(k*(1 + m) + 2*f*(1 + m - n)*x^n)/(k^2 + 4*d*f*x^(2 + 2*m) + 4*k*f*x^n + 4*f^2*x^(2*n))")]
[InlineData("x^m*(k*(1 + m) + 2*f*(1 + m - n)*x^n)/(k^2 - 4*d*f*x^(2 + 2*m) + 4*k*f*x^n + 4*f^2*x^(2*n))")]
public void IsIntegratedInSymbolicPowersOfX(string integrand)
=> DifferentiatesBack(integrand, new[] { 0.3, 0.9, 1.7, 2.6 });

private static void DifferentiatesBack(string integrand, double[] points)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
Assert.Contains("arctan(", integral.Stringize());
Entity Pinned(Entity e) => e.Substitute("d", 0.7).Substitute("f", 0.4).Substitute("k", 1.3).Substitute("m", 0.6).Substitute("n", 1.7);
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
var original = Pinned(integrand.ToEntity());
foreach (var at in points)
{
var want = original.Substitute("x", at).EvalNumerical();
var got = derivative.Substitute("x", at).EvalNumerical();
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
}
}
}
}
Loading