diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fad9d71b9..dc0d5029c 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -581,6 +581,26 @@ checked against mpmath's quadrature to forty digits | `"ln(x) * Shi(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(ln(x) * Shi(x), x, 0)` | `0` | | `"x * (Shi(1/x) - 1/x) * x^2".Limit("x", "+oo")` | unevaluated | `1/18` | +### An exponential of the variable beside powers of two linears is not taken out as a constant + +**Silent: wrong antiderivatives and definite integrals are left unevaluated.** The rule for a negative whole power of a +linear beside a power of another took any factor with a constant base out of the integral, `e^x` and `2^x` among them, +and answered the rest. So `e^x/(x^2 sqrt(1 + x))` was answered as `e^x` times the antiderivative of +`1/(x^2 sqrt(1 + x))`. Below integration by parts the same fault made the antiderivative of `e^(-x) arsinh(sqrt(x))` a +constant, and its definite integral `0` on every interval. These integrals are not elementary, and they are now left +unevaluated. The issue's own integral now searches for 35 s before it declines, where the wrong `0` took 7 s. Measured +on master `409d68805` and on this branch, each value checked against mpmath's quadrature +([#2717](https://github.com/asc-community/AngouriMath/issues/2717)). + +| `.Simplify()` | Was (`409d68805`) | Now | Value | +|---|---|---|---| +| `integral(exp(-x) * arsinh(sqrt(x)), x, 0, 1)` | `0` | unevaluated | `0.3526065850...` | +| `integral(exp(-x) * ln(sqrt(x + 1) + sqrt(x)), x, 1, 2)` | `0` | unevaluated | `0.2335110726...` | +| `integral(exp(-x) * ln(sqrt(x + 2) + sqrt(x)), x, 0, 1)` | `0.2190762915...` | unevaluated | `0.4766731655...` | +| `integral(exp(x) / (x^2 * sqrt(1 + x)), x, 1, 2)` | `-0.0851631790...` | unevaluated | `1.3326495259...` | +| `integral(2^x / ((x + 1)^3 * sqrt(x + 2)), x, 0, 1)` | `0.1857837807...` | unevaluated | `0.3129531028...` | +| `"exp(x)/(x^2*sqrt(1+x))".Integrate("x")` | `e ^ x * (-1 / x * sqrt(1 + x) - …)` | unevaluated | | + ### A real root beside a pole is found by the numeric search **More roots in the same finite set.** Where no analytical route answers, `Solve` gives the roots Newton's method diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index fa2017aab..2ce458faa 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -15952,13 +15952,17 @@ void Gather(Entity node, int times) Gather(expr, 1); foreach (var (@base, exponent) in factors) { + // A constant base under an exponent in x is no constant: `e^x/(x^2 sqrt(1 + x))` was + // answered as `e^x` times the antiderivative of the rest, and below by parts that made + // the antiderivative of `e^(-x) arsinh(sqrt(x))` a constant. + // https://github.com/asc-community/AngouriMath/issues/2717 + if (exponent.ContainsNode(x)) + return null; if (!@base.ContainsNode(x)) { constant *= MathS.Pow(@base, exponent); continue; } - if (exponent.ContainsNode(x)) - return null; if (TreeAnalyzer.TryGetPolyLinear(@base, x, out var slope, out _) && !TreeAnalyzer.IsZero(slope)) { var index = linears.FindIndex(pair => pair.Base == @base); diff --git a/Sources/Tests/UnitTests/Calculus/AnExponentialOfTheVariableBesideTheLinearsRecurrenceIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/AnExponentialOfTheVariableBesideTheLinearsRecurrenceIntegralTest.cs new file mode 100644 index 000000000..f07a9e95c --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AnExponentialOfTheVariableBesideTheLinearsRecurrenceIntegralTest.cs @@ -0,0 +1,87 @@ +// +// 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 +{ + /// + /// The recurrence for a negative whole power of a linear beside a power of another took a + /// factor with a constant base out of the integral as a constant, whatever its exponent: + /// e^x/(x^2 sqrt(1 + x)) was answered as e^x times the antiderivative of the rest. + /// Below by parts and the reciprocal substitution, that made the antiderivative of + /// e^(-x) arsinh(sqrt(x)) a constant, and its definite integral 0 on every interval. + /// https://github.com/asc-community/AngouriMath/issues/2717 + /// + [Trait("Area", "Calculus")] + public sealed class AnExponentialOfTheVariableBesideTheLinearsRecurrenceIntegralTest + { + private static readonly double[] Points = { 0.3, 0.7, 1.4, 2.1, 3.3 }; + + /// Declined, or an antiderivative whose derivative is the integrand. + private static void DeclinedOrDifferentiatesBack(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + if (integral.Stringize().Contains("integral(")) + return; + var derivative = integral.Substitute("C", 0).Differentiate("x"); + var original = integrand.ToEntity(); + foreach (var at in Points) + { + 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)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + } + + [Theory] + [InlineData("exp(x)/(x^2*sqrt(1+x))")] + [InlineData("exp(-x)/(x^2*sqrt(x+1))")] + [InlineData("2^x/((x+1)^3*sqrt(x+2))")] + [InlineData("exp(-1/x)/(x^2*sqrt(1+x))")] + [InlineData("exp(x)*x/((x+1)^2*sqrt(x))")] + [InlineData("exp(x)*(x+2)/((x+1)^2*sqrt(x+3))")] + [InlineData("exp(-x)*arsinh(sqrt(x))")] + public void Issue2717_AnExponentialOfTheVariableIsNotTakenOutAsAConstant(string integrand) + => DeclinedOrDifferentiatesBack(integrand); + + /// The recurrence still answers where every factor is algebraic. + [Theory] + [InlineData("1/(x^2*sqrt(1+x))")] + [InlineData("x^2/((x+1)^3*sqrt(x+2))")] + [InlineData("a/(x^2*sqrt(1+x))")] + public void Issue2717_TheRecurrenceStillAnswersAlgebraicIntegrands(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + DeclinedOrDifferentiatesBack(integrand.Replace("a", "3")); + } + + /// + /// Definite integrals that were answered with the wrong antiderivative's difference, with + /// their values from mpmath's quadrature: left unevaluated, or the value. The issue's own, + /// e^(-x) arsinh(sqrt(x)) over [0, 1], was 0 and is left unevaluated, + /// after a search too long for this suite. + /// + [Theory] + [InlineData("integral(exp(x)/(x^2*sqrt(1+x)), x, 1, 2)", 1.33264952598981399)] + [InlineData("integral(2^x/((x+1)^3*sqrt(x+2)), x, 0, 1)", 0.31295310287820328)] + public void Issue2717_TheDefiniteIntegralIsItsValueOrLeftUnevaluated(string integral, double value) + { + var answer = integral.ToEntity().Simplify(); + if (answer.Stringize().Contains("integral(")) + return; + var got = answer.EvalNumerical(); + Assert.True(Math.Abs((double)got.RealPart - value) < 1e-9 && Math.Abs((double)got.ImaginaryPart) < 1e-9, + $"{integral} is {answer}, {got}, where it is {value}"); + } + } +}