Skip to content

Commit 0d81cfe

Browse files
A power of x beside a function of a symbolic power of x is integrated by substituting the power, and a square root of a perfect square is the modulus at every depth (#1462)
`x^(n - 1) e^(x^n)` with a symbolic n was declined: the general substitution takes a concrete power, and nothing took a symbolic one. It is `e^(x^n)/n` under u = x^n, and `x^(k n - 1) g(x^n)` for a whole k is `u^(k - 1) g(u)/n`, which is Rubi's 6.5.2 and 6.6.2 -- `(e x)^(n - 1) (a + b sech(c + d x^n))^p`, where the power in front is `e^(n - 1) x^(n - 1)` for a positive e, and the answer says so. `1/sqrt(1 + csch(x)^2)` was answered wrongly: under u = tanh(x) it is `sqrt(u^2)/(u^2 - 1)`, and six levels down the rule for a root of a quadratic beside a linear read a perfect square as a root and answered with a logarithm of zero. A square root of a perfect square is now written as `sgn(x + h) (x + h)` at every depth, before anything reads it, and Euler's substitution declines a perfect square rather than substituting `t = sqrt(u^2) - u`. The square root only: `(1 + 2u + u^2)^(5/2)` under the exponential substitution is a sign written for a variable whose sign the substitution knows, and rewriting it sent `tanh(x)/(a + b tanh(x)^2)^(5/2)` from twenty seconds to past the budget. Family 6 of the Rubi suite: 315 -> 335 of 417, 0 wrong (one wrong before); family 7 215/270 unchanged; the 1774-problem suite 1707, 0 wrong, 0 timeout. Allocation gate passed on all 19 gated benchmarks. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent f75e9bf commit 0d81cfe

6 files changed

Lines changed: 317 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
993993
| `"sum(binomial(n,k)^2, k, 0, n)".ToEntity().Evaled` | parse error | `binomial(2 n, n)` |
994994
| `"sum(binomial(i,k), i, 0, n)".ToEntity().Evaled` | parse error | `binomial(n + 1, k + 1)` |
995995

996+
### A power of `x` beside a function of a symbolic power of `x` is integrated by substituting the power
997+
998+
`x^(n - 1) e^(x^n)` with a symbolic `n` was left as written: it is `e^(x^n)/n`, under `u = x^n`,
999+
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
1000+
6.6.2, where the power in front is written `(e x)^(n - 1)`, which is `e^(n - 1) x^(n - 1)` for a
1001+
positive `e` and the answer says so. And a square root of a perfect square is the modulus at
1002+
every depth: `1/sqrt(1 + csch(x)^2)` under `u = tanh(x)` is `sqrt(u^2)/(u^2 - 1)`, which the rule
1003+
for a root of a quadratic beside a linear read as a root of a quadratic and answered with a
1004+
logarithm of zero -- wrong on `master` since the hyperbolic entry above, unevaluated in 2.5.0,
1005+
and in no release -- and is `sgn(u) u/(u^2 - 1)`
1006+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
1007+
1008+
| Input | Was (2.5.0) | Now |
1009+
|---|---|---|
1010+
| `"x^(n-1)*e^(x^n)".Integrate("x")` | left unevaluated | `e^(x^n)/n` |
1011+
| `"(pe*x)^(-1+n)*(a+b*csch(c+d*x^n))".Integrate("x")` | left unevaluated | the antiderivative, `provided pe > 0` |
1012+
| `"(pe*x)^(-1+n)/(a+b*sech(c+d*x^n))^2".Integrate("x")` | left unevaluated | the antiderivative, by cases on `a` and `b` |
1013+
| `"1/sqrt(1+csch(x)^2)".Integrate("x")` | left unevaluated | `-sgn(tanh(x)) ln(tanh(x)^2 - 1)/2`, in exponentials |
1014+
| `"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` |
1015+
9961016
### `binomial(n, k)` is a function
9971017

9981018
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
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
293293

294+
[Tests/UnitTests/Calculus/APowerTimesAFunctionOfItsPowerTest.cs]
295+
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
296+
297+
[Tests/UnitTests/Calculus/RootOfAPerfectSquareIntegralTest.cs]
298+
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
299+
294300
[Tests/UnitTests/Calculus/BinomialIdentitiesTest.cs]
295301
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
296302

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9093,6 +9093,13 @@ Dictionary<EInteger, Entity> PowerOfQ(int j)
90939093
&& TryReadAPowerTimesARadicalQuadratic(expr, x))
90949094
return null;
90959095

