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
14 changes: 14 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26663,6 +26663,117 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab
return !stripped.ContainsNode(x);
}

/// <summary>
/// A function of <c>x^n</c> for a symbolic <c>n</c> beside a power of <c>x</c>,
/// <c>x^m R(x^n)</c> with <c>(m + 1)/n</c> rational, under <c>u = x^(n/d)</c>: Rubi's
/// <c>x^(-1 + 4n)/(a + b x^n + c x^(2n))</c> and <c>x^(-1 + n/4)/(b x^n + c x^(2n))</c>.
/// </summary>
/// <remarks>
/// <para>
/// With <c>g = x f(x)</c>, <c>int f dx = (d/n) int g/u du</c>, since <c>dx/x = (d/n) du/u</c>;
/// and every power <c>x^t</c> in <c>g</c> is <c>u^(t d/n)</c>, a whole power of <c>u</c>
/// where <c>t d/n</c> is a whole number, which makes it the same value for every
/// <c>x</c>. The search reads <c>x^(2n)</c> as a power of <c>x^n</c> only for a number
/// <c>n</c>, and found no candidate for <c>x^(n/4)</c>, which is not written; so these
/// were declined. The powers of <c>x</c> are gathered first, <c>x x^(-1 + 4n)</c> into
/// <c>x^(4n)</c>, and the unit is each exponent left, divided by up to six.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </para>
/// </remarks>
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;
// 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<Powf>().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);
// 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;
}

/// <summary>
/// A quotient whose numerator is a constant multiple of its denominator's derivative,
/// with a symbolic exponent about: <c>c ln(D)</c>, the constant read off the terms.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,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)`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// 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>
/// A function of <c>x^n</c> for a symbolic <c>n</c> beside a power of <c>x</c>,
/// <c>x^m R(x^n)</c> with <c>(m + 1)/n</c> rational, under <c>u = x^(n/d)</c>: Rubi's 1.2.3.2.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Checked by differentiating back with <c>a = 1.3</c>, <c>b = 0.7</c>, <c>c = 0.4</c> and
/// <c>n = 1.7</c>, at positive <c>x</c>, where a symbolic power of it is real.
/// </remarks>
[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")]
// 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);

/// <summary>
/// <c>coth(a + b ln(c x^n))^3/x</c> is written through the exponential form as a rational
/// function of <c>x^(2 n b)</c> with <c>(e^a)^2 (c^b)^2</c> in it, and its integral in
/// <c>u</c> 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.
/// </summary>
[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());
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}");
}
}
}
}
Loading