diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index be47d7a74..f745179c6 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 19c7d9d5c..8d863492d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -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; } + /// + /// An algebraic integrand in which a linear c + d x stands under a power inside a + /// sum, written in u = c + d x: x^3/(a + b (c + d x)^3) is + /// (u - c)^3/(d^4 (a + b u^3)), and (c + d x)^4/(a + b (c + d x)^3) is + /// u^4/(d (a + b u^3)), which the rules for a binomial answer. + /// + /// + /// In x the sum is a polynomial whose roots are those of a + b u^n 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 + /// + 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; + } + /// /// An integrand whose trigonometric functions have different multiples of one /// argument — sin(x)/cos(2x), cos(x)/(sin(x) tan(x/2)) — rewritten so that diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 0058ebd66..c2eaaa6ea 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -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 diff --git a/Sources/Tests/UnitTests/Calculus/OneShiftedLinearIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/OneShiftedLinearIntegralTest.cs new file mode 100644 index 000000000..2aab65546 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/OneShiftedLinearIntegralTest.cs @@ -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 +{ + /// + /// An algebraic integrand with a linear c + d x under a power inside a sum, written in + /// u = c + d x, as Rubi's 1.1.3.2, 1.2.3.2 and 1.3.1 write whole sections of it. + /// #718 + /// + /// + /// Checked by differentiating back at points where the integrand is real, never against a + /// printed form. + /// + [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}"); + } + + /// + /// 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. + /// + [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)); + } +}