Skip to content

Commit 080ae01

Browse files
A quadratic with a coefficient off the real line is integrated in the forms that hold there (#1599)
The table integrals of 1/sqrt(Q), 1/Q and 1/Q^n choose their form by the sign of the leading coefficient or of the discriminant. For one that is not real, i < 0 and i > 0 are each NaN, and a piecewise with no arm that holds is NaN with them: 1/(x^2 + i), 1/sqrt(i x^2 + 1) and sqrt(i x^2 + 1) were answered NaN + C, and 1/sqrt(i a x^2 + 1) with a piecewise that is NaN for every real a but zero. The product of two roots and Rubi's 4.3.2.1 row 260 come to the same integrals, and were NaN + C too. Each builder now carries an arm for a value off the real line, provided not q in RR, wherever the quantity it decides by holds the imaginary unit: the arctangent for 1/Q and 1/Q^n, the logarithm for 1/sqrt(Q), which use nothing about their root but sqrt(q)^2 = q. For the root the arm is owed when any coefficient is not real: with a real negative leading coefficient and a complex b or c the arcsine was taken, and 1/sqrt(-x^2 + (1 + i) x + 1) differentiated back to minus its integrand below x = -2. A real quadratic keeps its sign arms and its printed form. Rubi's rows with %i in the integrand, 2253 of them at a 3 s budget: 1053 solved, 27 wrong, 4 errors on master 1293b56; 1106 solved, 0 wrong, 1 error here. The 27 wrong were the NaN answers of the a + i a tan families. The families sample (912) and family 0 (1814) are unchanged, verdict for verdict. Closes #1598. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7f98f82 commit 080ae01

3 files changed

Lines changed: 184 additions & 10 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,25 @@ Each of these was checked by differentiating it back with the parameters pinned
309309
four points. The Rubi sample is unchanged at 231 of 463 with no wrong answers, so nothing that
310310
already had an antiderivative moves.
311311

312+
### `NaN` a third time, from a quadratic with a coefficient off the real line
313+
314+
The integrals of `1/sqrt(Q)`, `1/Q` and `1/Q^n` over a quadratic `Q` choose their form by the sign
315+
of the leading coefficient or of the discriminant, and had no arm for one that is not real: `i < 0`
316+
and `i > 0` are each `NaN`, the complex numbers not being ordered, and a piecewise with no arm that
317+
holds is `NaN` with them. With a symbol beside the imaginary unit the arms stayed standing, and none
318+
of them holds for a real value of it but zero. Where the sign was real and the rest of the quadratic
319+
was not, the arcsine was taken, and it differentiates back to minus the integrand on part of the
320+
line. Such a quadratic now takes the arctangent or the logarithm, which use nothing about their root
321+
but `sqrt(q)^2 = q` ([#1598](https://github.com/asc-community/AngouriMath/issues/1598)).
322+
323+
| Input | Was (2.5.0) | Now |
324+
|---|---|---|
325+
| `"1/(x^2 + i)".Integrate("x")`, and `1/(x^2 + i)^2`, `1/sqrt(i*x^2 + 1)`, `sqrt(i*x^2 + 1)` | `NaN + C` | `2 arctan(2x/sqrt(4i))/sqrt(4i) + C`, and the antiderivatives |
326+
| `"1/sqrt(i*a*x^2 + 1)".Integrate("x")` | a piecewise over `i a = 0`, `i a < 0` and `i a > 0`, `NaN` for every real `a` but zero | the same with an arm `provided not i a in RR`, the logarithm |
327+
| `"1/sqrt(-x^2 + (1 + i)*x + 1)".Integrate("x")` | `-arcsin((-2x + 1 + i)/sqrt(4 + 2i)) + C`, whose derivative is minus the integrand below `x = -2` | `ln(-2x + 1 + i + 2i sqrt(-x^2 + (1 + i) x + 1))/i + C` |
328+
| `"1/sqrt(-x^2 + i)".Integrate("x")` | `-arcsin(-2x/sqrt(4i)) + C` | `ln(-2x + 2i sqrt(-x^2 + i))/i + C`, an antiderivative as before, in the logarithm |
329+
| `"1/(sqrt(x)*sqrt(1 + i*x))".Integrate("x")`, and Rubi 4.3.2.1's `sqrt(tan(c + d x))/(a + i a tan(c + d x))^(5/2)` | `integral(...)` | the antiderivative |
330+
312331
### Trigonometric functions of multiples of one linear argument are written in it
313332

314333
`csc(a + b x) csc(2a + 2b x)^2` was left unevaluated, and `csc(1 + x) csc(2 + 2x)^2` ran twenty

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

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,42 @@ private static Entity IntegrateOverRootOfQuadratic(
759759
? numerator * x / MathS.Sqrt(c)
760760
: 2 * numerator * MathS.Sqrt(b * x + c) / b;
761761

762-
return MathS.Piecewise([
763-
new Entity.Providedf(linearCase, a.EqualTo(0)),
764-
new Entity.Providedf(arcsinCase, a < 0),
765-
new Entity.Providedf(logarithmCase, a > 0)
766-
]);
762+
var arms = new List<Entity.Providedf> { new Entity.Providedf(linearCase, a.EqualTo(0)) };
763+
if (ArmOffTheRealLine(logarithmCase, a, b, c) is { } offTheRealLine)
764+
arms.Add(offTheRealLine);
765+
arms.Add(new Entity.Providedf(arcsinCase, a < 0));
766+
arms.Add(new Entity.Providedf(logarithmCase, a > 0));
767+
return MathS.Piecewise(arms);
768+
}
769+
770+
/// <summary>
771+
/// The arm that a piecewise on a sign owes a quadratic off the real line, answered by
772+
/// <paramref name="form"/> wherever one of <paramref name="coefficients"/> is not real;
773+
/// <see langword="null"/> where none of them holds the imaginary unit, and the sign arms
774+
/// are left to decide.
775+
/// </summary>
776+
/// <remarks>
777+
/// The sign arms choose between forms by which is real where the integrand is, and off
778+
/// the real line that has nothing to go on. <c>i &lt; 0</c> and <c>i &gt; 0</c> are each
779+
/// NaN, the complex numbers not being ordered, and a piecewise with no arm that holds is
780+
/// NaN with them: <c>1/(x^2 + i)</c> and <c>1/sqrt(i x^2 + 1)</c> answered <c>NaN + C</c>,
781+
/// and <c>1/sqrt(i a x^2 + 1)</c> a piecewise that is NaN for every real <c>a</c> but zero.
782+
/// Nor is a real sign any help where the rest of the quadratic is not real: with
783+
/// <c>E = b^2 - 4ac</c> the arcsine differentiates back to <c>1/sqrt(Q)</c> only where
784+
/// <c>sqrt(E) sqrt(Q/E)</c> is <c>sqrt(Q)</c>, and <c>1/sqrt(-x^2 + (1 + i) x + 1)</c>
785+
/// came back as minus its integrand below -2. The arctangent and the logarithm use nothing about their root but
786+
/// <c>sqrt(q)^2 = q</c>, and are antiderivatives off the real line as well.
787+
/// https://github.com/asc-community/AngouriMath/issues/1598
788+
/// </remarks>
789+
private static Entity.Providedf? ArmOffTheRealLine(Entity form, params Entity[] coefficients)
790+
{
791+
Entity? offTheRealLine = null;
792+
foreach (var coefficient in coefficients)
793+
if (coefficient.InnerSimplified.Nodes.Any(node => node is Entity.Number.Complex number && !number.ImaginaryPart.EDecimal.IsZero))
794+
offTheRealLine = offTheRealLine is null
795+
? !coefficient.In(AngouriMath.Core.Domain.Real)
796+
: offTheRealLine | !coefficient.In(AngouriMath.Core.Domain.Real);
797+
return offTheRealLine is null ? null : new Entity.Providedf(form, offTheRealLine);
767798
}
768799

769800
/// <summary>
@@ -885,6 +916,8 @@ private static Entity IntegrateRationalQuadratic(Entity numerator, Entity a, Ent
885916
var cases = new List<Entity.Providedf>();
886917
if (!denominatorVanishesWithoutA)
887918
cases.Add(new Entity.Providedf(linearCase, a.EqualTo(0)));
919+
if (ArmOffTheRealLine(arctanCase, discriminant) is { } offTheRealLine)
920+
cases.Add(offTheRealLine);
888921
cases.Add(new Entity.Providedf(arctanCase, discriminant > 0));
889922
cases.Add(new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)));
890923
cases.Add(new Entity.Providedf(lnCase, discriminant < 0));
@@ -959,12 +992,17 @@ private static Entity IntegrateOverPowerOfQuadratic(
959992
: 2 * MathS.Arctan(derivative / sqrtDiscriminant) / sqrtDiscriminant;
960993
Entity firstPowerLog = withoutTheFirstPower ? Entity.Number.Integer.Zero
961994
: AntiderivativeLog((derivative - sqrtNegDiscriminant) / (derivative + sqrtNegDiscriminant)) / sqrtNegDiscriminant;
962-
return MathS.Piecewise([
995+
var arms = new List<Entity.Providedf>
996+
{
963997
new Entity.Providedf(linearCase, a.EqualTo(0)),
964-
new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)),
965-
new Entity.Providedf(Reduce(firstPowerArctan), discriminant > 0),
966-
new Entity.Providedf(Reduce(firstPowerLog), discriminant < 0)
967-
]).InnerSimplified;
998+
new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0))
999+
};
1000+
var arctanArm = Reduce(firstPowerArctan);
1001+
if (ArmOffTheRealLine(arctanArm, discriminant) is { } offTheRealLine)
1002+
arms.Add(offTheRealLine);
1003+
arms.Add(new Entity.Providedf(arctanArm, discriminant > 0));
1004+
arms.Add(new Entity.Providedf(Reduce(firstPowerLog), discriminant < 0));
1005+
return MathS.Piecewise(arms).InnerSimplified;
9681006

