@@ -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>
0 commit comments