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
13 changes: 13 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,26 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
return WithThePrimitiveDenominator(expandedBelow == Integer.One ? expandedAbove : expandedAbove / expandedBelow);
}

/// <summary>
/// Whether a constant in the symbols is zero as a value, decided as
/// <see cref="InLowestTermsOverTheSymbols"/> 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:
/// <c>-3 a^3 + 3 a^3</c>, which the expansion of <c>(-(a - a u^2) - (u^2 + 1) a)^3</c>
/// leaves at <c>u^6</c>, and <c>-(2 c/c - 1) + 1</c>.
/// https://github.com/asc-community/AngouriMath/issues/1665
/// </summary>
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);
}

/// <summary>
/// <paramref name="quotient"/> with the rational content of its denominator moved
/// up: the denominator's coefficients whole and coprime with a positive leading one,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EDecimal, Entity>();
// possibly very long process
while (maxpowP.GreaterThanOrEquals(maxpowQ))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// With symbols for the coefficients, the substitution leaves coefficients that are zero
/// as values and not as written. Under the root of the quotient,
/// <c>e^(3 acoth(a x))/(c - a c x)^3</c> is <c>u^4 (a - a u^2)</c> over
/// <c>(-(a - a u^2) - (u^2 + 1) a)^3</c>, which is <c>-8 a^3</c>, and the long division
/// read it as a sextic with <c>-3 a^3 + 3 a^3</c> for its leading coefficient. Divided
/// by that, the answer was NaN. <c>e^(2 acoth(a x)) sqrt(c - a c x)/x</c> leaves
/// <c>((c - u^2)/c - 1)(u^2 - c)</c>, whose constant term <c>-(c/c - 1) c</c> is zero;
/// read as a biquadratic, one of its roots in <c>u^2</c> 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
/// </summary>
[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}");
}
}

/// <summary>
/// The quotient of linears under the root leaves <c>(a (1 - u^2))^5</c> below the bar in
/// <c>u</c>, which the rational reader does not read as <c>a^5 (1 - u^2)^5</c> until it
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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:
/// <c>(-(a - a u^2) + -(u^2 + 1) a)^3</c> is <c>-8 a^3</c>, and expanded it leaves
/// <c>-3 a^3 + 3 a^3</c> at <c>u^6</c>. 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
/// </summary>
[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()));
}
}
Loading