Skip to content

Commit 9f2cc36

Browse files
A power of csc - sin or sec - cos is integrated as sine and cosine powers (#1760)
(csc(x) - sin(x))^(5/2) was declined, with the rest of Rubi's 4.7.7 to a power that is not whole of csc(x) - sin(x) or sec(x) - cos(x), either way up. csc(z) - sin(z) is cos(z)^2/sin(z) and sec(z) - cos(z) is sin(z)^2/cos(z); written so, the power is a product of powers of the sine and cosine, which the rule for those reads through the tangent, now reading a product and quotient of whole powers of the six functions inside a power. A product counts as not whole only where some function's power in it is not, so that (a csc(z)^2)^(-7/2) stays with the rule for an even power under a root. 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 a10be86 commit 9f2cc36

4 files changed

Lines changed: 178 additions & 24 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,20 @@ antiderivative on each side of it, as Rubi's is
570570
| `"1/((2 + 3*x^2)^(1/4)*(4 + 3*x^2))".ToEntity().Integrate("x")` | `integral(...)` | the same in `(2 + 3 x^2)^(1/4)` |
571571
| `"1/((-2 + 3*x^2)*(-1 + 3*x^2)^(1/4))".ToEntity().Integrate("x")` | `integral(...)` | `-(arctan(u) + artanh(u))/(2 sqrt(6))`, `u = sqrt(3) x/(sqrt(2) (-1 + 3 x^2)^(1/4))` |
572572

573+
### A power of `csc(x) - sin(x)` or `sec(x) - cos(x)` is integrated as sine and cosine powers
574+
575+
**Answers where there were none.** `(csc(x) - sin(x))^(5/2)` was declined, with the rest of Rubi's
576+
4.7.7 to a power that is not whole of either difference, either way up. `csc(z) - sin(z)` is
577+
`cos(z)^2/sin(z)` and `sec(z) - cos(z)` is `sin(z)^2/cos(z)`; written so, the power is a product of
578+
powers of the sine and cosine, which are integrated through the tangent, a quotient of whole powers
579+
of the six functions read inside a power as it is
580+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
581+
582+
| Input | Was (2.5.0) | Now |
583+
|---|---|---|
584+
| `"(csc(x) - sin(x))^(5/2)".ToEntity().Integrate("x")` | `integral(...)` | powers of `sin(x)` times `(cos(x)^2/sin(x))^(5/2)` |
585+
| `"1/(sec(x) - cos(x))^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | a root of `cos(x)` with a logarithm and an arctangent, times `(sin(x)^2/cos(x))^(-3/2)` |
586+
573587
### A product of powers of the trigonometric functions, some not whole, is integrated as sine and cosine powers
574588

575589
**Answers where there were none.** `(a sin(x))^(5/2) sqrt(b sec(x))` was declined, and so were

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

Lines changed: 110 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9090,33 +9090,38 @@ private static bool TryReadAHomogeneousTrigonometricPolynomial(
90909090
var (@base, power) = factor is Powf(var b, var p) && p.Evaled is Number.Rational r
90919091
? (b, r)
90929092
: (factor, (Number.Rational)Number.Integer.One);
9093-
// A constant multiple of a function, and a whole power of one inside the power:
9094-
// `(b tan(z)^3)^(3/2)`.
9095-
if (@base is Mulf(var left, var right))
9096-
@base = !left.ContainsNode(x) ? right : !right.ContainsNode(x) ? left : @base;
9097-
if (@base is Powf(var inner, Number.Integer innerPower))
9098-
(@base, power) = (inner, (Number.Rational)(power * innerPower));
90999093
if (underneath)
91009094
power = (Number.Rational)(-power);
9101-
if (power is not Number.Integer)
9102-
notWhole = true;
9103-
// The powers of the sine and of the cosine each function is.
9104-
(Entity Of, int OnSine, int OnCosine)? read = @base switch
9105-
{
9106-
Sinf(var of) => (of, 1, 0),
9107-
Cosf(var of) => (of, 0, 1),
9108-
Tanf(var of) => (of, 1, -1),
9109-
Cotanf(var of) => (of, -1, 1),
9110-
Secantf(var of) => (of, 0, -1),
9111-
Cosecantf(var of) => (of, -1, 0),
9112-
_ => null
9113-
};
9114-
if (read is null || argument is not null && argument != read.Value.Of)
9095+
// Inside the power, a constant multiple of a product and quotient of whole powers of
9096+
// the functions: `(b tan(z)^3)^(3/2)`, and `(cos(z)^2/sin(z))^(5/2)`, which is how
9097+
// `(csc(z) - sin(z))^(5/2)` is written. Not whole where some function's power is not:
9098+
// `(a csc(z)^2)^(-7/2)` is a whole power of the modulus of the cosecant, which the rule for
9099+
// an even power under a root answers with its sign.
9100+
if (!TryReadAMonomialInTheTrigonometricFunctions(@base, x, out var parts))
91159101
return null;
9116-
var (functionOf, onSine, onCosine) = read.Value;
9117-
argument = functionOf;
9118-
sine = (Number.Rational)(sine + power * onSine);
9119-
cosine = (Number.Rational)(cosine + power * onCosine);
9102+
foreach (var (function, times) in parts)
9103+
{
9104+
// The powers of the sine and of the cosine each function is.
9105+
(Entity Of, int OnSine, int OnCosine)? read = function switch
9106+
{
9107+
Sinf(var of) => (of, 1, 0),
9108+
Cosf(var of) => (of, 0, 1),
9109+
Tanf(var of) => (of, 1, -1),
9110+
Cotanf(var of) => (of, -1, 1),
9111+
Secantf(var of) => (of, 0, -1),
9112+
Cosecantf(var of) => (of, -1, 0),
9113+
_ => null
9114+
};
9115+
if (read is null || argument is not null && argument != read.Value.Of)
9116+
return null;
9117+
var (functionOf, onSine, onCosine) = read.Value;
9118+
argument = functionOf;
9119+
var weight = (Number.Rational)(power * Number.Integer.Create(times));
9120+
if (weight is not Number.Integer)
9121+
notWhole = true;
9122+
sine = (Number.Rational)(sine + weight * onSine);
9123+
cosine = (Number.Rational)(cosine + weight * onCosine);
9124+
}
91209125
}
91219126
if (argument is null || !notWhole
91229127
|| !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out _) || TreeAnalyzer.IsZero(slope))
@@ -9150,6 +9155,87 @@ private static bool TryReadAHomogeneousTrigonometricPolynomial(
91509155
}
91519156

91529157

9158+
/// <summary>
9159+
/// A difference of a trigonometric function and its reciprocal's partner written as the
9160+
/// quotient it is: <c>csc(z) - sin(z)</c> is <c>cos(z)^2/sin(z)</c> and <c>sec(z) - cos(z)</c>
9161+
/// is <c>sin(z)^2/cos(z)</c>, so that a power of either is a product of powers of the sine
9162+
/// and cosine; the integrand so written is asked as the same question.
9163+
/// </summary>
9164+
/// <remarks>
9165+
/// <c>(csc(x) - sin(x))^(5/2)</c> was declined, with the rest of Rubi's 4.7.7 to a power that
9166+
/// is not whole of either difference, either way up. As the quotient it is a product of
9167+
/// powers of the sine and cosine, which the rule beside it reads through the tangent.
9168+
/// https://github.com/asc-community/AngouriMath/issues/718
9169+
/// </remarks>
9170+
internal static Entity? SolveByWritingADifferenceOfReciprocalFunctionsAsAQuotient(Entity expr, Entity.Variable x, bool integrateByParts)
9171+
{
9172+
var rewritten = expr.Replace(node =>
9173+
{
9174+
if (!node.ContainsNode(x))
9175+
return node;
9176+
// Each term a signed function: `csc(z)`, `-sin(z)`, `-1 * cos(z)`.
9177+
(Entity Function, int Sign) Signed(Entity term, int sign) => term switch
9178+
{
9179+
Mulf(Number.Integer { EInteger: var one }, var f) when one.Equals(EInteger.FromInt32(-1)) => (f, -sign),
9180+
Mulf(var f, Number.Integer { EInteger: var one }) when one.Equals(EInteger.FromInt32(-1)) => (f, -sign),
9181+
_ => (term, sign)
9182+
};
9183+
List<(Entity Function, int Sign)>? signed = node switch
9184+
{
9185+
Minusf(var minuend, var subtrahend) => new() { Signed(minuend, 1), Signed(subtrahend, -1) },
9186+
Sumf(var left, var right) when left is not (Sumf or Minusf) && right is not (Sumf or Minusf) => new() { Signed(left, 1), Signed(right, 1) },
9187+
_ => null
9188+
};
9189+
if (signed is null)
9190+
return node;
9191+
var (first, second) = (signed[0], signed[1]);
9192+
if (first.Sign == second.Sign)
9193+
return node;
9194+
var (positive, negative) = first.Sign > 0 ? (first.Function, second.Function) : (second.Function, first.Function);
9195+
return (positive, negative) switch
9196+
{
9197+
(Cosecantf(var a), Sinf(var b)) when a == b => MathS.Pow(MathS.Cos(a), 2) / MathS.Sin(a),
9198+
(Sinf(var a), Cosecantf(var b)) when a == b => -MathS.Pow(MathS.Cos(a), 2) / MathS.Sin(a),
9199+
(Secantf(var a), Cosf(var b)) when a == b => MathS.Pow(MathS.Sin(a), 2) / MathS.Cos(a),
9200+
(Cosf(var a), Secantf(var b)) when a == b => -MathS.Pow(MathS.Sin(a), 2) / MathS.Cos(a),
9201+
_ => node
9202+
};
9203+
});
9204+
return rewritten == expr ? null : Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts);
9205+
}
9206+
9207+
/// <summary>
9208+
/// <paramref name="expr"/> read as a constant times a product of whole powers of the six
9209+
/// trigonometric functions, each with how many times it divides or multiplies; false where
9210+
/// anything else holds <paramref name="x"/>.
9211+
/// </summary>
9212+
private static bool TryReadAMonomialInTheTrigonometricFunctions(Entity expr, Entity.Variable x, out List<(Entity Function, int Times)> parts)
9213+
{
9214+
var read = new List<(Entity Function, int Times)>();
9215+
parts = read;
9216+
return Read(expr, 1);
9217+
9218+
bool Read(Entity e, int times)
9219+
{
9220+
switch (e)
9221+
{
9222+
case var constant when !constant.ContainsNode(x):
9223+
return true;
9224+
case Mulf(var left, var right):
9225+
return Read(left, times) && Read(right, times);
9226+
case Divf(var above, var below):
9227+
return Read(above, times) && Read(below, -times);
9228+
case Powf(var inner, Number.Integer { EInteger: var power }) when power.CanFitInInt32():
9229+
return Read(inner, times * power.ToInt32Unchecked());
9230+
case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf:
9231+
read.Add((e, times));
9232+
return true;
9233+
default:
9234+
return false;
9235+
}
9236+
}
9237+
}
9238+
91539239
/// <summary>
91549240
/// <paramref name="factors"/> with a whole power of a product read as the product of the
91559241
/// powers, a negative one on the other side of the bar: <c>1/(cos(x)^(7/2) sqrt(sin(x)))</c>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
917917
// A power of the cotangent that is not whole beside the tangent, written as the
918918
// tangent's with the constant that takes in front, which the substitution below reads.
919919
if ((answer = IndefiniteIntegralSolver.SolveByWritingAPowerOfTheCotangentInTheTangent(expr, x, integrateByParts)) is { }) return answer;
920+
// A difference of a function and its reciprocal's partner, `csc(z) - sin(z)`, as the
921+
// quotient of powers it is, for the rule after it.
922+
if ((answer = IndefiniteIntegralSolver.SolveByWritingADifferenceOfReciprocalFunctionsAsAQuotient(expr, x, integrateByParts)) is { }) return answer;
920923
// A product of powers of the trigonometric functions of one argument, some not whole,
921924
// whose powers of the sine and cosine add up to an even number: the tangent's form
922925
// times a constant, which the substitution below reads.
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+
/// A power that is not whole of <c>csc(x) - sin(x)</c> or <c>sec(x) - cos(x)</c>, which are
16+
/// <c>cos(x)^2/sin(x)</c> and <c>sin(x)^2/cos(x)</c>: written so, a product of powers of the
17+
/// sine and cosine, read through the tangent. Rubi's 4.7.7.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
19+
/// </summary>
20+
/// <remarks>
21+
/// Compared as complex numbers on both sides of the zeros of the sine and the cosine, where the
22+
/// integrands are complex on some of the intervals.
23+
/// </remarks>
24+
[Trait("Area", "Calculus")]
25+
public sealed class DifferenceOfReciprocalFunctionsIntegralTest
26+
{
27+
[Theory]
28+
[InlineData("(csc(x) - sin(x))^(5/2)")]
29+
[InlineData("(csc(x) - sin(x))^(3/2)")]
30+
[InlineData("1/(csc(x) - sin(x))^(1/2)")]
31+
[InlineData("1/(csc(x) - sin(x))^(7/2)")]
32+
[InlineData("(sec(x) - cos(x))^(5/2)")]
33+
[InlineData("1/(sec(x) - cos(x))^(3/2)")]
34+
[InlineData("(sin(x) - csc(x))^(3/2)")]
35+
public void IsAProductOfPowersOfTheSineAndCosine(string integrand)
36+
{
37+
var integral = integrand.ToEntity().Integrate("x");
38+
Assert.DoesNotContain("integral(", integral.Stringize());
39+
var derivative = integral.Substitute("C", 0).Differentiate("x");
40+
var original = integrand.ToEntity();
41+
foreach (var at in new[] { -2.3, -1.1, -0.4, 0.4, 1.1, 1.9, 2.6 })
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)