@@ -4144,6 +4144,7 @@ void Add(int k, bool isSine, Entity coefficient)
41444144 private static Entity? OverAPowerOfALinear(Entity expr, Entity.Variable x, Entity argument)
41454145 {
41464146 var (sine, cosine) = (MathS.Sin(argument), MathS.Cos(argument));
4147+ var (tangent, cotangent, secant, cosecant) = (MathS.Tan(argument), MathS.Cotan(argument), MathS.Sec(argument), MathS.Cosec(argument));
41474148 var s = Variable.CreateUnique(expr, "s");
41484149 var c = Variable.CreateUnique(expr + s, "c");
41494150 Entity constant = Number.Integer.One;
@@ -4157,12 +4158,18 @@ void Add(int k, bool isSine, Entity coefficient)
41574158 constant = underneath ? constant / factor : constant * factor;
41584159 continue;
41594160 }
4160- // A polynomial in the sine and the cosine, above the bar.
4161- if (factor.ContainsNode(sine) || factor.ContainsNode(cosine))
4161+ // A polynomial in the sine and the cosine, above the bar, the other four written as
4162+ // quotients of the two: `tan(u) cos(u)^2`, which the substitution x = tan(u)
4163+ // leaves, is `s c`, and what is not a polynomial after that is declined below.
4164+ if (factor.ContainsNode(sine) || factor.ContainsNode(cosine) || factor.ContainsNode(tangent)
4165+ || factor.ContainsNode(cotangent) || factor.ContainsNode(secant) || factor.ContainsNode(cosecant))
41624166 {
4163- var read = factor.Replace(node => node == sine ? s : node == cosine ? c : node);
4164- if (underneath || read.ContainsNode(x))
4167+ var read = factor.Replace(node =>
4168+ node == sine ? s : node == cosine ? c : node == tangent ? s / c : node == cotangent ? c / s
4169+ : node == secant ? 1 / c : node == cosecant ? 1 / s : node);
4170+ if (read.ContainsNode(x))
41654171 return null;
4172+ read = underneath ? 1 / read : read;
41664173 trigonometric = trigonometric is null ? read : trigonometric * read;
41674174 continue;
41684175 }
@@ -4472,9 +4479,13 @@ private static (Entity Elementary, Entity Si, Entity Ci) APowerTimesASineOrACosi
44724479 {
44734480 Sinf(var argument) when argument.ContainsNode(x) => argument,
44744481 Cosf(var argument) when argument.ContainsNode(x) => argument,
4482+ Tanf(var argument) when argument.ContainsNode(x) => argument,
4483+ Cotanf(var argument) when argument.ContainsNode(x) => argument,
4484+ Secantf(var argument) when argument.ContainsNode(x) => argument,
4485+ Cosecantf(var argument) when argument.ContainsNode(x) => argument,
44754486 Powf(var @base, Number.Integer) => ASineOrCosineArgument(@base, x),
44764487 Mulf(var left, var right) => ASineOrCosineArgument(left, x) ?? ASineOrCosineArgument(right, x),
4477- Divf(var numerator, _ ) => ASineOrCosineArgument(numerator, x),
4488+ Divf(var numerator, var denominator ) => ASineOrCosineArgument(numerator, x) ?? ASineOrCosineArgument(denominator , x),
44784489 Sumf(var left, var right) => ASineOrCosineArgument(left, x) ?? ASineOrCosineArgument(right, x),
44794490 Minusf(var left, var right) => ASineOrCosineArgument(left, x) ?? ASineOrCosineArgument(right, x),
44804491 _ => null,
@@ -5254,6 +5265,48 @@ private static bool TryReadTrigonometricDenominator(
52545265 }
52555266 }
52565267
5268+ /// <summary>
5269+ /// Whether <paramref name="inverse"/>, or a function of it and of nothing else in
5270+ /// <paramref name="x"/>, is a factor below the bar of <paramref name="expr"/> or a
5271+ /// negative power of one.
5272+ /// </summary>
5273+ private static bool TheInverseIsBelowTheBar(Entity expr, Entity inverse, Entity.Variable x)
5274+ {
5275+ var u = Variable.CreateUnique(expr, "u_inv");
5276+ return FactorsOfTheIntegrand(expr).Any(pair =>
5277+ (pair.Underneath || pair.Factor is Powf(_, Number.Integer { EInteger.Sign: < 0 }))
5278+ && pair.Factor.ContainsNode(inverse) && !pair.Factor.Substitute(inverse, u).ContainsNode(x));
5279+ }
5280+
5281+ /// <summary>
5282+ /// <see cref="SolveByInverseTrigonometricSubstitution"/> where the inverse function is
5283+ /// below the bar, asked before the general substitution search: under the substitution the
5284+ /// integrand is the sine and cosine integrals' at once, and the search in front of it spent
5285+ /// five seconds on <c>x/((c + a^2 c x^2)^2 arctan(a x))</c> finding nothing. Only that
5286+ /// question is asked in <c>u</c>, of <see cref="SolveATrigonometricOfALinearOverAPowerOfALinear"/>.
5287+ /// Where the substitution leaves anything else -- a quadratic it does not read as a
5288+ /// multiple of the radicand, <c>(a^2 + a^2 tan(u)^2)^3</c> -- the whole search in
5289+ /// <c>u</c> runs past thirty seconds, on integrands the general substitution search
5290+ /// answers in under one.
5291+ /// </summary>
5292+ internal static Entity? SolveAReciprocalOfAnInverseTrigonometricFunction(Entity expr, Entity.Variable x, bool integrateByParts)
5293+ {
5294+ if (expr is not (Divf or Mulf or Powf))
5295+ return null;
5296+ Entity? inverse = null;
5297+ foreach (var node in expr.Nodes)
5298+ {
5299+ if (node is not (Arcsinf or Arccosf or Arctanf or Arcsecantf or Arccosecantf or Arccotanf))
5300+ continue;
5301+ if (inverse is not null && inverse != node)
5302+ return null;
5303+ inverse = node;
5304+ }
5305+ return inverse is not null && TheInverseIsBelowTheBar(expr, inverse, x)
5306+ ? UndoingTheInverseTrigonometricFunction(expr, x, integrateByParts, onlyTheSineAndCosineIntegrals: true)
5307+ : null;
5308+ }
5309+
52575310 /// <summary>
52585311 /// An integrand holding an inverse trigonometric function of the variable itself,
52595312 /// integrated by the substitution that undoes it: <c>x = sin(u)</c> for <c>arcsin(x)</c>,
@@ -5297,6 +5350,15 @@ private static bool TryReadTrigonometricDenominator(
52975350 /// https://github.com/asc-community/AngouriMath/issues/718
52985351 /// </remarks>
52995352 internal static Entity? SolveByInverseTrigonometricSubstitution(Entity expr, Entity.Variable x, bool integrateByParts)
5353+ => UndoingTheInverseTrigonometricFunction(expr, x, integrateByParts, onlyTheSineAndCosineIntegrals: false);
5354+
5355+ /// <summary>
5356+ /// <see cref="SolveByInverseTrigonometricSubstitution"/>, or, with
5357+ /// <paramref name="onlyTheSineAndCosineIntegrals"/>, the substitution taken because the
5358+ /// inverse function is below the bar and the question in <c>u</c> asked of the sine and
5359+ /// cosine integrals' rule alone.
5360+ /// </summary>
5361+ private static Entity? UndoingTheInverseTrigonometricFunction(Entity expr, Entity.Variable x, bool integrateByParts, bool onlyTheSineAndCosineIntegrals)
53005362 {
53015363 // The one inverse function, of the variable or of a linear in it.
53025364 Entity? inverse = null;
@@ -5416,13 +5478,25 @@ private static bool TryReadTrigonometricDenominator(
54165478 var powerOfTheInverse = expr.Nodes.Any(node =>
54175479 node is Powf(var @base, Number.Integer power) && power.EInteger.CompareTo(EInteger.One) > 0
54185480 && @base.ContainsNode(inverse) && !@base.Substitute(inverse, u).ContainsNode(x));
5419- if (radicalsRemoved == 0 && !exponentialOfTheInverse && !powerOfTheInverse)
5420- return null;
5421- // The cosecant and the cotangent carry the sign of u into the root, and a first
5422- // power of either beside a root is parts' -- `arccot(x)/(1 + x^2)^(3/2)` is
5423- // `x arccot(x)/sqrt(1 + x^2) + 1/sqrt(1 + x^2)` there, with no sign in it.
5424- if (inverse is Arccosecantf or Arccotanf && !exponentialOfTheInverse && !powerOfTheInverse)
5425- return null;
5481+ // Or, asked for the sine and cosine integrals, the inverse function below the bar, or
5482+ // a function of it alone there: `x^m/(a + b arcsin(c x))^n` is a polynomial in the
5483+ // sine and cosine over a power of `a + b u`, where parts in x has no integral of the
5484+ // reciprocal to take.
5485+ if (onlyTheSineAndCosineIntegrals)
5486+ {
5487+ if (!TheInverseIsBelowTheBar(expr, inverse, x))
5488+ return null;
5489+ }
5490+ else
5491+ {
5492+ if (radicalsRemoved == 0 && !exponentialOfTheInverse && !powerOfTheInverse)
5493+ return null;
5494+ // The cosecant and the cotangent carry the sign of u into the root, and a first
5495+ // power of either beside a root is parts' -- `arccot(x)/(1 + x^2)^(3/2)` is
5496+ // `x arccot(x)/sqrt(1 + x^2) + 1/sqrt(1 + x^2)` there, with no sign in it.
5497+ if (inverse is Arccosecantf or Arccotanf && !exponentialOfTheInverse && !powerOfTheInverse)
5498+ return null;
5499+ }
54265500 // And nothing else of `x` under a root, which the construction did not reach:
54275501 // `x^3 arcsin(x)/sqrt(1 - x^4)` under the sine is a root of `1 - sin(u)^4`, worse
54285502 // than what it came from, and 1.4 s of declining it.
@@ -5467,7 +5541,10 @@ node is Powf(var @base, Number.Rational power) && power is not Number.Integer &&
54675541 // The same question in another variable, not a step in the search for it: asked at
54685542 // the top when this was, so the closed rules that answer only at the top --
54695543 // `e^u sin(u)^3` is the exponential-times-trigonometric rule's -- are consulted.
5470- if (Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts) is not { } result)
5544+ var result = onlyTheSineAndCosineIntegrals
5545+ ? SolveATrigonometricOfALinearOverAPowerOfALinear(integrand, u)
5546+ : Integration.ComputeAsAQuestionOfItsOwn(integrand, u, integrateByParts);
5547+ if (result is null)
54715548 return null;
54725549 if (result.ContainsNode(signOfU))
54735550 // The sign squared is one, and the sign itself is the sign of x.
0 commit comments