Skip to content

Commit b53d799

Browse files
Integrate a power of x times the logarithm of a polynomial plus a root, by parts (#1955)
With D = A^2 - B^2 Q, the derivative of ln(A + B sqrt(Q)) is D'/(2 D) + (Q (A B' - A' B) + A B Q'/2)/(D sqrt(Q)), an identity of the algebra with no branch in it, so by parts what is left is a rational function and one over the root, which the rules for those answer. The search differentiated the logarithm as written, a quotient with the root in both parts, and ran past the budget. Only for a D that is not constant: the inverse hyperbolic functions, whose D is, are answered already. Rubi's 3.5: rows 136, 143, 146 and 147 go from unevaluated or timed out to solved, 0 wrong. Families 0 and 1 and samples of 2-8 otherwise identical; gate and suite pass. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent da07bd9 commit b53d799

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ It is written out again in the precision asked for now
231231
| `Rational.Create(128, 12167)`'s `EDecimal`, read at the default precision and then under 300 digits | 100 digits | 300 digits |
232232
| `sqrt(46) * 128/12167` evaluated under 300 digits, with that rational | relative error `4.7e-101` | correct to 300 digits |
233233

234+
### A power of x times the logarithm of a polynomial plus a root is integrated by parts over the conjugate
235+
236+
**Answers where there were none.** `x^3 ln(-1 + 4 x + 4 sqrt((x - 1) x))` was declined, and so were three more of Rubi's 3.5,
237+
with `x^(3/2)`, `x^(-3/2)` and `x^(-5/2)` beside the same logarithm. With `D = A^2 - B^2 Q`, the
238+
derivative of `ln(A + B sqrt(Q))` is `D'/(2 D) + (Q (A B' - A' B) + A B Q'/2)/(D sqrt(Q))` -- here `D = 8 x + 1` -- so by
239+
parts what is left is a rational function and one over the root
240+
([#233](https://github.com/asc-community/AngouriMath/issues/233)).
241+
242+
| Input | Was (2.5.0) | Now |
243+
|---|---|---|
244+
| `"x^3*ln(-1+4*x+4*sqrt((x-1)*x))".ToEntity().Integrate("x")` | `integral(...)` | `x^4 ln(...)/4` minus a polynomial, `ln(8 x + 1)` and terms over `sqrt(x^2 - x)` |
245+
234246
### The last digits of an evaluated logarithm, exponential, real power or complex power
235247

236248
**Silent, and in the hundredth digit.** `ln`, `log`, `e^x`, a real base to a real power and the

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,97 @@ private static bool IsAnInverseFunctionTimesARationalFunction(Entity expr, Entit
442442
return inverse == 1;
443443
}
444444

445+
/// <summary>
446+
/// A power of <c>x</c> times the logarithm of <c>A + B sqrt(Q)</c>, for polynomials <c>A</c>,
447+
/// <c>B</c> and <c>Q</c>, by parts, with the logarithm's derivative written over the conjugate:
448+
/// with <c>D = A^2 - B^2 Q</c>, it is
449+
/// <c>D'/(2 D) + (Q (A B' - A' B) + A B Q'/2)/(D sqrt(Q))</c>, a rational function and one over
450+
/// the root, which the rules for those answer. Rubi's 3.5,
451+
/// <c>x^3 ln(-1 + 4 x + 4 sqrt((x - 1) x))</c>, where <c>D = 8 x + 1</c>.
452+
/// https://github.com/asc-community/AngouriMath/issues/233
453+
/// </summary>
454+
/// <remarks>
455+
/// The search differentiates the logarithm as written, a quotient with the root in both
456+
/// parts, and ran past the budget on what was left. Only for a <c>D</c> that is not a
457+
/// constant: for one that is, <c>ln(x + sqrt(x^2 + 1))</c> and the other inverse hyperbolic
458+
/// functions, the derivative is the root's alone, and the rules for those answer them.
459+
/// The derivative is an identity of the algebra, <c>sqrt(Q)^2 = Q</c>, with no branch in it.
460+
/// </remarks>
461+
internal static Entity? SolveByPartsAgainstALogarithmOfARadicalSum(Entity expr, Entity.Variable x)
462+
{
463+
Entity constant = Number.Integer.One;
464+
Logf? logarithm = null;
465+
Entity power = Number.Integer.Zero;
466+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
467+
{
468+
if (!factor.ContainsNode(x))
469+
{
470+
constant = underneath ? constant / factor : constant * factor;
471+
continue;
472+
}
473+
if (!underneath && logarithm is null && factor is Logf ln && ln.Base == MathS.e)
474+
{
475+
logarithm = ln;
476+
continue;
477+
}
478+
Entity? k = factor == x ? Number.Integer.One : factor is Powf(var raisedX, Number.Rational r) && raisedX == x ? r : null;
479+
if (k is null)
480+
return null;
481+
power = underneath ? power - k : power + k;
482+
}
483+
if (logarithm is null || logarithm.Antilogarithm is not (Sumf or Minusf) || power.InnerSimplified.Evaled is not Number.Rational m
484+
|| m == Number.Integer.MinusOne)
485+
return null;
486+
// The argument as A + B sqrt(Q), term by term.
487+
Entity a = Number.Integer.Zero, b = Number.Integer.Zero;
488+
Entity? radicand = null;
489+
foreach (var term in Sumf.LinearChildren(logarithm.Antilogarithm))
490+
{
491+
Entity? root = null;
492+
Entity rest = Number.Integer.One;
493+
foreach (var factor in Mulf.LinearChildren(term))
494+
{
495+
if (root is null && factor is Powf(var q, Number.Rational half) && half == Number.Rational.Create(1, 2) && q.ContainsNode(x))
496+
root = q;
497+
else
498+
rest *= factor;
499+
}
500+
if (root is null)
501+
{
502+
a += term;
503+
continue;
504+
}
505+
if (radicand is not null && radicand != root)
506+
return null;
507+
radicand = root;
508+
b += rest;
509+
}
510+
if (radicand is null
511+
|| !TreeAnalyzer.TryGetPolynomial(a, x, out var aRead) || aRead.Keys.Any(k => k.Sign < 0)
512+
|| !TreeAnalyzer.TryGetPolynomial(b, x, out var bRead) || bRead.Keys.Any(k => k.Sign < 0)
513+
|| !TreeAnalyzer.TryGetPolynomial(radicand, x, out var qRead) || qRead.Keys.Any(k => k.Sign < 0) || qRead.Keys.All(k => k.IsZero))
514+
return null;
515+
// D = A^2 - B^2 Q, and the numerator over the root, as polynomials.
516+
Entity Written(Entity polynomial) => TreeAnalyzer.TryGetPolynomial(polynomial, x, out var read)
517+
? read.Where(monomial => monomial.Value.InnerSimplified.Evaled is not Number.Complex { IsZero: true })
518+
.Aggregate((Entity)Number.Integer.Zero, (sum, monomial) => sum + monomial.Value.InnerSimplified * (monomial.Key.IsZero ? Number.Integer.One : monomial.Key.Equals(EInteger.One) ? x : MathS.Pow(x, Number.Integer.Create(monomial.Key))))
519+
: polynomial;
520+
var d = Written(a * a - b * b * radicand);
521+
if (!TreeAnalyzer.TryGetPolynomial(d, x, out var dRead) || dRead.Keys.All(k => k.IsZero) || d == Number.Integer.Zero)
522+
return null;
523+
var (da, db, dq) = (a.Differentiate(x), b.Differentiate(x), radicand.Differentiate(x));
524+
var overTheRoot = Written(radicand * (a * db - da * b) + a * b * dq / 2);
525+
var raised = (m + 1).InnerSimplified;
526+
Entity xRaised = raised == Number.Integer.One ? x : MathS.Pow(x, raised);
527+
var rational = xRaised * Written(d.Differentiate(x)) / (2 * d);
528+
var radical = xRaised * overTheRoot / (d * MathS.Sqrt(radicand));
529+
if (Integration.ComputeAsAQuestionOfItsOwn(rational, x, integrateByParts: true) is not { } first
530+
|| Integration.ComputeAsAQuestionOfItsOwn(radical, x, integrateByParts: true) is not { } second
531+
|| first.Nodes.Concat(second.Nodes).Any(node => node is Integralf || node == MathS.NaN))
532+
return null;
533+
return (constant * (xRaised * logarithm - first - second) / raised).InnerSimplified;
534+
}
535+
445536
/// <summary>
446537
/// An integrand that is a constant multiple of <c>D'/D</c> for a sum <c>D</c> below the
447538
/// bar holding a root, decided at sampled points and answered as that multiple of

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ private static Entity WithItsPiecewiseSumsCombined(Entity expr)
787787
// A power of a quotient of linears beside whole powers of them, which is how e^(n acoth(a x))
788788
// over a power of c - a^2 c x^2 arrives folded, under t = the quotient.
789789
if ((answer = IndefiniteIntegralSolver.SolveByTheMoebiusSubstitutionOfAQuotientOfLinears(expr, x)) is { }) return answer;
790+
// And a power of x times the logarithm of A + B sqrt(Q), by parts, its derivative over the conjugate.
791+
if ((answer = IndefiniteIntegralSolver.SolveByPartsAgainstALogarithmOfARadicalSum(expr, x)) is { }) return answer;
790792
// A power of an exponential with a positive base is the exponential of the product,
791793
// exactly, and only that spelling is one the exponential rules read.
792794
if ((answer = IndefiniteIntegralSolver.SolveByFlatteningAPowerOfAnExponential(expr, x, integrateByParts)) is { }) return answer;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// A power of x times <c>ln(A + B sqrt(Q))</c>, by parts, with the logarithm's derivative written
16+
/// over the conjugate, <c>D = A^2 - B^2 Q</c>: a rational function plus one over the root. The
17+
/// search differentiated it as written and ran past its budget. Rubi's 3.5.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
19+
/// </summary>
20+
[Trait("Area", "Calculus")]
21+
public sealed class APowerTimesALogarithmOfARadicalSumIntegralTest
22+
{
23+
[Theory]
24+
[InlineData("ln(-1 + 4*x + 4*sqrt((x - 1)*x))")]
25+
[InlineData("x^3*ln(-1 + 4*x + 4*sqrt((x - 1)*x))")]
26+
[InlineData("x*ln(2*x + sqrt(x^2 + 3))")]
27+
public void ByPartsOverTheConjugate(string integrand)
28+
{
29+
var integral = integrand.ToEntity().Integrate("x");
30+
var text = integral.Stringize();
31+
Assert.DoesNotContain("integral(", text);
32+
var derivative = integral.Substitute("C", 0).Differentiate("x");
33+
var original = integrand.ToEntity();
34+
var compared = 0;
35+
// Where the root is real and the logarithm's argument positive.
36+
foreach (var at in new[] { 1.2, 1.7, 2.3, 3.1, 4.4, 6.9 })
37+
{
38+
var want = original.Substitute("x", at).EvalNumerical();
39+
var got = derivative.Substitute("x", at).EvalNumerical();
40+
if (want.IsNaN)
41+
continue;
42+
compared++;
43+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
44+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
45+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
46+
}
47+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)