From 7d336105ce84d659b8d78a5b933cba3e38b60995 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 5 Oct 2026 02:33:19 +0000 Subject: [PATCH 1/2] A function of x^n for a symbolic n beside a power of x is integrated in a power of x x^(-1 + 4n)/(a + b x^n + c x^(2n)) and the rest of Rubi's 1.2.3.2 functions of x^n beside a power x^m with (m + 1)/n rational were declined: the substitution search reads x^(2n) as a power of x^n only for a number n, and offered nothing for x^(n/2), which is not written. Under u = x^(n/d) every such power is a whole power of u, and dx/x is (d/n) du/u. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 14 +++++ .../Integration/IndefiniteIntegralSolver.cs | 56 +++++++++++++++++++ .../Integration/Integration.Definition.cs | 4 ++ ...werOfXWithASymbolicExponentIntegralTest.cs | 50 +++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index dc39a1fe6..e5171816f 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -213,6 +213,20 @@ rules' already | `"asin(sqrt(1 + x) - sqrt(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `arcsin(sqrt(1 + x) - sqrt(x))` | | `"x^3*atan(-sqrt(x)+sqrt(1+x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in the arctangent | +### A function of `x^n` for a symbolic `n` beside a power of `x` is integrated in a power of `x` + +**Answers where there were none.** `x^(-1 + 4n)/(a + b x^n + c x^(2n))` and the rest of Rubi's +1.2.3.2 functions of `x^n` beside a power `x^m` with `(m + 1)/n` rational were declined: the +substitution search reads `x^(2n)` as a power of `x^n` only for a number `n`, and offered nothing +for `x^(n/2)`, which is not written. Under `u = x^(n/d)` every such power is a whole power of `u`, +and `dx/x` is `(d/n) du/u` ([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^(-1+4*n)/(a+b*x^n+c*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | powers of `x^n`, a logarithm and an arctangent of it, over `n` | +| `"1/(x*(a+b*x^n+c*x^(2*n)))".ToEntity().Integrate("x")` | `integral(...)` | `ln(x^n)/(a n)`, a logarithm and an arctangent of `x^n` | +| `"x^(-1+n/2)/(a+b*x^n+c*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | arctangents of `x^(n/2)` over the roots of `a + b u + c u^2`, over `n` | + ### Rational functions of `x` and `x^n` with symbols in them are taken apart **Answers where there were none.** The partial fractions took apart a denominator with symbols in it diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 875a31f1c..ad5bdbd83 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -24585,6 +24585,62 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab return !stripped.ContainsNode(x); } + /// + /// A function of x^n for a symbolic n beside a power of x, + /// x^m R(x^n) with (m + 1)/n rational, under u = x^(n/d): Rubi's + /// x^(-1 + 4n)/(a + b x^n + c x^(2n)) and x^(-1 + n/4)/(b x^n + c x^(2n)). + /// + /// + /// + /// With g = x f(x), int f dx = (d/n) int g/u du, since dx/x = (d/n) du/u; + /// and every power x^t in g is u^(t d/n), a whole power of u + /// where t d/n is a whole number, which makes it the same value for every + /// x. The search reads x^(2n) as a power of x^n only for a number + /// n, and found no candidate for x^(n/4), which is not written; so these + /// were declined. The powers of x are gathered first, x x^(-1 + 4n) into + /// x^(4n), and the unit is each exponent left, divided by up to six. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + /// + internal static Entity? SolveByAPowerOfXWithASymbolicExponent(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base == x && exponent is not Number && !exponent.ContainsNode(x))) + return null; + // Over one bar first, so that an x inside `(x (a + b x^n))^(-1)` is a factor to gather. + var g = Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(x * expr)); + var exponents = g.Nodes.OfType().Where(power => power.Base == x && power.Exponent is not Number && !power.Exponent.ContainsNode(x)) + .Select(power => power.Exponent).Distinct().OrderBy(exponent => exponent.Complexity).ToList(); + if (exponents.Count == 0) + return null; + var u = Variable.CreateUnique(expr, "u_power"); + foreach (var unit in exponents) + foreach (var d in new[] { 1, 2, 3, 4, 6 }) + { + var step = (unit / d).InnerSimplified; + // Every power of x a whole power of u, and no x left bare or elsewhere. + var whole = true; + var inU = g.Replace(node => + { + if (!whole || node is not Powf(var @base, var exponent) || @base != x) + return node; + if (Functions.PartialFractions.Bare((exponent / step).Simplify()) is Number.Integer power) + return power == Number.Integer.One ? u : MathS.Pow(u, power); + whole = false; + return node; + }); + if (!whole || inU.ContainsNode(x)) + continue; + // Divided by u through the gathering, where `u/u` left in is a quotient the + // chain declines; the constant `d/n` outside it. + var overU = Functions.PartialFractions.Bare(Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(inU) * MathS.Pow(u, -1)).InnerSimplified); + if (Integration.ComputeIndefiniteIntegral(overU, u, integrateByParts) is not { } inTermsOfU) + return null; + var answer = (d / unit * inTermsOfU).Substitute(u, MathS.Pow(x, step)).InnerSimplified; + return answer.Nodes.Any(node => node is Number.Complex { IsNaN: true }) ? null : answer; + } + return null; + } + /// /// A quotient whose numerator is a constant multiple of its denominator's derivative, /// with a symbolic exponent about: c ln(D), the constant read off the terms. diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 15f973f6b..0350cf272 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -868,6 +868,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // Two roots of linears with one slope, rationalised together by their sum: before the // substitution search, which reads each root on its own. if ((answer = IndefiniteIntegralSolver.SolveBySubstitutingTheSumOfTwoRootsOfLinears(expr, x, integrateByParts)) is { }) return answer; + // A function of `x^n` for a symbolic `n` beside a power of `x`, under `u = x^(n/d)`: + // before the substitution search, which reads `x^(2n)` as a power of `x^n` only for a + // number `n`. + if ((answer = IndefiniteIntegralSolver.SolveByAPowerOfXWithASymbolicExponent(expr, x, integrateByParts)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer; // A logarithmic derivative the substitution above could not read for a symbol in // an exponent: `(x^(n-1) - 1)/(x^n - n x)`. diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs new file mode 100644 index 000000000..eb6d09b0f --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs @@ -0,0 +1,50 @@ +// +// 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 +{ + /// + /// A function of x^n for a symbolic n beside a power of x, + /// x^m R(x^n) with (m + 1)/n rational, under u = x^(n/d): Rubi's 1.2.3.2. + /// #718 + /// + /// + /// Checked by differentiating back with a = 1.3, b = 0.7, c = 0.4 and + /// n = 1.7, at positive x, where a symbolic power of it is real. + /// + [Trait("Area", "Calculus")] + public sealed class PowerOfXWithASymbolicExponentIntegralTest + { + [Theory] + [InlineData("x^(-1 + 4*n)/(a + b*x^n + c*x^(2*n))")] + [InlineData("x^(-1 + n)/(b*x^n + c*x^(2*n))")] + [InlineData("1/(x*(a + b*x^n + c*x^(2*n)))")] + // Under u = x^(n/2). + [InlineData("x^(-1 + n/2)/(a + b*x^n + c*x^(2*n))")] + [InlineData("sqrt(a + b*x^n + c*x^(2*n))/x")] + public void IsIntegratedInAPowerOfX(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("b", 0.7).Substitute("c", 0.4).Substitute("n", 1.7); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + foreach (var at in new[] { 0.3, 0.8, 1.4, 2.1 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + var got = derivative.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + } + } +} From db4510ca61c063fddf43245f72287a4137a210b5 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 5 Oct 2026 07:08:15 +0000 Subject: [PATCH 2/2] A function of x^n is taken in a power of x where it is algebraic, its constants named coth(a + b ln(c x^n))^3/x was answered in a second and timed out: the exponential form of coth makes it a rational function of x^(2 n b) with (e^a)^2 (c^b)^2 in it, and the integral in u carried that constant through the partial fractions for over half a minute. Each constant atom with a symbol in it, e^a and c^b, is named for the integral in u and written back, which keeps what relates the coefficients: a^2 + 2 a b u + b^2 u^2 stays a perfect square. And a power of x under a function is the function's, which the search reads. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Integration/IndefiniteIntegralSolver.cs | 57 ++++++++++++++++++- ...werOfXWithASymbolicExponentIntegralTest.cs | 18 +++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index ad5bdbd83..ebe91116a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -24606,6 +24606,13 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab { if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base == x && exponent is not Number && !exponent.ContainsNode(x))) return null; + // An algebraic function of the powers only. Under a function the power is the + // function's, and the search finds the substitution for it: `coth(a + b ln(c x^n))^3/x` + // is `t = ln(c x^n)`, and in `u = x^n` it was `coth(a + b ln(c u))^3/u`, the same + // question in another letter at the cost of a second search. + if (expr.Nodes.Any(node => node.ContainsNode(x) + && node is not (Sumf or Minusf or Mulf or Divf or Variable) && !(node is Powf(_, var exponent) && !exponent.ContainsNode(x)))) + return null; // Over one bar first, so that an x inside `(x (a + b x^n))^(-1)` is a factor to gather. var g = Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(x * expr)); var exponents = g.Nodes.OfType().Where(power => power.Base == x && power.Exponent is not Number && !power.Exponent.ContainsNode(x)) @@ -24633,10 +24640,58 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab // Divided by u through the gathering, where `u/u` left in is a quotient the // chain declines; the constant `d/n` outside it. var overU = Functions.PartialFractions.Bare(Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(inU) * MathS.Pow(u, -1)).InnerSimplified); - if (Integration.ComputeIndefiniteIntegral(overU, u, integrateByParts) is not { } inTermsOfU) + // Each constant with a symbol in it that is not built from symbols by arithmetic + // and whole powers -- `e^a`, `c^b`, a function of the symbols -- named while the + // integral in u is asked, and written back into what it gives: + // `coth(a + b ln(c x^n))^3/x` arrives here as + // `((e^a)^2 (c^b)^2 x^(2 n b) + 1)^3/(...)^3/x`, whose integral in u was past half a + // minute with `(e^a)^2 (c^b)^2` carried through the partial fractions, and is under + // one in `k^2 m^2`. Those only: a coefficient named whole hides what relates it to + // the others, and `a^2 + 2 a b u + b^2 u^2` named so is a quadratic with no double + // root, answered wrongly. + var named = new List<(Variable Name, Entity Value)>(); + var namedOverU = NameTheAtoms(overU); + if (Integration.ComputeIndefiniteIntegral(namedOverU, u, integrateByParts) is not { } inTermsOfU) return null; + foreach (var (name, value) in named) + inTermsOfU = inTermsOfU.Substitute(name, value); var answer = (d / unit * inTermsOfU).Substitute(u, MathS.Pow(x, step)).InnerSimplified; return answer.Nodes.Any(node => node is Number.Complex { IsNaN: true }) ? null : answer; + + // Arithmetic and whole powers taken apart; a root of something with u in it keeps + // its exponent; any other constant with a symbol in it is an atom, and named. + Entity NameTheAtoms(Entity e) + { + switch (e) + { + case Number or Variable: + return e; + case Sumf(var left, var right): + return NameTheAtoms(left) + NameTheAtoms(right); + case Minusf(var left, var right): + return NameTheAtoms(left) - NameTheAtoms(right); + case Mulf(var left, var right): + return NameTheAtoms(left) * NameTheAtoms(right); + case Divf(var above, var below): + return NameTheAtoms(above) / NameTheAtoms(below); + case Powf(var @base, Number.Integer whole): + return MathS.Pow(NameTheAtoms(@base), whole); + case Powf(var @base, var exponent) when @base.ContainsNode(u) && !exponent.ContainsNode(u): + return MathS.Pow(NameTheAtoms(@base), exponent); + default: + return !e.ContainsNode(u) && e.Vars.Any() ? Named(e) : e; + } + } + + Entity Named(Entity value) + { + foreach (var (name, earlier) in named) + if (earlier == value) + return name; + var fresh = Variable.CreateUnique(overU + named.Aggregate((Entity)Number.Integer.Zero, (sum, pair) => sum + pair.Name), "k_named"); + named.Add((fresh, value)); + return fresh; + } } return null; } diff --git a/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs index eb6d09b0f..3afce6da9 100644 --- a/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/PowerOfXWithASymbolicExponentIntegralTest.cs @@ -30,7 +30,23 @@ public sealed class PowerOfXWithASymbolicExponentIntegralTest // Under u = x^(n/2). [InlineData("x^(-1 + n/2)/(a + b*x^n + c*x^(2*n))")] [InlineData("sqrt(a + b*x^n + c*x^(2*n))/x")] - public void IsIntegratedInAPowerOfX(string integrand) + // A perfect square, which stays one in u: its coefficients are not named apart. + [InlineData("1/(x*(a^2 + 2*a*b*x^n + b^2*x^(2*n))^(3/2))")] + public void IsIntegratedInAPowerOfX(string integrand) => DifferentiatesBack(integrand); + + /// + /// coth(a + b ln(c x^n))^3/x is written through the exponential form as a rational + /// function of x^(2 n b) with (e^a)^2 (c^b)^2 in it, and its integral in + /// u is asked with that constant named: carried through the partial fractions as + /// written it was past half a minute, where named it is under one. A regression makes + /// this crawl rather than fail. + /// + [Theory] + [InlineData("coth(a + b*ln(c*x^n))^3/x")] + [InlineData("tanh(a + b*ln(c*x^n))^3/x")] + public void ACompoundConstantIsNamedInThePower(string integrand) => DifferentiatesBack(integrand); + + private static void DifferentiatesBack(string integrand) { var integral = integrand.ToEntity().Integrate("x"); Assert.DoesNotContain("integral(", integral.Stringize());