Skip to content

Commit 2b86349

Browse files
Arms no real coefficients reach are left out of a symbolic quadratic's antiderivative (#1783)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 9114959 commit 2b86349

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,21 @@ also how the substitution `u = x^2` writes `x/(a + c x^4)^2` since 2.5.0, having
358358
| `"(x/(a + c*x^2))^2".ToEntity().Integrate("x")` | `integral(...)` | the same |
359359
| `"x/(a + c*x^4)^2".ToEntity().Integrate("x")` | the answer, in 0.1 s; 22 s on the unreleased master | the answer, in 0.06 s |
360360

361+
### Arms no real coefficients reach are left out of a symbolic quadratic's antiderivative
362+
363+
**Shorter answers.** An integrand over a quadratic with symbols in it is answered by arms on its
364+
leading coefficient and on the sign of its discriminant `4 a c - b^2`, and two of them could not be
365+
reached: with no linear term and a number for the constant one the discriminant is zero only where
366+
the leading coefficient is, which the first arm takes -- and that arm divided by it; and with no
367+
constant term the discriminant is `-b^2`, never positive for a real `b`. Both are left out, which is
368+
up to a third of such an answer, and of every answer built on one, as `1/(x (a + b x^n))` is
369+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
370+
371+
| Input | Was (2.5.0) | Now |
372+
|---|---|---|
373+
| `"1/(1 + q*x^2)".ToEntity().Integrate("x")` | four arms, the third `-2/(2 q x)` on `4 q = 0` | three, without it |
374+
| `"1/(x*(a + b*x))".ToEntity().Integrate("x")` | four arms, the second an arctangent on `-a^2 > 0` | three, without it |
375+
361376
### A polynomial with symbols in it that is a binomial or an even quartic in a shifted variable is integrated in it
362377

363378
**Answers where there were none.** `1/(c^2 x^3 + 3 b c x^2 + 3 b^2 x + 3 a b)` was left unevaluated,

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,18 +960,47 @@ private static Entity IntegrateRationalQuadratic(Entity numerator, Entity a, Ent
960960
var sqrtNegDiscriminant = MathS.Sqrt(-discriminant);
961961
var lnCase = numerator * AntiderivativeLog((twoAxPlusB - sqrtNegDiscriminant) / (twoAxPlusB + sqrtNegDiscriminant)) / sqrtNegDiscriminant;
962962

963-
// Return as piecewise based on a and discriminant
963+
// Return as piecewise based on a and discriminant, without the arms no real
964+
// coefficients reach: see ReachesAPositiveDiscriminant and ReachesAZeroDiscriminant.
964965
var cases = new List<Entity.Providedf>();
965966
if (!denominatorVanishesWithoutA)
966967
cases.Add(new Entity.Providedf(linearCase, a.EqualTo(0)));
967968
if (ArmOffTheRealLine(arctanCase, discriminant) is { } offTheRealLine)
968969
cases.Add(offTheRealLine);
969-
cases.Add(new Entity.Providedf(arctanCase, discriminant > 0));
970-
cases.Add(new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)));
970+
if (ReachesAPositiveDiscriminant(b, c))
971+
cases.Add(new Entity.Providedf(arctanCase, discriminant > 0));
972+
if (ReachesAZeroDiscriminant(b, c))
973+
cases.Add(new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)));
971974
cases.Add(new Entity.Providedf(lnCase, discriminant < 0));
972975
return MathS.Piecewise(cases).InnerSimplified;
973976
}
974977

