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
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,27 @@ leading coefficient once expanded, and declined: it is read expanded and bare no
| `"sin(x)/(a+b*sin(x))^3".Integrate("x")`, Rubi's 4.1.2.1 row 242 | `integral(...)` | the antiderivative |
| `"4*x*(1+x^2)/(a*x^2+2*b*x+a)^3".Integrate("x")` | `integral(...)` | the antiderivative |

### A cube or fourth root of a quadratic binomial beside another is integrated where that is elementary

**Answers where there were none.** `1/((A + B x^2)^(1/3) (C + D x^2))` is an elliptic integral
except where `B C + 3 A D = 0` or `B C - 9 A D = 0`, and `1/((A + B x^2)^(1/4) (C + D x^2))` except
where `B C - 2 A D = 0`. At those ratios it is arctangents and logarithms of quotients in the root,
as Rubi's 1.1.2.3 gives them, and it was left unevaluated. It is answered there now, with symbols
in the constants too: by the sign of `B/A` for the cube root, and by the signs of `A` and `B` for
the fourth. With `A` and `B` both positive, the fourth root's answer jumps at `x = 0`, and is an
antiderivative on each side of it, as Rubi's is
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"1/((1 - x^2)^(1/3)*(3 + x^2))".ToEntity().Integrate("x")` | `integral(...)` | arctangents and logarithms in `x` and `(1 - x^2)^(1/3)` |
| `"1/((1 + x^2)^(1/3)*(9 + x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same in `(1 + x^2)^(1/3)` |
| `"1/((a + b*x^2)^(1/3)*(3*a - b*x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same in `(a + b x^2)^(1/3)`, by the sign of `b/a` |
| `"1/((a + b*x^2)^(1/3)*(9*a + b*x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same, by the sign of `b/a` |
| `"1/((a + b*x^2)^(1/4)*(2*a + b*x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same in `(a + b x^2)^(1/4)`, by the signs of `a` and `b` |
| `"1/((2 + 3*x^2)^(1/4)*(4 + 3*x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same in `(2 + 3 x^2)^(1/4)` |
| `"1/((-2 + 3*x^2)*(-1 + 3*x^2)^(1/4))".ToEntity().Integrate("x")` | `integral(...)` | `-(arctan(u) + artanh(u))/(2 sqrt(6))`, `u = sqrt(3) x/(sqrt(2) (-1 + 3 x^2)^(1/4))` |

### A rational function of the sine and cosine with symbols in it is integrated by the half angle

`sin(x)^2/(a + b cos(x))` was left unevaluated while `sin(x)^2/(2 + 3 cos(x))` was answered. Under
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4049,6 +4049,132 @@ private static bool TryReadAsABinomialIn(Entity expr, Entity.Variable x, out Ent
return !c.ContainsNode(x) && n >= 1;
}

