Skip to content

Commit a10be86

Browse files
A quotient by the square of a base is integrated by parts against the base's reciprocal (#1759)
x^2/(a x cos(a x) - sin(a x))^2 was declined, with the rest of Rubi's 4.7.7 over the square of a x cos(a x) - sin(a x) or of cos(a x) + a x sin(a x). A quotient N/(M v^2) by the square of a base v is g v'/v^2 with g = N/(M v'), and v'/v^2 is the derivative of -1/v, so by parts its integral is -g/v plus that of g'/v. Taken where the base's derivative is one term and v cancels from g'/v; the tower ansatz is asked first, and its answer kept where it has one. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 4c3aa3b commit a10be86

4 files changed

Lines changed: 145 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,21 @@ but `sqrt(q)^2 = q` ([#1598](https://github.com/asc-community/AngouriMath/issues
838838
| `"1/sqrt(-x^2 + i)".Integrate("x")` | `-arcsin(-2x/sqrt(4i)) + C` | `ln(-2x + 2i sqrt(-x^2 + i))/i + C`, an antiderivative as before, in the logarithm |
839839
| `"1/(sqrt(x)*sqrt(1 + i*x))".Integrate("x")`, and Rubi 4.3.2.1's `sqrt(tan(c + d x))/(a + i a tan(c + d x))^(5/2)` | `integral(...)` | the antiderivative |
840840

841+
### A quotient by the square of a base is integrated by parts against the base's reciprocal
842+
843+
**Answers where there were none.** `x^2/(a x cos(a x) - sin(a x))^2` was declined, with the rest of
844+
Rubi's 4.7.7 over the square of `a x cos(a x) - sin(a x)` or `cos(a x) + a x sin(a x)`. A quotient
845+
`N/(M v^2)` by the square of a base with a function of `x` in it is `g v'/v^2` with `g = N/(M v')`, and
846+
`v'/v^2` is the derivative of `-1/v`: its integral is `-g/v` plus the integral of `g'/v`, taken where
847+
the base's derivative is one term and `v` cancels from `g'/v`
848+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
849+
850+
| Input | Was (2.5.0) | Now |
851+
|---|---|---|
852+
| `"x^2/(a*x*cos(a*x) - sin(a*x))^2".ToEntity().Integrate("x")` | `integral(...)` | `x/(a^2 sin(a x) (a x cos(a x) - sin(a x))) - cot(a x)/a^3` |
853+
| `"sin(a*x)^2/(a*x*cos(a*x) - sin(a*x))^2".ToEntity().Integrate("x")` | `integral(...)` | a quotient by `a x cos(a x) - sin(a x)` and `1/(a^2 x)` |
854+
| `"sin(a*x)^3/(x*(a*x*cos(a*x) - sin(a*x))^2)".ToEntity().Integrate("x")` | `integral(...)` | the same with the sine integral `Si(a x)` |
855+
841856
### Trigonometric functions of multiples of one linear argument are written in it
842857

843858
`csc(a + b x) csc(2a + 2b x)^2` was left unevaluated, and `csc(1 + x) csc(2 + 2x)^2` ran twenty

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,81 @@ private static bool IsADerivativeOfAFactorBelowTheBar(Entity expr, Entity.Variab
367367
return Integration.ComputeAsTheSameQuestion(written, x, integrateByParts);
368368
}
369369

370+
/// <summary>
371+
/// A quotient by the square of a base, by parts against the base's reciprocal, where the
372+
/// base's derivative makes the rest exact: <c>N/(M v^2)</c> is <c>g v'/v^2</c> with
373+
/// <c>g = N/(M v')</c>, and <c>v'/v^2</c> is the derivative of <c>-1/v</c>, so the integral
374+
/// is <c>-g/v</c> plus the integral of <c>g'/v</c>; taken only where <c>v</c> cancels from
375+
/// that, with the functions of <paramref name="x"/> in it taken for indeterminates.
376+
/// </summary>
377+
/// <remarks>
378+
/// <c>x^2/(a x cos(a x) - sin(a x))^2</c> was declined, with the rest of Rubi's 4.7.7 by the
379+
/// square of <c>a x cos(a x) - sin(a x)</c> or <c>cos(a x) + a x sin(a x)</c>: the base's
380+
/// derivative is <c>-a^2 x sin(a x)</c>, so <c>g</c> is <c>-x csc(a x)/a^2</c>, and
381+
/// <c>g'</c> is <c>a^2 v/(a^2 sin(a x))^2</c>, whose integral is <c>-cot(a x)/a^3</c>. Only
382+
/// for a base with a function of <paramref name="x"/> in it that is not a polynomial, since
383+
/// a quotient of polynomials is the partial fractions', and only where <c>v</c> cancels,
384+
/// since otherwise the remainder is the harder question.
385+
/// https://github.com/asc-community/AngouriMath/issues/718
386+
/// </remarks>
387+
internal static Entity? SolveByPartsAgainstTheReciprocalOfASquaredBase(Entity expr, Entity.Variable x)
388+
{
389+
if (!TryReadAsQuotient(expr, out var numerator, out var denominator) || !denominator.ContainsNode(x))
390+
return null;
391+
Entity? squared = null;
392+
Entity rest = Number.Integer.One;
393+
foreach (var factor in Mulf.LinearChildren(denominator))
394+
if (squared is null && factor is Powf(var @base, Number.Integer { EInteger: var power }) && power.Equals(EInteger.FromInt32(2))
395+
&& @base is Sumf or Minusf && @base.ContainsNode(x)
396+
&& @base.Nodes.Any(node => node.ContainsNode(x) && IsAFunctionOfTheVariable(node))
397+
// The variable outside the functions as well, as in `a x cos(a x) - sin(a x)`: a
398+
// base that is a function of `sin(x)` alone, `(a + b sin(x))^2`, is the
399+
// substitutions' and the reductions', and was answered in a second where this
400+
// ran past the budget.
401+
&& @base.Replace(node => IsAFunctionOfTheVariable(node) ? Number.Integer.One : node).ContainsNode(x))
402+
squared = @base;
403+
else
404+
rest = rest * factor;
405+
if (squared is null)
406+
return null;
407+
// A power of x every term of the base has goes to the rest: `x^2 v^2` arrives as
408+
// `(a x^2 cos(a x) - x sin(a x))^2`, the square of `x v`.
409+
var lowest = Sumf.LinearChildren(squared).Select(term => Mulf.LinearChildren(term)
410+
.Select(factor => factor == x ? 1 : factor is Powf(var b, Number.Integer { EInteger: var n }) && b == x && n.CanFitInInt32() ? n.ToInt32Unchecked() : 0)
411+
.Sum()).Min();
412+
if (lowest > 0)
413+
{
414+
squared = (squared / MathS.Pow(x, lowest)).Expand().InnerSimplified;
415+
rest = rest * MathS.Pow(x, 2 * lowest);
416+
}
417+
// Expanded, since the derivative of `a x cos(a x)` and of `sin(a x)` each have an
418+
// `a cos(a x)`, which cancel only once multiplied out.
419+
var derivative = squared.Differentiate(x).Expand().InnerSimplified;
420+
// A derivative of one term, so that what it leaves below the bar of the remainder is
421+
// no new square of a sum to take apart the same way.
422+
if (TreeAnalyzer.IsZero(derivative) || derivative is Sumf or Minusf)
423+
return null;
424+
// The ring's answer first, where it has one: `x^2/(x cos(x) - sin(x))^2` is
425+
// `(x sin(x) + cos(x))/(x cos(x) - sin(x))` there, continuous across the zeros of the
426+
// base's derivative, where `-g/v` and the remainder each have a pole that cancels.
427+
if (SolveByTrigonometricTowerAnsatz(expr, x) is { } fromTheRing)
428+
return fromTheRing;
429+
var (gTop, gBottom) = CancelledWithFunctionsAsIndeterminates(numerator, rest * derivative, x, expr);
430+
// g' over v, cancelled: (A' B - A B')/(B^2 v) for g = A/B.
431+
var above = (gTop.Differentiate(x) * gBottom - gTop * gBottom.Differentiate(x)).InnerSimplified;
432+
var (rTop, rBottom) = CancelledWithFunctionsAsIndeterminates(above, MathS.Pow(gBottom, 2) * squared, x, expr);
433+
if (rBottom.Nodes.Any(node => node == squared) || !rTop.ContainsNode(x) && !rBottom.ContainsNode(x) && TreeAnalyzer.IsZero(rTop))
434+
return null;
435+
var remainder = Functions.PartialFractions.Bare(rTop / rBottom);
436+
if (Integration.ComputeIndefiniteIntegral(remainder, x) is not { } integral || integral.Nodes.Any(node => node is Integralf))
437+
return null;
438+
return -gTop / (gBottom * squared) + integral;
439+
}
440+
441+
/// <summary>Whether <paramref name="node"/> is a trigonometric function, a logarithm or an exponential.</summary>
442+
private static bool IsAFunctionOfTheVariable(Entity node)
443+
=> node is Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Logf or Powf(Number, _);
444+
370445
/// <summary>Whether <paramref name="expr"/> holds a fractional power of something in <paramref name="x"/>.</summary>
371446
private static bool HasARadicalOf(Entity expr, Entity.Variable x)
372447
=> expr.Nodes.Any(node => node is Powf(var @base, Number.Rational power) && power is not Number.Integer && @base.ContainsNode(x));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
784784
// is `x^2/(L^2 sqrt(q))` cancelled, and was five terms over `L^5 (1 + L sqrt(q))`
785785
// each searched for seconds when the reduction reached it first.
786786
if ((answer = IndefiniteIntegralSolver.SolveByCancellingWithFunctionsAsIndeterminates(expr, x, integrateByParts)) is { }) return answer;
787+
// A quotient by the square of a base with a function in it, by parts against the base's
788+
// reciprocal where the base's derivative makes the rest exact.
789+
if ((answer = IndefiniteIntegralSolver.SolveByPartsAgainstTheReciprocalOfASquaredBase(expr, x)) is { }) return answer;
787790
// A polynomial over a power of a linear and something that is not a polynomial,
788791
// the polynomial written in powers of the linear at its root: the part the power
789792
// divides goes over the rest alone. Before the split, so that the whole polynomial
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 quotient by the square of a base with a function in it, by parts against the base's
16+
/// reciprocal: <c>N/(M v^2)</c> is <c>g v'/v^2</c> with <c>g = N/(M v')</c>, whose integral is
17+
/// <c>-g/v</c> plus that of <c>g'/v</c>, which the base cancels from where its derivative is
18+
/// one term. <c>x^2/(a x cos(a x) - sin(a x))^2</c> was declined; Rubi's 4.7.7.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
20+
/// </summary>
21+
/// <remarks>
22+
/// Checked by differentiating back with <c>a = 1.3</c> on both sides of zero.
23+
/// </remarks>
24+
[Trait("Area", "Calculus")]
25+
public sealed class SquaredBaseByPartsIntegralTest
26+
{
27+
[Theory]
28+
[InlineData("x^2/(a*x*cos(a*x) - sin(a*x))^2")]
29+
[InlineData("sin(a*x)^2/(a*x*cos(a*x) - sin(a*x))^2")]
30+
[InlineData("sin(a*x)^3/(x*(a*x*cos(a*x) - sin(a*x))^2)")]
31+
[InlineData("sin(a*x)^4/(x^2*(a*x*cos(a*x) - sin(a*x))^2)")]
32+
[InlineData("x^2/(cos(a*x) + a*x*sin(a*x))^2")]
33+
[InlineData("cos(a*x)^2/(cos(a*x) + a*x*sin(a*x))^2")]
34+
[InlineData("cos(a*x)^3/(x*(cos(a*x) + a*x*sin(a*x))^2)")]
35+
public void IsIntegratedAgainstTheReciprocalOfTheBase(string integrand)
36+
{
37+
var integral = integrand.ToEntity().Integrate("x");
38+
Assert.DoesNotContain("integral(", integral.Stringize());
39+
Entity Pinned(Entity e) => e.Substitute("a", 1.3);
40+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
41+
var original = Pinned(integrand.ToEntity());
42+
foreach (var at in new[] { -2.3, -1.1, -0.4, 0.4, 1.1, 2.3 })
43+
{
44+
var want = original.Substitute("x", at).EvalNumerical();
45+
var got = derivative.Substitute("x", at).EvalNumerical();
46+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
47+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
48+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)