9096+
// A perfect square under the root is no radical: `sqrt(u^2)` is `|u|`, which
9097+
// `SolveByTakingARootOfAPerfectSquare` writes as `sgn(u) u`, and the substitution
9098+
// written for it -- `t = sqrt(u^2) - u`, which is zero on half the line -- is
9099+
// declined rather than made.
9100+
if ((b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is Number.Complex { IsZero: true })
9101+
return null;
9102+
90969103
var t = Variable.CreateUnique(expr, "t_euler");
90979104
Entity xInT, rootInT;
90989105
Entity backSubstitution;
@@ -10041,6 +10048,146 @@ Entity Without(Entity side)
1004110048
return answer;
1004210049
}
1004310050

10051+
/// <summary>
10052+
/// <c>x^(n - 1) g(x^n)</c> with a symbolic <c>n</c>, which is <c>g(u)/n</c> under
10053+
/// <c>u = x^n</c>: the power in front is the derivative of the power inside up to the
10054+
/// constant <c>n</c>, and the rest mentions <c>x</c> only as <c>x^n</c>. Rubi's 6.5.2 and
10055+
/// 6.6.2 -- <c>(e x)^(n - 1) (a + b sech(c + d x^n))^p</c> -- write the power in front as
10056+
/// <c>(e x)^(n - 1)</c>, which is <c>e^(n - 1) x^(n - 1)</c> for a positive <c>e</c>, and
10057+
/// the answer says so. A concrete <c>n</c> is the general substitution's; a symbolic
10058+
/// one was nobody's, and <c>x^(n - 1) e^(x^n)</c> was declined.
10059+
/// https://github.com/asc-community/AngouriMath/issues/718
10060+
/// </summary>
10061+
internal static Entity? SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(Entity expr, Entity.Variable x, bool integrateByParts)
10062+
{
10063+
Entity? inFront = null;
10064+
Entity? scale = null;
10065+
Entity? rest = null;
10066+
Entity constant = Number.Integer.One;
10067+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
10068+
{
10069+
if (!factor.ContainsNode(x))
10070+
{
10071+
constant = underneath ? constant / factor : constant * factor;
10072+
continue;
10073+
}
10074+
// The power in front: x^m, or (e x)^m with e free of x, to a symbolic m.
10075+
if (inFront is null && !underneath && factor is Powf(var @base, var exponent) && !exponent.ContainsNode(x)
10076+
&& exponent.Evaled is not Number && exponent.Vars.Any()
10077+
&& (@base == x || @base is Mulf(var l, var r) && (l == x && !r.ContainsNode(x) || r == x && !l.ContainsNode(x))))
10078+
{
10079+
inFront = exponent;
10080+
scale = @base == x ? null : @base is Mulf(var l2, var r2) ? (l2 == x ? r2 : l2) : null;
10081+
continue;
10082+
}
10083+
var piece = underneath ? Number.Integer.One / factor : factor;
10084+
rest = rest is null ? piece : rest * piece;
10085+
}
10086+
if (inFront is null || rest is null)
10087+
return null;
10088+
// The rest mentions x only as x^n, one n throughout.
10089+
Entity? n = null;
10090+
foreach (var node in rest.Nodes)
10091+
if (node is Powf(var b, var e) && b == x && !e.ContainsNode(x))
10092+
{
10093+
if (n is null)
10094+
n = e;
10095+
else if (n != e)
10096+
return null;
10097+
}
10098+
if (n is null || n.Evaled is Number)
10099+
return null;
10100+
// m + 1 = k n for a whole k >= 1: x^(k n - 1) g(x^n) is u^(k - 1) g(u)/n.
10101+
// Bare: the simplification attaches `provided not n = 0` to the quotient it
10102+
// cancelled, and the generic case is what every rule answers in.
10103+
var timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).InnerSimplified);
10104+
if (timesN is not Number.Integer k || k.EInteger.Sign <= 0)
10105+
{
10106+
timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).Simplify());
10107+
if (timesN is not Number.Integer k2 || k2.EInteger.Sign <= 0)
10108+
return null;
10109+
k = k2;
10110+
}
10111+
var u = Variable.CreateUnique(expr, "u_pow");
10112+
var inU = rest.Replace(node => node is Powf(var b, var e) && b == x && e == n ? u : node);
10113+
if (inU.ContainsNode(x))
10114+
return null;
10115+
if (k != Number.Integer.One)
10116+
inU = MathS.Pow(u, (k - Number.Integer.One).InnerSimplified) * inU;
10117+
if (Integration.ComputeAsAQuestionOfItsOwn(inU, u, integrateByParts) is not { } inner)
10118+
return null;
10119+
Entity answer = constant * inner.Substitute(u, MathS.Pow(x, n)) / n;
10120+
// (e x)^(n - 1) is e^(n - 1) x^(n - 1) for a positive e, which the answer states.
10121+
if (scale is { })
10122+
{
10123+
answer = MathS.Pow(scale, inFront) * answer;
10124+
if (scale.Evaled is not Number.Real { IsPositive: true })
10125+
answer = answer.Provided(new Greaterf(scale, Number.Integer.Zero));
10126+
}
10127+
return answer;
10128+
}
10129+
10130+
/// <summary>
10131+
/// A square root of a perfect square in <paramref name="x"/> is the modulus:
10132+
/// <c>sqrt(x^2)</c> is <c>|x|</c>, which for a real <c>x</c> is <c>sgn(x) x</c>, and
10133+
/// <c>sqrt(a (x + h)^2)</c> is <c>sqrt(a) sgn(x + h) (x + h)</c> for a positive number
10134+
/// <c>a</c>. Every rule that reads a root of a quadratic assumes it is not one:
10135+
/// <c>1/sqrt(1 + csch(x)^2)</c> under <c>u = tanh(x)</c> is <c>sqrt(u^2)/(u^2 - 1)</c>,
10136+
/// and the table rule for <c>sqrt(a w^2 + b w + c)</c> beside a linear, reached after the
10137+
/// partial fractions and a reciprocal, answered it with a logarithm of zero.
10138+
/// https://github.com/asc-community/AngouriMath/issues/718
10139+
/// </summary>
10140+
/// <remarks>
10141+
/// At every depth, unlike <see cref="SolveByTakingASquareFactorOutOfARoot"/>, since the
10142+
/// misreading is at depth; and the square root only, not <c>(x^2 + 2x + 1)^(5/2)</c>,
10143+
/// which that rule writes as <c>sgn(x + 1) (x + 1)^5</c> at the top. Below a
10144+
/// substitution the variable is one whose sign the substitution knows, and a sign
10145+
/// written for it is a factor the rest of the search has to carry:
10146+
/// <c>tanh(x)/(a + b tanh(x)^2)^(5/2)</c> under <c>u = e^(2x)</c> holds
10147+
/// <c>(1 + 2u + u^2)^(5/2)</c>, and with that rewritten the search ran past its budget
10148+
/// where it had answered in twenty seconds.
10149+
/// </remarks>
10150+
internal static Entity? SolveByTakingARootOfAPerfectSquare(Entity expr, Entity.Variable x, bool integrateByParts)
10151+
{
10152+
// Asked of every integrand at every depth, so the radicand is read as a polynomial
10153+
// only where it is small enough to be a written square: x^2, or a x^2 + b x + c.
10154+
static bool MayBeARootOfASquare(Entity node, Entity.Variable x)
10155+
=> node is Powf(var radicand, Number.Rational r) && r.ERational.Equals(ERational.Create(1, 2))
10156+
&& radicand.ContainsNode(x) && radicand.Complexity <= 12
10157+
&& radicand.Nodes.Count(inner => inner == x) <= 2;
10158+
if (!expr.Nodes.Any(node => MayBeARootOfASquare(node, x)))
10159+
return null;
10160+
var changed = false;
10161+
var written = expr.Replace(node =>
10162+
{
10163+
if (!MayBeARootOfASquare(node, x))
10164+
return node;
10165+
var (radicand, exponent) = (Powf)node;
10166+
var r = (Number.Rational)exponent;
10167+
if (!TreeAnalyzer.TryGetPolynomial(radicand, x, out var monomials) || monomials.Count == 0)
10168+
return node;
10169+
var degree = monomials.Keys.Max()!;
10170+
if (!degree.Equals(EInteger.FromInt32(2)) || monomials.Keys.Any(k => k.Sign < 0))
10171+
return node;
10172+
var a = monomials.TryGetValue(EInteger.FromInt32(2), out var a2) ? a2 : Number.Integer.Zero;
10173+
var b = monomials.TryGetValue(EInteger.One, out var b1) ? b1 : Number.Integer.Zero;
10174+
var c = monomials.TryGetValue(EInteger.Zero, out var c0) ? c0 : Number.Integer.Zero;
10175+
if (a.Evaled is not Number.Real { IsPositive: true } leading || b.Evaled is not Number.Real || c.Evaled is not Number.Real
10176+
|| (b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is not Number.Complex { IsZero: true })
10177+
return node;
10178+
// a (x + h)^2 with h = b/(2a): (a (x + h)^2)^r is a^r |x + h|^(2r), and with 2r odd
10179+
// (a rational is held in lowest terms) that is a^r sgn(x + h) (x + h)^(2r).
10180+
var h = (b / (Number.Integer.Create(2) * a)).InnerSimplified;
10181+
var linear = h == Number.Integer.Zero ? x : (x + h).InnerSimplified;
10182+
var twoR = Number.Integer.Create(r.ERational.Numerator);
10183+
Entity power = twoR == Number.Integer.One ? linear : MathS.Pow(linear, twoR);
10184+
Entity signed = MathS.Signum(linear) * power;
10185+
changed = true;
10186+
return leading == Number.Integer.One ? signed : MathS.Pow(leading, r) * signed;
10187+
});
10188+
return changed ? Integration.ComputeAsTheSameQuestion(written, x, integrateByParts) : null;
10189+
}
10190+
1004410191
/// <summary>
1004510192
/// A whole power of a product in which the variable stands beside a constant, written
1004610193
/// as the product of the powers: <c>(c x)^2</c> is <c>c^2 x^2</c>, exact for a whole

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
501501
// An exponential of a multiple of a logarithm is a power of the argument, which is
502502
// how every inverse hyperbolic function under an exponential arrives.
503503
if ((answer = IndefiniteIntegralSolver.SolveByFoldingAnExponentialOfALogarithm(expr, x, integrateByParts)) is { }) return answer;
504+
// A fractional power of a perfect square is the power of the modulus, sgn(P) P^(2r).
505+
if ((answer = IndefiniteIntegralSolver.SolveByTakingARootOfAPerfectSquare(expr, x, integrateByParts)) is { }) return answer;
506+
// x^(n - 1) g(x^n) with a symbolic n is g(u)/n under u = x^n.
507+
if ((answer = IndefiniteIntegralSolver.SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(expr, x, integrateByParts)) is { }) return answer;
504508
// A whole power of a product of a constant and the variable, as the product of
505509
// the powers, which is how the inverse hyperbolic secant and cosecant arrive.
506510
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfProducts(expr, x, integrateByParts)) is { }) return answer;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// <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
16+
/// <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>,
17+
/// which is <c>e^(n - 1) x^(n - 1)</c> for a positive <c>e</c>, and the answer says so.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
19+
/// </summary>
20+
[Trait("Area", "Calculus")]
21+
public sealed class APowerTimesAFunctionOfItsPowerTest
22+
{
23+
private static readonly double[] Points = { 0.3, 0.7, 1.1, 1.9, 2.6 };
24+
25+
/// <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>
26+
private static void DifferentiatesBack(string integrand)
27+
{
28+
var integral = integrand.ToEntity().Integrate("x");
29+
Assert.DoesNotContain("integral(", integral.Stringize());
30+
Entity Pinned(Entity e) => e.Substitute("C", 0).Substitute("n", "5/2".ToEntity()).Substitute("a", "1/3".ToEntity()).Substitute("b", "1/2".ToEntity())
31+
.Substitute("c", "3/2".ToEntity()).Substitute("d", "2/3".ToEntity()).Substitute("pe", "5/4".ToEntity());
32+
var derivative = Pinned(integral).Differentiate("x");
33+
var original = Pinned(integrand.ToEntity());
34+
var compared = 0;
35+
foreach (var at in Points)
36+
{
37+
var got = derivative.Substitute("x", at).EvalNumerical();
38+
var want = original.Substitute("x", at).EvalNumerical();
39+
if (got.IsNaN || want.IsNaN)
40+
continue;
41+
compared++;
42+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
43+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
44+
Assert.True(difference / scale < 1e-9,
45+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
46+
}
47+
Assert.True(compared >= 4, $"only {compared} of {Points.Length} points were comparable for {integrand}");
48+
}
49+
50+
[Theory]
51+
[InlineData("x^(n-1)*e^(x^n)")]
52+
[InlineData("x^(2*n-1)*e^(x^n)")]
53+
[InlineData("(pe*x)^(-1+n)*(a+b*csch(c+d*x^n))")]
54+
[InlineData("(pe*x)^(-1+n)/(a+b*csch(c+d*x^n))")]
55+
[InlineData("(pe*x)^(-1+n)*sech(c+d*x^n)^2")]
56+
[InlineData("(pe*x)^(-1+n)/(a+b*sech(c+d*x^n))^2")]
57+
public void APowerTimesAFunctionOfItsPower(string integrand) => DifferentiatesBack(integrand);
58+
59+
[Fact]
60+
public void TheScaleIsSaidToBePositive()
61+
{
62+
var integral = "(pe*x)^(-1+n)*e^(x^n)".ToEntity().Integrate("x");
63+
Assert.Contains(integral.Nodes, node => node is Entity.Providedf(_, var predicate) && predicate == "pe > 0".ToEntity());
64+
Assert.Equal("e^(x^n)/n".ToEntity().Simplify(), "x^(n-1)*e^(x^n)".ToEntity().Integrate("x").Substitute("C", 0).Simplify());
65+
}
66+
67+
/// <summary>A power in front that is not one less than a multiple of the power inside is left alone.</summary>
68+
[Fact]
69+
public void APowerThatIsNotTheDerivativeIsLeftAsWritten()
70+
=> Assert.Contains("integral(", "x^n*e^(x^n)".ToEntity().Integrate("x").Stringize());
71+
}
72+
}

0 commit comments

Comments
 (0)