/// <summary>
/// A root of a quadratic binomial beside another, <c>1/((A + B x^2)^(1/3) (C + D x^2))</c>
/// with <c>B C + 3 A D = 0</c> or <c>B C - 9 A D = 0</c>, and
/// <c>1/((A + B x^2)^(1/4) (C + D x^2))</c> with <c>B C - 2 A D = 0</c>: the ratios at which
/// the integral, elliptic otherwise, is elementary, in closed form.
/// </summary>
/// <remarks>
/// <para>
/// With <c>y</c> the root, <c>a = A^(1/3)</c>, and <c>q = sqrt(B/A)</c> or <c>sqrt(-B/A)</c> by its sign:
/// <code>
/// B C + 3 A D = 0, B/A &gt; 0: -2^(1/3) q/(4 a D) (atan(a q x/(a + 2^(1/3) y)) - atan(q x)/3
/// - (artanh(sqrt(3) (a - 2^(1/3) y)/(a q x)) + artanh(sqrt(3)/(q x)))/sqrt(3))
/// B/A &lt; 0: 2^(1/3) q/(4 a D) (artanh(a q x/(a + 2^(1/3) y)) - artanh(q x)/3
/// + (atan(sqrt(3) (a - 2^(1/3) y)/(a q x)) + atan(sqrt(3)/(q x)))/sqrt(3))
/// B C - 9 A D = 0, B/A &gt; 0: q/(12 a D) (atan((a - y)^2/(3 a^2 q x)) + atan(q x/3)
/// - sqrt(3) artanh(sqrt(3) (a - y)/(a q x)))
/// B/A &lt; 0: q/(12 a D) (artanh((a - y)^2/(3 a^2 q x)) - artanh(q x/3)
/// - sqrt(3) atan(sqrt(3) (a - y)/(a q x)))
/// </code>
/// and for the fourth root, with <c>s = sqrt(A + B x^2)</c> and <c>r = sqrt(|B|)</c>, times
/// <c>B/D</c>, which takes <c>C + D x^2</c> to <c>2 A + B x^2</c>:
/// <code>
/// A &gt; 0, B &gt; 0: -(atan(A^(3/4) (1 + s/sqrt(A))/(x y r)) + artanh(A^(3/4) (1 - s/sqrt(A))/(x y r)))/(2 A^(3/4) r)
/// A &gt; 0, B &lt; 0: (atan(A^(3/4) (1 - s/sqrt(A))/(x y r)) + artanh(A^(3/4) (1 + s/sqrt(A))/(x y r)))/(2 A^(3/4) r)
/// A &lt; 0: -(atan(u) + artanh(u))/(2 (-A)^(3/4) sqrt(2) r), u = x r/((-A)^(1/4) y sqrt(2))
/// </code>
/// Rubi's, from its 1.1.2.3, written for any constants with the ratio; each was checked by
/// differentiating it at points in every sign case. The derivative of each uses only that
/// the roots are roots, so for a sign that is a symbol's both forms are given, each where
/// it holds.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </para>
/// </remarks>
internal static Entity? SolveAnEllipticLookingQuotientOfBinomials(Entity expr, Entity.Variable x)
{
var (numerator, denominator) = Functions.SingleQuotient.Of(expr);
if (numerator.ContainsNode(x))
return null;
Entity constant = numerator;
Entity? radicand = null;
var order = 0;
Entity? other = null;
foreach (var factor in Mulf.LinearChildren(denominator))
{
if (!factor.ContainsNode(x))
constant = constant / factor;
else if (radicand is null && factor is Powf(var @base, Number.Rational power) && power.ERational.Numerator.Equals(EInteger.One)
&& (power.ERational.Denominator.Equals(EInteger.FromInt32(3)) || power.ERational.Denominator.Equals(EInteger.FromInt32(4))))
{
radicand = @base;
order = power.ERational.Denominator.ToInt32Unchecked();
}
else if (other is null && factor is not Powf)
other = factor;
else
return null;
}
if (radicand is null || other is null || EvenQuadratic(radicand) is not var (a0, b0) || EvenQuadratic(other) is not var (c0, d0))
return null;
var (A, B, C, D) = (a0, b0, c0, d0);
var y = MathS.Pow(radicand, Number.Rational.Create(1, order));
Entity? answer = null;
if (order == 3 && VanishesIdentically(B * C + 3 * A * D))
{
var a = MathS.Pow(A, Number.Rational.Create(1, 3));
var cube = MathS.Pow(2, Number.Rational.Create(1, 3));
var three = MathS.Sqrt(3);
var quarter = LowestOverTheSymbols(1 / (4 * D));
Entity Positive(Entity q) => -quarter * cube * q / a * (MathS.Arctan(a * q * x / (a + cube * y)) - MathS.Arctan(q * x) / 3
- (MathS.Hyperbolic.Artanh(three * (a - cube * y) / (a * q * x)) + MathS.Hyperbolic.Artanh(three / (q * x))) / three);
Entity Negative(Entity q) => quarter * cube * q / a * (MathS.Hyperbolic.Artanh(a * q * x / (a + cube * y)) - MathS.Hyperbolic.Artanh(q * x) / 3
+ (MathS.Arctan(three * (a - cube * y) / (a * q * x)) + MathS.Arctan(three / (q * x))) / three);
var ratio = LowestOverTheSymbols(B / A);
answer = BySign(ratio, Positive(MathS.Sqrt(ratio)), Negative(MathS.Sqrt(LowestOverTheSymbols(-ratio))));
}
else if (order == 3 && VanishesIdentically(B * C - 9 * A * D))
{
var a = MathS.Pow(A, Number.Rational.Create(1, 3));
var three = MathS.Sqrt(3);
var twelfth = LowestOverTheSymbols(1 / (12 * D));
Entity Positive(Entity q) => twelfth * q / a * (MathS.Arctan(MathS.Pow(a - y, 2) / (3 * MathS.Pow(a, 2) * q * x)) + MathS.Arctan(q * x / 3)
- three * MathS.Hyperbolic.Artanh(three * (a - y) / (a * q * x)));
Entity Negative(Entity q) => twelfth * q / a * (MathS.Hyperbolic.Artanh(MathS.Pow(a - y, 2) / (3 * MathS.Pow(a, 2) * q * x)) - MathS.Hyperbolic.Artanh(q * x / 3)
- three * MathS.Arctan(three * (a - y) / (a * q * x)));
var ratio = LowestOverTheSymbols(B / A);
answer = BySign(ratio, Positive(MathS.Sqrt(ratio)), Negative(MathS.Sqrt(LowestOverTheSymbols(-ratio))));
}
else if (order == 4 && VanishesIdentically(B * C - 2 * A * D))
{
var s = MathS.Sqrt(radicand);
var threeQuarters = Number.Rational.Create(3, 4);
Entity BothPositive()
{
var r = MathS.Sqrt(B);
return -(MathS.Arctan(MathS.Pow(A, threeQuarters) * (1 + s / MathS.Sqrt(A)) / (x * y * r))
+ MathS.Hyperbolic.Artanh(MathS.Pow(A, threeQuarters) * (1 - s / MathS.Sqrt(A)) / (x * y * r))) / (2 * MathS.Pow(A, threeQuarters) * r);
}
Entity SlopeNegative()
{
var r = MathS.Sqrt(-B);
return (MathS.Arctan(MathS.Pow(A, threeQuarters) * (1 - s / MathS.Sqrt(A)) / (x * y * r))
+ MathS.Hyperbolic.Artanh(MathS.Pow(A, threeQuarters) * (1 + s / MathS.Sqrt(A)) / (x * y * r))) / (2 * MathS.Pow(A, threeQuarters) * r);
}
Entity ConstantNegative()
{
var r = MathS.Sqrt(B);
var u = x * r / (MathS.Pow(-A, Number.Rational.Create(1, 4)) * y * MathS.Sqrt(2));
return -(MathS.Arctan(u) + MathS.Hyperbolic.Artanh(u)) / (2 * MathS.Pow(-A, threeQuarters) * MathS.Sqrt(2) * r);
}
var toTheRatio = LowestOverTheSymbols(B / D);
var form = BySigns(A, B, BothPositive(), SlopeNegative(), ConstantNegative());
answer = toTheRatio == Number.Integer.One ? form : toTheRatio * form;
}
return answer is null ? null : (constant * answer).InnerSimplified;

// A + B x^2, both nonzero.
(Entity, Entity)? EvenQuadratic(Entity polynomial)
{
if (!TreeAnalyzer.TryGetPolynomial(polynomial, x, out var terms) || terms.Count != 2
|| !terms.TryGetValue(EInteger.Zero, out var free) || !terms.TryGetValue(EInteger.FromInt32(2), out var square)
|| free.ContainsNode(x) || square.ContainsNode(x) || VanishesIdentically(free) || VanishesIdentically(square))
return null;
return (free, square);
}
}

