Skip to content

Commit eaf9d91

Browse files
Rafael-SOWNetclaude
andcommitted
A half-angle integrand with symbols in it is not simplified
Under t = tan(x/2) the integrand was simplified before it was integrated, and with symbols in it the simplifier's search grew past any budget: sec(x)^2/(a + b cos(x))^3 spent 43 s in it and the fourth power more than two minutes, where integrating what it returned takes 0.14 s. With a symbol in it the integrand is put over one bar and not simplified; one without a symbol is simplified as before. Two things the simplifier did are done instead, since the steps after it read them: whole powers of products are written as products of powers, so that the 1 + t^2 the division takes off as a factor is not hidden inside ((1 + t^2)(1 - t^2))^6, and a base of a lower degree than it is written in is written as its polynomial, a (1 + t^2) + a (1 - t^2) as 2 a, where read as a quadratic its leading coefficient is zero as a value only. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 2340e8a commit eaf9d91

3 files changed

Lines changed: 110 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ poles of `tan(x/2)`, as every half-angle answer.
630630
| `"1/(a+b*cos(x)+c*cos(x)^2)".Integrate("x")` | unevaluated | the cosine form |
631631
| `"csc(x)^2/(a-a*sin(x)^2)".Integrate("x")` | unevaluated | `-cot(x)/a` and two terms rational in `tan(x/2)` |
632632

633+
### A rational function of the sine and cosine with symbols in it is no longer simplified under the half angle
634+
635+
**Answers where there were none.** Under `t = tan(x/2)` the integrand was simplified before it was
636+
integrated, and with symbols in it the simplifier's search grew past any budget:
637+
`sec(x)^2/(a + b cos(x))^3` spent 43 s in it and `sec(x)^2/(a + b cos(x))^4` more than two minutes,
638+
where integrating what it returned takes a tenth of a second. With a symbol in it the integrand is
639+
now put over one bar with its whole powers of products written as products of powers, and not
640+
simplified further; one without a symbol is simplified as before
641+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
642+
643+
| Input | Was (2.5.0) | Now |
644+
|---|---|---|
645+
| `"sec(x)^2/(a + b*cos(x))^3".ToEntity().Integrate("x")` | `integral(...)` | a piecewise antiderivative in `tan(x/2)` |
646+
| `"sec(x)^2/(a + b*cos(x))^4".ToEntity().Integrate("x")` | `integral(...)` | the same |
647+
| `"sec(c + d*x)^2/(a + b*cos(c + d*x))^4".ToEntity().Integrate("x")` | `integral(...)` | the same in `tan((c + d x)/2)` |
648+
633649
### A linear over a quadratic beside the root of another quadratic is integrated
634650

635651
`(g + h x)/(A sqrt(B))`, `A` and `B` two different quadratics, was left unevaluated wherever a

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22842,7 +22842,25 @@ static bool IsOneOfTheFour(Entity node, Entity argument)
2284222842
// 1/(1 - sin(x)) rewrote to 2/((t^2 + 1)(1 + (-2)t/(t^2 + 1))) and stopped there, one
2284322843
// distribution short of 2/(t^2 - 2t + 1), which is integrated at once.
2284422844
// https://github.com/asc-community/AngouriMath/issues/1239
22845-
var integrand = Functions.SingleQuotient.Combine(inT * 2 / (rate * (1 + tSquared))).Simplify();
22845+
// Once combined, a quotient with a symbol in its coefficients is not simplified: the
22846+
// simplifier's search grows with the symbols, and `sec(x)^2/(a + b cos(x))^3` spent 43 s
22847+
// in it where integrating what it returned took a tenth of a second. Two things the
22848+
// simplifier did are done here, since what follows reads them. Whole powers of
22849+
// products are written as products of powers: `((1 + t^2)(1 - t^2))^6` keeps the
22850+
// `1 + t^2` the division below takes off as a factor out of its sight, and
22851+
// `tan(x)^6/(a + b sec(x))` was declined that way. And a base of a lower degree than it
22852+
// is written in is written as its polynomial: `a (1 + t^2) + a (1 - t^2)` is `2 a`, and
22853+
// read as a quadratic its leading coefficient is zero as a value only.
22854+
var combined = Functions.SingleQuotient.Combine(inT * 2 / (rate * (1 + tSquared)));
22855+
Entity integrand;
22856+
if (combined.Vars.Any(symbol => symbol != t))
22857+
{
22858+
var (combinedAbove, combinedBelow) = Functions.SingleQuotient.Of(combined.InnerSimplified);
22859+
integrand = (WithDegenerateBasesLowered(WithWholePowersDistributed(combinedAbove), t)
22860+
/ WithDegenerateBasesLowered(WithWholePowersDistributed(combinedBelow), t)).InnerSimplified;
22861+
}
22862+
else
22863+
integrand = combined.Simplify();
2284622864

2284722865
// Collapsing the nesting attaches a condition saying the denominator it cleared is
2284822866
// non-zero, and that denominator is 1 + t^2 -- so 1/(1 + cos(x)) comes out as
@@ -22900,6 +22918,61 @@ static bool NoRemainder(Entity rest)
2290022918
: null;
2290122919
}
2290222920

