Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,24 @@ one form across the zeros.
| `"sqrt(a + a*sin(x))".Integrate("x")` | unevaluated (`a^2 = a^2` is not decided by evaluation) | `-2 sqrt(2a) sgn(sin(u)) cos(u)` |
| `"sqrt(1 + sin(x))".Integrate("x")` | `-2 cos(x)/sqrt(1 + sin(x))` | unchanged, the closed rule's |

### A half-odd power of the secant beside the cosine is integrated as the cosine's

**Answers where there were none.** `sec(x)^(3/2)/sqrt(1 + cos(x))` was declined while
`1/(cos(x)^(3/2) sqrt(1 + cos(x)))`, which it is wherever the cosine is positive, was answered: the
substitutions that read a function of the cosine read the secant as `1/cos` to a whole power only.
A half-odd power of the secant beside the cosine is written as the cosine's on the other side of
the bar now, `sec(x)^p = K/cos(x)^p`, with `K = sec(x)^p cos(x)^p` in front of the answer: `K` is 1
where the cosine is positive and -1 where it is negative, so the answer holds on both sides. The
same for the cosecant beside the sine. Rubi's 4.2.2.1, 4.2.3.1 and 4.2.4.2
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"sec(x)^(3/2)/sqrt(1 + cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `K` |
| `"1/((a + a*cos(x))^(7/2)*sec(x)^(7/2))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, over `K` |
| `"(A + c*cos(x)^2)*sec(x)^(11/2)*sqrt(a + a*cos(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `K` |
| `"sqrt(csc(x))*sqrt(a + a*sin(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(x/2)`, times `csc(x)^(1/2) sin(x)^(1/2)` |

### Half-odd powers of `a ± a sec` and `a ± a csc` are integrated by the half-angle tangent

Rubi's `(a + b sec)^m (d sec)^n` files with `a^2 = b^2` hold about a thousand problems with a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23725,6 +23725,82 @@ bool IsAPolynomialInBoth(Entity polynomial)
return null;
}

/// <summary>
/// A half-odd power of the secant or the cosecant, in an integrand with the cosine or the
/// sine of the same argument in it, written as a power of the cosine or the sine:
/// <c>sec(z)^p = K/cos(z)^p</c> with <c>K = sec(z)^p cos(z)^p</c>, which is 1 wherever the
/// cosine is positive and -1 wherever it is negative, so it stands in front of the answer.
/// </summary>
/// <remarks>
/// <para>
/// <c>sec(x)^(3/2)/sqrt(1 + cos(x))</c> was declined while
/// <c>1/(cos(x)^(3/2) sqrt(1 + cos(x)))</c>, which it is wherever the cosine is positive,
/// was answered: the substitutions that read a function of the cosine read the secant as
/// <c>1/cos</c> to a whole power only. Only beside the cosine, so that a power of the
/// secant with no cosine keeps the rules for the secant. Rubi's 4.2.2.1, 4.2.3.1 and
/// 4.2.4.2.
/// </para>
/// <para>
/// Only a half-odd power of the secant itself, because that is what keeps <c>K</c> real:
/// the integrand is then real exactly where the rewritten one is, and an answer for the
/// rewritten one, which holds where it is real, holds where the integrand is. A constant
/// inside the power, <c>sqrt(b sec(x))</c>, or a quarter makes <c>K</c> imaginary on one
/// side, and the integrand real where the rewritten one is not: for a negative <c>b</c>,
/// <c>sqrt(b sec(x))</c> is real where the cosine is negative and <c>1/sqrt(cos(x))</c>
/// is not, and nothing about an answer for the second says anything there.
/// </para>
/// https://github.com/asc-community/AngouriMath/issues/718
/// </remarks>
internal static Entity? SolveByWritingAPowerOfTheSecantInTheCosine(Entity expr, Entity.Variable x, bool integrateByParts)
{
if (!expr.Nodes.Any(node => node is Secantf or Cosecantf))
return null;
var factors = FactorsOfTheIntegrand(expr);
var index = -1;
Entity? reciprocal = null;
Number.Rational? exponent = null;
for (var k = 0; k < factors.Count; k++)
{
if (factors[k].Factor is not Powf((Secantf or Cosecantf) and var found, var power)
|| power.Evaled is not Number.Rational fraction || fraction is Number.Integer
|| !fraction.ERational.Denominator.Equals(EInteger.FromInt32(2)) || !found.ContainsNode(x))
continue;
if (index >= 0)
return null;
(index, reciprocal, exponent) = (k, found, fraction);
}
if (index < 0 || reciprocal is null || exponent is null)
return null;
var function = reciprocal is Secantf(var argument) ? MathS.Cos(argument) : MathS.Sin(((Cosecantf)reciprocal).Argument);
// The reciprocal nowhere else and the function somewhere: the power is then all of
// the secant there is, and the rest of the integrand a function of the cosine.
if (expr.Nodes.Count(node => node == reciprocal) != 1 || !expr.ContainsNode(function))
return null;
var (written, underneath) = factors[index];
// The power is K/cos^p above the bar and cos^p/K below it. The cosine's power goes to
// the side where it is positive, so that the rest is one quotient as the rules read it.
var negative = exponent.ERational.IsNegative;
var raised = MathS.Pow(function, negative ? -exponent : exponent);
var raisedAbove = underneath != negative;
Entity? above = null;
Entity? below = null;
for (var k = 0; k < factors.Count; k++)
{
var (factor, side) = k == index ? (raised, !raisedAbove) : factors[k];
if (factor == Number.Integer.One)
continue;
if (side)
below = below is null ? factor : below * factor;
else
above = above is null ? factor : above * factor;
}
var numerator = above ?? Number.Integer.One;
var rewritten = below is null ? numerator : numerator / below;
var constant = written * MathS.Pow(function, exponent);
return Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts)
?.Pipe(answer => underneath ? answer / constant : answer * constant);
}

