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
20 changes: 20 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ power of the variable every term holds comes out the same way.
| `"x^2/((a*x + a)*(1 - x^2))".Integrate("x")` | unevaluated | the partial fractions over `(x + 1)^2 (1 - x)`, over `a` |
| `"cot(x)^3/(a + b*csc(x))".Integrate("x")` | unevaluated | `-1/(b sin(x)) - a ln(sin(x))/b^2 + (b^2 - a^2) ln(a sin(x) + b)/(a b^2)`, once `a u^4 + b u^3` is read as `u^3 (a u + b)` |

### Fractional powers of `a ± a sin` are integrated by the half angle at which they are squares

**Improvement, not silent.** `(A + C sin(y)^2)/((c - c sin(y))^(3/2) sqrt(a + a sin(y)))` and
its kin ran past any budget: the substitution search has no radical of a trigonometric function
it can rationalise, and the closed rule for `(a + b sin)^n` with `a^2 = b^2` reads one such power
alone. `1 + sin(y)` is `2 sin(u)^2` and `1 - sin(y)` is `2 cos(u)^2` for `u = y/2 + pi/4`
(`1 ± cos(y)` the same with `u = y/2`), so each such power is a whole power of a sine or cosine
of `u` times its sign — a constant between the zeros, written in front — and what is left is
rational in `sin(u)` and `cos(u)`, which the rules answer. Exact for any `a`: `1 + sin(y)` is not
negative, so `(a q)^p = a^p q^p` for the principal powers whatever `a` is; `a^2 = b^2` is decided,
not assumed, and a lone positive power still goes to the closed rule first, which answers it in
one form across the zeros.