22921+
/// <summary>
22922+
/// <paramref name="product"/> with each whole power of a product written as the product
22923+
/// of the powers, the rest as written: <c>((1 + t^2)(1 - t^2))^6</c> is
22924+
/// <c>(1 + t^2)^6 (1 - t^2)^6</c>.
22925+
/// </summary>
22926+
private static Entity WithWholePowersDistributed(Entity product)
22927+
{
22928+
Entity written = Number.Integer.One;
22929+
foreach (var factor in Mulf.LinearChildren(product))
22930+
{
22931+
if (factor is Powf(Mulf inner, Number.Integer { EInteger.Sign: > 0 } power))
22932+
foreach (var innerFactor in Mulf.LinearChildren(WithWholePowersDistributed(inner)))
22933+
written *= innerFactor is Powf(var @base, Number.Integer exponent)
22934+
? MathS.Pow(@base, Number.Integer.Create(exponent.EInteger.Multiply(power.EInteger)))
22935+
: MathS.Pow(innerFactor, power);
22936+
else
22937+
written *= factor;
22938+
}
22939+
return written;
22940+
}
22941+
22942+
/// <summary>
22943+
/// <paramref name="product"/> with each base that is a sum written as its polynomial in
22944+
/// <paramref name="t"/> where that is of a lower degree than the base is written in, and
22945+
/// as written otherwise: <c>a (1 + t^2) + a (1 - t^2)</c> is <c>2 a</c>.
22946+
/// </summary>
22947+
private static Entity WithDegenerateBasesLowered(Entity product, Entity.Variable t)
22948+
{
22949+
Entity written = Number.Integer.One;
22950+
foreach (var factor in Mulf.LinearChildren(product))
22951+
{
22952+
var (@base, power) = factor is Powf(var raised, Number.Integer exponent) ? (raised, (Entity)exponent) : (factor, Number.Integer.One);
22953+
if (@base is Sumf or Minusf && @base.ContainsNode(t) && TreeAnalyzer.TryGetPolynomial(@base, t, out var terms)
22954+
&& terms.Count > 0 && terms.All(term => term.Key.Sign >= 0 && !term.Value.ContainsNode(t)))
22955+
{
22956+
var writtenDegree = @base.Nodes.Select(node => node is Powf(var raised, Number.Integer exponent) && raised == t ? exponent.EInteger
22957+
: node == t ? EInteger.One : EInteger.Zero).Max()!;
22958+
var kept = terms.Where(term => !Functions.PartialFractions.IsZeroAsAValue(term.Value)).ToList();
22959+
var degree = kept.Select(term => term.Key).DefaultIfEmpty(EInteger.Zero).Max()!;
22960+
if (degree.CompareTo(writtenDegree) < 0)
22961+
{
22962+
Entity polynomial = Number.Integer.Zero;
22963+
foreach (var term in kept.OrderByDescending(term => term.Key))
22964+
{
22965+
var coefficient = term.Value.InnerSimplified;
22966+
polynomial += term.Key.IsZero ? coefficient : coefficient * MathS.Pow(t, Number.Integer.Create(term.Key));
22967+
}
22968+
@base = polynomial.InnerSimplified;
22969+
}
22970+
}
22971+
written *= power == Number.Integer.One ? @base : MathS.Pow(@base, power);
22972+
}
22973+
return written;
22974+
}
22975+
2290322976
/// <summary>
2290422977
/// <paramref name="denominator"/> with one power fewer of a factor that is
2290522978
/// <c>1 + t^2</c>, the rest of the product as written; null where no factor is.

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,29 @@ public void ASumTheOrdinaryRulesAnswerBetter(string integrand)
235235
[InlineData("sec(x)^2/(a + b*cos(x))")]
236236
[InlineData("tan(x)^4/(a + a*cos(x))")]
237237
[InlineData("csc(x)^2/(a + a*cos(x))")]
238-
public void ARationalFunctionWithSymbolsForCoefficients(string integrand)
238+
public void ARationalFunctionWithSymbolsForCoefficients(string integrand) => DifferentiatesBackWithSymbols(integrand);
239+
240+
/// <summary>
241+
/// A power of <c>a + b cos(x)</c> past the square beside a power of the secant. The
242+
/// substitution's integrand in <c>t</c> is not simplified where a symbol is in it:
243+
/// simplified, it took 43 s at the cube and more than two minutes at the fourth power,
244+
/// where integrating it takes a tenth of a second.
245+
/// </summary>
246+
[Theory]
247+
[InlineData("sec(x)^2/(a + b*cos(x))^3")]
248+
[InlineData("sec(x)^2/(a + b*cos(x))^4")]
249+
[InlineData("sec(c + d*x)^2/(a + b*cos(c + d*x))^4")]
250+
[InlineData("sec(x)/(a + b*cos(x))^3")]
251+
[InlineData("1/(cos(x)*(a + b*cos(x))^2)")]
252+
public void APowerOfALinearInTheCosineBesideASecant(string integrand) => DifferentiatesBackWithSymbols(integrand);
253+
254+
private static void DifferentiatesBackWithSymbols(string integrand)
239255
{
240256
var integral = integrand.ToEntity().Integrate("x");
241257
Assert.DoesNotContain("integral(", integral.Stringize());
242-
var derivative = integral.Substitute("C", 0).Differentiate("x").Substitute("a", 1.7).Substitute("b", 0.6);
243-
var original = integrand.ToEntity().Substitute("a", 1.7).Substitute("b", 0.6);
258+
Entity Pin(Entity e) => e.Substitute("a", 1.7).Substitute("b", 0.6).Substitute("c", 0.3).Substitute("d", 1.1);
259+
var derivative = Pin(integral.Substitute("C", 0).Differentiate("x"));
260+
var original = Pin(integrand.ToEntity());
244261
var compared = 0;
245262
foreach (var at in Points)
246263
{

0 commit comments

Comments
 (0)