Skip to content

Commit 763f7ba

Browse files
A cosine and a sine over a power of another such sum are integrated through its derivative (#1763)
sin(x)/(a + b cos(x) + c sin(x)) was declined, with the rest of Rubi's 4.7.7 quotients of A + B cos(x) + C sin(x) by a whole power of a + b cos(x) + c sin(x); master answered the squares and cubes after twenty seconds or so, and 1/(a + b cos(x) + c sin(x))^2 with coefficients past the fiftieth degree. With S the base, the numerator is alpha S + beta S' + gamma, and beta S'/S^n integrates to a logarithm or a power of S; for the rest, d/dy (T S^m) with T = -S' writes int S^(-k) through int S^(1-k) and int S^(2-k), down to int 1/S, which the half-angle tangent answers as before. 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 bf5d1a1 commit 763f7ba

4 files changed

Lines changed: 225 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,21 @@ interval between the poles of the tangent, as the substitution's are
707707
| `"(A + B*tan(x))/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)` | the same |
708708
| `"1/(a + i*a*tan(c + d*x))^3".ToEntity().Integrate("x")` | `integral(...)` | the same in `tan(c + d x)` |
709709

710+
### A cosine and a sine over a power of another such sum are integrated through its derivative
711+
712+
**Answers where there were none.** `sin(x)/(a + b cos(x) + c sin(x))` was declined, with the rest of
713+
Rubi's 4.7.7 quotients of `A + B cos(x) + C sin(x)` by a whole power of `a + b cos(x) + c sin(x)`,
714+
the squares and cubes after searches past the budget. The numerator is a multiple of the
715+
denominator, one of its derivative and a constant, and each power of the denominator is brought
716+
down to its reciprocal by an identity of derivatives; the reciprocal is answered as before
717+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
718+
719+
| Input | Was (2.5.0) | Now |
720+
|---|---|---|
721+
| `"sin(x)/(a + b*cos(x) + c*sin(x))".ToEntity().Integrate("x")` | `integral(...)` | `c x/(b^2 + c^2) - b ln(a + b cos(x) + c sin(x))/(b^2 + c^2)` and a multiple of the reciprocal's antiderivative |
722+
| `"1/(a + b*cos(x) + c*sin(x))^2".ToEntity().Integrate("x")` | `integral(...)` | `(b sin(x) - c cos(x))/(a + b cos(x) + c sin(x))` less `a` times the reciprocal's antiderivative, over `b^2 + c^2 - a^2` |
723+
| `"(k + p*cos(x) + q*sin(x))/(a + b*cos(x) + c*sin(x))^3".ToEntity().Integrate("x")` | `integral(...)` | powers of the denominator and the reciprocal's antiderivative |
724+
710725
### An integrand with the imaginary unit in it is not simplified by the rule that scales the variable
711726

712727
**Answers where there were none.** The rule that scales the variable by the integrand's one symbol

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

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8885,6 +8885,161 @@ private static (Entity Coefficient, Entity Degree)? TheMonomial(Entity expr, Ent
88858885
: null;
88868886
}
88878887

8888+
/// <summary>
8889+
/// <c>(A + B cos(y) + C sin(y))/(a + b cos(y) + c sin(y))^n</c>, for a whole <c>n</c>, by
8890+
/// the denominator, its derivative and a constant: the numerator is
8891+
/// <c>alpha S + beta S' + gamma</c> for <c>S</c> the base, and each power of <c>S</c>
8892+
/// comes down to <c>1/S</c>, which the half-angle tangent answers.
8893+
/// </summary>
8894+
/// <remarks>
8895+
/// <para>
8896+
/// With <c>S' = c cos(y) - b sin(y)</c>, matching the cosine, the sine and the constant
8897+
/// gives <c>alpha = (B b + C c)/(b^2 + c^2)</c>, <c>beta = (B c - C b)/(b^2 + c^2)</c> and
8898+
/// <c>gamma = A - a alpha</c>; <c>beta S'/S^n</c> integrates to a logarithm or a power of
8899+
/// <c>S</c>. For the powers, with <c>T = b sin(y) - c cos(y) = -S'</c>,
8900+
/// <c>T^2 = b^2 + c^2 - (S - a)^2</c>, from which
8901+
/// </para>
8902+
/// <code>
8903+
/// d/dy (T S^m) = (1 + m) S^(m+1) - a (1 + 2m) S^m + m (a^2 - b^2 - c^2) S^(m-1)
8904+
/// </code>
8905+
/// <para>
8906+
/// which for <c>m = 1 - k</c> writes <c>int S^(-k)</c> through <c>int S^(1-k)</c> and
8907+
/// <c>int S^(2-k)</c>, down to <c>int 1/S</c> and <c>y</c>. Rubi's
8908+
/// <c>(A + B cos(x) + C sin(x))/(a + b cos(x) + c sin(x))</c> and its kin were declined:
8909+
/// under the half-angle tangent each is a rational function over a quadratic with every
8910+
/// coefficient a symbol, beside <c>1 + t^2</c>, and the square of the denominator was
8911+
/// answered with coefficients past the fiftieth degree in them.
8912+
/// </para>
8913+
/// <para>
8914+
/// Exact, as the answer for <c>1/S</c> it is built on: a linear combination and an
8915+
/// identity of derivatives. <c>a^2 = b^2 + c^2</c>, where the base is a square of the
8916+
/// half angle and the recurrence divides by zero, is declined here, as is
8917+
/// <c>b^2 + c^2 = 0</c>, where the base is <c>a + b e^(±i y)</c>; both decided. A constant
8918+
/// numerator over the first power is <c>1/S</c> itself, left to the rules that answer it.
8919+
/// https://github.com/asc-community/AngouriMath/issues/718
8920+
/// </para>
8921+
/// </remarks>
8922+
internal static Entity? SolveACosineAndASineOverAPowerOfAnother(Entity expr, Entity.Variable x, bool integrateByParts)
8923+
{
8924+
if (!TryReadAsQuotient(expr, out var numerator, out var denominator))
8925+
return null;
8926+
var (@base, power) = denominator is Powf(var inner, var exponent) && exponent.Evaled is Number.Integer { EInteger: var whole }
8927+
? (inner, whole)
8928+
: (denominator, EInteger.One);
8929+
if (power.Sign <= 0 || power.CompareTo(EInteger.FromInt32(MaximumPowerOfACosineAndASine)) > 0)
8930+
return null;
8931+
Entity? argument = null;
8932+
bool TryRead(Entity sum, out Entity constant, out Entity cosine, out Entity sine)
8933+
{
8934+
constant = cosine = sine = Number.Integer.Zero;
8935+
foreach (var term in Sumf.LinearChildren(sum))
8936+
{
8937+
if (!term.ContainsNode(x))
8938+
{
8939+
constant = constant == Number.Integer.Zero ? term : constant + term;
8940+
continue;
8941+
}
8942+
Entity coefficient = Number.Integer.One;
8943+
Entity? function = null;
8944+
foreach (var factor in Mulf.LinearChildren(term))
8945+
if (!factor.ContainsNode(x))
8946+
coefficient = coefficient == Number.Integer.One ? factor : coefficient * factor;
8947+
else if (function is null && factor is Sinf or Cosf)
8948+
function = factor;
8949+
else
8950+
return false;
8951+
var inner = function!.DirectChildren.First();
8952+
if (argument is not null && inner != argument)
8953+
return false;
8954+
argument = inner;
8955+
if (function is Cosf)
8956+
{
8957+
if (cosine != Number.Integer.Zero) return false;
8958+
cosine = coefficient;
8959+
}
8960+
else
8961+
{
8962+
if (sine != Number.Integer.Zero) return false;
8963+
sine = coefficient;
8964+
}
8965+
}
8966+
return true;
8967+
}
8968+
// The base with a constant, a cosine and a sine: with one of the two functions alone the
8969+
// half angle reads it, and without the constant it is one cosine turned by a phase,
8970+
// which the rotation answers.
8971+
if (!TryRead(@base, out var a, out var b, out var c) || a == Number.Integer.Zero || b == Number.Integer.Zero
8972+
|| c == Number.Integer.Zero || !TryRead(numerator, out var bigA, out var bigB, out var bigC) || argument is null)
8973+
return null;
8974+
if (power.Equals(EInteger.One) && bigB == Number.Integer.Zero && bigC == Number.Integer.Zero)
8975+
return null;
8976+
if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _) || rate.ContainsNode(x)
8977+
|| rate.Evaled is Number.Complex { IsZero: true })
8978+
return null;
8979+
static bool IsZero(Entity value)
8980+
{
8981+
var simplified = value.InnerSimplified;
8982+
return simplified.Evaled is Number.Complex { IsZero: true }
8983+
|| simplified.Vars.Any() && Functions.PartialFractions.Bare(simplified.Simplify()).Evaled is Number.Complex { IsZero: true };
8984+
}
8985+
var squares = MathS.Sqr(b) + MathS.Sqr(c);
8986+
var discriminant = MathS.Sqr(a) - squares;
8987+
if (IsZero(squares) || IsZero(discriminant))
8988+
return null;
8989+
8990+
var s = @base;
8991+
var t = b * MathS.Sin(argument) - c * MathS.Cos(argument);
8992+
var alpha = (bigB * b + bigC * c) / squares;
8993+
var beta = (bigB * c - bigC * b) / squares;
8994+
var gamma = bigA - a * alpha;
8995+
// int S^(-k) dx, from int 1/S as the integrator answers it and x.
8996+
Entity? reciprocal = null;
8997+
var computed = new Dictionary<int, Entity>();
8998+
Entity? Reciprocal(int k)
8999+
{
9000+
if (k == 0)
9001+
return x;
9002+
if (computed.TryGetValue(k, out var known))
9003+
return known;
9004+
Entity? result;
9005+
if (k == 1)
9006+
result = reciprocal ??= Integration.ComputeIndefiniteIntegral(1 / s, x, integrateByParts);
9007+
else
9008+
{
9009+
if (Reciprocal(k - 1) is not { } previous || Reciprocal(k - 2) is not { } beforeThat)
9010+
return null;
9011+
result = (t * MathS.Pow(s, 1 - k) / rate - (2 - k) * beforeThat + a * (3 - 2 * k) * previous)
9012+
/ ((1 - k) * discriminant);
9013+
}
9014+
if (result is not null)
9015+
computed[k] = result;
9016+
return result;
9017+
}
9018+
var n = power.ToInt32Checked();
9019+
Entity answer = Number.Integer.Zero;
9020+
if (alpha != Number.Integer.Zero && !IsZero(alpha))
9021+
{
9022+
if (Reciprocal(n - 1) is not { } lower)
9023+
return null;
9024+
answer += alpha * lower;
9025+
}
9026+
if (beta != Number.Integer.Zero && !IsZero(beta))
9027+
answer += beta / rate * (n == 1 ? MathS.Ln(s) : MathS.Pow(s, 1 - n) / (1 - n));
9028+
if (gamma != Number.Integer.Zero && !IsZero(gamma))
9029+
{
9030+
if (Reciprocal(n) is not { } same)
9031+
return null;
9032+
answer += gamma * same;
9033+
}
9034+
return answer == Number.Integer.Zero ? null : answer;
9035+
}
9036+
9037+
/// <summary>
9038+
/// The largest power of the denominator <see cref="SolveACosineAndASineOverAPowerOfAnother"/>
9039+
/// steps through: each step is a term of the answer.
9040+
/// </summary>
9041+
private const int MaximumPowerOfACosineAndASine = 6;
9042+
88889043
/// <summary>
88899044
/// A sum of a cosine and a sine of one argument turned into one cosine:
88909045
/// <c>a cos(y) + b sin(y)</c> is <c>R cos(u)</c> for <c>R = sqrt(a^2 + b^2)</c> and

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
877877
// Several trigonometric arguments that are multiples of one linear with an offset or a
878878
// symbolic slope, written in that linear: before the substitution search, which reads
879879
// each function on its own.
880+
// A cosine and a sine over a power of another such sum, through the denominator, its
881+
// derivative and a constant, down to the reciprocal of the base: before the substitution
882+
// search, which reads the quotient term by term.
883+
if ((answer = IndefiniteIntegralSolver.SolveACosineAndASineOverAPowerOfAnother(expr, x, integrateByParts)) is { }) return answer;
880884
if ((answer = IndefiniteIntegralSolver.SolveByWritingMultiplesOfOneLinearArgument(expr, x, integrateByParts)) is { }) return answer;
881885
// A constant out of a fractional power of a trigonometric factor: `sqrt(b sec(x))` is
882886
// `sqrt(b) sqrt(sec(x))` for a positive `b`, which meets the other powers of the
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/// <c>(A + B cos(x) + C sin(x))/(a + b cos(x) + c sin(x))^n</c> through the denominator, its
16+
/// derivative and a constant, each power of the denominator brought down to its reciprocal.
17+
/// Rubi's 4.7.7.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
19+
/// </summary>
20+
/// <remarks>
21+
/// Checked by differentiating back with the symbols pinned, inside <c>(-pi, pi)</c>, where the
22+
/// half-angle tangent the reciprocal is answered in is continuous.
23+
/// </remarks>
24+
[Trait("Area", "Calculus")]
25+
public sealed class CosineAndSineOverAPowerOfAnotherIntegralTest
26+
{
27+
[Theory]
28+
[InlineData("sin(x)/(a + b*cos(x) + c*sin(x))")]
29+
[InlineData("(k + q*cos(x))/(a + b*cos(x) + c*sin(x))")]
30+
[InlineData("(k + p*cos(x) + q*sin(x))/(a + b*cos(x) + c*sin(x))^2")]
31+
[InlineData("(p*cos(x) + q*sin(x))/(a + b*cos(x) + c*sin(x))^3")]
32+
[InlineData("1/(a + b*cos(x) + c*sin(x))^2")]
33+
public void IsWrittenThroughItsDenominator(string integrand)
34+
{
35+
var integral = integrand.ToEntity().Integrate("x");
36+
Assert.DoesNotContain("integral(", integral.Stringize());
37+
Entity Pinned(Entity e) => e.Substitute("a", 2.3).Substitute("b", 0.9).Substitute("c", 0.7)
38+
.Substitute("k", 1.1).Substitute("p", 0.6).Substitute("q", 0.8);
39+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
40+
var original = Pinned(integrand.ToEntity());
41+
foreach (var at in new[] { -2.6, -1.6, -0.4, 0.4, 1.4, 2.0 })
42+
{
43+
var want = original.Substitute("x", at).EvalNumerical();
44+
var got = derivative.Substitute("x", at).EvalNumerical();
45+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
46+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
47+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)