9691007
Entity Reduce(Entity firstPower)
9701008
{
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// The table integrals over a quadratic <c>Q</c> -- of <c>1/sqrt(Q)</c>, <c>1/Q</c> and
16+
/// <c>1/Q^n</c> -- chose their form by the sign of the leading coefficient or of the
17+
/// discriminant, and had no arm for one off the real line.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1598">#1598</a>
19+
/// </summary>
20+
/// <remarks>
21+
/// <c>i &lt; 0</c> and <c>i &gt; 0</c> are each NaN, so <c>1/(x^2 + i)</c> was answered
22+
/// <c>NaN + C</c>, and with a real sign in front and the rest of the quadratic complex, the
23+
/// arcsine taken for it differentiated back to minus the integrand on part of the line. Every
24+
/// integrand here is complex along the whole line, so the check compares complex values.
25+
/// </remarks>
26+
[Trait("Area", "Calculus")]
27+
public sealed class QuadraticOffTheRealLineTest
28+
{
29+
private static void DifferentiatesBack(string integrand)
30+
{
31+
var integral = integrand.ToEntity().Integrate("x");
32+
var written = integral.Stringize();
33+
Assert.DoesNotContain("integral(", written);
34+
Assert.DoesNotContain("NaN", written);
35+
36+
var derivative = integral.Substitute("C", 0).Differentiate("x")
37+
.Substitute("a", 1.3).Substitute("c", 0.2).Substitute("d", 0.9);
38+
var original = integrand.ToEntity()
39+
.Substitute("a", 1.3).Substitute("c", 0.2).Substitute("d", 0.9);
40+
var compared = 0;
41+
foreach (var at in new[] { -2.3, -1.1, -0.4, 0.31, 0.83, 1.7, 2.9 })
42+
{
43+
var got = derivative.Substitute("x", at).EvalNumerical();
44+
var want = original.Substitute("x", at).EvalNumerical();
45+
if (got.IsNaN || want.IsNaN)
46+
continue;
47+
compared++;
48+
var difference = got - want;
49+
var distance = Math.Abs((double)difference.RealPart) + Math.Abs((double)difference.ImaginaryPart);
50+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
51+
Assert.True(distance / scale < 1e-9,
52+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, "
53+
+ $"where the integrand is {want}");
54+
}
55+
Assert.True(compared >= 5,
56+
$"only {compared} points were comparable for {integrand}");
57+
}
58+
59+
/// <summary>A coefficient that is not real, where every arm was a sign and none held.</summary>
60+
[Theory]
61+
[InlineData("1/(x^2 + i)")]
62+
[InlineData("1/(i*x^2 + 1)")]
63+
[InlineData("1/(x^2 + x + i)")]
64+
[InlineData("x/(i*x^2 + x + 1)")]
65+
[InlineData("1/(x^2 + i)^2")]
66+
[InlineData("1/(x^2 + (1 + i)*x + 1)^2")]
67+
[InlineData("1/sqrt(i*x^2 + 1)")]
68+
[InlineData("sqrt(i*x^2 + 1)")]
69+
public void ANonRealCoefficientIsAnswered(string integrand) => DifferentiatesBack(integrand);
70+
71+
/// <summary>
72+
/// With a symbol beside the imaginary unit, the arms were <c>i a &lt; 0</c> and
73+
/// <c>i a &gt; 0</c>, and neither holds for a real <c>a</c> but zero.
74+
/// </summary>
75+
[Theory]
76+
[InlineData("1/sqrt(i*a*x^2 + 1)")]
77+
[InlineData("1/(i*a*x^2 + 1)")]
78+
public void ASymbolBesideTheImaginaryUnitIsAnswered(string integrand) => DifferentiatesBack(integrand);
79+
80+
/// <summary>
81+
/// A real negative leading coefficient with the rest of the quadratic complex took the
82+
/// arcsine, which differentiated back to minus the integrand below -2 for the first of
83+
/// these and on half the points for the others.
84+
/// </summary>
85+
[Theory]
86+
[InlineData("1/sqrt(-x^2 + (1 + i)*x + 1)")]
87+
[InlineData("1/sqrt(-x^2 + (3 - 2*i)*x - 1)")]
88+
[InlineData("1/sqrt(-2*x^2 - 5*i*x + 3*i)")]
89+
[InlineData("sqrt(-x^2 + (1 + i)*x + 1)")]
90+
public void ARealSignBesideComplexTermsIsAnswered(string integrand) => DifferentiatesBack(integrand);
91+
92+
/// <summary>
93+
/// Reached through a substitution: the product of the two roots, and Rubi's 4.3.2.1 row
94+
/// 260, which were answered <c>NaN + C</c> once they came to one of the table integrals.
95+
/// </summary>
96+
[Theory]
97+
[InlineData("1/(sqrt(x)*sqrt(1 + i*x))")]
98+
[InlineData("sqrt(x)/sqrt(1 + i*x)")]
99+
[InlineData("sqrt(tan(c + d*x))/(a + i*a*tan(c + d*x))^(5/2)")]
100+
public void ReachedThroughASubstitution(string integrand) => DifferentiatesBack(integrand);
101+
102+
/// <summary>
103+
/// A real quadratic keeps its sign arms, and the forms that are real where it is: no arm
104+
/// for a value off the real line is added where nothing in it is off the real line.
105+
/// </summary>
106+
[Theory]
107+
[InlineData("1/sqrt(a*x^2 + 1)")]
108+
[InlineData("1/(a*x^2 + x + 1)")]
109+
[InlineData("1/(a*x^2 + 1)^2")]
110+
public void ARealQuadraticKeepsItsSignArms(string integrand)
111+
{
112+
var written = integrand.ToEntity().Integrate("x").Stringize();
113+
Assert.DoesNotContain(" in ", written);
114+
Assert.Contains("> 0", written);
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)