/// <summary>
/// A <b>binomial differential</b> <c>x^m (a + b x^n)^(p/q)</c>, in the two of Chebyshev's
/// three cases that are not a whole power: <c>(m + 1)/n</c> whole, or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// `a + b x^n`. After the quadratic, which is the case `n = 2` and answers it through
// the trigonometric substitution -- a shorter answer than a root of a root.
if ((answer = IndefiniteIntegralSolver.SolveABinomialDifferential(expr, x)) is { }) return answer;
// And a root of a quadratic binomial beside another at the ratios where the integral is
// elementary: `1/((1 - x^2)^(1/3) (3 + x^2))`, which is no binomial differential.
if ((answer = IndefiniteIntegralSolver.SolveAnEllipticLookingQuotientOfBinomials(expr, x)) is { }) return answer;
// A rational function of x^n beside `(c + d x^n)^(k - 1/n)`, rationalised by
// `u = x/(c + d x^n)^(1/n)`: Timofeev's `1/((1 + x^4)(2 + x^4)^(1/4))` is `1/(1 + u^4)`.
if ((answer = IndefiniteIntegralSolver.SolveByDividingByTheRoot(expr, x)) is { }) return answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static void DifferentiatesBack(string integrand)
/// </summary>
[Theory]
[InlineData("1/(x*((-1 + x)*(q - 2*q*x + x^2))^(1/3))")]
[InlineData("1/((3 - x^2)*(1 + x^2)^(1/3))")]
[InlineData("1/((3 - x^2)*(2 + x^2)^(1/3))")]
public void DeclinedWhereTheFieldIsNotHeld(string integrand)
=> Assert.Contains("integral(", integrand.ToEntity().Integrate("x").Stringize());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// 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>
/// A cube or fourth root of a quadratic binomial beside another, at the ratios where the
/// integral, elliptic otherwise, is elementary: Rubi's 1.1.2.3.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Checked by differentiating back at points where the integrand is real, with the symbols
/// pinned in each sign case the closed forms are written for.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class EllipticLookingQuotientOfBinomialsTest
{
private static void DifferentiatesBack(string integrand, double[] points, params (string, double)[] pins)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
var derivative = integral.Substitute("C", 0).Differentiate("x");
Entity original = integrand.ToEntity();
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>
/// <c>B C + 3 A D = 0</c> and <c>B C - 9 A D = 0</c> under a cube root, for each sign of
/// <c>b/a</c>, and <c>B C - 2 A D = 0</c> under a fourth root, for each sign of <c>b</c> with
/// <c>a</c> positive and for <c>a</c> negative.
/// </summary>
[Theory]
[InlineData("1/((a + b*x^2)^(1/3)*(3*a - b*x^2))", 1.3, 0.7, new[] { 0.3, 0.9, 1.7, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/3)*(3*a - b*x^2))", 1.3, -0.7, new[] { 0.3, 0.9, 1.1, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/3)*(9*a + b*x^2))", 1.3, 0.7, new[] { 0.3, 0.9, 1.7, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/3)*(9*a + b*x^2))", 1.3, -0.7, new[] { 0.3, 0.9, 1.1, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/4)*(2*a + b*x^2))", 1.3, 0.7, new[] { 0.3, 0.9, 1.7, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/4)*(2*a + b*x^2))", 1.3, -0.7, new[] { 0.3, 0.9, 1.1, -0.6 })]
[InlineData("1/((a + b*x^2)^(1/4)*(2*a + b*x^2))", -1.3, 0.7, new[] { 1.5, 2.0, 2.6, -1.8 })]
public void AtTheRatioWhereItIsElementary(string integrand, double a, double b, double[] points)
=> DifferentiatesBack(integrand, points, ("a", a), ("b", b));

