@@ -3608,6 +3608,159 @@ Entity Step(ERational power)
36083608 return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
36093609 }
36103610
3611+ /// <summary>
3612+ /// Powers of <c>a ± a sin(y)</c> with a symbolic exponent, beside a power of
3613+ /// <c>g cos(y)</c> and anything else in the sine alone, under <c>u = sin(y)</c>:
3614+ /// <c>(1 + sin(y))(1 - sin(y))</c> is <c>cos(y)^2</c>, so each factor is a power of
3615+ /// <c>1 + u</c> or of <c>1 - u</c> up to a factor constant on each interval where it is
3616+ /// defined, and <c>dy = du/cos(y)</c> is one more pair of half powers. The cosine's the
3617+ /// same way, by <c>u = cos(y)</c> beside a power of <c>g sin(y)</c>.
3618+ /// </summary>
3619+ /// <remarks>
3620+ /// <para>
3621+ /// Rubi's <c>(a + b sin)^m (c + d sin)^n</c> files with <c>a^2 = b^2</c> and
3622+ /// <c>c^2 = d^2</c> hold about a hundred and twenty problems with a symbolic exponent,
3623+ /// <c>(a + a sin(e + f x))^m sqrt(c - c sin(e + f x))</c> the plainest, and none was
3624+ /// answered: the half angle at which <c>1 ± sin(y)</c> is a square, the rule before this
3625+ /// one, wants numeric powers, and the substitution search spent the budget on them. What
3626+ /// is asked in <c>u</c> is <c>(1 + u)^A (1 - u)^B R(u)</c>, which is elementary where one
3627+ /// of <c>A</c>, <c>B</c> and <c>A + B</c> is a whole number and the rest is a
3628+ /// polynomial, and is declined where it is not.
3629+ /// </para>
3630+ /// <para>
3631+ /// The answer is <c>K G(sin(y))/F</c>, for <c>G</c> the antiderivative in <c>u</c>,
3632+ /// <c>F</c> the slope of <c>y</c> and <c>K</c> the powers as written over the form they
3633+ /// were rewritten to and over <c>cos(y)</c>: a quotient whose logarithmic derivative is
3634+ /// zero, so the answer holds whatever <c>a</c>, <c>g</c> and the exponents are --
3635+ /// <c>(a (1 + u))^m</c> is not <c>a^m (1 + u)^m</c> for every <c>a</c>, and the quotient
3636+ /// of the two is constant on each interval either way. At the question asked only, and
3637+ /// only where an exponent is symbolic: numeric half powers are the half angle's.
3638+ /// https://github.com/asc-community/AngouriMath/issues/718
3639+ /// </para>
3640+ /// </remarks>
3641+ internal static Entity? SolveSymbolicPowersOfOnePlusMinusASineThroughTheSine(Entity expr, Entity.Variable x, bool integrateByParts)
3642+ {
3643+ if (!Integration.AnsweringTheQuestionAsked)
3644+ return null;
3645+ Entity? argument = null;
3646+ foreach (var node in expr.Nodes)
3647+ {
3648+ if (TrigonometricArgument(node) is not { } thisArgument || !thisArgument.ContainsNode(x))
3649+ continue;
3650+ if (argument is null)
3651+ argument = thisArgument;
3652+ else if (argument != thisArgument)
3653+ return null;
3654+ }
3655+ if (argument is null || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var rate, out _)
3656+ || rate.ContainsNode(x) || TreeAnalyzer.IsZero(rate))
3657+ return null;
3658+ var sine = MathS.Sin(argument);
3659+ var cosine = MathS.Cos(argument);
3660+ // Each factor as a base and an exponent, a power of a power read as one power: the
3661+ // quotient of the two spellings is constant on each interval, and K carries it.
3662+ var factors = new List<(Entity Factor, Entity Base, Entity Exponent)>();
3663+ foreach (var factor in Mulf.LinearChildren(expr))
3664+ {
3665+ Entity @base = factor, exponent = Number.Integer.One;
3666+ while (@base is Powf(var inner, var power))
3667+ {
3668+ @base = inner;
3669+ exponent = power * exponent;
3670+ }
3671+ if (exponent.ContainsNode(x))
3672+ return null;
3673+ factors.Add((factor, @base, exponent.InnerSimplified));
3674+ }
3675+ // Which function the sums are of: all of one kind.
3676+ bool? ofTheSine = null;
3677+ foreach (var (_, @base, _) in factors)
3678+ if (@base.ContainsNode(x) && ReadAsOnePlusMinusAFunction(@base, sine, cosine) is var (_, _, isSine))
3679+ {
3680+ if (ofTheSine is { } kind && kind != isSine)
3681+ return null;
3682+ ofTheSine = isSine;
3683+ }
3684+ if (ofTheSine is not { } sineKind)
3685+ return null;
3686+ var function = sineKind ? sine : cosine;
3687+ var complement = sineKind ? cosine : sine;
3688+ var u = Variable.CreateUnique(expr, "u_one_plus_minus");
3689+ // The exponents of 1 + f, of 1 - f and of the complement, the factors that carry
3690+ // them as they stand, and the rest in u.
3691+ Entity plus = Number.Integer.Zero, minus = Number.Integer.Zero, ofTheComplement = Number.Integer.Zero;
3692+ Entity asItIs = Number.Integer.One, rest = Number.Integer.One;
3693+ var symbolic = false;
3694+ foreach (var (factor, @base, exponent) in factors)
3695+ {
3696+ if (!factor.ContainsNode(x))
3697+ {
3698+ rest *= factor;
3699+ continue;
3700+ }
3701+ if (ReadAsOnePlusMinusAFunction(@base, sine, cosine) is var (_, isPlus, _))
3702+ {
3703+ if (isPlus)
3704+ plus += exponent;
3705+ else
3706+ minus += exponent;
3707+ asItIs *= factor;
3708+ symbolic |= exponent is not Number;
3709+ continue;
3710+ }
3711+ if (AsAPowerOfTheComplement(@base) is { } sign)
3712+ {
3713+ ofTheComplement += sign * exponent;
3714+ asItIs *= factor;
3715+ symbolic |= exponent is not Number;
3716+ continue;
3717+ }
3718+ var inU = factor.Substitute(function, u);
3719+ if (inU.ContainsNode(x))
3720+ return null;
3721+ rest *= inU;
3722+ }
3723+ if (!symbolic)
3724+ return null;
3725+ // dy = du/complement takes one more half power of each.
3726+ var half = Number.Rational.Create(1, 2);
3727+ var a = (plus + ofTheComplement * half - half).Simplify();
3728+ var b = (minus + ofTheComplement * half - half).Simplify();
3729+ // A zero exponent leaves its factor out: `(1 - u)^0` is one provided `u` is not one, and
3730+ // the condition would stand between the question and the rules that answer it.
3731+ Entity PowerOf(Entity @base, Entity exponent) => exponent == Number.Integer.Zero ? Number.Integer.One : MathS.Pow(@base, exponent);
3732+ var integrand = (PowerOf(1 + u, a) * PowerOf(1 - u, b) * rest).InnerSimplified;
3733+ if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result)
3734+ return null;
3735+ // d sin(y) = cos(y) dy and d cos(y) = -sin(y) dy.
3736+ Entity constant = asItIs * PowerOf(1 + function, (-a).InnerSimplified) * PowerOf(1 - function, (-b).InnerSimplified) / (rate * complement);
3737+ if (!sineKind)
3738+ constant = -constant;
3739+ var answer = constant * result.Substitute(u, function);
3740+ return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;
3741+
3742+ // 1 for a constant times the complement, -1 for a constant times its reciprocal
3743+ // function, null for anything else.
3744+ int? AsAPowerOfTheComplement(Entity @base)
3745+ {
3746+ int? found = null;
3747+ foreach (var part in Mulf.LinearChildren(@base))
3748+ {
3749+ if (!part.ContainsNode(x))
3750+ continue;
3751+ if (found is not null)
3752+ return null;
3753+ if (part == complement)
3754+ found = 1;
3755+ else if (sineKind ? part is Secantf(var s) && s == argument : part is Cosecantf(var c) && c == argument)
3756+ found = -1;
3757+ else
3758+ return null;
3759+ }
3760+ return found;
3761+ }
3762+ }
3763+
36113764 /// <summary>
36123765 /// Half-odd powers of <c>a ± a sec(y)</c>, beside powers of <c>d sec(y)</c> or
36133766 /// <c>d cos(y)</c> and anything rational in the sine and cosine of <c>y</c>, by the
0 commit comments