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 @@ -153,6 +153,24 @@ answer on the far side of a root gets a real expression where it used to get a c
| `1/(x*(-4+x^2)^4)` | 8,472 ms | 1,878 ms |
| `1/((1+x)^3*(2+x)^3)` | 1,343 ms | 492 ms |

### Two square roots of linears with one slope are rationalised together

`sqrt(L1)` and `sqrt(L2)` with `L1 - L2` a constant are rational in their sum
`v = sqrt(L1) + sqrt(L2)`: the difference is `(L1 - L2)/v`, and `x` is rational in `v`. An inverse
function of the difference was declined where it is elementary: Charlwood's
`arcsin(sqrt(1 + x) - sqrt(x))`, after five seconds, and Rubi's 5.3.7 has the arctangent's powers
of x beside it. Under `v` each is an inverse function of `1/v` beside a rational function of `v`,
which parts closes, and written back through `1/v = (sqrt(L1) - sqrt(L2))/(L1 - L2)` the answer
holds the difference as the integrand does. Only where the sum or the difference stands inside an
inverse trigonometric function or a logarithm: a rational function of the two roots is the radical
rules' already
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"asin(sqrt(1 + x) - sqrt(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `arcsin(sqrt(1 + x) - sqrt(x))` |
| `"x^3*atan(-sqrt(x)+sqrt(1+x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in the arctangent |

### The hyperbolic functions have antiderivatives, and so does anything rational in `e^(k x)`

An integrand rational in `e^(k x)` becomes a rational function of one variable under
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15053,6 +15053,121 @@ Entity SquareOf(Entity term)
}
}

/// <summary>
/// Two square roots of linears with one slope, <c>sqrt(L1)</c> and <c>sqrt(L2)</c> with
/// <c>L1 - L2 = k</c> a constant, rationalised together by their sum
/// <c>v = sqrt(L1) + sqrt(L2)</c>: their difference is <c>k/v</c>, so
/// <c>sqrt(L1) = (v + k/v)/2</c>, <c>sqrt(L2) = (v - k/v)/2</c>, and
/// <c>dx = (v^4 - k^2)/(2 m v^3) dv</c> for the slope <c>m</c>.
/// </summary>
/// <remarks>
/// <para>
/// Charlwood's <c>arcsin(sqrt(1 + x) - sqrt(x))</c> is <c>arcsin(1/v) (v^4 - 1)/(2 v^3)</c>,
/// which parts closes. Asked as written, the remainder parts leaves in x is a nested root
/// no rule reads, and it was declined after five seconds, and after sixty on a slow
/// runner. Rubi's 5.3.7 has the arctangent's powers of x beside it.
/// </para>
/// <para>
/// The identities hold for the principal roots wherever v is not zero: the sum times the
/// difference is <c>L1 - L2</c> for any values of the two, so nothing is assumed about
/// signs. At the question asked or one below it, since it lands on the chain in v.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </para>
/// </remarks>
internal static Entity? SolveBySubstitutingTheSumOfTwoRootsOfLinears(Entity expr, Entity.Variable x, bool integrateByParts)
{
if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
return null;
// Exactly two radicands under half-odd powers, each linear, with one slope.
var radicands = new List<Entity>();
foreach (var node in expr.Nodes)
if (node is Powf(var @base, Number.Rational power) && power is not Number.Integer && @base.ContainsNode(x))
{
if (!power.ERational.Denominator.Equals(EInteger.FromInt32(2)) || !power.ERational.Numerator.CanFitInInt32())
return null;
if (!radicands.Contains(@base))
radicands.Add(@base);
}
if (radicands.Count != 2
|| !TreeAnalyzer.TryGetPolyLinear(radicands[0], x, out var m1, out var n1)
|| !TreeAnalyzer.TryGetPolyLinear(radicands[1], x, out var m2, out var n2)
|| VanishesIdentically(m1) || !VanishesIdentically(m1 - m2) || VanishesIdentically(n1 - n2))
return null;
// Only where the sum or the difference of the two roots stands inside an inverse
// trigonometric function or a logarithm, which nothing else reads. A rational function
// of the roots is the radical rules' already: asked here first,
// `x^2/(sqrt(a + b x) + sqrt(c + b x))` ran out its budget where the conjugate answers it
// in 37 ms, and so did `sqrt(x - 1) sqrt(x + 1)/(1 + x - x^2)`, which the quotient of the
// two roots answers in 244 ms, and Timofeev's `arccos(sqrt(x/(1 + x)))`, a quotient of
// the two roots under the function.
if (!expr.Nodes.Any(node => node is Arcsinf or Arccosf or Arctanf or Arccotanf or Arcsecantf or Arccosecantf or Logf
&& node.Nodes.Any(inner => SignOfTheTwoRoots(inner, radicands) != (0, 0))))
return null;
var k = (n1 - n2).InnerSimplified;
var v = Variable.CreateUnique(expr, "v_roots");
var roots = new[] { (v + k / v) / 2, (v - k / v) / 2 };
// The sum is v and the difference k/v, taken whole first so that the rules in v see
// `arcsin(1/v)` rather than the difference of the two roots written in v.
var inV = expr.Replace(node => SignOfTheTwoRoots(node, radicands) switch
{
(1, 1) => v,
(1, -1) => k / v,
(-1, 1) => -k / v,
_ => node,
});
inV = inV.Replace(node => node is Powf(var @base, Number.Rational power) && power is not Number.Integer
&& radicands.IndexOf(@base) is var index and >= 0
? MathS.Pow(roots[index], Number.Integer.Create(power.ERational.Numerator))
: node);
// Everything else in x through the second root: x = ((v - k/v)^2/4 - n2)/m.
inV = (inV.Substitute(x, (MathS.Sqr(roots[1]) - n2) / m1) * (MathS.Pow(v, 4) - MathS.Sqr(k)) / (2 * m1 * MathS.Pow(v, 3))).InnerSimplified;
if (inV.ContainsNode(x) || inV.Nodes.Any(node => node == MathS.NaN))
return null;
if (Integration.ComputeAsAQuestionOfItsOwn(inV, v, integrateByParts) is not { } inTermsOfV)
return null;
// v is positive wherever the integrand is real: a sum of two principal roots of
// reals, not both zero since their squares differ. Its sign is one, and a reciprocal
// of it is the difference over k, written back so.
var sum = MathS.Sqrt(radicands[0]) + MathS.Sqrt(radicands[1]);
var difference = (MathS.Sqrt(radicands[0]) - MathS.Sqrt(radicands[1])) / k;
var answer = inTermsOfV.Replace(node => node switch
{
Signumf(var signed) when signed == v => Number.Integer.One,
Absf(var measured) when measured == v => v,
Divf(var above, var below) when below == v => above * difference,
Powf(var @base, Number.Integer { EInteger.Sign: < 0 } power) when @base == v => MathS.Pow(difference, (-power).InnerSimplified),
_ => node,
}).Substitute(v, sum).InnerSimplified;
return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
}

/// <summary>
/// (1, 1) where <paramref name="node"/> is the sum of the two roots, (1, -1) the first less
/// the second, (-1, 1) the second less the first, and (0, 0) otherwise.
/// </summary>
private static (int, int) SignOfTheTwoRoots(Entity node, List<Entity> radicands)
{
if (node is not (Sumf or Minusf))
return (0, 0);
var terms = Sumf.LinearChildren(node).ToList();
if (terms.Count != 2)
return (0, 0);
var signs = new int[2];
foreach (var term in terms)
{
var (sign, root) = term switch
{
Mulf(Number.Integer { EInteger: var minusOne }, var rest) when minusOne.Equals(EInteger.FromInt32(-1)) => (-1, rest),
_ => (1, term),
};
if (root is not Powf(var @base, Number.Rational half) || half != Number.Rational.Create(1, 2)
|| radicands.IndexOf(@base) is not (var index and >= 0) || signs[index] != 0)
return (0, 0);
signs[index] = sign;
}
return (signs[0], signs[1]);
}

internal static Entity? SolveByCombiningRadicals(Entity expr, Entity.Variable x, bool integrateByParts)
{
// A secant or cosecant under a root is the reciprocal of a cosine or sine there:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// `sqrt(a + a sin(x))` for a positive `a` only. Before the substitution search, which
// spends the budget on the roots.
if ((answer = IndefiniteIntegralSolver.SolveByTakingAConstantOutOfAFractionalPower(expr, x, integrateByParts)) is { }) return answer;
// Two roots of linears with one slope, rationalised together by their sum: before the
// 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 logarithmic derivative the substitution above could not read for a symbol in
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.
Expand Down
34 changes: 34 additions & 0 deletions Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,39 @@ public void ARootOfAProductOfPowersOfLinears(string integrand, double[] points)
[Fact]
public void AFactoredRootBesideItsFactorsRoots()
=> DifferentiatesBack("x^2*(1-x^2)^(1/4)*sqrt(1+x)/(sqrt(1-x)*(sqrt(1-x)-sqrt(1+x)))", 0.31, 0.57, 0.83, -0.2, -0.6);

/// <summary>
/// Two square roots of linears with one slope, rationalised together by their sum
/// <c>v = sqrt(L1) + sqrt(L2)</c>, whose difference is <c>(L1 - L2)/v</c>: an inverse
/// function of the difference is one of <c>1/v</c> beside a rational function of v, which
/// parts closes. Charlwood's arcsine was declined after five seconds, and Rubi's 5.3.7
/// has the arctangent's powers of x.
/// </summary>
[Theory]
[InlineData("asin(sqrt(1 + x) - sqrt(x))")]
[InlineData("acos(sqrt(1 + x) - sqrt(x))")]
[InlineData("x^3*atan(-sqrt(x)+sqrt(1+x))")]
public void AnInverseFunctionOfTheDifferenceOfTwoRootsOfOneSlope(string integrand)
=> DifferentiatesBack(integrand, 0.3, 0.7, 1.1, 1.4);

/// <summary>
/// Two such roots where they stand in no inverse function or logarithm are the radical
/// rules', and stay theirs: taken under v first, each of these ran out the
/// corpus's budget, where they are answered in under 40 ms. Rubi's 1.3.2, and Timofeev's
/// 127, a quotient of the two roots under the arccosine.
/// </summary>
[Theory]
[InlineData("x^2/(sqrt(a + b*x) + sqrt(c + b*x))")]
[InlineData("acos(sqrt(x/(1 + x)))")]
public void WhatTheOtherRulesAnswerStaysTheirs(string integrand)
=> DifferentiatesBackPinned(integrand, new[] { 0.3, 0.7, 1.1, 1.4 }, ("a", 2), ("b", 1.5), ("c", 0.7));

/// <summary>
/// And a product of the two roots, Rubi's 1.2.1.4: answered through their quotient in a
/// quarter of a second, and past the corpus's budget when taken under v first.
/// </summary>
[Fact]
public void AProductOfTwoRootsOfOneSlopeStaysTheirs()
=> DifferentiatesBack("sqrt(-1 + x)*sqrt(1 + x)/(1 + x - x^2)", 1.3, 1.7, 2.4, 3.1);
}
}
Loading