/// <summary>With numbers in the constants.</summary>
[Theory]
[InlineData("1/((1 - x^2)^(1/3)*(3 + x^2))", new[] { 0.3, 0.6, 0.9, -0.5 })]
[InlineData("1/((1 + x^2)^(1/3)*(9 + x^2))", new[] { 0.3, 0.9, 1.7, -0.6 })]
[InlineData("1/((3 - x^2)*(1 + x^2)^(1/3))", new[] { 0.3, 0.9, 1.4, -0.6 })]
[InlineData("1/((2 + 3*x^2)^(1/4)*(4 + 3*x^2))", new[] { 0.3, 0.9, 1.7, -0.6 })]
[InlineData("1/((-2 + 3*x^2)*(-1 + 3*x^2)^(1/4))", new[] { 0.7, 1.1, 1.9, -1.3 })]
public void WithNumbers(string integrand, double[] points)
=> DifferentiatesBack(integrand, points);

/// <summary>
/// Across 0, where the cube root's answers are written over <c>x</c>: the value is the one
/// quadrature gives.
/// </summary>
[Theory]
[InlineData("integral(1/((1 + x^2)^(1/3)*(9 + x^2)), x, -1, 1)", 0.197369774498477432459)]
[InlineData("integral(1/((1 - x^2)^(1/3)*(3 + x^2)), x, -1/2, 1/2)", 0.334352054745427304208)]
public void ADefiniteIntegralAcrossZero(string integral, double expected)
{
var value = integral.ToEntity().Simplify();
Assert.IsNotType<Entity.Integralf>(value);
var number = value.EvalNumerical();
Assert.True(Math.Abs((double)number.RealPart - expected) < 1e-9 && Math.Abs((double)number.ImaginaryPart) < 1e-9,
$"{integral} is {value}, {number}, where quadrature gives {expected}");
}
}
}
Loading