Skip to content

Commit ff71e4b

Browse files
Two roots of linears with one slope are rationalised together by their sum (#1576)
sqrt(L1) and sqrt(L2) with L1 - L2 = k a constant are rational in their sum v = sqrt(L1) + sqrt(L2): the difference is k/v, sqrt(L1) = (v + k/v)/2, sqrt(L2) = (v - k/v)/2, and dx = (v^4 - k^2)/(2 m v^3) dv for the slope m. The identities hold for the principal roots wherever v is not zero, so nothing is assumed about signs. An inverse function of the difference was declined where it is elementary -- Charlwood's arcsin(sqrt(1 + x) - sqrt(x)) after five seconds, which is what ABareInverseFunctionByPartsTest's TheRemainderMayBeOutOfReach timed on macOS at 63.8 s against a 60 s bound -- and under v it is arcsin(1/v) beside a rational function of v, which parts closes. The sum and the difference are taken whole before the roots, so the rules in v see 1/v, and on the way back 1/v is written as the difference over k, so the answer holds the difference as the integrand does; v is positive wherever the integrand is real, so its sign is one. At the question asked or one below it, since it lands on the chain in v, and only where the sum or the difference of the roots stands inside an inverse trigonometric function or a logarithm: a rational function of the two roots is the radical rules' already, and taken under v first three of theirs ran out the budget. Measured with work/intbench against a8a2b7e, 3 s a problem: the 55 problems with two roots of linears of one slope, 41 -> 45 solved (Charlwood's arcsine, Rubi's 5.3.7:177, 178 and 184), none lost, in 26 s instead of 56 s; family 0, 1750 -> 1751; the families sample (912), one gain and none lost. Part of #718. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c2013cf commit ff71e4b

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ answer on the far side of a root gets a real expression where it used to get a c
153153
| `1/(x*(-4+x^2)^4)` | 8,472 ms | 1,878 ms |
154154
| `1/((1+x)^3*(2+x)^3)` | 1,343 ms | 492 ms |
155155

156+
### Two square roots of linears with one slope are rationalised together
157+
158+
`sqrt(L1)` and `sqrt(L2)` with `L1 - L2` a constant are rational in their sum
159+
`v = sqrt(L1) + sqrt(L2)`: the difference is `(L1 - L2)/v`, and `x` is rational in `v`. An inverse
160+
function of the difference was declined where it is elementary: Charlwood's
161+
`arcsin(sqrt(1 + x) - sqrt(x))`, after five seconds, and Rubi's 5.3.7 has the arctangent's powers
162+
of x beside it. Under `v` each is an inverse function of `1/v` beside a rational function of `v`,
163+
which parts closes, and written back through `1/v = (sqrt(L1) - sqrt(L2))/(L1 - L2)` the answer
164+
holds the difference as the integrand does. Only where the sum or the difference stands inside an
165+
inverse trigonometric function or a logarithm: a rational function of the two roots is the radical
166+
rules' already
167+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
168+
169+
| Input | Was (2.5.0) | Now |
170+
|---|---|---|
171+
| `"asin(sqrt(1 + x) - sqrt(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `arcsin(sqrt(1 + x) - sqrt(x))` |
172+
| `"x^3*atan(-sqrt(x)+sqrt(1+x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in the arctangent |
173+
156174
### The hyperbolic functions have antiderivatives, and so does anything rational in `e^(k x)`
157175

158176
An integrand rational in `e^(k x)` becomes a rational function of one variable under

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15652,6 +15652,121 @@ Entity SquareOf(Entity term)
1565215652
}
1565315653
}
1565415654

