diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b5268044d..7ed198de3 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -335,6 +335,19 @@ zero-discriminant arm, which gives the right answer. Each of these was checked by differentiating it back with the parameters pinned and comparing at four points. Nothing that already had an antiderivative changes. +### `e^(k acoth(a x))` beside a power of `c - a c x` is integrated + +**Improvement, not silent.** `e^(3 acoth(a x))/(c - a c x)^3` and `e^(2 acoth(a x)) sqrt(c - a c x)/x` +were left unevaluated. The radical substitution leaves coefficients that are zero as values and not +as written, `-6 a^3 + 6 a^3` and `1/c c - 1`, and the long division and the rule for a biquadratic +read them as nonzero. They are now decided over one bar, expanded, and at pinned symbols. Rubi's +7.4.2 ([#1665](https://github.com/asc-community/AngouriMath/issues/1665)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"e^(3*acoth(a*x))/(c-a*c*x)^3".Integrate("x")`, and over `(c - a c x)^4` | left unevaluated | an antiderivative in `sqrt((a x + 1)/(a x - 1))` | +| `"e^(2*acoth(a*x))*sqrt(c-a*c*x)/x".Integrate("x")`, and over `x^2` | left unevaluated | an antiderivative in `sqrt(c - a c x)`, by cases on the sign of `c` | + ### `NaN` again, from an exponent that was read as written rather than as a number **A wrong answer, and a second one of the same kind.** `(a^2 + 2abx^2 + b^2x^4)^3/x^7` came back as diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs index 05c1991d6..a0938ab7a 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs @@ -654,6 +654,26 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant) return WithThePrimitiveDenominator(expandedBelow == Integer.One ? expandedAbove : expandedAbove / expandedBelow); } + /// + /// Whether a constant in the symbols is zero as a value, decided as + /// decides it: over one bar, its numerator + /// expanded is zero as written, as a number, or at two sets of pinned symbols. A + /// coefficient read off a written polynomial can be zero so and not as written: + /// -3 a^3 + 3 a^3, which the expansion of (-(a - a u^2) - (u^2 + 1) a)^3 + /// leaves at u^6, and -(2 c/c - 1) + 1. + /// https://github.com/asc-community/AngouriMath/issues/1665 + /// + internal static bool IsZeroAsAValue(Entity constant) + { + if (constant == Integer.Zero || constant.Evaled is Complex { IsZero: true }) + return true; + if (!constant.Vars.Any()) + return false; + var (above, _) = SingleQuotient.Of(SingleQuotient.Combine(constant).InnerSimplified); + var expandedAbove = Bare(above.Expand().InnerSimplified); + return expandedAbove == Integer.Zero || expandedAbove.Evaled is Complex { IsZero: true } || IsZeroAtPinnedSymbols(expandedAbove); + } + /// /// with the rational content of its denominator moved /// up: the denominator's coefficients whole and coprime with a positive leading one, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index f9f7317fc..92e2966c9 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -1405,14 +1405,19 @@ over is Entity.Powf(var @base, var power) ? var a = below.TryGetValue(EInteger.Zero, out var a0) ? a0 : Number.Integer.Zero; var b = below.TryGetValue(EInteger.FromInt32(2), out var b0) ? b0 : Number.Integer.Zero; var c = below[EInteger.FromInt32(4)]; - if (a == Number.Integer.Zero || a.Evaled is Number.Complex { IsZero: true }) + // Zero as a value, and not only as written: the coefficients are read off the + // denominator as it is written, and `((c - u^2)/c - 1)(u^2 - c)` is `-u^2 (u^2 - c)/c`, + // with `-(c/c - 1) c` for its constant term. Read as a biquadratic, one of its roots + // in `u^2` was that zero, and `atan(u/sqrt(0))/sqrt(0)` made the answer NaN. + // https://github.com/asc-community/AngouriMath/issues/1665 + if (Functions.PartialFractions.IsZeroAsAValue(a) || Functions.PartialFractions.IsZeroAsAValue(c)) return null; if (!TreeAnalyzer.TryGetPolynomial(numerator, x, out var above) || above.Count == 0 || above.Keys.Any(power => power.Sign < 0 || !power.IsEven) || above.Values.Any(coefficient => coefficient.ContainsNode(x))) return null; var discriminant = Functions.PartialFractions.Bare((b * b - 4 * a * c).Simplify()); - if (discriminant == Number.Integer.Zero || discriminant.Evaled is Number.Complex { IsZero: true }) + if (Functions.PartialFractions.IsZeroAsAValue(discriminant)) return null; // The numerator in w = x^2, divided down by c w^2 + b w + a to a remainder d + e w. diff --git a/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs b/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs index 0ff5c4f34..92b61c348 100644 --- a/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs +++ b/Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs @@ -214,6 +214,22 @@ private static (Entity Divided, Entity Remainder)? DivideOnePolynomial( if (!replaceVars && ((maxvalQ.Vars.Any() && !genericCase) || maxvalQ.Evaled == Integer.Create(0))) return null; + // A leading coefficient with a symbol in it can be zero as a value without being zero + // as written: `(-(a - a u^2) - (u^2 + 1) a)^3` is `-8 a^3`, and expanded it leaves + // `-3 a^3 + 3 a^3` at `u^6`, which nothing above collects. Divided by that, the + // quotient was NaN. The divisor's degree is that of its first coefficient that is + // not zero. https://github.com/asc-community/AngouriMath/issues/1665 + while (maxvalQ.Vars.Any() && PartialFractions.IsZeroAsAValue(maxvalQ)) + { + _ = powersOfQ.Remove(maxpowQ); + if (powersOfQ.Count == 0) + return null; + maxpowQ = powersOfQ.Keys.Max() ?? throw new AngouriBugException("No null expected"); + maxvalQ = powersOfQ[maxpowQ]; + if (maxvalQ.Evaled == Integer.Create(0)) + return null; + } + var result = new Dictionary(); // possibly very long process while (maxpowP.GreaterThanOrEquals(maxpowQ)) diff --git a/Sources/Tests/UnitTests/Calculus/ExponentialOfALogarithmIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ExponentialOfALogarithmIntegralTest.cs index 001b6f5db..171160ee6 100644 --- a/Sources/Tests/UnitTests/Calculus/ExponentialOfALogarithmIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ExponentialOfALogarithmIntegralTest.cs @@ -55,6 +55,53 @@ private static void DifferentiatesBack(string integrand) [InlineData("(3 - 3/(4*x^2))^3/e^(2*acoth(2*x))")] public void AnExponentialOfAnInverseHyperbolicCotangent(string integrand) => DifferentiatesBack(integrand); + /// + /// With symbols for the coefficients, the substitution leaves coefficients that are zero + /// as values and not as written. Under the root of the quotient, + /// e^(3 acoth(a x))/(c - a c x)^3 is u^4 (a - a u^2) over + /// (-(a - a u^2) - (u^2 + 1) a)^3, which is -8 a^3, and the long division + /// read it as a sextic with -3 a^3 + 3 a^3 for its leading coefficient. Divided + /// by that, the answer was NaN. e^(2 acoth(a x)) sqrt(c - a c x)/x leaves + /// ((c - u^2)/c - 1)(u^2 - c), whose constant term -(c/c - 1) c is zero; + /// read as a biquadratic, one of its roots in u^2 was too, and the answer divided + /// by its root. Rubi's 7.4.2, #225-#227 and #364-#365. + /// https://github.com/asc-community/AngouriMath/issues/1665 + /// + [Theory] + [InlineData("e^(3*acoth(a*x))/(c-a*c*x)^3", "a=1.37,c=2.11", new[] { 1.1, 2.3, -1.4, -3.2 })] + [InlineData("e^(3*acoth(a*x))/(c-a*c*x)^3", "a=0.71,c=-1.9", new[] { 1.6, -2.5 })] + [InlineData("e^(3*acoth(a*x))/(c-a*c*x)^4", "a=1.37,c=2.11", new[] { 1.1, 2.3, -1.4 })] + [InlineData("e^(2*acoth(a*x))*sqrt(c-a*c*x)/x", "a=1.37,c=2.11", new[] { 0.3, -0.5, -2.0 })] + [InlineData("e^(2*acoth(a*x))*sqrt(c-a*c*x)/x", "a=1.37,c=-2.11", new[] { 1.5, 3.0 })] + [InlineData("e^(2*acoth(a*x))*sqrt(c-a*c*x)/x^2", "a=1.37,c=2.11", new[] { 0.3, -0.5, -2.0 })] + public void ACoefficientThatIsZeroAsAValueIsReadAsZero(string integrand, string pins, double[] points) + { + var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + Entity Pin(Entity e) + { + foreach (var pin in pins.Split(',')) + { + var parts = pin.Split('='); + e = e.Substitute(parts[0], double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture)); + } + return e; + } + var derivative = Pin(integral).Differentiate("x"); + var original = Pin(integrand.ToEntity()); + foreach (var at in points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + Assert.False(want.IsNaN, $"the integrand {integrand} has no value at x = {at}"); + 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}"); + } + } + /// /// The quotient of linears under the root leaves (a (1 - u^2))^5 below the bar in /// u, which the rational reader does not read as a^5 (1 - u^2)^5 until it diff --git a/Sources/Tests/UnitTests/Common/LongDivisionLeadingCoefficientTest.cs b/Sources/Tests/UnitTests/Common/LongDivisionLeadingCoefficientTest.cs new file mode 100644 index 000000000..116b2332f --- /dev/null +++ b/Sources/Tests/UnitTests/Common/LongDivisionLeadingCoefficientTest.cs @@ -0,0 +1,60 @@ +// +// 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 AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Common +{ + /// + /// The long division reads its divisor's degree off the divisor expanded, and a coefficient + /// with a symbol in it can be zero as a value without being zero as written: + /// (-(a - a u^2) + -(u^2 + 1) a)^3 is -8 a^3, and expanded it leaves + /// -3 a^3 + 3 a^3 at u^6. The division took that for the leading coefficient + /// and divided by it, and the quotient was NaN. + /// https://github.com/asc-community/AngouriMath/issues/1665 + /// + [Trait("Area", "Common")] + public sealed class LongDivisionLeadingCoefficientTest + { + [Fact] + public void ACoefficientThatIsZeroAsAValueIsNoDegree() + { + var u = MathS.Var("u"); + var dividend = "u^4 (a - a u^2)".ToEntity(); + // As the substitution writes it: with `- (u^2 + 1) a` the expansion collects to `-8 a^3`. + var divisor = "(-(a - a u^2) + -(u^2 + 1) a)^3".ToEntity(); + var divided = Functions.TreeAnalyzer.PolynomialLongDivision(dividend, divisor, genericCase: true, inTermsOf: u); + Assert.NotNull(divided); + var (quotient, remainder) = divided!.Value; + Assert.DoesNotContain("NaN", quotient.Stringize()); + Assert.DoesNotContain("NaN", remainder.Stringize()); + foreach (var (a, at) in new[] { (1.37, 0.4), (-0.83, 1.9), (2.71, -1.3) }) + { + Entity Pinned(Entity e) => e.Substitute("a", a).Substitute(u, at); + var got = Pinned(quotient + remainder).EvalNumerical(); + var want = Pinned(dividend / divisor).EvalNumerical(); + Assert.True(((got - want).Abs() as Entity.Number.Real)!.EDecimal.ToDouble() < 1e-9 * System.Math.Max(1, (want.Abs() as Entity.Number.Real)!.EDecimal.ToDouble()), + $"the quotient and remainder are {got} at a = {a}, u = {at}, where the division is {want}"); + } + } + + [Theory] + [InlineData("-3 * a^3 + 3 * a^3")] + [InlineData("-(2 * 1/c * c - 1) + 1")] + [InlineData("-(c/c - 1) * c")] + public void IsZeroAsAValue(string coefficient) + => Assert.True(Functions.PartialFractions.IsZeroAsAValue(coefficient.ToEntity())); + + [Theory] + [InlineData("-3 * a^3 + 2 * a^3")] + [InlineData("2 * 1/c * c")] + [InlineData("a - b")] + public void IsNotZeroAsAValue(string coefficient) + => Assert.False(Functions.PartialFractions.IsZeroAsAValue(coefficient.ToEntity())); + } +}