Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity.Providedf> { 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);
}

/// <summary>
/// The arm that a piecewise on a sign owes a quadratic off the real line, answered by
/// <paramref name="form"/> wherever one of <paramref name="coefficients"/> is not real;
/// <see langword="null"/> where none of them holds the imaginary unit, and the sign arms
/// are left to decide.
/// </summary>
/// <remarks>
/// 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. <c>i &lt; 0</c> and <c>i &gt; 0</c> are each
/// NaN, the complex numbers not being ordered, and a piecewise with no arm that holds is
/// NaN with them: <c>1/(x^2 + i)</c> and <c>1/sqrt(i x^2 + 1)</c> answered <c>NaN + C</c>,
/// and <c>1/sqrt(i a x^2 + 1)</c> a piecewise that is NaN for every real <c>a</c> but zero.
/// Nor is a real sign any help where the rest of the quadratic is not real: with
/// <c>E = b^2 - 4ac</c> the arcsine differentiates back to <c>1/sqrt(Q)</c> only where
/// <c>sqrt(E) sqrt(Q/E)</c> is <c>sqrt(Q)</c>, and <c>1/sqrt(-x^2 + (1 + i) x + 1)</c>
/// came back as minus its integrand below -2. The arctangent and the logarithm use nothing about their root but
/// <c>sqrt(q)^2 = q</c>, and are antiderivatives off the real line as well.
/// https://github.com/asc-community/AngouriMath/issues/1598
/// </remarks>
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);
}

/// <summary>
Expand Down Expand Up @@ -885,6 +916,8 @@ private static Entity IntegrateRationalQuadratic(Entity numerator, Entity a, Ent
var cases = new List<Entity.Providedf>();
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));
Expand Down Expand Up @@ -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<Entity.Providedf>
{
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)
{
Expand Down
117 changes: 117 additions & 0 deletions Sources/Tests/UnitTests/Calculus/QuadraticOffTheRealLineTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The table integrals over a quadratic <c>Q</c> -- of <c>1/sqrt(Q)</c>, <c>1/Q</c> and
/// <c>1/Q^n</c> -- chose their form by the sign of the leading coefficient or of the
/// discriminant, and had no arm for one off the real line.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1598">#1598</a>
/// </summary>
/// <remarks>
/// <c>i &lt; 0</c> and <c>i &gt; 0</c> are each NaN, so <c>1/(x^2 + i)</c> was answered
/// <c>NaN + C</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.
/// </remarks>
[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}");
}

/// <summary>A coefficient that is not real, where every arm was a sign and none held.</summary>
[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);

/// <summary>
/// With a symbol beside the imaginary unit, the arms were <c>i a &lt; 0</c> and
/// <c>i a &gt; 0</c>, and neither holds for a real <c>a</c> but zero.
/// </summary>
[Theory]
[InlineData("1/sqrt(i*a*x^2 + 1)")]
[InlineData("1/(i*a*x^2 + 1)")]
public void ASymbolBesideTheImaginaryUnitIsAnswered(string integrand) => DifferentiatesBack(integrand);

/// <summary>
/// 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.
/// </summary>
[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);

/// <summary>
/// Reached through a substitution: the product of the two roots, and Rubi's 4.3.2.1 row
/// 260, which were answered <c>NaN + C</c> once they came to one of the table integrals.
/// </summary>
[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);

/// <summary>
/// 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.
/// </summary>
[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);
}
}
}
Loading