15655+
/// <summary>
15656+
/// Two square roots of linears with one slope, <c>sqrt(L1)</c> and <c>sqrt(L2)</c> with
15657+
/// <c>L1 - L2 = k</c> a constant, rationalised together by their sum
15658+
/// <c>v = sqrt(L1) + sqrt(L2)</c>: their difference is <c>k/v</c>, so
15659+
/// <c>sqrt(L1) = (v + k/v)/2</c>, <c>sqrt(L2) = (v - k/v)/2</c>, and
15660+
/// <c>dx = (v^4 - k^2)/(2 m v^3) dv</c> for the slope <c>m</c>.
15661+
/// </summary>
15662+
/// <remarks>
15663+
/// <para>
15664+
/// Charlwood's <c>arcsin(sqrt(1 + x) - sqrt(x))</c> is <c>arcsin(1/v) (v^4 - 1)/(2 v^3)</c>,
15665+
/// which parts closes. Asked as written, the remainder parts leaves in x is a nested root
15666+
/// no rule reads, and it was declined after five seconds, and after sixty on a slow
15667+
/// runner. Rubi's 5.3.7 has the arctangent's powers of x beside it.
15668+
/// </para>
15669+
/// <para>
15670+
/// The identities hold for the principal roots wherever v is not zero: the sum times the
15671+
/// difference is <c>L1 - L2</c> for any values of the two, so nothing is assumed about
15672+
/// signs. At the question asked or one below it, since it lands on the chain in v.
15673+
/// https://github.com/asc-community/AngouriMath/issues/718
15674+
/// </para>
15675+
/// </remarks>
15676+
internal static Entity? SolveBySubstitutingTheSumOfTwoRootsOfLinears(Entity expr, Entity.Variable x, bool integrateByParts)
15677+
{
15678+
if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
15679+
return null;
15680+
// Exactly two radicands under half-odd powers, each linear, with one slope.
15681+
var radicands = new List<Entity>();
15682+
foreach (var node in expr.Nodes)
15683+
if (node is Powf(var @base, Number.Rational power) && power is not Number.Integer && @base.ContainsNode(x))
15684+
{
15685+
if (!power.ERational.Denominator.Equals(EInteger.FromInt32(2)) || !power.ERational.Numerator.CanFitInInt32())
15686+
return null;
15687+
if (!radicands.Contains(@base))
15688+
radicands.Add(@base);
15689+
}
15690+
if (radicands.Count != 2
15691+
|| !TreeAnalyzer.TryGetPolyLinear(radicands[0], x, out var m1, out var n1)
15692+
|| !TreeAnalyzer.TryGetPolyLinear(radicands[1], x, out var m2, out var n2)
15693+
|| VanishesIdentically(m1) || !VanishesIdentically(m1 - m2) || VanishesIdentically(n1 - n2))
15694+
return null;
15695+
// Only where the sum or the difference of the two roots stands inside an inverse
15696+
// trigonometric function or a logarithm, which nothing else reads. A rational function
15697+
// of the roots is the radical rules' already: asked here first,
15698+
// `x^2/(sqrt(a + b x) + sqrt(c + b x))` ran out its budget where the conjugate answers it
15699+
// in 37 ms, and so did `sqrt(x - 1) sqrt(x + 1)/(1 + x - x^2)`, which the quotient of the
15700+
// two roots answers in 244 ms, and Timofeev's `arccos(sqrt(x/(1 + x)))`, a quotient of
15701+
// the two roots under the function.
15702+
if (!expr.Nodes.Any(node => node is Arcsinf or Arccosf or Arctanf or Arccotanf or Arcsecantf or Arccosecantf or Logf
15703+
&& node.Nodes.Any(inner => SignOfTheTwoRoots(inner, radicands) != (0, 0))))
15704+
return null;
15705+
var k = (n1 - n2).InnerSimplified;
15706+
var v = Variable.CreateUnique(expr, "v_roots");
15707+
var roots = new[] { (v + k / v) / 2, (v - k / v) / 2 };
15708+
// The sum is v and the difference k/v, taken whole first so that the rules in v see
15709+
// `arcsin(1/v)` rather than the difference of the two roots written in v.
15710+
var inV = expr.Replace(node => SignOfTheTwoRoots(node, radicands) switch
15711+
{
15712+
(1, 1) => v,
15713+
(1, -1) => k / v,
15714+
(-1, 1) => -k / v,
15715+
_ => node,
15716+
});
15717+
inV = inV.Replace(node => node is Powf(var @base, Number.Rational power) && power is not Number.Integer
15718+
&& radicands.IndexOf(@base) is var index and >= 0
15719+
? MathS.Pow(roots[index], Number.Integer.Create(power.ERational.Numerator))
15720+
: node);
15721+
// Everything else in x through the second root: x = ((v - k/v)^2/4 - n2)/m.
15722+
inV = (inV.Substitute(x, (MathS.Sqr(roots[1]) - n2) / m1) * (MathS.Pow(v, 4) - MathS.Sqr(k)) / (2 * m1 * MathS.Pow(v, 3))).InnerSimplified;
15723+
if (inV.ContainsNode(x) || inV.Nodes.Any(node => node == MathS.NaN))
15724+
return null;
15725+
if (Integration.ComputeAsAQuestionOfItsOwn(inV, v, integrateByParts) is not { } inTermsOfV)
15726+
return null;
15727+
// v is positive wherever the integrand is real: a sum of two principal roots of
15728+
// reals, not both zero since their squares differ. Its sign is one, and a reciprocal
15729+
// of it is the difference over k, written back so.
15730+
var sum = MathS.Sqrt(radicands[0]) + MathS.Sqrt(radicands[1]);
15731+
var difference = (MathS.Sqrt(radicands[0]) - MathS.Sqrt(radicands[1])) / k;
15732+
var answer = inTermsOfV.Replace(node => node switch
15733+
{
15734+
Signumf(var signed) when signed == v => Number.Integer.One,
15735+
Absf(var measured) when measured == v => v,
15736+
Divf(var above, var below) when below == v => above * difference,
15737+
Powf(var @base, Number.Integer { EInteger.Sign: < 0 } power) when @base == v => MathS.Pow(difference, (-power).InnerSimplified),
15738+
_ => node,
15739+
}).Substitute(v, sum).InnerSimplified;
15740+
return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
15741+
}
15742+
15743+
/// <summary>
15744+
/// (1, 1) where <paramref name="node"/> is the sum of the two roots, (1, -1) the first less
15745+
/// the second, (-1, 1) the second less the first, and (0, 0) otherwise.
15746+
/// </summary>
15747+
private static (int, int) SignOfTheTwoRoots(Entity node, List<Entity> radicands)
15748+
{
15749+
if (node is not (Sumf or Minusf))
15750+
return (0, 0);
15751+
var terms = Sumf.LinearChildren(node).ToList();
15752+
if (terms.Count != 2)
15753+
return (0, 0);
15754+
var signs = new int[2];
15755+
foreach (var term in terms)
15756+
{
15757+
var (sign, root) = term switch
15758+
{
15759+
Mulf(Number.Integer { EInteger: var minusOne }, var rest) when minusOne.Equals(EInteger.FromInt32(-1)) => (-1, rest),
15760+
_ => (1, term),
15761+
};
15762+
if (root is not Powf(var @base, Number.Rational half) || half != Number.Rational.Create(1, 2)
15763+
|| radicands.IndexOf(@base) is not (var index and >= 0) || signs[index] != 0)
15764+
return (0, 0);
15765+
signs[index] = sign;
15766+
}
15767+
return (signs[0], signs[1]);
15768+
}
15769+
1565515770
internal static Entity? SolveByCombiningRadicals(Entity expr, Entity.Variable x, bool integrateByParts)
1565615771
{
1565715772
// A secant or cosecant under a root is the reciprocal of a cosine or sine there:

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
741741
// `sqrt(a + a sin(x))` for a positive `a` only. Before the substitution search, which
742742
// spends the budget on the roots.
743743
if ((answer = IndefiniteIntegralSolver.SolveByTakingAConstantOutOfAFractionalPower(expr, x, integrateByParts)) is { }) return answer;
744+
// Two roots of linears with one slope, rationalised together by their sum: before the
745+
// substitution search, which reads each root on its own.
746+
if ((answer = IndefiniteIntegralSolver.SolveBySubstitutingTheSumOfTwoRootsOfLinears(expr, x, integrateByParts)) is { }) return answer;
744747
if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer;
745748
// A logarithmic derivative the substitution above could not read for a symbol in
746749
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.

‎Sources/Tests/UnitTests/Calculus/TwoLinearRadicalsIntegralTest.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,39 @@ public void ARootOfAProductOfPowersOfLinears(string integrand, double[] points)
107107
[Fact]
108108
public void AFactoredRootBesideItsFactorsRoots()
109109
=> 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);
110+
111+
/// <summary>
112+
/// Two square roots of linears with one slope, rationalised together by their sum
113+
/// <c>v = sqrt(L1) + sqrt(L2)</c>, whose difference is <c>(L1 - L2)/v</c>: an inverse
114+
/// function of the difference is one of <c>1/v</c> beside a rational function of v, which
115+
/// parts closes. Charlwood's arcsine was declined after five seconds, and Rubi's 5.3.7
116+
/// has the arctangent's powers of x.
117+
/// </summary>
118+
[Theory]
119+
[InlineData("asin(sqrt(1 + x) - sqrt(x))")]
120+
[InlineData("acos(sqrt(1 + x) - sqrt(x))")]
121+
[InlineData("x^3*atan(-sqrt(x)+sqrt(1+x))")]
122+
public void AnInverseFunctionOfTheDifferenceOfTwoRootsOfOneSlope(string integrand)
123+
=> DifferentiatesBack(integrand, 0.3, 0.7, 1.1, 1.4);
124+
125+
/// <summary>
126+
/// Two such roots where they stand in no inverse function or logarithm are the radical
127+
/// rules', and stay theirs: taken under v first, each of these ran out the
128+
/// corpus's budget, where they are answered in under 40 ms. Rubi's 1.3.2, and Timofeev's
129+
/// 127, a quotient of the two roots under the arccosine.
130+
/// </summary>
131+
[Theory]
132+
[InlineData("x^2/(sqrt(a + b*x) + sqrt(c + b*x))")]
133+
[InlineData("acos(sqrt(x/(1 + x)))")]
134+
public void WhatTheOtherRulesAnswerStaysTheirs(string integrand)
135+
=> DifferentiatesBackPinned(integrand, new[] { 0.3, 0.7, 1.1, 1.4 }, ("a", 2), ("b", 1.5), ("c", 0.7));
136+
137+
/// <summary>
138+
/// And a product of the two roots, Rubi's 1.2.1.4: answered through their quotient in a
139+
/// quarter of a second, and past the corpus's budget when taken under v first.
140+
/// </summary>
141+
[Fact]
142+
public void AProductOfTwoRootsOfOneSlopeStaysTheirs()
143+
=> DifferentiatesBack("sqrt(-1 + x)*sqrt(1 + x)/(1 + x - x^2)", 1.3, 1.7, 2.4, 3.1);
110144
}
111145
}

0 commit comments

Comments
 (0)