Skip to content

Commit f46d55f

Browse files
A root of a quadratic in the tangent is rotated until the quadratic has no linear term (#1475)
`1/sqrt(1 + 2 tan(x) + 3 tan(x)^2)` was left as written, though `1/((1 + t^2) sqrt(A + C t^2))` -- what the tangent substitution leaves of it once the linear term is gone -- was already answered. Under `x = y + arctan(m)` the tangent becomes `(tan(y) + m)/(1 - m tan(y))`, the Möbius map that preserves `1 + tan^2`, and the quadratic's linear coefficient becomes `b(1 - m^2) + 2(c - a)m`: zero for a root of `b m^2 + 2(a - c)m - b`, whose discriminant `4((a - c)^2 + b^2)` is never negative, so the rotation is always real. The two roots are the quadratic form's perpendicular directions and either will do. The radicand is assembled -- `A = a + bm + cm^2`, `C = am^2 - bm + c` -- and not substituted into: writing the quotient inside the root leaves a nesting that nothing downstream reduces, which is how the first version of this rule turned a decline into a ninety-second search. The modulus that `sqrt(N/(1 - mS)^2)` leaves comes out in front as `sgn(1 - m tan(y))`, constant between its zeros. A numeric rotation only. With symbolic coefficients `m` is a nested surd, the rotated quadratic is written in it, and what the rational integrator is handed is a quotient over that field: two minutes and no answer, where the integrand is declined in under a second unrotated. That bound is on the work rather than on the mathematics, and a test pins that the symbolic shape is still declined quickly. Family 4 of the Rubi suite: 290/422 either way, 0 wrong -- its rows of this family are symbolic; family 6 377/417 and the 1774-problem suite 1707 unchanged. Suite 12655 passed; allocation gate passed on all 19 gated benchmarks. Each new row differentiates back at five points. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0c2ad34 commit f46d55f

4 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,29 @@ times a cosine of `u`, which the closed rules answer. Rubi's 4.2.10
11891189
| `"x^3*sqrt(a-a*cos(x))".Integrate("x")` | left unevaluated | the antiderivative |
11901190
| `"x^2*sqrt(a+a*sin(x))".Integrate("x")` | left unevaluated | the antiderivative |
11911191

1192+
### A root of a quadratic in the tangent is rotated until the quadratic has no linear term
1193+
1194+
`1/sqrt(1 + 2 tan(x) + 3 tan(x)^2)` was left as written. Under `x = y + arctan(m)` the tangent
1195+
becomes `(tan(y) + m)/(1 - m tan(y))` -- the Möbius map that preserves `1 + tan^2`, which is the
1196+
factor the tangent substitution's `dx` brings -- and the quadratic's linear coefficient becomes
1197+
`b(1 - m^2) + 2(c - a)m`, zero for a root of `b m^2 + 2(a - c)m - b`, whose discriminant
1198+
`4((a - c)^2 + b^2)` is never negative. The rotated quadratic `A + C tan(y)^2` goes through the
1199+
tangent substitution to `1/((1 + t^2) sqrt(A + C t^2))`, which was already answered. The radicand
1200+
is *assembled* -- `A = a + bm + cm^2`, `C = am^2 - bm + c` -- rather than substituted into, since
1201+
writing the quotient inside the root leaves a nesting nothing downstream reduces; and a
1202+
`sgn(1 - m tan(y))` comes out in front, the modulus that `sqrt(N/(1 - mS)^2)` leaves.
1203+
1204+
A numeric rotation only: with symbolic coefficients `m` is a nested surd, the rotated quadratic is
1205+
written in it, and the rational integrator is handed a quotient over that field -- two minutes and
1206+
no answer, where the integrand is declined in under a second unrotated, so the symbolic shape
1207+
keeps its decline ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
1208+
1209+
| Input | Was (2.5.0) | Now |
1210+
|---|---|---|
1211+
| `"1/sqrt(1+2*tan(x)+3*tan(x)^2)".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(1 - m tan(x - arctan(m)))` in front |
1212+
| `"tan(x)/sqrt(2+tan(x)+tan(x)^2)".Integrate("x")` | left unevaluated | the antiderivative |
1213+
| `"tan(x)^2/(2+tan(x)+tan(x)^2)^(3/2)".Integrate("x")` | left unevaluated | the antiderivative |
1214+
11921215
### `binomial(n, k)` is a function
11931216

11941217
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14317,6 +14317,120 @@ private static (Entity Factor, Entity Inside)? TryTakeACommonLinearFactorOfASum(
1431714317
return (factored, inside);
1431814318
}
1431914319

14320+
/// <summary>
14321+
/// A root of a quadratic in <c>tan(x)</c> with a linear term, rotated until it has none:
14322+
/// <c>1/sqrt(a + b tan(x) + c tan(x)^2)</c> is <c>1/sqrt(A + C tan(y)^2)</c> under
14323+
/// <c>x = y + arctan(m)</c>, and the tangent substitution then leaves
14324+
/// <c>1/((1 + t^2) sqrt(A + C t^2))</c>, which is answered in closed form.
14325+
/// </summary>
14326+
/// <remarks>
14327+
/// <para>
14328+
/// <c>tan(y + f)</c> is <c>(tan(y) + m)/(1 - m tan(y))</c> with <c>m = tan(f)</c>, which
14329+
/// is the Möbius map that preserves <c>1 + tan^2</c> -- the factor the tangent
14330+
/// substitution's <c>dx</c> brings. Under it the quadratic's linear coefficient becomes
14331+
/// <c>b(1 - m^2) + 2(c - a)m</c>, zero for a root of <c>b m^2 + 2(a - c)m - b</c>, whose
14332+
/// discriminant <c>4((a - c)^2 + b^2)</c> is never negative: the two roots are the
14333+
/// quadratic form's two perpendicular directions and either will do.
14334+
/// </para>
14335+
/// <para>
14336+
/// The radicand is <b>assembled</b> rather than substituted into. Writing
14337+
/// <c>tan(x)</c> as the quotient inside the root leaves a nested quotient that nothing
14338+
/// downstream reduces, so the substitution never sees the rotated quadratic; here the
14339+
/// root becomes <c>N(S)^p (1 - m S)^(-2p)</c> with <c>N(S) = A + C S^2</c> computed in
14340+
/// closed form, <c>A = a + b m + c m^2</c> and <c>C = a m^2 - b m + c</c>. For a half-odd
14341+
/// power the modulus is what the root leaves -- <c>sqrt(N/(1 - mS)^2)</c> is
14342+
/// <c>sqrt(N)/|1 - mS|</c> -- so a <c>sgn(1 - m tan(y))</c> comes out in front, constant
14343+
/// between its zeros as every such sign in these rules is.
14344+
/// </para>
14345+
/// <para>
14346+
/// The antiderivative is of the rotated integrand, so the answer is it at
14347+
/// <c>x - arctan(m)</c>: a constant shift of the argument, which an antiderivative is
14348+
/// free to have. Rubi's 4.3.9 and the cotangent shapes the tangent substitution rewrites
14349+
/// into tangents on the way in.
14350+
/// https://github.com/asc-community/AngouriMath/issues/718
14351+
/// </para>
14352+
/// </remarks>
14353+
internal static Entity? SolveByRotatingAwayTheLinearTermInTheTangent(Entity expr, Entity.Variable x, bool integrateByParts)
14354+
{
14355+
if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
14356+
return null;
14357+
var tangent = MathS.Tan(x);
14358+
var written = expr.Replace(node => node is Cotanf(var cotangent) && cotangent == x ? 1 / tangent : node);
14359+
if (!written.ContainsNode(tangent))
14360+
return null;
14361+
// A function of the tangent alone: anything else in x is another question.
14362+
if (written.Replace(node => node == tangent ? Number.Integer.One : node).ContainsNode(x))
14363+
return null;
14364+
// One radicand, a quadratic in the tangent with a linear term, to a half-odd power.
14365+
Entity? a = null, b = null, c = null;
14366+
var roots = new List<(Entity Node, Number.Rational Power)>();
14367+
foreach (var node in written.Nodes)
14368+
{
14369+
if (node is not Powf(var radicand, Number.Rational power) || power is Number.Integer || !radicand.ContainsNode(x))
14370+
continue;
14371+
if (!power.ERational.Denominator.Equals(EInteger.FromInt32(2)))
14372+
return null;
14373+
var inT = radicand.Substitute(tangent, Variable.CreateVariableOrConstant("t_rot"));
14374+
if (inT.ContainsNode(x)
14375+
|| !TreeAnalyzer.TryGetPolynomial(inT, Variable.CreateVariableOrConstant("t_rot"), out var monomials) || monomials.Count == 0
14376+
|| monomials.Keys.Any(k => k.Sign < 0 || k.CompareTo(EInteger.FromInt32(2)) > 0))
14377+
return null;
14378+
var thisA = monomials.TryGetValue(EInteger.Zero, out var a0) ? a0 : Number.Integer.Zero;
14379+
var thisB = monomials.TryGetValue(EInteger.One, out var b1) ? b1 : Number.Integer.Zero;
14380+
var thisC = monomials.TryGetValue(EInteger.FromInt32(2), out var c2) ? c2 : Number.Integer.Zero;
14381+
if (a is not null && (a != thisA || b != thisB || c != thisC))
14382+
return null;
14383+
(a, b, c) = (thisA, thisB, thisC);
14384+
roots.Add((node, power));
14385+
}
14386+
if (a is null || b is null || c is null || roots.Count == 0
14387+
|| TreeAnalyzer.IsZero(b) || b.Evaled is Number.Complex { IsZero: true })
14388+
return null;
14389+
14390+
// m = ((c - a) + sqrt((a - c)^2 + b^2))/b, a root of b m^2 + 2(a - c) m - b.
14391+
var m = ((c - a + MathS.Sqrt((MathS.Sqr(a - c) + MathS.Sqr(b)).InnerSimplified)) / b).InnerSimplified;
14392+
// A number: with symbolic coefficients `m` is a nested surd, the rotated quadratic's
14393+
// are written in it, and what the substitution hands the rational integrator is a
14394+
// quotient over that field -- two minutes and no answer, where the integrand is
14395+
// declined in a tenth of a second unrotated. The rotation is exact for any real
14396+
// coefficients and this is a bound on the work, not on the mathematics.
14397+
if (m.Evaled is not Number.Real { IsFinite: true })
14398+
return null;
14399+
var rotatedTangent = ((tangent + m) / (1 - m * tangent)).InnerSimplified;
14400+
var scale = (1 - m * tangent).InnerSimplified;
14401+
var rotatedA = (a + b * m + c * MathS.Sqr(m)).InnerSimplified;
14402+
var rotatedC = (a * MathS.Sqr(m) - b * m + c).InnerSimplified;
14403+
var quadratic = (rotatedA + rotatedC * MathS.Sqr(tangent)).InnerSimplified;
14404+
14405+
// Each root out of the way first, so that the tangents inside it are not rewritten
14406+
// twice: the radicand's rotation is the closed form above, not the substitution.
14407+
var placeholders = new List<(Variable Symbol, Entity Value)>();
14408+
var skeleton = written;
14409+
Entity signs = Number.Integer.One;
14410+
foreach (var (node, power) in roots)
14411+
{
14412+
var symbol = Variable.CreateUnique(written, "r_rot" + placeholders.Count);
14413+
skeleton = skeleton.Substitute(node, symbol);
14414+
// (N/(1 - mS)^2)^(p/2) is N^(p/2) |1 - mS|^(-p), and |z|^(-p) is sgn(z)^p z^(-p).
14415+
var numerator = Number.Integer.Create(power.ERational.Numerator);
14416+
if (!numerator.EInteger.IsEven)
14417+
signs = signs * MathS.Signum(scale);
14418+
placeholders.Add((symbol, MathS.Pow(quadratic, power) * MathS.Pow(scale, (-numerator).InnerSimplified)));
14419+
}
14420+
if (skeleton.ContainsNode(x) && !skeleton.ContainsNode(tangent))
14421+
return null;
14422+
var rotated = skeleton.Substitute(tangent, rotatedTangent);
14423+
foreach (var (symbol, value) in placeholders)
14424+
rotated = rotated.Substitute(symbol, value);
14425+
rotated = rotated.InnerSimplified;
14426+
if (rotated.ContainsNode(x) is false || rotated.Nodes.Any(node => node == MathS.NaN))
14427+
return null;
14428+
if (Integration.ComputeAsAQuestionOfItsOwn(rotated, x, integrateByParts) is not { } answer)
14429+
return null;
14430+
var shifted = (signs == Number.Integer.One ? answer : signs * answer).Substitute(x, (x - MathS.Arctan(m)).InnerSimplified);
14431+
return shifted.Nodes.Any(node => node == MathS.NaN) ? null : shifted;
14432+
}
14433+
1432014434
/// <summary>
1432114435
/// A pair of hyperbolic powers two apart whose coefficients kill the reduction's
1432214436
/// residual: <c>cosh(y)^p - (p - 1)/p cosh(y)^(p - 2)</c> is

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
654654
// alone is both rules' and the reduction's answer for it is shorter.
655655
if ((answer = IndefiniteIntegralSolver.SolveByTrigonometricPowerSubstitution(expr, x)) is { }) return answer;
656656
if ((answer = IndefiniteIntegralSolver.SolveByTangentSubstitution(expr, x, integrateByParts)) is { }) return answer;
657+
// A root of a quadratic in the tangent with a linear term, rotated until it has
658+
// none: `1/sqrt(a + b tan(x) + c tan(x)^2)` is `1/sqrt(A + C tan(y)^2)` under
659+
// `x = y + arctan(m)`, which the substitution above then answers.
660+
if ((answer = IndefiniteIntegralSolver.SolveByRotatingAwayTheLinearTermInTheTangent(expr, x, integrateByParts)) is { }) return answer;
657661
// And the logarithm's own substitution, beside the tangent's. It goes the other way
658662
// -- `x = e^u`, so it *introduces* an exponential rather than cancelling one -- which
659663
// is why the general substitution does not find it and why it pays: the integrator

‎Sources/Tests/UnitTests/Calculus/MixedTrigonometricArgumentsTest.cs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,40 @@ public void ARootOfAProductOfTwoFunctionsIsNotLetThrough(string integrand)
186186
var integral = integrand.ToEntity().Integrate("x");
187187
Assert.DoesNotContain("NaN", integral.Stringize());
188188
}
189+
190+
/// <summary>
191+
/// A root of a quadratic in the tangent with a linear term, rotated until it has none:
192+
/// <c>x = y + arctan(m)</c> with <c>m</c> a root of <c>b m^2 + 2(a - c) m - b</c> makes
193+
/// <c>a + b tan(x) + c tan(x)^2</c> into <c>A + C tan(y)^2</c>, and the tangent
194+
/// substitution then leaves <c>1/((1 + t^2) sqrt(A + C t^2))</c>, which is answered.
195+
/// The radicand is assembled in closed form: substituting the Möbius quotient into it
196+
/// leaves a nesting that nothing downstream reduces.
197+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
198+
/// </summary>
199+
[Theory]
200+
[InlineData("1/sqrt(1 + 2*tan(x) + 3*tan(x)^2)")]
201+
[InlineData("1/sqrt(2 + tan(x) + tan(x)^2)")]
202+
[InlineData("1/sqrt(3 - 2*tan(x) + tan(x)^2)")]
203+
[InlineData("tan(x)/sqrt(2 + tan(x) + tan(x)^2)")]
204+
[InlineData("tan(x)^2/(2 + tan(x) + tan(x)^2)^(3/2)")]
205+
public void AQuadraticInTheTangentIsRotatedUntilItHasNoLinearTerm(string integrand)
206+
=> DifferentiatesBack(integrand);
207+
208+
/// <summary>
209+
/// With symbolic coefficients the rotation is a nested surd, the rotated quadratic is
210+
/// written in it, and what the rational integrator is handed is a quotient over that
211+
/// field: two minutes and no answer, where the integrand is declined in a moment
212+
/// unrotated. The rule asks for a numeric rotation, and this pins that the verdict for
213+
/// the symbolic shape is still a quick decline rather than a search.
214+
/// </summary>
215+
[Fact]
216+
public void ASymbolicQuadraticInTheTangentIsDeclinedAndNotSearched()
217+
{
218+
var watch = System.Diagnostics.Stopwatch.StartNew();
219+
var integral = "1/sqrt(a + b*tan(x) + c*tan(x)^2)".ToEntity().Integrate("x");
220+
watch.Stop();
221+
Assert.Contains("integral(", integral.Stringize());
222+
Assert.True(watch.Elapsed < IntegrationDecline.Guard, $"the symbolic shape took {watch.Elapsed.TotalSeconds:F1} s");
223+
}
189224
}
190225
}

0 commit comments

Comments
 (0)