Skip to content

Commit 7b7f28e

Browse files
Fractional powers of a ± a sin(y) are integrated by the half angle at which they are squares (#1405)
(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, and 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, since 1 + sin(y) is not negative and (a q)^p = a^p q^p for the principal powers whatever a is; a^2 = b^2 is decided, not assumed; a lone positive power goes to the closed rule first, which answers it in one form across the zeros. Before the substitution search, which spent twenty seconds on the radicals. Rubi's family 4 sample: 261 to 277 of 422, sixteen rows and none lost, six of them timeouts before. The 1774-problem suite is unchanged at 1706 with 0 wrong. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 852c747 commit 7b7f28e

5 files changed

Lines changed: 271 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,26 @@ power of the variable every term holds comes out the same way.
369369
| `"x^2/((a*x + a)*(1 - x^2))".Integrate("x")` | unevaluated | the partial fractions over `(x + 1)^2 (1 - x)`, over `a` |
370370
| `"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)` |
371371

372+
### Fractional powers of `a ± a sin` are integrated by the half angle at which they are squares
373+
374+
**Improvement, not silent.** `(A + C sin(y)^2)/((c - c sin(y))^(3/2) sqrt(a + a sin(y)))` and
375+
its kin ran past any budget: the substitution search has no radical of a trigonometric function
376+
it can rationalise, and the closed rule for `(a + b sin)^n` with `a^2 = b^2` reads one such power
377+
alone. `1 + sin(y)` is `2 sin(u)^2` and `1 - sin(y)` is `2 cos(u)^2` for `u = y/2 + pi/4`
378+
(`1 ± cos(y)` the same with `u = y/2`), so each such power is a whole power of a sine or cosine
379+
of `u` times its sign — a constant between the zeros, written in front — and what is left is
380+
rational in `sin(u)` and `cos(u)`, which the rules answer. Exact for any `a`: `1 + sin(y)` is not
381+
negative, so `(a q)^p = a^p q^p` for the principal powers whatever `a` is; `a^2 = b^2` is decided,
382+
not assumed, and a lone positive power still goes to the closed rule first, which answers it in
383+
one form across the zeros.
384+
385+
| | Was | Is |
386+
|---|---|---|
387+
| `"(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 |
388+
| `"(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)) (…)` |
389+
| `"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)` |
390+
| `"sqrt(1 + sin(x))".Integrate("x")` | `-2 cos(x)/sqrt(1 + sin(x))` | unchanged, the closed rule's |
391+
372392
### A condition the answer states on its own is no longer repeated beside it
373393

374394
**Improvement, not silent.** `Simplify` returned `(1 - ln(x))/x^2 provided not x = 0` for the

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
445445
[Tests/UnitTests/Calculus/CombinedRadicalsTest.cs]
446446
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
447447

448+
[Tests/UnitTests/Calculus/HalfAngleSquareTest.cs]
449+
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
450+
448451
[Tests/UnitTests/Calculus/DenominatorContentTest.cs]
449452
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
450453

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

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,176 @@ Entity Step(ERational power)
22602260
return Step(n).InnerSimplified;
22612261
}
22622262

2263+
/// <summary>
2264+
/// Fractional powers of <c>a ± a sin(y)</c>, or of <c>a ± a cos(y)</c>, beside anything
2265+
/// rational in the sine and cosine of <c>y</c>, by the half angle at which they are
2266+
/// squares: <c>1 + sin(y)</c> is <c>2 sin(u)^2</c> and <c>1 - sin(y)</c> is
2267+
/// <c>2 cos(u)^2</c> for <c>u = y/2 + pi/4</c>, so <c>(a + a sin(y))^(3/2)</c> is
2268+
/// <c>a^(3/2) 2^(3/2) sgn(sin(u)) sin(u)^3</c>, the sign a constant between the zeros of
2269+
/// the sine that comes out in front of the integral, and what is left is rational in
2270+
/// <c>sin(u)</c> and <c>cos(u)</c> -- <c>sin(y)</c> being <c>2 sin(u)^2 - 1</c> and
2271+
/// <c>cos(y)</c> being <c>2 sin(u) cos(u)</c>. For the cosine, <c>u = y/2</c>, with
2272+
/// <c>1 + cos(y) = 2 cos(u)^2</c> and <c>1 - cos(y) = 2 sin(u)^2</c>.
2273+
/// </summary>
2274+
/// <remarks>
2275+
/// <para>
2276+
/// Rubi's <c>(a + b sin)^m (c + d sin)^n (A + B sin + C sin^2)</c> files with
2277+
/// <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>
2278+
/// and its kin -- were timeouts: the substitution search has no radical of a
2279+
/// trigonometric function it can rationalise, and <see cref="SolveAHalfPowerOfOnePlusASine"/>
2280+
/// reads one such power alone. This is that rule's identity applied to the whole
2281+
/// integrand at once.
2282+
/// </para>
2283+
/// <para>
2284+
/// Exact, for any <c>a</c>: <c>1 + sin(y)</c> is not negative, so <c>(a q)^p = a^p q^p</c>
2285+
/// for the principal powers whatever <c>a</c> is, and <c>(2 sin(u)^2)^p</c> is
2286+
/// <c>2^p |sin(u)|^(2p)</c>. <c>a^2 = b^2</c> is decided, not assumed. Whole products
2287+
/// <c>2p</c> only, so that the sign is a power of a sign and what is handed on is a whole
2288+
/// power of the sine; at the top only, as every rule that writes a sign for a function.
2289+
/// </para>
2290+
/// https://github.com/asc-community/AngouriMath/issues/718
2291+
/// </remarks>
2292+
internal static Entity? SolveByTheHalfAngleWhereOnePlusASineIsASquare(Entity expr, Entity.Variable x, bool integrateByParts)
2293+
{
2294+
if (!Integration.AnsweringTheQuestionAsked)
2295+
return null;
2296+
Entity? argument = null;
2297+
foreach (var node in expr.Nodes)
2298+
{
2299+
if (TrigonometricArgument(node) is not { } thisArgument || !thisArgument.ContainsNode(x))
2300+
continue;
2301+
if (argument is null)
2302+
argument = thisArgument;
2303+
else if (argument != thisArgument)
2304+
return null;
2305+
}
2306+
if (argument is null || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _)
2307+
|| rate.ContainsNode(x) || TreeAnalyzer.IsZero(rate))
2308+
return null;
2309+
if (!expr.Nodes.All(node => !node.ContainsNode(x)
2310+
|| node is Variable or Sumf or Minusf or Mulf or Divf or Sinf or Cosf or Secantf or Cosecantf or Tanf or Cotanf
2311+
|| node is Powf(_, Number.Rational)))
2312+
return null;
2313+
var sine = MathS.Sin(argument);
2314+
var cosine = MathS.Cos(argument);
2315+
// Which of the two functions the half powers are of: all of one kind, or none.
2316+
bool? ofTheSine = null;
2317+
var found = false;
2318+
foreach (var node in expr.Nodes)
2319+
{
2320+
if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(x))
2321+
continue;
2322+
if (ReadAsOnePlusMinusAFunction(radicand, sine, cosine) is not var (_, _, isSine))
2323+
return null;
2324+
if (!exponent.ERational.Denominator.Equals(EInteger.FromInt32(2)))
2325+
return null;
2326+
if (ofTheSine is { } kind && kind != isSine)
2327+
return null;
2328+
ofTheSine = isSine;
2329+
found = true;
2330+
}
2331+
if (!found || ofTheSine is not { } sineKind)
2332+
return null;
2333+
// One such power alone, to a positive exponent, is the closed rule's first, which
2334+
// answers it in a single form across the zeros of `1 + sin(y)` where this one
2335+
// writes a sign; the closed rule wants `a^2 = b^2` decided by evaluation, so
2336+
// `sqrt(a + a sin(x))` with a symbol falls through to here.
2337+
if (expr is Powf(_, Number.Rational { ERational.Sign: > 0 }) && SolveAHalfPowerOfOnePlusASine(expr, x) is { } closed)
2338+
return closed;
2339+
2340+
var u = Variable.CreateUnique(expr, "u_half_square");
2341+
var sinU = MathS.Sin(u);
2342+
var cosU = MathS.Cos(u);
2343+
// sin(y) and cos(y) in u, and the two squares.
2344+
Entity sineInU = sineKind ? 2 * MathS.Sqr(sinU) - 1 : 2 * sinU * cosU;
2345+
Entity cosineInU = sineKind ? 2 * sinU * cosU : 2 * MathS.Sqr(cosU) - 1;
2346+
// 1 + f is twice the square of `plus`, 1 - f twice the square of `minus`.
2347+
Entity plus = sineKind ? sinU : cosU;
2348+
Entity minus = sineKind ? cosU : sinU;
2349+
Entity signs = Number.Integer.One;
2350+
var inTheTwo = expr.Replace(node => node switch
2351+
{
2352+
Secantf(var a) when a == argument => 1 / cosine,
2353+
Cosecantf(var a) when a == argument => 1 / sine,
2354+
Tanf(var a) when a == argument => sine / cosine,
2355+
Cotanf(var a) when a == argument => cosine / sine,
2356+
_ => node,
2357+
});
2358+
var rewritten = inTheTwo.Replace(node =>
2359+
{
2360+
if (node is not Powf(var radicand, Number.Rational exponent) || exponent is Number.Integer || !radicand.ContainsNode(x))
2361+
return node;
2362+
if (ReadAsOnePlusMinusAFunction(radicand, sine, cosine) is not var (a, positive, _))
2363+
return node;
2364+
var square = positive ? plus : minus;
2365+
var twoP = exponent.ERational.Numerator;
2366+
// (a (1 ± f))^p = a^p 2^p |s|^(2p) = a^p 2^p sgn(s)^(2p) s^(2p), and 2p is odd.
2367+
signs = signs * MathS.Signum(square);
2368+
Entity constant = MathS.Pow(2, exponent);
2369+
if (a != Number.Integer.One)
2370+
constant = MathS.Pow(a, exponent) * constant;
2371+
return constant * MathS.Pow(square, Number.Integer.Create(twoP));
2372+
});
2373+
var inU = rewritten.Substitute(sine, sineInU).Substitute(cosine, cosineInU);
2374+
if (inU.ContainsNode(x))
2375+
return null;
2376+
// dx = 2 du / rate.
2377+
var integrand = (inU * 2 / rate).InnerSimplified;
2378+
if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result)
2379+
return null;
2380+
var back = sineKind ? argument / 2 + MathS.pi / 4 : argument / 2;
2381+
var answer = (signs == Number.Integer.One ? result : signs * result).Substitute(u, back);
2382+
return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
2383+
}
2384+
2385+
/// <summary>
2386+
/// <paramref name="radicand"/> read as <c>a (1 ± f)</c> for <c>f</c> the given sine or
2387+
/// cosine: the constant <c>a</c>, whether the sign is plus, and whether <c>f</c> is the
2388+
/// sine. Null for anything else, including <c>a + b f</c> with <c>a^2 ≠ b^2</c>.
2389+
/// </summary>
2390+
private static (Entity Constant, bool Plus, bool IsSine)? ReadAsOnePlusMinusAFunction(Entity radicand, Entity sine, Entity cosine)
2391+
{
2392+
Entity a = Number.Integer.Zero;
2393+
Entity? b = null;
2394+
Entity? function = null;
2395+
foreach (var term in Sumf.LinearChildren(radicand))
2396+
{
2397+
if (!term.ContainsNode(sine) && !term.ContainsNode(cosine))
2398+
{
2399+
a += term;
2400+
continue;
2401+
}
2402+
Entity coefficient = Number.Integer.One;
2403+
foreach (var factor in Mulf.LinearChildren(term))
2404+
{
2405+
if (factor == sine || factor == cosine)
2406+
{
2407+
if (function is not null)
2408+
return null;
2409+
function = factor;
2410+
}
2411+
else if (!factor.ContainsNode(sine) && !factor.ContainsNode(cosine))
2412+
coefficient *= factor;
2413+
else
2414+
return null;
2415+
}
2416+
if (function is null || b is not null)
2417+
return null;
2418+
b = coefficient;
2419+
}
2420+
if (b is null || function is null)
2421+
return null;
2422+
a = a.InnerSimplified;
2423+
b = b.InnerSimplified;
2424+
if (a.Evaled is Number.Complex { IsZero: true })
2425+
return null;
2426+
var plus = (a - b).InnerSimplified.Evaled is Number.Complex { IsZero: true } || (a - b).Simplify().Evaled is Number.Complex { IsZero: true };
2427+
var minus = !plus && ((a + b).InnerSimplified.Evaled is Number.Complex { IsZero: true } || (a + b).Simplify().Evaled is Number.Complex { IsZero: true });
2428+
if (!plus && !minus)
2429+
return null;
2430+
return (a, plus, function == sine);
2431+
}
2432+
22632433
/// <summary>
22642434
/// A whole power of the secant or cosecant, brought down two at a time by the standard
22652435
/// reduction until the power rule for the first or the zeroth takes over.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
543543
// among the coefficients, split over the quadratic's two roots: under the
544544
// half-angle it is a quartic in t with a symbol in every coefficient.
545545
if ((answer = IndefiniteIntegralSolver.SolveARationalFunctionOfASineOverASymbolicQuadratic(expr, x, integrateByParts)) is { }) return answer;
546+
// Fractional powers of `a ± a sin(y)` beside anything rational in the sine and
547+
// cosine, by the half angle at which they are squares: `1 + sin(y)` is `2 sin(u)^2`. Before
548+
// the substitution search, which spent twenty seconds on the radicals of the sine.
549+
if ((answer = IndefiniteIntegralSolver.SolveByTheHalfAngleWhereOnePlusASineIsASquare(expr, x, integrateByParts)) is { }) return answer;
546550
if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer;
547551
// A logarithmic derivative the substitution above could not read for a symbol in
548552
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 System.Collections.Generic;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// Fractional powers of <c>a ± a sin(y)</c>, or of <c>a ± a cos(y)</c>, beside anything
17+
/// rational in the sine and cosine of <c>y</c>, by the half angle at which they are squares:
18+
/// <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
19+
/// times its sign, and the sign is a constant between the zeros that comes out in front.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
21+
/// </summary>
22+
/// <remarks>
23+
/// Rubi's <c>(A + C sin^2)/((c - c sin)^(3/2) sqrt(a + a sin))</c> and its kin were timeouts.
24+
/// Every answer is differentiated back with every symbol pinned, on both sides of a zero of
25+
/// the half-angle sine, and with <c>a</c> negative as well as positive: <c>1 + sin(y)</c> is
26+
/// not negative, so <c>(a q)^p = a^p q^p</c> for the principal powers whatever <c>a</c> is.
27+
/// </remarks>
28+
[Trait("Area", "Calculus")]
29+
public sealed class HalfAngleSquareTest
30+
{
31+
private static readonly double[] Points = { 0.3, 1.1, 2.6, 3.9, 5.4 };
32+
33+
private static void DifferentiatesBack(string integrand, double a)
34+
{
35+
var integral = integrand.ToEntity().Integrate("x");
36+
Assert.DoesNotContain("integral(", integral.Stringize());
37+
Assert.DoesNotContain("NaN", integral.Stringize());
38+
39+
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 };
40+
Entity Pin(Entity e)
41+
{
42+
foreach (var pair in pinned)
43+
e = e.Substitute(pair.Key, pair.Value);
44+
return e;
45+
}
46+
var derivative = Pin(integral).Differentiate("x");
47+
var original = Pin(integrand.ToEntity());
48+
foreach (var at in Points)
49+
{
50+
var got = derivative.Substitute("x", at).EvalNumerical();
51+
var want = original.Substitute("x", at).EvalNumerical();
52+
var difference = Math.Abs((double)(got - want).RealPart)
53+
+ Math.Abs((double)(got - want).ImaginaryPart);
54+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
55+
Assert.True(difference / scale < 1e-7,
56+
$"d/dx of the antiderivative of {integrand} at a = {a} is {got} at x = {at}, "
57+
+ $"where the integrand is {want}");
58+
}
59+
}
60+
61+
[Theory]
62+
[InlineData("(a + a*sin(pe + f*x))^(1/2)/(c - c*sin(pe + f*x))^(5/2)")]
63+
[InlineData("(A + B*sin(pe + f*x))/((a + a*sin(pe + f*x))*(c - c*sin(pe + f*x))^(5/2))")]
64+
[InlineData("(a + a*sin(pe + f*x))^(3/2)*(c - c*sin(pe + f*x))^(1/2)*(A + B*sin(pe + f*x))")]
65+
[InlineData("(a + a*cos(x))^(3/2)*sin(x)^2")]
66+
[InlineData("(A + B*cos(c + d*x))*sec(c + d*x)/(a + a*cos(c + d*x))^(1/2)")]
67+
[InlineData("sqrt(a + a*sin(x))")]
68+
public void TheHalfAngleMakesThePowerWhole(string integrand)
69+
{
70+
DifferentiatesBack(integrand, 2);
71+
DifferentiatesBack(integrand, -3);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)