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
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ one reads a numeric slope, and writes an offset out by the addition formula, whi
| `"csc(1+x)*csc(2+2*x)^2".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `cos(1 + x)` |
| `"sin(a+b*x)^3/sin(2*a+2*b*x)^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `tan(a + b x)` |

### A function of one linear under a power inside a sum is written in that linear

`x^3/(a + b (c + d x)^3)` was left unevaluated, and `(c + d x)^4/(a + b (c + d x)^3)` ran for more
than eighty seconds. Written in `x`, the sum is a polynomial whose roots are those of a binomial
moved and scaled, which the partial fractions do not read. Where one linear `c + d x` stands under
the powers inside sums, and `x` elsewhere only in sums, products and powers, the integrand is
written in `u = c + d x` first. Rubi's 1.1.3.2, 1.2.3.2 and 1.3.1 write whole sections so
([#718](https://github.com/asc-community/AngouriMath/issues/718)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"x^3/(a + b*(c + d*x)^3)".ToEntity().Integrate("x")` | `integral(...)` | powers and logarithms of `c + d x` and of a quadratic in it, and an arctangent |
| `"(c + d*x)^4/(a + b*(c + d*x)^3)".ToEntity().Integrate("x")` | ran for more than eighty seconds | `(c + d x)^2/(2 b)`, and the same logarithms and arctangent |
| `"(c*m + d*m*x)^3/(a + b*(c + d*x)^3)^2".ToEntity().Integrate("x")` | `integral(...)` | `m^3` times a rational function of `c + d x`, the logarithms and the arctangent |
| `"(c + d*x)^4/(a + b*(c + d*x)^2 + k*(c + d*x)^4)^2".ToEntity().Integrate("x")` | `integral(...)` | a rational function of `c + d x` and arctangents |

### A root of an even power is the modulus, and is no longer read as the power

**Wrong answers, silent.** `(u^2)^(3/2)` is `|u|^3`, and two readers took it for `u^3`: the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9248,6 +9248,70 @@ static bool IsEvenIn(Entity polynomial, Entity.Variable even, Entity.Variable ot
return back.Nodes.Any(node => node == MathS.NaN) ? null : back;
}

/// <summary>
/// An algebraic integrand in which a linear <c>c + d x</c> stands under a power inside a
/// sum, written in <c>u = c + d x</c>: <c>x^3/(a + b (c + d x)^3)</c> is
/// <c>(u - c)^3/(d^4 (a + b u^3))</c>, and <c>(c + d x)^4/(a + b (c + d x)^3)</c> is
/// <c>u^4/(d (a + b u^3))</c>, which the rules for a binomial answer.
/// </summary>
/// <remarks>
/// In x the sum is a polynomial whose roots are those of <c>a + b u^n</c> moved and
/// scaled, which the partial fractions do not read, and the substitution search reads
/// one function at a time: Rubi's 1.1.3.2, 1.2.3.2 and 1.3.1 write whole sections so.
/// Only where one linear, up to a constant multiple, stands under the powers inside sums,
/// with an offset or a slope other than 1, and x stands nowhere but in polynomials and
/// powers of them: written in u, nothing is left in x. At the question asked or one below
/// it, since it lands on the chain in u.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </remarks>
internal static Entity? SolveByWritingAFunctionOfOneShiftedLinear(Entity expr, Entity.Variable x, bool integrateByParts)
{
if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
return null;
// Algebraic in x: sums, products, quotients and powers with exponents free of it.
foreach (var node in expr.Nodes)
if (node.ContainsNode(x) && node is not (Variable or Sumf or Minusf or Mulf or Divf)
&& !(node is Powf(_, var exponent) && !exponent.ContainsNode(x)))
return null;
Entity? linear = null;
Entity? slope = null;
Entity? offset = null;
foreach (var node in expr.Nodes)
{
if (node is not (Sumf or Minusf))
continue;
foreach (var term in Sumf.LinearChildren(node))
foreach (var factor in Mulf.LinearChildren(term))
{
if (factor is not Powf(var @base, _) || !@base.ContainsNode(x)
|| !TreeAnalyzer.TryGetPolyLinear(@base, x, out var d, out var c)
|| d.ContainsNode(x) || c.ContainsNode(x) || TreeAnalyzer.IsZero(d))
continue;
if (linear is null)
(linear, slope, offset) = (@base, d, c);
else if (!VanishesIdentically(slope! * c - d * offset!))
return null;
}
}
if (linear is null || slope is null || offset is null
|| VanishesIdentically(offset) && VanishesIdentically(slope - 1))
return null;
// x = (u - c)/d, and each base linear in u written as one.
var u = Variable.CreateUnique(expr, "u_linear");
var inU = expr.Substitute(x, (u - offset) / slope).InnerSimplified.Replace(node =>
node is Powf(var @base, var power) && @base.ContainsNode(u)
&& TreeAnalyzer.TryGetPolyLinear(@base, u, out var d, out var c) && !d.ContainsNode(u) && !c.ContainsNode(u)
? MathS.Pow(Functions.PartialFractions.InLowestTermsOverTheSymbols(d) * u + Functions.PartialFractions.InLowestTermsOverTheSymbols(c), power)
: node);
if (inU.ContainsNode(x))
return null;
// dx = du/d.
if (Integration.ComputeAsAQuestionOfItsOwn((inU / slope).InnerSimplified, u, integrateByParts) is not { } answer)
return null;
var back = answer.Substitute(u, linear);
return back.Nodes.Any(node => node == MathS.NaN) ? null : back;
}

/// <summary>
/// An integrand whose trigonometric functions have <b>different multiples</b> of one
/// argument — <c>sin(x)/cos(2x)</c>, <c>cos(x)/(sin(x) tan(x/2))</c> — rewritten so that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// the question asked only, and the polynomial term below writes `1/u` as `u^(-1)` and
// asks that a level down: `1/(e^atanh(a x) (c - c/(a x))^(3/2))` reached it nowhere.
if ((answer = IndefiniteIntegralSolver.SolveByWritingEachLinearOnce(expr, x, integrateByParts)) is { }) return answer;
// A linear under a power inside a sum, written as the variable: `x^3/(a + b (c + d x)^3)`.
// Before the polynomial term, which takes the leading coefficient out of a power of
// the sum and writes what is left out in powers of x, where the linear is gone; and
// before the substitution search, which reads one function at a time.
if ((answer = IndefiniteIntegralSolver.SolveByWritingAFunctionOfOneShiftedLinear(expr, x, integrateByParts)) is { }) return answer;
if ((answer = IndefiniteIntegralSolver.SolveAsPolynomialTerm(expr, x, integrateByParts)) is { }) return answer;
// A product of powers of the variable with a power of a constant multiple of it
// among them, `(c x)^m x^n`, by the power rule with the written power kept as
Expand Down
70 changes: 70 additions & 0 deletions Sources/Tests/UnitTests/Calculus/OneShiftedLinearIntegralTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// 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>
/// An algebraic integrand with a linear <c>c + d x</c> under a power inside a sum, written in
/// <c>u = c + d x</c>, as Rubi's 1.1.3.2, 1.2.3.2 and 1.3.1 write whole sections of it.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
/// <remarks>
/// Checked by differentiating back at points where the integrand is real, never against a
/// printed form.
/// </remarks>
[Trait("Area", "Calculus")]
public sealed class OneShiftedLinearIntegralTest
{
private static void DifferentiatesBack(string integrand, double[] points, params (string, double)[] pins)
{
var integral = integrand.ToEntity().Integrate("x");
Assert.DoesNotContain("integral(", integral.Stringize());

var derivative = integral.Substitute("C", 0).Differentiate("x");
Entity original = integrand.ToEntity();
foreach (var (name, value) in pins)
{
derivative = derivative.Substitute(name, value);
original = original.Substitute(name, value);
}
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 >= 3, $"only {compared} points could be compared for {integrand}");
}

/// <summary>
/// A binomial and a trinomial in the linear, with the variable outside it as a power of
/// x, as a power of the linear, or as a multiple of the linear: each declined, or ran past
/// twenty seconds, written in x.
/// </summary>
[Theory]
[InlineData("x^3/(a + b*(c + d*x)^3)")]
[InlineData("(c + d*x)^4/(a + b*(c + d*x)^3)")]
[InlineData("1/((c + d*x)^4*(a + b*(c + d*x)^3))")]
[InlineData("(c*m + d*m*x)^3/(a + b*(c + d*x)^3)^2")]
[InlineData("(c + d*x)^4/(a + b*(c + d*x)^2 + k*(c + d*x)^4)^2")]
public void WrittenInTheLinear(string integrand)
=> DifferentiatesBack(integrand, new[] { 0.3, 0.9, 1.7, 2.6 },
("a", 1.3), ("b", 0.7), ("c", 0.4), ("d", 1.1), ("k", 0.6), ("m", 2.3));
}
}
Loading