978+
/// <summary>
979+
/// Whether <c>4 a c - b^2</c> can be positive past the arms before it: not with no
980+
/// constant term and a real <c>b</c>, where it is <c>-b^2</c>. <c>1/(u (a + b u))</c>,
981+
/// which every <c>1/(x (a + b x^n))</c> comes to, carried an arctangent on
982+
/// <c>-a^2 > 0</c>. Real as written only: symbols and real numbers by arithmetic and
983+
/// whole powers. Under `sqrt(-(a + b x)^2)` the coefficient is `sqrt(-b^2) a/b`, and the
984+
/// discriminant is `a^2`.
985+
/// </summary>
986+
private static bool ReachesAPositiveDiscriminant(Entity b, Entity c)
987+
=> !TreeAnalyzer.IsZero(c) || !b.InnerSimplified.Nodes.All(node => node switch
988+
{
989+
Entity.Variable or Entity.Number.Real => true,
990+
Entity.Sumf or Entity.Minusf or Entity.Mulf or Entity.Divf => true,
991+
Entity.Powf(_, Entity.Number.Integer) => true,
992+
_ => false
993+
});
994+
995+
/// <summary>
996+
/// Whether <c>4 a c - b^2</c> can be zero where <c>a</c> is not, which the first arm
997+
/// takes: not with no linear term and a number other than 0 for the constant one, where it
998+
/// is <c>4 a c</c>. <c>1/(1 + q u^2)</c> carried a perfect-square arm on <c>4 q = 0</c>,
999+
/// dividing by <c>q</c>.
1000+
/// </summary>
1001+
private static bool ReachesAZeroDiscriminant(Entity b, Entity c)
1002+
=> !TreeAnalyzer.IsZero(b) || c is not Entity.Number || c.Evaled is Entity.Number.Complex { IsZero: true };
1003+
9751004
/// <summary>
9761005
/// ∫ k / (a x^2 + b x + c)^n dx for a whole n of two or more, by the reduction that
9771006
/// takes one power off the denominator at a time.
@@ -1040,15 +1069,14 @@ private static Entity IntegrateOverPowerOfQuadratic(
10401069
: 2 * MathS.Arctan(derivative / sqrtDiscriminant) / sqrtDiscriminant;
10411070
Entity firstPowerLog = withoutTheFirstPower ? Entity.Number.Integer.Zero
10421071
: AntiderivativeLog((derivative - sqrtNegDiscriminant) / (derivative + sqrtNegDiscriminant)) / sqrtNegDiscriminant;
1043-
var arms = new List<Entity.Providedf>
1044-
{
1045-
new Entity.Providedf(linearCase, a.EqualTo(0)),
1046-
new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0))
1047-
};
1072+
var arms = new List<Entity.Providedf> { new Entity.Providedf(linearCase, a.EqualTo(0)) };
1073+
if (ReachesAZeroDiscriminant(b, c))
1074+
arms.Add(new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)));
10481075
var arctanArm = Reduce(firstPowerArctan);
10491076
if (ArmOffTheRealLine(arctanArm, discriminant) is { } offTheRealLine)
10501077
arms.Add(offTheRealLine);
1051-
arms.Add(new Entity.Providedf(arctanArm, discriminant > 0));
1078+
if (ReachesAPositiveDiscriminant(b, c))
1079+
arms.Add(new Entity.Providedf(arctanArm, discriminant > 0));
10521080
arms.Add(new Entity.Providedf(Reduce(firstPowerLog), discriminant < 0));
10531081
return MathS.Piecewise(arms).InnerSimplified;
10541082

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using System.Linq;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// A symbolic quadratic below the bar is answered by arms on its leading coefficient and the
17+
/// sign of its discriminant, and an arm no real coefficients reach is left out: with no
18+
/// linear term and a number for the constant one the discriminant is zero only where the
19+
/// leading coefficient is, and with no constant term it is never positive.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
21+
/// </summary>
22+
[Trait("Area", "Calculus")]
23+
public sealed class QuadraticDenominatorArmsTest
24+
{
25+
[Theory]
26+
[InlineData("1/(1 + q*x^2)")]
27+
[InlineData("1/(3 + q*x^2)^2")]
28+
[InlineData("1/(x*(a + q*x))")]
29+
[InlineData("1/(x*(a + q*x))^2")]
30+
public void HasThreeArms(string integrand)
31+
{
32+
var integral = integrand.ToEntity().Integrate("x");
33+
Assert.Equal(3, integral.Nodes.OfType<Entity.Piecewise>().Sum(piecewise => piecewise.Cases.Count()));
34+
foreach (var q in new[] { 0.7, -0.7 })
35+
{
36+
Entity Pinned(Entity e) => e.Substitute("q", q).Substitute("a", 1.3);
37+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
38+
var original = Pinned(integrand.ToEntity());
39+
foreach (var at in new[] { 0.2, 0.5 })
40+
{
41+
var want = original.Substitute("x", at).EvalNumerical();
42+
var got = derivative.Substitute("x", at).EvalNumerical();
43+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
44+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
45+
$"d/dx of the antiderivative of {integrand} at q = {q} is {got} at x = {at}, where the integrand is {want}");
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)