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
18 changes: 18 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,24 @@ here that writes one does: left inside, a rule below differentiated it, and
| `"sqrt(a^2+2*a*b*x+b^2*x^2)*sqrt(c+pe*x+d*x^2)".Integrate("x")` | left unevaluated | the antiderivative |
| `"(a^2+2*a*b*x+b^2*x^2)^(5/2)".Integrate("x")` | the antiderivative, through the substitution search | `sgn(a + b x) (a + b x)^6 |b|^5/(6 b)` up to the form |

### The root of a quadratic that is a square is the modulus of a linear, and was read as any quadratic

**Wrong answers, silent.** The table's entry for `k/sqrt(Q)` and `sqrt(Q)` reads a quadratic by the
sign of its leading coefficient: an arcsine over the root of the discriminant, or the logarithm of
`2 a x + b + 2 sqrt(a) sqrt(Q)`. Where `Q` is a square the discriminant is zero, and that logarithm
is of zero beyond the root. So `1/sqrt(x^2 + 2x + 1)` had no value for any `x < -1`, where it is
`1/|x + 1|`, and with a negative leading coefficient there was no value anywhere. The root of a
square is now the modulus of its linear, `sqrt(a (x + h)^2) = sqrt(a) |x + h|`, for a leading
coefficient of either sign ([#1670](https://github.com/asc-community/AngouriMath/issues/1670)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"1/sqrt(x^2+2*x+1)".Integrate("x")` | `ln(2 x + 2 + 2 sqrt(x^2 + 2 x + 1))`, `ln(0)` for every `x < -1` | `sgn(x + 1) ln(x + 1)` |
| `"sqrt(x^2+2*x+1)".Integrate("x")` | `(2 x + 2) sqrt(x^2 + 2 x + 1)/4 provided not 2 x + 2 + 2 sqrt(x^2 + 2 x + 1) = 0`, which says nothing for `x <= -1` | `sgn(x + 1) (x^2/2 + x)` |
| `"1/sqrt(-4-4*x-x^2)".Integrate("x")`, and `sqrt(-4 - 4 x - x^2)` | `NaN` | `sgn(x + 2) ln(i x + 2 i)/i`, and `i sgn(x + 2) (x^2/2 + 2 x)` |
| `"1/sqrt(-a^2-2*a*b*x-b^2*x^2)".Integrate("x")` | a piecewise whose arcsine divides by `sqrt(0)`, `NaN` for every real `b` but zero | a logarithm over `sqrt(-b^2)`, `provided b^2 > 0` |
| `"1/(x*sqrt(a^2+2*a*b*x+b^2*x^2))".Integrate("x")`, the same with `-a^2 - 2 a b x - b^2 x^2`, or with `d + e x` for `x` | left unevaluated | the antiderivative, `provided b^2 > 0` |

### A power of the variable times a sine or cosine of a logarithm, and a power of a monomial

`x^2 sin(a + b ln(c x^n))` and `(c x^n)^b` were both left as written. Two rules, each exact:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13697,14 +13697,11 @@ private static bool IsAWholeOrHalfOddMultiple(Entity timesN)
/// Symbolically as well as numerically: for <c>a^2 + 2 a b x + b^2 x^2</c> it is
/// <c>4 a^2 b^2 - 4 a^2 b^2</c>, which <see cref="Entity.InnerSimplified"/> does not
/// collect, so a guard that asks only what it evaluates to reads the square as an
/// ordinary quadratic.
/// ordinary quadratic. Asked of every quadratic under a root the table meets, so the
/// simplification is only for a discriminant that is zero with its symbols pinned.
/// </summary>
private static bool IsAPerfectSquareDiscriminant(Entity a, Entity b, Entity c)
{
var discriminant = (b * b - Number.Integer.Create(4) * a * c).InnerSimplified;
return discriminant.Evaled is Number.Complex { IsZero: true }
|| discriminant.Vars.Any() && Functions.PartialFractions.Bare(discriminant.Simplify()).Evaled is Number.Complex { IsZero: true };
}
internal static bool IsAPerfectSquareDiscriminant(Entity a, Entity b, Entity c)
=> VanishesIdentically(b * b - Number.Integer.Create(4) * a * c);

/// <summary>
/// A square root of a perfect square in <paramref name="x"/> is the modulus:
Expand Down Expand Up @@ -13798,15 +13795,17 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x)
var b = monomials.TryGetValue(half, out var b1) ? b1 : Number.Integer.Zero;
var c = monomials.TryGetValue(EInteger.Zero, out var c0) ? c0 : Number.Integer.Zero;
Entity w = half.Equals(EInteger.One) ? x : MathS.Pow(x, Number.Integer.Create(half));
// A positive leading coefficient, since `sqrt(a (x + h)^2)` is `sqrt(a) |x + h|`:
// a positive number, or one positive for a real parameter -- `b^2` is, and
// `a^2 + 2 a b x + b^2 x^2` is the square Rubi writes -- whose condition travels
// A leading coefficient of known sign, since `sqrt(a (x + h)^2)` is
// `sqrt(a) |x + h|` for either: a number, or one positive or negative for a real
// parameter -- `b^2` is, and `a^2 + 2 a b x + b^2 x^2` is the square Rubi writes,
// and `-b^2` is, whose square has an imaginary root -- whose condition travels
// with the answer. The other two coefficients are real or symbolic, not a
// number off the line, for the same reason.
// number off the line, so that `(x + h)^2` is not negative.
// A whole power needs no sign of anything: (a (w + h)^2)^n is a^n (w + h)^(2n).
if (!whole && a.Evaled is not Number.Real { IsPositive: true })
if (!whole && a.Evaled is not Number.Real { IsZero: false })
{
if (!IsPositiveForARealParameter(a, x, out var leadingAssumed, assumed))
if (!IsPositiveForARealParameter(a, x, out var leadingAssumed, assumed)
&& !IsPositiveForARealParameter((-a).InnerSimplified, x, out leadingAssumed, assumed))
return node;
assumed = leadingAssumed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,11 @@ _ when GaussianMoment(expr, x) is { } moment => moment,
/ (rate * rate + frequency * frequency),

// ∫ sqrt(ax^2 + bx + c) dx, which is one integration by parts away from the
// reciprocal form below and is written in terms of it.
// reciprocal form below and is written in terms of it. Not of a square, as below.
Entity.Powf(var radicand, Entity.Number.Rational(Entity.Number.Integer(1), Entity.Number.Integer(2))) when
TreeAnalyzer.TryGetPolyQuadratic(radicand, x, out var qa, out var qb, out var qc)
&& qa.Evaled is Entity.Number.Complex { IsZero: false }
&& !IndefiniteIntegralSolver.IsAPerfectSquareDiscriminant(qa, qb, qc)
=> IntegrateRootOfQuadratic(qa, qb, qc, radicand, x),

// ∫ k / (x^2 * sqrt(ax^2 + c)) dx, the shape a trigonometric substitution is
Expand All @@ -496,10 +497,17 @@ _ when TryReadOverSquareTimesRoot(expr, x, out var overFactor, out var overRadic

// ∫ k / sqrt(ax^2 + bx + c) dx -- the arcsine and logarithm forms. Without
// these, 1/sqrt(1 - x^2) had no antiderivative at all.
// Not of a square: its root is the modulus of a linear, `sqrt(a (x + h)^2)` being
// `sqrt(a) |x + h|`, which the rule for a root of a perfect square writes. Here the
// arcsine divides by the root of the discriminant, zero, and the logarithm is of
// `2a (x + h) + 2a |x + h|`, zero beyond the root: `1/sqrt(x^2 + 2x + 1)` was
// `ln(0)` for every x below -1, and `1/sqrt(-a^2 - 2abx - b^2 x^2)` NaN everywhere.
// https://github.com/asc-community/AngouriMath/issues/1670
Entity.Divf(var numerator,
Entity.Powf(var radicand, Entity.Number.Rational(Entity.Number.Integer(1), Entity.Number.Integer(2)))) when
!numerator.ContainsNode(x)
&& TreeAnalyzer.TryGetPolyQuadratic(radicand, x, out var ra, out var rb, out var rc)
&& !IndefiniteIntegralSolver.IsAPerfectSquareDiscriminant(ra, rb, rc)
=> IntegrateOverRootOfQuadratic(numerator, ra, rb, rc, radicand, x),

// ∫ (px + q)/(ax^2 + bx + c) dx. Only the constant numerator was covered, so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,72 @@ public void AWholePowerOfASquareInAPowerOfTheVariable(string integrand)
Assert.True(compared >= 5, $"only {compared} points were comparable for {integrand}");
}

/// <summary>
/// The table's arcsine and logarithm for a root of a quadratic are not for a square: the
/// arcsine divides by the root of the discriminant, and the logarithm is of zero beyond
/// the root, so <c>1/sqrt(x^2 + 2x + 1)</c> was <c>ln(0)</c> for every x below -1 and
/// <c>1/sqrt(-a^2 - 2 a b x - b^2 x^2)</c> NaN everywhere. Compared at every point where
/// the integrand has a value, on both sides of each root -- an answer with none there is
/// wrong, not incomparable -- with the leading coefficient of either sign: <c>-b^2</c> is
/// negative for a real <c>b</c>, and the root of its square is imaginary. Rubi's
/// 1.2.1.2 #2737 among them.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1670">#1670</a>
/// </summary>
[Theory]
[InlineData("1/sqrt(x^2 + 2*x + 1)")]
[InlineData("sqrt(x^2 + 2*x + 1)")]
[InlineData("3/sqrt(9*x^2 - 6*x + 1)")]
[InlineData("1/sqrt(-4 - 4*x - x^2)")]
[InlineData("sqrt(-4 - 4*x - x^2)")]
[InlineData("1/sqrt(-a^2 - 2*a*b*x - b^2*x^2)")]
[InlineData("x/sqrt(-a^2 - 2*a*b*x - b^2*x^2)")]
[InlineData("1/(x*sqrt(-a^2 - 2*a*b*x - b^2*x^2))")]
[InlineData("1/(x*sqrt(a^2 + 2*a*b*x + b^2*x^2))")]
[InlineData("1/((d + h*x)*sqrt(a^2 + 2*a*b*x + b^2*x^2))")]
[InlineData("1/((d + h*x)*sqrt(-a^2 - 2*a*b*x - b^2*x^2))")]
public void TheTableDoesNotReadASquareAsAQuadratic(string integrand)
{
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0);
Assert.DoesNotContain("integral(", integral.Stringize());
HoldsWhereverTheIntegrandHasAValue(integrand, integral);
}

/// <summary>
/// A square with a symbol of no known sign in front, <c>c (a + b x)^2</c>, is not read as
/// one, and the table's forms gave no value between its roots: declined, or answered
/// everywhere the integrand has a value.
/// </summary>
[Fact]
public void ASquareOfUnknownSignIsNotAnsweredWrongly()
{
var integrand = "1/(x*sqrt(c*(a + b*x)^2))";
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0);
if (!integral.Stringize().Contains("integral("))
HoldsWhereverTheIntegrandHasAValue(integrand, integral);
}

private static void HoldsWhereverTheIntegrandHasAValue(string integrand, Entity integral)
{
Entity Pin(Entity e) => e.Substitute("a", 1.3).Substitute("b", 0.7).Substitute("c", 0.6)
.Substitute("d", 1.9).Substitute("h", 1.1);
var derivative = Pin(integral).Differentiate("x");
var original = Pin(integrand.ToEntity());
var compared = 0;
foreach (var at in new[] { -3.7, -2.6, -1.8, -1.2, -0.6, 0.4, 1.9 })
{
var want = original.Substitute("x", at).EvalNumerical();
if (want.IsNaN)
continue;
var got = derivative.Substitute("x", at).EvalNumerical();
compared++;
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
Assert.True(difference / scale < 1e-9,
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}: {integral}");
}
Assert.True(compared >= 6, $"only {compared} points were comparable for {integrand}");
}

[Fact]
public void TheSignIsTheSignOfTheLinearFactor()
{
Expand Down
Loading