/// <summary>
/// A rational function of <c>sin(x)</c> and <c>cos(x)</c>, turned into a rational function
/// of one variable by the half-angle substitution <c>t = tan(x/2)</c> and handed to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
if (expr is Entity.Sumf or Entity.Minusf
&& (answer = IndefiniteIntegralSolver.SolveByGatheringACommonPolynomialFactorOfASum(expr, x, integrateByParts)) is { })
return answer;
// A power of the secant that is not whole beside the cosine, written as the cosine's
// with the constant that takes in front, which the substitutions below read.
if ((answer = IndefiniteIntegralSolver.SolveByWritingAPowerOfTheSecantInTheCosine(expr, x, integrateByParts)) is { }) return answer;
// The half-angle substitution goes *after* linearity, and that is not a preference.
// It fires on anything built from sines and cosines, and it answers `cos(x) + 1` with
// a correct expression in tan(x/2) some forty characters long where splitting the sum
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//

using System;
using AngouriMath.Extensions;
using Xunit;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// A half-odd power of the secant beside the cosine, or of the cosecant beside the sine,
/// which is the cosine's power on the other side of the bar times <c>sec(x)^p cos(x)^p</c>,
/// 1 where the cosine is positive and -1 where it is negative. Compared on both sides, where
/// the integrand is real and where it is not, which is where the constant is what keeps the
/// answer right. Rubi's 4.2.2.1, 4.2.3.1 and 4.2.4.2.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
[Trait("Area", "Calculus")]
public sealed class PowerOfTheSecantBesideTheCosineIntegralTest
{
[Theory]
[InlineData("sec(x)^(3/2)/sqrt(1 + cos(x))")]
[InlineData("sec(x)^(5/2)*(a + a*cos(x))^(3/2)")]
[InlineData("1/((a + a*cos(x))^(7/2)*sec(x)^(7/2))")]
[InlineData("(a + a*cos(x))^(3/2)*(A + B*cos(x))/sqrt(sec(x))")]
[InlineData("(A + c*cos(x)^2)*sec(x)^(11/2)*sqrt(a + a*cos(x))")]
[InlineData("sqrt(csc(x))*sqrt(a + a*sin(x))")]
[InlineData("1/((a + a*sin(x))^(3/2)*csc(x)^(3/2))")]
public void OnBothSidesOfTheCosine(string integrand)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
Assert.DoesNotContain("NaN", integral.Stringize());
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("A", 0.9).Substitute("B", 1.4).Substitute("c", 0.6);
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
var original = Pinned(integrand.ToEntity());
var compared = 0;
foreach (var at in new[] { -2.6, -1.1, -0.4, 0.3, 0.8, 1.2, 2.0, 2.7 })
{
var want = original.Substitute("x", at).EvalNumerical();
if (want.IsNaN)
continue;
compared++;
var got = derivative.Substitute("x", at).EvalNumerical();
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
}
Assert.True(compared >= 6, $"only {compared} points could be compared for {integrand}");
}
}
}
Loading