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
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,23 @@ either now; and the secant quotient was simplified to `cos/(b^(5/2) d)` through
`(1/cos)^(3/2) = cos^(-3/2)`, which is wrong for every negative cosine and cancelled against the
same wrong step on the denominator. On the 1774-problem independent suites this is 1707 to 1705, the two `asin` rows, with the sign error gone.

### A power of x comes out of a fractional power of a sum whose every term has it

`(a x^j + b x^n)^p` with a fractional `p`, the power of x common to every term inside the power,
was integrated only where the sum factored over the rationals. Now `x^j` comes out:
`(a x^j + b x^n)^p` is `K x^(j p) (a + b x^(n - j))^p` with `K = (a x^j + b x^n)^p / (x^(j p) (a + b x^(n - j))^p)`,
which is constant wherever x and the sum are not zero and goes in front of the integral. For an even
whole `j` and a whole `j p` it is `sgn(x)^(j p)`. An odd whole `j` out of a sum of whole powers is
left as it was, since the sum is real on both sides of 0 and `x^(j p)` is not. Rubi's 1.1.4.2 and
1.1.4.3, `(c x)^m (a x^j + b x^n)^p` ([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"1/sqrt(a*x^2 + b*x^5)".ToEntity().Integrate("x")` | `integral(...)` | `sgn(x)` times the integral of `1/(x sqrt(a + b x^3))` |
| `"x*sqrt(x^2*(a + b*x^3))".ToEntity().Integrate("x")` | `(a + b x^3)^(3/2)/(9/2 b)`, wrong where `x < 0` | `sgn(x) (a + b x^3)^(3/2)/(9/2 b)` |
| `"1/(x*sqrt(b*x^(2/3) + a*x))".ToEntity().Integrate("x")` | `integral(...)` | `K` times an antiderivative in `sqrt(b + a x^(1/3))` |
| `"x/sqrt(1 + 1/(c*x)^2)".ToEntity().Integrate("x")` | `integral(...)` | `sgn(x) (x sqrt(x^2 + 1/c^2)/2 - ln(2 x + 2 sqrt(x^2 + 1/c^2))/(2 c^2))`, for any `c` but 0 |

### A rational function of a sine over a symbolic quadratic in it is integrated over the two roots

**Improvement, not silent.** `sin(x)/(a + b sin(x) + c sin(x)^2)` and everything rational in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15517,6 +15517,156 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x)
return assumed == Entity.Boolean.True ? answer : answer.Provided(assumed);
}

/// <summary>
/// A fractional power of a sum whose every term has x to a power in it,
/// <c>(a x^j + b x^n)^p</c> with <c>0 &lt; j &lt; n</c>, as <c>K x^(j p) (a + b x^(n - j))^p</c>:
/// the integral is <c>K</c> times the integral with <c>x^(j p) (a + b x^(n - j))^p</c> in its
/// place, where <c>K = (a x^j + b x^n)^p / (x^(j p) (a + b x^(n - j))^p)</c>.
/// </summary>
/// <remarks>
/// <para>
/// <c>K</c> is constant on every interval where x and the sum are not zero: with
/// <c>S = x^j T</c>, its logarithmic derivative is <c>p (S'/S - j/x - T'/T)</c>, and
/// <c>S'/S = j/x + T'/T</c>. So it goes in front of the integral, as the factor of a square
/// does in <see cref="SolveByWritingAPowerOfASquareAsAPowerOfItsRoot"/>. For an even whole
/// <c>j</c> and a whole <c>j p</c> it is <c>sgn(x)^(j p)</c>: <c>x^j</c> is not negative for a
/// real x, so <c>(x^j T)^p</c> is <c>|x|^(j p) T^p</c>.
/// </para>
/// <para>
/// Rubi's 1.1.4.2 and 1.1.4.3 write these by the hundred: <c>1/sqrt(a x^2 + b x^5)</c> and
/// <c>1/(x sqrt(b x^(2/3) + a x))</c> were declined, the first since the factorization
/// over the rationals does not read a symbol, and the second since nothing took the power
/// of x out of the root. With it out, the binomial is what the rules for
/// <c>x^m (a + b x^n)^p</c> read. At the top only, where the answer is the caller's.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </para>
/// </remarks>
internal static Entity? SolveByTakingAPowerOfXOutOfAFractionalPower(Entity expr, Entity.Variable x, bool integrateByParts)
{
if (!Integration.AnsweringTheQuestionAsked)
return null;
// The sum as a factor of the integrand, above the bar or below it.
var (above, below) = Functions.SingleQuotient.Of(expr);
(Entity Sum, Entity Exponent, Number.Rational Lowest, Entity Quotient, bool Below)? found = null;
foreach (var (side, isBelow) in new[] { (above, false), (below, true) })
foreach (var factor in Mulf.LinearChildren(side))
{
if (factor is not Powf(var sum, var power) || power is Number.Integer || power.ContainsNode(x)
|| !sum.ContainsNode(x) || sum.Complexity > 60)
continue;
if ((OutOfASum(sum) ?? OutOfAProduct(sum)) is not var (lowest, rest))
continue;
// An odd whole power of x out of a sum of whole powers: the sum is real on both
// sides of 0 and x^(j p) is not, and the rules for the sum as written answer it
// on both. An even one comes out as its sign, and a fractional one is real for a
// positive x only, as the sum already was.
if (lowest is Number.Integer { EInteger.IsEven: false } && !sum.Nodes.Any(node => node is Powf(var @base, Number.Rational r) && @base == x && r is not Number.Integer))
continue;
// Two such would want two factors.
if (found is { })
return null;
found = (sum, power, lowest, rest, isBelow);
}
if (found is not var (theSum, exponentOfTheSum, j, theRest, sumIsBelow)
|| above.Nodes.Concat(below.Nodes).Count(node => node == theSum) != 1)
return null;
var outside = (j * exponentOfTheSum).InnerSimplified;
Entity Without(Entity side)
{
Entity product = Number.Integer.One;
foreach (var factor in Mulf.LinearChildren(side))
if (!(factor is Powf(var sum, var power) && sum == theSum && power == exponentOfTheSum))
product = product == Number.Integer.One ? factor : product * factor;
return product;
}
var replacement = MathS.Pow(x, outside) * MathS.Pow(theRest, exponentOfTheSum);
var written = sumIsBelow ? Without(above) / (replacement * Without(below)) : replacement * Without(above) / below;
if (Integration.ComputeIndefiniteIntegral(written.InnerSimplified, x, integrateByParts) is not { } integral)
return null;
// K: the sign of x to the power j p where that is all it is, which is its own reciprocal,
// and the quotient otherwise.
var aSign = j is Number.Integer { EInteger.IsEven: true } && outside is Number.Integer;
Entity constant = aSign
? (((Number.Integer)outside).EInteger.IsEven ? Number.Integer.One : MathS.Signum(x))
: MathS.Pow(theSum, exponentOfTheSum) / replacement;
var answer = constant == Number.Integer.One ? integral : sumIsBelow && !aSign ? integral / constant : constant * integral;
return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer;

// A sum, each term a coefficient times a rational power of x: the lowest power, not 0,
// and what is left with it taken out of every term.
(Number.Rational, Entity)? OutOfASum(Entity sum)
{
if (sum is not (Sumf or Minusf))
return null;
var terms = new List<(Entity Coefficient, Number.Rational Exponent)>();
foreach (var term in Sumf.LinearChildren(sum))
{
if (PowerOfX(term, x) is not { } read)
return null;
terms.Add(read);
}
if (terms.Count < 2)
return null;
var lowest = terms.Select(term => term.Exponent).Aggregate((least, next) => next.ERational.CompareTo(least.ERational) < 0 ? next : least);
if (lowest.ERational.IsZero)
return null;
Entity rest = Number.Integer.Zero;
foreach (var (coefficient, exponent) in terms)
{
var shifted = Number.Rational.Create(exponent.ERational.Subtract(lowest.ERational));
rest += shifted is Number.Integer { EInteger.IsZero: true } ? coefficient
: coefficient * (shifted == Number.Integer.One ? x : MathS.Pow(x, shifted));
}
return (lowest, rest);
}
// A product or a quotient with powers of x among its factors and something else of x:
// `x^2 (a + b x^3)`, `(a + b x^3)/x`.
(Number.Rational, Entity)? OutOfAProduct(Entity product)
{
if (product is not (Mulf or Divf))
return null;
var (numerator, denominator) = Functions.SingleQuotient.Of(product);
var exponent = ERational.Zero;
Entity rest = Number.Integer.One;
foreach (var (side, sign) in new[] { (numerator, 1), (denominator, -1) })
foreach (var factor in Mulf.LinearChildren(side))
{
if (factor == x)
exponent = sign > 0 ? exponent.Add(ERational.One) : exponent.Subtract(ERational.One);
else if (factor is Powf(var @base, Number.Rational power) && @base == x)
exponent = sign > 0 ? exponent.Add(power.ERational) : exponent.Subtract(power.ERational);
else
rest = sign > 0 ? rest * factor : rest / factor;
}
return exponent.IsZero || !rest.ContainsNode(x) ? null : (Number.Rational.Create(exponent), rest);
}

// A term as a coefficient free of x and a rational exponent of x, through products and
// quotients.
static (Entity Coefficient, Number.Rational Exponent)? PowerOfX(Entity term, Entity.Variable x)
{
if (!term.ContainsNode(x))
return (term, Number.Integer.Zero);
switch (term)
{
case Entity.Variable v when v == x:
return (Number.Integer.One, Number.Integer.One);
case Powf(var @base, Number.Rational power) when @base == x:
return (Number.Integer.One, power);
case Mulf(var left, var right):
if (PowerOfX(left, x) is not var (lc, le) || PowerOfX(right, x) is not var (rc, re))
return null;
return (lc * rc, Number.Rational.Create(le.ERational.Add(re.ERational)));
case Divf(var left, var right):
if (PowerOfX(left, x) is not var (nc, ne) || PowerOfX(right, x) is not var (dc, de))
return null;
return (nc / dc, Number.Rational.Create(ne.ERational.Subtract(de.ERational)));
default:
return null;
}
}
}

/// <summary>
/// A power of a square written out, <c>(A + B w + C w^2)^p</c> with <c>B^2 = 4 A C</c> and
/// <c>w = x^k</c>, as the power of its root, <c>L = w + B/(2 C)</c>: the integral is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// And any power of a square in any power of x, as the power of its root times a factor
// constant where the root is not zero: `x^2 (a^2 + 2 a b x^3 + b^2 x^6)^p`.
if ((answer = IndefiniteIntegralSolver.SolveByWritingAPowerOfASquareAsAPowerOfItsRoot(expr, x, integrateByParts)) is { }) return answer;
// And a fractional power of a sum whose every term has x in it, the power of x taken out
// with the factor that is constant where it is not zero: `1/sqrt(a x^2 + b x^5)`.
if ((answer = IndefiniteIntegralSolver.SolveByTakingAPowerOfXOutOfAFractionalPower(expr, x, integrateByParts)) is { }) return answer;
// A power of a multiple of a quadratic's derivative beside a power of the quadratic is
// a binomial in the derivative: `(b d + 2 c d x)^m (a + b x + c x^2)^p`.
if ((answer = IndefiniteIntegralSolver.SolveByTheDerivativeOfAQuadraticAsTheVariable(expr, x, integrateByParts)) is { }) return answer;
Expand Down
38 changes: 38 additions & 0 deletions Sources/Tests/UnitTests/Calculus/CombinedRadicalsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ public void ARepeatedFactorComesOutOfTheRootWithItsSign(string integrand)
Assert.Contains("sgn(", integrand.ToEntity().Integrate("x").Stringize());
}

/// <summary>
/// The power of x common to every term of a root of a sum with symbols in it:
/// <c>sqrt(a x^2 + b x^5)</c> is <c>|x| sqrt(a + b x^3)</c>, and the sign of x comes out in
/// front where the power it leaves is odd; <c>sqrt(b x^(2/3) + a x)</c> is
/// <c>x^(1/3) sqrt(b + a x^(1/3))</c> times a factor that is constant where x is not zero.
/// Checked with the symbols pinned after integrating, on both sides of 0 where x is
/// under a whole power.
/// </summary>
[Theory]
[InlineData("1/sqrt(a*x^2 + b*x^5)", new[] { -1.1, -0.6, 0.4, 1.2, 2.3 })]
[InlineData("1/sqrt(x^2*(a + b*x^3))", new[] { -1.1, -0.6, 0.4, 1.2, 2.3 })]
[InlineData("x/(a*x^2 + b*x^3)^(3/2)", new[] { -1.5, -0.6, 0.4, 1.2, 2.3 })]
[InlineData("sqrt(a*x^4 + b*x^6)", new[] { -1.5, -0.6, 0.4, 1.2, 2.3 })]
// A fractional power of x, and the factor that is constant in front: real where x is positive.
[InlineData("1/(x*sqrt(b*x^(2/3) + a*x))", new[] { 0.3, 0.9, 1.7, 2.6 })]
[InlineData("1/(x^2*(b*x^(1/2) + a*x)^(1/2))", new[] { 0.3, 0.9, 1.7, 2.6 })]
public void APowerOfXComesOutOfARootWithSymbolsInIt(string integrand, double[] points)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
var derivative = integral.Substitute("C", 0).Differentiate("x").Substitute("a", 1.3).Substitute("b", 0.7);
var original = integrand.ToEntity().Substitute("a", 1.3).Substitute("b", 0.7);
var compared = 0;
foreach (var at in points)
{
var got = derivative.Substitute("x", at).EvalNumerical();
var want = original.Substitute("x", at).EvalNumerical();
if (got.IsNaN || want.IsNaN)
continue;
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));
Assert.True(difference / scale < 1e-8,
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
}
Assert.True(compared >= 4, $"only {compared} of {points.Length} points were comparable for {integrand}");
}

/// <summary>Inside <c>(-1, 1)</c>, where <c>sqrt(1 - x^2)</c> is real.</summary>
private static readonly double[] InsideTheUnitInterval = { -0.7, -0.3, 0.2, 0.55, 0.85 };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,23 @@ Entity Pinned(Entity e) => e.Substitute("C", 0).Substitute("a", "1/3".ToEntity()
}

[Fact]
public void TheAnswerSaysTheParameterIsReal()
public void TheAnswerNeedsNoAssumptionOnTheParameter()
{
// c^2 > 0 is exactly "c is real and not zero", which is what taking sqrt(c^2 x^2)
// for |c| |x| assumed.
// x^(-2) is positive for a real x, so the root of 1 + 1/(c x)^2 is the root of
// x^2 + 1/c^2 over |x| whatever c is: the answer holds for a c off the real line, on
// both sides of 0, and says nothing of c but that it is not zero.
var integral = "x/sqrt(1 + 1/(c*x)^2)".ToEntity().Integrate("x");
Assert.Contains(integral.Nodes, node => node is Providedf(_, var predicate) && predicate.Nodes.Any(inner => inner == "c^2 > 0".ToEntity()));
Assert.DoesNotContain(integral.Nodes, node => node is Providedf(_, var predicate) && predicate.Nodes.Any(inner => inner == "c^2 > 0".ToEntity()));
var c = "0.7 + 0.4i".ToEntity();
var derivative = integral.Substitute("C", 0).Differentiate("x").Substitute("c", c);
var integrand = "x/sqrt(1 + 1/(c*x)^2)".ToEntity().Substitute("c", c);
foreach (var at in new[] { 0.5, 1.3, -0.8, -1.7 })
{
var got = derivative.Substitute("x", at).EvalNumerical();
var want = integrand.Substitute("x", at).EvalNumerical();
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) < 1e-9,
$"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}");
}
}
}
}
Loading