| | Was | Is |
|---|---|---|
| `"(A + C*sin(x)^2)/((c - c*sin(x))^(3/2)*sqrt(a + a*sin(x)))".Integrate("x")` | unevaluated after the budget | `sgn(cos(u)) sgn(sin(u)) (…)` in `u = x/2 + pi/4`, half a second |
| `"(a + a*sin(x))^(1/2)/(c - c*sin(x))^(5/2)".Integrate("x")` | unevaluated | `sgn(sin(u)) sgn(cos(u)) sqrt(2a)/(4 c^(5/2)) (…)` |
| `"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 condition the answer states on its own is no longer repeated beside it

**Improvement, not silent.** `Simplify` returned `(1 - ln(x))/x^2 provided not x = 0` for the
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Calculus/CombinedRadicalsTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Calculus/HalfAngleSquareTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Calculus/DenominatorContentTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,176 @@ Entity Step(ERational power)
return Step(n).InnerSimplified;
}

/// <summary>
/// Fractional powers of <c>a ± a sin(y)</c>, or of <c>a ± a cos(y)</c>, beside anything
/// rational in the sine and cosine of <c>y</c>, by the half angle at which they are
/// squares: <c>1 + sin(y)</c> is <c>2 sin(u)^2</c> and <c>1 - sin(y)</c> is
/// <c>2 cos(u)^2</c> for <c>u = y/2 + pi/4</c>, so <c>(a + a sin(y))^(3/2)</c> is
/// <c>a^(3/2) 2^(3/2) sgn(sin(u)) sin(u)^3</c>, the sign a constant between the zeros of
/// the sine that comes out in front of the integral, and what is left is rational in
/// <c>sin(u)</c> and <c>cos(u)</c> -- <c>sin(y)</c> being <c>2 sin(u)^2 - 1</c> and
/// <c>cos(y)</c> being <c>2 sin(u) cos(u)</c>. For the cosine, <c>u = y/2</c>, with
/// <c>1 + cos(y) = 2 cos(u)^2</c> and <c>1 - cos(y) = 2 sin(u)^2</c>.
/// </summary>
/// <remarks>
/// <para>
/// Rubi's <c>(a + b sin)^m (c + d sin)^n (A + B sin + C sin^2)</c> files with
/// <c>a^2 = b^2</c> and <c>c^2 = d^2</c> -- <c>(A + C sin^2)/((c - c sin)^(3/2) sqrt(a + a sin))</c>
/// and its kin -- were timeouts: the substitution search has no radical of a
/// trigonometric function it can rationalise, and <see cref="SolveAHalfPowerOfOnePlusASine"/>
/// reads one such power alone. This is that rule's identity applied to the whole
/// integrand at once.
/// </para>
/// <para>
/// Exact, for any <c>a</c>: <c>1 + sin(y)</c> is not negative, so <c>(a q)^p = a^p q^p</c>
/// for the principal powers whatever <c>a</c> is, and <c>(2 sin(u)^2)^p</c> is
/// <c>2^p |sin(u)|^(2p)</c>. <c>a^2 = b^2</c> is decided, not assumed. Whole products
/// <c>2p</c> only, so that the sign is a power of a sign and what is handed on is a whole
/// power of the sine; at the top only, as every rule that writes a sign for a function.
/// </para>
/// https://github.com/asc-community/AngouriMath/issues/718
/// </remarks>
internal static Entity? SolveByTheHalfAngleWhereOnePlusASineIsASquare(Entity expr, Entity.Variable x, bool integrateByParts)
{
if (!Integration.AnsweringTheQuestionAsked)
return null;
Entity? argument = null;
foreach (var node in expr.Nodes)
{
if (TrigonometricArgument(node) is not { } thisArgument || !thisArgument.ContainsNode(x))
continue;
if (argument is null)
argument = thisArgument;
else if (argument != thisArgument)
return null;
}
if (argument is null || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _)
|| rate.ContainsNode(x) || TreeAnalyzer.IsZero(rate))
return null;
if (!expr.Nodes.All(node => !node.ContainsNode(x)
|| node is Variable or Sumf or Minusf or Mulf or Divf or Sinf or Cosf or Secantf or Cosecantf or Tanf or Cotanf
|| node is Powf(_, Number.Rational)))
return null;
var sine = MathS.Sin(argument);
var cosine = MathS.Cos(argument);
// Which of the two functions the half powers are of: all of one kind, or none.
bool? ofTheSine = null;
var found = false;
foreach (var node in expr.Nodes)
{
if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(x))
continue;
if (ReadAsOnePlusMinusAFunction(radicand, sine, cosine) is not var (_, _, isSine))
return null;
if (!exponent.ERational.Denominator.Equals(EInteger.FromInt32(2)))
return null;
if (ofTheSine is { } kind && kind != isSine)
return null;
ofTheSine = isSine;
found = true;
}
if (!found || ofTheSine is not { } sineKind)
return null;
// One such power alone, to a positive exponent, is the closed rule's first, which
// answers it in a single form across the zeros of `1 + sin(y)` where this one
// writes a sign; the closed rule wants `a^2 = b^2` decided by evaluation, so
// `sqrt(a + a sin(x))` with a symbol falls through to here.
if (expr is Powf(_, Number.Rational { ERational.Sign: > 0 }) && SolveAHalfPowerOfOnePlusASine(expr, x) is { } closed)
return closed;

var u = Variable.CreateUnique(expr, "u_half_square");
var sinU = MathS.Sin(u);
var cosU = MathS.Cos(u);
// sin(y) and cos(y) in u, and the two squares.
Entity sineInU = sineKind ? 2 * MathS.Sqr(sinU) - 1 : 2 * sinU * cosU;
Entity cosineInU = sineKind ? 2 * sinU * cosU : 2 * MathS.Sqr(cosU) - 1;
// 1 + f is twice the square of `plus`, 1 - f twice the square of `minus`.
Entity plus = sineKind ? sinU : cosU;
Entity minus = sineKind ? cosU : sinU;
Entity signs = Number.Integer.One;
var inTheTwo = expr.Replace(node => node switch
{
Secantf(var a) when a == argument => 1 / cosine,
Cosecantf(var a) when a == argument => 1 / sine,
Tanf(var a) when a == argument => sine / cosine,
Cotanf(var a) when a == argument => cosine / sine,
_ => node,
});
var rewritten = inTheTwo.Replace(node =>
{
if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(x))
return node;
if (ReadAsOnePlusMinusAFunction(radicand, sine, cosine) is not var (a, positive, _))
return node;
var square = positive ? plus : minus;
var twoP = exponent.ERational.Numerator;
// (a (1 ± f))^p = a^p 2^p |s|^(2p) = a^p 2^p sgn(s)^(2p) s^(2p), and 2p is odd.
signs = signs * MathS.Signum(square);
Entity constant = MathS.Pow(2, exponent);
if (a != Number.Integer.One)
constant = MathS.Pow(a, exponent) * constant;
return constant * MathS.Pow(square, Number.Integer.Create(twoP));
});
var inU = rewritten.Substitute(sine, sineInU).Substitute(cosine, cosineInU);
if (inU.ContainsNode(x))
return null;
// dx = 2 du / rate.
var integrand = (inU * 2 / rate).InnerSimplified;
if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result)
return null;
var back = sineKind ? argument / 2 + MathS.pi / 4 : argument / 2;
var answer = (signs == Number.Integer.One ? result : signs * result).Substitute(u, back);
return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
}

/// <summary>
/// <paramref name="radicand"/> read as <c>a (1 ± f)</c> for <c>f</c> the given sine or
/// cosine: the constant <c>a</c>, whether the sign is plus, and whether <c>f</c> is the
/// sine. Null for anything else, including <c>a + b f</c> with <c>a^2 ≠ b^2</c>.
/// </summary>
private static (Entity Constant, bool Plus, bool IsSine)? ReadAsOnePlusMinusAFunction(Entity radicand, Entity sine, Entity cosine)
{
Entity a = Number.Integer.Zero;
Entity? b = null;
Entity? function = null;
foreach (var term in Sumf.LinearChildren(radicand))
{
if (!term.ContainsNode(sine) && !term.ContainsNode(cosine))
{
a += term;
continue;
}
Entity coefficient = Number.Integer.One;
foreach (var factor in Mulf.LinearChildren(term))
{
if (factor == sine || factor == cosine)
{
if (function is not null)
return null;
function = factor;
}
else if (!factor.ContainsNode(sine) && !factor.ContainsNode(cosine))
coefficient *= factor;
else
return null;
}
if (function is null || b is not null)
return null;
b = coefficient;
}
if (b is null || function is null)
return null;
a = a.InnerSimplified;
b = b.InnerSimplified;
if (a.Evaled is Number.Complex { IsZero: true })
return null;
var plus = (a - b).InnerSimplified.Evaled is Number.Complex { IsZero: true } || (a - b).Simplify().Evaled is Number.Complex { IsZero: true };
var minus = !plus && ((a + b).InnerSimplified.Evaled is Number.Complex { IsZero: true } || (a + b).Simplify().Evaled is Number.Complex { IsZero: true });
if (!plus && !minus)
return null;
return (a, plus, function == sine);
}

/// <summary>
/// A whole power of the secant or cosecant, brought down two at a time by the standard
/// reduction until the power rule for the first or the zeroth takes over.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// among the coefficients, split over the quadratic's two roots: under the
// half-angle it is a quartic in t with a symbol in every coefficient.
if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionOfASineOverASymbolicQuadratic(expr, x, integrateByParts)) is { }) return answer;
// Fractional powers of `a ± a sin(y)` beside anything rational in the sine and
// cosine, by the half angle at which they are squares: `1 + sin(y)` is `2 sin(u)^2`. Before
// the substitution search, which spent twenty seconds on the radicals of the sine.
if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusASineIsASquare(expr, x, integrateByParts)) is { }) return answer;
if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer;
// A logarithmic derivative the substitution above could not read for a symbol in
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.
Expand Down
74 changes: 74 additions & 0 deletions Sources/Tests/UnitTests/Calculus/HalfAngleSquareTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// 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 System.Collections.Generic;
using AngouriMath.Extensions;
using Xunit;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// Fractional powers of <c>a ± a sin(y)</c>, or of <c>a ± a cos(y)</c>, beside anything
/// rational in the sine and cosine of <c>y</c>, by the half angle at which they are squares:
/// <c>1 + sin(y)</c> is <c>2 sin(y/2 + pi/4)^2</c>, so the power is a whole power of a sine
/// times its sign, and the sign is a constant between the zeros that comes out in front.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Rubi's <c>(A + C sin^2)/((c - c sin)^(3/2) sqrt(a + a sin))</c> and its kin were timeouts.
/// Every answer is differentiated back with every symbol pinned, on both sides of a zero of
/// the half-angle sine, and with <c>a</c> negative as well as positive: <c>1 + sin(y)</c> is
/// not negative, so <c>(a q)^p = a^p q^p</c> for the principal powers whatever <c>a</c> is.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class HalfAngleSquareTest
{
private static readonly double[] Points = { 0.3, 1.1, 2.6, 3.9, 5.4 };

private static void DifferentiatesBack(string integrand, double a)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
Assert.DoesNotContain("NaN", integral.Stringize());

var pinned = new Dictionary<string, double> { ["a"] = a, ["c"] = 3, ["A"] = 1, ["B"] = 2, ["C"] = 1.5, ["pe"] = 0.3, ["f"] = 1.7, ["d"] = 1.3 };
Entity Pin(Entity e)
{
foreach (var pair in pinned)
e = e.Substitute(pair.Key, pair.Value);
return e;
}
var derivative = Pin(integral).Differentiate("x");
var original = Pin(integrand.ToEntity());
foreach (var at in Points)
{
var got = derivative.Substitute("x", at).EvalNumerical();
var want = original.Substitute("x", at).EvalNumerical();
var difference = Math.Abs((double)(got - want).RealPart)
+ Math.Abs((double)(got - want).ImaginaryPart);
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
Assert.True(difference / scale < 1e-7,
$"d/dx of the antiderivative of {integrand} at a = {a} is {got} at x = {at}, "
+ $"where the integrand is {want}");
}
}

[Theory]
[InlineData("(a + a*sin(pe + f*x))^(1/2)/(c - c*sin(pe + f*x))^(5/2)")]
[InlineData("(A + B*sin(pe + f*x))/((a + a*sin(pe + f*x))*(c - c*sin(pe + f*x))^(5/2))")]
[InlineData("(a + a*sin(pe + f*x))^(3/2)*(c - c*sin(pe + f*x))^(1/2)*(A + B*sin(pe + f*x))")]
[InlineData("(a + a*cos(x))^(3/2)*sin(x)^2")]
[InlineData("(A + B*cos(c + d*x))*sec(c + d*x)/(a + a*cos(c + d*x))^(1/2)")]
[InlineData("sqrt(a + a*sin(x))")]
public void TheHalfAngleMakesThePowerWhole(string integrand)
{
DifferentiatesBack(integrand, 2);
DifferentiatesBack(integrand, -3);
}
}
}
Loading