From eb2f9255bab082942b879d79ff54edac507a606f Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 29 Sep 2026 21:24:52 +0000 Subject: [PATCH 1/2] A quadratic with a coefficient off the real line is integrated in the forms that hold there 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. Closes #1598. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 19 +++ .../Integration/IntegralPatterns.cs | 58 +++++++-- .../Calculus/QuadraticOffTheRealLineTest.cs | 117 ++++++++++++++++++ 3 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/QuadraticOffTheRealLineTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 529a33eea..149ff029f 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -309,6 +309,25 @@ Each of these was checked by differentiating it back with the parameters pinned four points. The Rubi sample is unchanged at 231 of 463 with no wrong answers, so nothing that already had an antiderivative moves. +### `NaN` a third time, from a quadratic with a coefficient off the real line + +The integrals of `1/sqrt(Q)`, `1/Q` and `1/Q^n` over a quadratic `Q` choose their form by the sign +of the leading coefficient or of the discriminant, and had no arm for one that is not real: `i < 0` +and `i > 0` are each `NaN`, the complex numbers not being ordered, and a piecewise with no arm that +holds is `NaN` with them. With a symbol beside the imaginary unit the arms stayed standing, and none +of them holds for a real value of it but zero. Where the sign was real and the rest of the quadratic +was not, the arcsine was taken, and it differentiates back to minus the integrand on part of the +line. Such a quadratic now takes the arctangent or the logarithm, which use nothing about their root +but `sqrt(q)^2 = q` ([#1598](https://github.com/asc-community/AngouriMath/issues/1598)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"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 | +| `"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 | +| `"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` | +| `"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 | +| `"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 | + ### Trigonometric functions of multiples of one linear argument are written in it `csc(a + b x) csc(2a + 2b x)^2` was left unevaluated, and `csc(1 + x) csc(2 + 2x)^2` ran twenty diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs index db36a0e29..aa1be5e6d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs @@ -759,11 +759,42 @@ private static Entity IntegrateOverRootOfQuadratic( ? numerator * x / MathS.Sqrt(c) : 2 * numerator * MathS.Sqrt(b * x + c) / b; - return MathS.Piecewise([ - new Entity.Providedf(linearCase, a.EqualTo(0)), - new Entity.Providedf(arcsinCase, a < 0), - new Entity.Providedf(logarithmCase, a > 0) - ]); + var arms = new List { new Entity.Providedf(linearCase, a.EqualTo(0)) }; + if (ArmOffTheRealLine(logarithmCase, a, b, c) is { } offTheRealLine) + arms.Add(offTheRealLine); + arms.Add(new Entity.Providedf(arcsinCase, a < 0)); + arms.Add(new Entity.Providedf(logarithmCase, a > 0)); + return MathS.Piecewise(arms); + } + + /// + /// The arm that a piecewise on a sign owes a quadratic off the real line, answered by + /// wherever one of is not real; + /// where none of them holds the imaginary unit, and the sign arms + /// are left to decide. + /// + /// + /// The sign arms choose between forms by which is real where the integrand is, and off + /// the real line that has nothing to go on. i < 0 and i > 0 are each + /// NaN, the complex numbers not being ordered, and a piecewise with no arm that holds is + /// NaN with them: 1/(x^2 + i) and 1/sqrt(i x^2 + 1) answered NaN + C, + /// and 1/sqrt(i a x^2 + 1) a piecewise that is NaN for every real a but zero. + /// Nor is a real sign any help where the rest of the quadratic is not real: the arcsine + /// differentiates back to 1/sqrt(Q) only where sqrt(D) sqrt(Q/D) is + /// sqrt(Q), and 1/sqrt(-x^2 + (1 + i) x + 1) came back as minus its + /// integrand below -2. The arctangent and the logarithm use nothing about their root but + /// sqrt(q)^2 = q, and are antiderivatives off the real line as well. + /// https://github.com/asc-community/AngouriMath/issues/1598 + /// + private static Entity.Providedf? ArmOffTheRealLine(Entity form, params Entity[] coefficients) + { + Entity? offTheRealLine = null; + foreach (var coefficient in coefficients) + if (coefficient.InnerSimplified.Nodes.Any(node => node is Entity.Number.Complex number && !number.ImaginaryPart.EDecimal.IsZero)) + offTheRealLine = offTheRealLine is null + ? !coefficient.In(AngouriMath.Core.Domain.Real) + : offTheRealLine | !coefficient.In(AngouriMath.Core.Domain.Real); + return offTheRealLine is null ? null : new Entity.Providedf(form, offTheRealLine); } /// @@ -885,6 +916,8 @@ private static Entity IntegrateRationalQuadratic(Entity numerator, Entity a, Ent var cases = new List(); if (!denominatorVanishesWithoutA) cases.Add(new Entity.Providedf(linearCase, a.EqualTo(0))); + if (ArmOffTheRealLine(arctanCase, discriminant) is { } offTheRealLine) + cases.Add(offTheRealLine); cases.Add(new Entity.Providedf(arctanCase, discriminant > 0)); cases.Add(new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0))); cases.Add(new Entity.Providedf(lnCase, discriminant < 0)); @@ -959,12 +992,17 @@ private static Entity IntegrateOverPowerOfQuadratic( : 2 * MathS.Arctan(derivative / sqrtDiscriminant) / sqrtDiscriminant; Entity firstPowerLog = withoutTheFirstPower ? Entity.Number.Integer.Zero : AntiderivativeLog((derivative - sqrtNegDiscriminant) / (derivative + sqrtNegDiscriminant)) / sqrtNegDiscriminant; - return MathS.Piecewise([ + var arms = new List + { new Entity.Providedf(linearCase, a.EqualTo(0)), - new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)), - new Entity.Providedf(Reduce(firstPowerArctan), discriminant > 0), - new Entity.Providedf(Reduce(firstPowerLog), discriminant < 0) - ]).InnerSimplified; + new Entity.Providedf(perfectSquareCase, discriminant.EqualTo(0)) + }; + var arctanArm = Reduce(firstPowerArctan); + if (ArmOffTheRealLine(arctanArm, discriminant) is { } offTheRealLine) + arms.Add(offTheRealLine); + arms.Add(new Entity.Providedf(arctanArm, discriminant > 0)); + arms.Add(new Entity.Providedf(Reduce(firstPowerLog), discriminant < 0)); + return MathS.Piecewise(arms).InnerSimplified; Entity Reduce(Entity firstPower) { diff --git a/Sources/Tests/UnitTests/Calculus/QuadraticOffTheRealLineTest.cs b/Sources/Tests/UnitTests/Calculus/QuadraticOffTheRealLineTest.cs new file mode 100644 index 000000000..c554da6e0 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/QuadraticOffTheRealLineTest.cs @@ -0,0 +1,117 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The table integrals over a quadratic Q -- of 1/sqrt(Q), 1/Q and + /// 1/Q^n -- chose their form by the sign of the leading coefficient or of the + /// discriminant, and had no arm for one off the real line. + /// #1598 + /// + /// + /// i < 0 and i > 0 are each NaN, so 1/(x^2 + i) was answered + /// NaN + C, and with a real sign in front and the rest of the quadratic complex, the + /// arcsine taken for it differentiated back to minus the integrand on part of the line. Every + /// integrand here is complex along the whole line, so the check compares complex values. + /// + [Trait("Area", "Calculus")] + public sealed class QuadraticOffTheRealLineTest + { + private static void DifferentiatesBack(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + var written = integral.Stringize(); + Assert.DoesNotContain("integral(", written); + Assert.DoesNotContain("NaN", written); + + var derivative = integral.Substitute("C", 0).Differentiate("x") + .Substitute("a", 1.3).Substitute("c", 0.2).Substitute("d", 0.9); + var original = integrand.ToEntity() + .Substitute("a", 1.3).Substitute("c", 0.2).Substitute("d", 0.9); + var compared = 0; + foreach (var at in new[] { -2.3, -1.1, -0.4, 0.31, 0.83, 1.7, 2.9 }) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + if (got.IsNaN || want.IsNaN) + continue; + compared++; + var difference = got - want; + var distance = Math.Abs((double)difference.RealPart) + Math.Abs((double)difference.ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); + Assert.True(distance / scale < 1e-9, + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, " + + $"where the integrand is {want}"); + } + Assert.True(compared >= 5, + $"only {compared} points were comparable for {integrand}"); + } + + /// A coefficient that is not real, where every arm was a sign and none held. + [Theory] + [InlineData("1/(x^2 + i)")] + [InlineData("1/(i*x^2 + 1)")] + [InlineData("1/(x^2 + x + i)")] + [InlineData("x/(i*x^2 + x + 1)")] + [InlineData("1/(x^2 + i)^2")] + [InlineData("1/(x^2 + (1 + i)*x + 1)^2")] + [InlineData("1/sqrt(i*x^2 + 1)")] + [InlineData("sqrt(i*x^2 + 1)")] + public void ANonRealCoefficientIsAnswered(string integrand) => DifferentiatesBack(integrand); + + /// + /// With a symbol beside the imaginary unit, the arms were i a < 0 and + /// i a > 0, and neither holds for a real a but zero. + /// + [Theory] + [InlineData("1/sqrt(i*a*x^2 + 1)")] + [InlineData("1/(i*a*x^2 + 1)")] + public void ASymbolBesideTheImaginaryUnitIsAnswered(string integrand) => DifferentiatesBack(integrand); + + /// + /// A real negative leading coefficient with the rest of the quadratic complex took the + /// arcsine, which differentiated back to minus the integrand below -2 for the first of + /// these and on half the points for the others. + /// + [Theory] + [InlineData("1/sqrt(-x^2 + (1 + i)*x + 1)")] + [InlineData("1/sqrt(-x^2 + (3 - 2*i)*x - 1)")] + [InlineData("1/sqrt(-2*x^2 - 5*i*x + 3*i)")] + [InlineData("sqrt(-x^2 + (1 + i)*x + 1)")] + public void ARealSignBesideComplexTermsIsAnswered(string integrand) => DifferentiatesBack(integrand); + + /// + /// Reached through a substitution: the product of the two roots, and Rubi's 4.3.2.1 row + /// 260, which were answered NaN + C once they came to one of the table integrals. + /// + [Theory] + [InlineData("1/(sqrt(x)*sqrt(1 + i*x))")] + [InlineData("sqrt(x)/sqrt(1 + i*x)")] + [InlineData("sqrt(tan(c + d*x))/(a + i*a*tan(c + d*x))^(5/2)")] + public void ReachedThroughASubstitution(string integrand) => DifferentiatesBack(integrand); + + /// + /// A real quadratic keeps its sign arms, and the forms that are real where it is: no arm + /// for a value off the real line is added where nothing in it is off the real line. + /// + [Theory] + [InlineData("1/sqrt(a*x^2 + 1)")] + [InlineData("1/(a*x^2 + x + 1)")] + [InlineData("1/(a*x^2 + 1)^2")] + public void ARealQuadraticKeepsItsSignArms(string integrand) + { + var written = integrand.ToEntity().Integrate("x").Stringize(); + Assert.DoesNotContain(" in ", written); + Assert.Contains("> 0", written); + } + } +} From f774d506f89499869865b6a367bc11fa1ce1871d Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 29 Sep 2026 21:37:26 +0000 Subject: [PATCH 2/2] Name the quantity the arcsine's condition is about Co-Authored-By: Claude Opus 5.5 --- .../Functions/Continuous/Integration/IntegralPatterns.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs index aa1be5e6d..23264dd46 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs @@ -779,10 +779,10 @@ private static Entity IntegrateOverRootOfQuadratic( /// NaN, the complex numbers not being ordered, and a piecewise with no arm that holds is /// NaN with them: 1/(x^2 + i) and 1/sqrt(i x^2 + 1) answered NaN + C, /// and 1/sqrt(i a x^2 + 1) a piecewise that is NaN for every real a but zero. - /// Nor is a real sign any help where the rest of the quadratic is not real: the arcsine - /// differentiates back to 1/sqrt(Q) only where sqrt(D) sqrt(Q/D) is - /// sqrt(Q), and 1/sqrt(-x^2 + (1 + i) x + 1) came back as minus its - /// integrand below -2. The arctangent and the logarithm use nothing about their root but + /// Nor is a real sign any help where the rest of the quadratic is not real: with + /// E = b^2 - 4ac the arcsine differentiates back to 1/sqrt(Q) only where + /// sqrt(E) sqrt(Q/E) is sqrt(Q), and 1/sqrt(-x^2 + (1 + i) x + 1) + /// came back as minus its integrand below -2. The arctangent and the logarithm use nothing about their root but /// sqrt(q)^2 = q, and are antiderivatives off the real line as well. /// https://github.com/asc-community/AngouriMath/issues/1598 ///