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
20 changes: 20 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,26 @@ the even or the odd indices, each `2^(n - 1)` — Prop 8.4.4, Thm 8.4.6, Ex 8.3.
| `"sum(binomial(n,k)^2, k, 0, n)".ToEntity().Evaled` | parse error | `binomial(2 n, n)` |
| `"sum(binomial(i,k), i, 0, n)".ToEntity().Evaled` | parse error | `binomial(n + 1, k + 1)` |

### A power of `x` beside a function of a symbolic power of `x` is integrated by substituting the power

`x^(n - 1) e^(x^n)` with a symbolic `n` was left as written: it is `e^(x^n)/n`, under `u = x^n`,
and with it `x^(k n - 1) g(x^n)` for a whole `k`, which is `u^(k - 1) g(u)/n` -- Rubi's 6.5.2 and
6.6.2, where the power in front is written `(e x)^(n - 1)`, which is `e^(n - 1) x^(n - 1)` for a
positive `e` and the answer says so. And a square root of a perfect square is the modulus at
every depth: `1/sqrt(1 + csch(x)^2)` under `u = tanh(x)` is `sqrt(u^2)/(u^2 - 1)`, which the rule
for a root of a quadratic beside a linear read as a root of a quadratic and answered with a
logarithm of zero -- wrong on `master` since the hyperbolic entry above, unevaluated in 2.5.0,
and in no release -- and is `sgn(u) u/(u^2 - 1)`
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"x^(n-1)*e^(x^n)".Integrate("x")` | left unevaluated | `e^(x^n)/n` |
| `"(pe*x)^(-1+n)*(a+b*csch(c+d*x^n))".Integrate("x")` | left unevaluated | the antiderivative, `provided pe > 0` |
| `"(pe*x)^(-1+n)/(a+b*sech(c+d*x^n))^2".Integrate("x")` | left unevaluated | the antiderivative, by cases on `a` and `b` |
| `"1/sqrt(1+csch(x)^2)".Integrate("x")` | left unevaluated | `-sgn(tanh(x)) ln(tanh(x)^2 - 1)/2`, in exponentials |
| `"x^4/sech(2*ln(c*x))^(1/2)".Integrate("x")` | left unevaluated | `sgn(x) (1 + c^4 x^4)^(3/2)/(6 sqrt(2) c^5)` up to the form, `provided c^4 > 0` |

### `binomial(n, k)` is a function

**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Calculus/APowerTimesAFunctionOfItsPowerTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Calculus/RootOfAPerfectSquareIntegralTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9093,6 +9093,13 @@ Dictionary<EInteger, Entity> PowerOfQ(int j)
&& TryReadAPowerTimesARadicalQuadratic(expr, x))
return null;

// A perfect square under the root is no radical: `sqrt(u^2)` is `|u|`, which
// `SolveByTakingARootOfAPerfectSquare` writes as `sgn(u) u`, and the substitution
// written for it -- `t = sqrt(u^2) - u`, which is zero on half the line -- is
// declined rather than made.
if ((b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is Number.Complex { IsZero: true })
return null;

var t = Variable.CreateUnique(expr, "t_euler");
Entity xInT, rootInT;
Entity backSubstitution;
Expand Down Expand Up @@ -10041,6 +10048,146 @@ Entity Without(Entity side)
return answer;
}

/// <summary>
/// <c>x^(n - 1) g(x^n)</c> with a symbolic <c>n</c>, which is <c>g(u)/n</c> under
/// <c>u = x^n</c>: the power in front is the derivative of the power inside up to the
/// constant <c>n</c>, and the rest mentions <c>x</c> only as <c>x^n</c>. Rubi's 6.5.2 and
/// 6.6.2 -- <c>(e x)^(n - 1) (a + b sech(c + d x^n))^p</c> -- write the power in front as
/// <c>(e x)^(n - 1)</c>, which is <c>e^(n - 1) x^(n - 1)</c> for a positive <c>e</c>, and
/// the answer says so. A concrete <c>n</c> is the general substitution's; a symbolic
/// one was nobody's, and <c>x^(n - 1) e^(x^n)</c> was declined.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </summary>
internal static Entity? SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(Entity expr, Entity.Variable x, bool integrateByParts)
{
Entity? inFront = null;
Entity? scale = null;
Entity? rest = null;
Entity constant = Number.Integer.One;
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
{
if (!factor.ContainsNode(x))
{
constant = underneath ? constant / factor : constant * factor;
continue;
}
// The power in front: x^m, or (e x)^m with e free of x, to a symbolic m.
if (inFront is null && !underneath && factor is Powf(var @base, var exponent) && !exponent.ContainsNode(x)
&& exponent.Evaled is not Number && exponent.Vars.Any()
&& (@base == x || @base is Mulf(var l, var r) && (l == x && !r.ContainsNode(x) || r == x && !l.ContainsNode(x))))
{
inFront = exponent;
scale = @base == x ? null : @base is Mulf(var l2, var r2) ? (l2 == x ? r2 : l2) : null;
continue;
}
var piece = underneath ? Number.Integer.One / factor : factor;
rest = rest is null ? piece : rest * piece;
}
if (inFront is null || rest is null)
return null;
// The rest mentions x only as x^n, one n throughout.
Entity? n = null;
foreach (var node in rest.Nodes)
if (node is Powf(var b, var e) && b == x && !e.ContainsNode(x))
{
if (n is null)
n = e;
else if (n != e)
return null;
}
if (n is null || n.Evaled is Number)
return null;
// m + 1 = k n for a whole k >= 1: x^(k n - 1) g(x^n) is u^(k - 1) g(u)/n.
// Bare: the simplification attaches `provided not n = 0` to the quotient it
// cancelled, and the generic case is what every rule answers in.
var timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).InnerSimplified);
if (timesN is not Number.Integer k || k.EInteger.Sign <= 0)
{
timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).Simplify());
if (timesN is not Number.Integer k2 || k2.EInteger.Sign <= 0)
return null;
k = k2;
}
var u = Variable.CreateUnique(expr, "u_pow");
var inU = rest.Replace(node => node is Powf(var b, var e) && b == x && e == n ? u : node);
if (inU.ContainsNode(x))
return null;
if (k != Number.Integer.One)
inU = MathS.Pow(u, (k - Number.Integer.One).InnerSimplified) * inU;
if (Integration.ComputeAsAQuestionOfItsOwn(inU, u, integrateByParts) is not { } inner)
return null;
Entity answer = constant * inner.Substitute(u, MathS.Pow(x, n)) / n;
// (e x)^(n - 1) is e^(n - 1) x^(n - 1) for a positive e, which the answer states.
if (scale is { })
{
answer = MathS.Pow(scale, inFront) * answer;
if (scale.Evaled is not Number.Real { IsPositive: true })
answer = answer.Provided(new Greaterf(scale, Number.Integer.Zero));
}
return answer;
}

/// <summary>
/// A square root of a perfect square in <paramref name="x"/> is the modulus:
/// <c>sqrt(x^2)</c> is <c>|x|</c>, which for a real <c>x</c> is <c>sgn(x) x</c>, and
/// <c>sqrt(a (x + h)^2)</c> is <c>sqrt(a) sgn(x + h) (x + h)</c> for a positive number
/// <c>a</c>. Every rule that reads a root of a quadratic assumes it is not one:
/// <c>1/sqrt(1 + csch(x)^2)</c> under <c>u = tanh(x)</c> is <c>sqrt(u^2)/(u^2 - 1)</c>,
/// and the table rule for <c>sqrt(a w^2 + b w + c)</c> beside a linear, reached after the
/// partial fractions and a reciprocal, answered it with a logarithm of zero.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </summary>
/// <remarks>
/// At every depth, unlike <see cref="SolveByTakingASquareFactorOutOfARoot"/>, since the
/// misreading is at depth; and the square root only, not <c>(x^2 + 2x + 1)^(5/2)</c>,
/// which that rule writes as <c>sgn(x + 1) (x + 1)^5</c> at the top. Below a
/// substitution the variable is one whose sign the substitution knows, and a sign
/// written for it is a factor the rest of the search has to carry:
/// <c>tanh(x)/(a + b tanh(x)^2)^(5/2)</c> under <c>u = e^(2x)</c> holds
/// <c>(1 + 2u + u^2)^(5/2)</c>, and with that rewritten the search ran past its budget
/// where it had answered in twenty seconds.
/// </remarks>
internal static Entity? SolveByTakingARootOfAPerfectSquare(Entity expr, Entity.Variable x, bool integrateByParts)
{
// Asked of every integrand at every depth, so the radicand is read as a polynomial
// only where it is small enough to be a written square: x^2, or a x^2 + b x + c.
static bool MayBeARootOfASquare(Entity node, Entity.Variable x)
=> node is Powf(var radicand, Number.Rational r) && r.ERational.Equals(ERational.Create(1, 2))
&& radicand.ContainsNode(x) && radicand.Complexity <= 12
&& radicand.Nodes.Count(inner => inner == x) <= 2;
if (!expr.Nodes.Any(node => MayBeARootOfASquare(node, x)))
return null;
var changed = false;
var written = expr.Replace(node =>
{
if (!MayBeARootOfASquare(node, x))
return node;
var (radicand, exponent) = (Powf)node;
var r = (Number.Rational)exponent;
if (!TreeAnalyzer.TryGetPolynomial(radicand, x, out var monomials) || monomials.Count == 0)
return node;
var degree = monomials.Keys.Max()!;
if (!degree.Equals(EInteger.FromInt32(2)) || monomials.Keys.Any(k => k.Sign < 0))
return node;
var a = monomials.TryGetValue(EInteger.FromInt32(2), out var a2) ? a2 : Number.Integer.Zero;
var b = monomials.TryGetValue(EInteger.One, out var b1) ? b1 : Number.Integer.Zero;
var c = monomials.TryGetValue(EInteger.Zero, out var c0) ? c0 : Number.Integer.Zero;
if (a.Evaled is not Number.Real { IsPositive: true } leading || b.Evaled is not Number.Real || c.Evaled is not Number.Real
|| (b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is not Number.Complex { IsZero: true })
return node;
// a (x + h)^2 with h = b/(2a): (a (x + h)^2)^r is a^r |x + h|^(2r), and with 2r odd
// (a rational is held in lowest terms) that is a^r sgn(x + h) (x + h)^(2r).
var h = (b / (Number.Integer.Create(2) * a)).InnerSimplified;
var linear = h == Number.Integer.Zero ? x : (x + h).InnerSimplified;
var twoR = Number.Integer.Create(r.ERational.Numerator);
Entity power = twoR == Number.Integer.One ? linear : MathS.Pow(linear, twoR);
Entity signed = MathS.Signum(linear) * power;
changed = true;
return leading == Number.Integer.One ? signed : MathS.Pow(leading, r) * signed;
});
return changed ? Integration.ComputeAsTheSameQuestion(written, x, integrateByParts) : null;
}

/// <summary>
/// A whole power of a product in which the variable stands beside a constant, written
/// as the product of the powers: <c>(c x)^2</c> is <c>c^2 x^2</c>, exact for a whole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// An exponential of a multiple of a logarithm is a power of the argument, which is
// how every inverse hyperbolic function under an exponential arrives.
if ((answer = IndefiniteIntegralSolver.SolveByFoldingAnExponentialOfALogarithm(expr, x, integrateByParts)) is { }) return answer;
// A fractional power of a perfect square is the power of the modulus, sgn(P) P^(2r).
if ((answer = IndefiniteIntegralSolver.SolveByTakingARootOfAPerfectSquare(expr, x, integrateByParts)) is { }) return answer;
// x^(n - 1) g(x^n) with a symbolic n is g(u)/n under u = x^n.
if ((answer = IndefiniteIntegralSolver.SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(expr, x, integrateByParts)) is { }) return answer;
// A whole power of a product of a constant and the variable, as the product of
// the powers, which is how the inverse hyperbolic secant and cosecant arrive.
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfProducts(expr, x, integrateByParts)) is { }) return answer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// 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>
/// <c>x^(k n - 1) g(x^n)</c> with a symbolic <c>n</c> is <c>u^(k - 1) g(u)/n</c> under
/// <c>u = x^n</c>; Rubi's 6.5.2 and 6.6.2 write the power in front as <c>(e x)^(n - 1)</c>,
/// which is <c>e^(n - 1) x^(n - 1)</c> for a positive <c>e</c>, and the answer says so.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
[Trait("Area", "Calculus")]
public sealed class APowerTimesAFunctionOfItsPowerTest
{
private static readonly double[] Points = { 0.3, 0.7, 1.1, 1.9, 2.6 };

/// <summary>Pinned with <c>n = 5/2</c>, <c>a = 1/3</c>, <c>b = 1/2</c>, <c>c = 3/2</c>, <c>d = 2/3</c>, <c>e = 5/4</c>.</summary>
private static void DifferentiatesBack(string integrand)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());
Entity Pinned(Entity e) => e.Substitute("C", 0).Substitute("n", "5/2".ToEntity()).Substitute("a", "1/3".ToEntity()).Substitute("b", "1/2".ToEntity())
.Substitute("c", "3/2".ToEntity()).Substitute("d", "2/3".ToEntity()).Substitute("pe", "5/4".ToEntity());
var derivative = Pinned(integral).Differentiate("x");
var original = Pinned(integrand.ToEntity());
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) + 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}");
}
Assert.True(compared >= 4, $"only {compared} of {Points.Length} points were comparable for {integrand}");
}

[Theory]
[InlineData("x^(n-1)*e^(x^n)")]
[InlineData("x^(2*n-1)*e^(x^n)")]
[InlineData("(pe*x)^(-1+n)*(a+b*csch(c+d*x^n))")]
[InlineData("(pe*x)^(-1+n)/(a+b*csch(c+d*x^n))")]
[InlineData("(pe*x)^(-1+n)*sech(c+d*x^n)^2")]
[InlineData("(pe*x)^(-1+n)/(a+b*sech(c+d*x^n))^2")]
public void APowerTimesAFunctionOfItsPower(string integrand) => DifferentiatesBack(integrand);

[Fact]
public void TheScaleIsSaidToBePositive()
{
var integral = "(pe*x)^(-1+n)*e^(x^n)".ToEntity().Integrate("x");
Assert.Contains(integral.Nodes, node => node is Entity.Providedf(_, var predicate) && predicate == "pe > 0".ToEntity());
Assert.Equal("e^(x^n)/n".ToEntity().Simplify(), "x^(n-1)*e^(x^n)".ToEntity().Integrate("x").Substitute("C", 0).Simplify());
}

/// <summary>A power in front that is not one less than a multiple of the power inside is left alone.</summary>
[Fact]
public void APowerThatIsNotTheDerivativeIsLeftAsWritten()
=> Assert.Contains("integral(", "x^n*e^(x^n)".ToEntity().Integrate("x").Stringize());
}
}
Loading
Loading