Skip to content

Commit eaf3cde

Browse files
A coefficient that is zero as a value is no degree, nor a root (#1682)
* A coefficient that is zero as a value is no degree, nor a root With symbols for its coefficients, the radical substitution leaves coefficients that are zero as values and not as written. `e^(3 acoth(a x))/(c - a c x)^3` becomes `u^4 (a - a u^2)` over `(-(a - a u^2) + -(u^2 + 1) a)^3`, which is `-8 a^3`; expanded, it leaves `-3 a^3 + 3 a^3` at `u^6`, which nothing collects, and the long division divided by that and answered 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, and the biquadratic rule took one of its roots in `u^2` to be zero. `PartialFractions.IsZeroAsAValue` decides zero the way `InLowestTermsOverTheSymbols` does: over one bar, the numerator expanded is zero as written, as a number, or at two sets of pinned symbols. The long division drops a leading coefficient of the divisor that is zero so, and the biquadratic rule declines a constant term, leading coefficient or discriminant that is. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A breaking-changes entry for e^(k acoth(a x)) beside a power of c - a c x Measured on the 2.5.0 tag and on this branch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 6e2b2eb commit eaf3cde

6 files changed

Lines changed: 163 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ zero-discriminant arm, which gives the right answer.
334334
Each of these was checked by differentiating it back with the parameters pinned and comparing at
335335
four points. Nothing that already had an antiderivative changes.
336336

337+
### `e^(k acoth(a x))` beside a power of `c - a c x` is integrated
338+
339+
**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`
340+
were left unevaluated. The radical substitution leaves coefficients that are zero as values and not
341+
as written, `-6 a^3 + 6 a^3` and `1/c c - 1`, and the long division and the rule for a biquadratic
342+
read them as nonzero. They are now decided over one bar, expanded, and at pinned symbols. Rubi's
343+
7.4.2 ([#1665](https://github.com/asc-community/AngouriMath/issues/1665)).
344+
345+
| Input | Was (2.5.0) | Now |
346+
|---|---|---|
347+
| `"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))` |
348+
| `"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` |
349+
337350
### `NaN` again, from an exponent that was read as written rather than as a number
338351

339352
**A wrong answer, and a second one of the same kind.** `(a^2 + 2abx^2 + b^2x^4)^3/x^7` came back as

‎Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,26 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
654654
return WithThePrimitiveDenominator(expandedBelow == Integer.One ? expandedAbove : expandedAbove / expandedBelow);
655655
}
656656

657+
/// <summary>
658+
/// Whether a constant in the symbols is zero as a value, decided as
659+
/// <see cref="InLowestTermsOverTheSymbols"/> decides it: over one bar, its numerator
660+
/// expanded is zero as written, as a number, or at two sets of pinned symbols. A
661+
/// coefficient read off a written polynomial can be zero so and not as written:
662+
/// <c>-3 a^3 + 3 a^3</c>, which the expansion of <c>(-(a - a u^2) - (u^2 + 1) a)^3</c>
663+
/// leaves at <c>u^6</c>, and <c>-(2 c/c - 1) + 1</c>.
664+
/// https://github.com/asc-community/AngouriMath/issues/1665
665+
/// </summary>
666+
internal static bool IsZeroAsAValue(Entity constant)
667+
{
668+
if (constant == Integer.Zero || constant.Evaled is Complex { IsZero: true })
669+
return true;
670+
if (!constant.Vars.Any())
671+
return false;
672+
var (above, _) = SingleQuotient.Of(SingleQuotient.Combine(constant).InnerSimplified);
673+
var expandedAbove = Bare(above.Expand().InnerSimplified);
674+
return expandedAbove == Integer.Zero || expandedAbove.Evaled is Complex { IsZero: true } || IsZeroAtPinnedSymbols(expandedAbove);
675+
}
676+
657677
/// <summary>
658678
/// <paramref name="quotient"/> with the rational content of its denominator moved
659679
/// up: the denominator's coefficients whole and coprime with a positive leading one,

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,14 +1405,19 @@ over is Entity.Powf(var @base, var power) ?
14051405
var a = below.TryGetValue(EInteger.Zero, out var a0) ? a0 : Number.Integer.Zero;
14061406
var b = below.TryGetValue(EInteger.FromInt32(2), out var b0) ? b0 : Number.Integer.Zero;
14071407
var c = below[EInteger.FromInt32(4)];
1408-
if (a == Number.Integer.Zero || a.Evaled is Number.Complex { IsZero: true })
1408+
// Zero as a value, and not only as written: the coefficients are read off the
1409+
// denominator as it is written, and `((c - u^2)/c - 1)(u^2 - c)` is `-u^2 (u^2 - c)/c`,
1410+
// with `-(c/c - 1) c` for its constant term. Read as a biquadratic, one of its roots
1411+
// in `u^2` was that zero, and `atan(u/sqrt(0))/sqrt(0)` made the answer NaN.
1412+
// https://github.com/asc-community/AngouriMath/issues/1665
1413+
if (Functions.PartialFractions.IsZeroAsAValue(a) || Functions.PartialFractions.IsZeroAsAValue(c))
14091414
return null;
14101415
if (!TreeAnalyzer.TryGetPolynomial(numerator, x, out var above) || above.Count == 0
14111416
|| above.Keys.Any(power => power.Sign < 0 || !power.IsEven) || above.Values.Any(coefficient => coefficient.ContainsNode(x)))
14121417
return null;
14131418

14141419
var discriminant = Functions.PartialFractions.Bare((b * b - 4 * a * c).Simplify());
1415-
if (discriminant == Number.Integer.Zero || discriminant.Evaled is Number.Complex { IsZero: true })
1420+
if (Functions.PartialFractions.IsZeroAsAValue(discriminant))
14161421
return null;
14171422

14181423
// The numerator in w = x^2, divided down by c w^2 + b w + a to a remainder d + e w.

‎Sources/AngouriMath/Functions/TreeAnalyzer/LongDivision.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,22 @@ private static (Entity Divided, Entity Remainder)? DivideOnePolynomial(
214214
if (!replaceVars && ((maxvalQ.Vars.Any() && !genericCase) || maxvalQ.Evaled == Integer.Create(0)))
215215
return null;
216216

217+
// A leading coefficient with a symbol in it can be zero as a value without being zero
218+
// as written: `(-(a - a u^2) - (u^2 + 1) a)^3` is `-8 a^3`, and expanded it leaves
219+
// `-3 a^3 + 3 a^3` at `u^6`, which nothing above collects. Divided by that, the
220+
// quotient was NaN. The divisor's degree is that of its first coefficient that is
221+
// not zero. https://github.com/asc-community/AngouriMath/issues/1665
222+
while (maxvalQ.Vars.Any() && PartialFractions.IsZeroAsAValue(maxvalQ))
223+
{
224+
_ = powersOfQ.Remove(maxpowQ);
225+
if (powersOfQ.Count == 0)
226+
return null;
227+
maxpowQ = powersOfQ.Keys.Max() ?? throw new AngouriBugException("No null expected");
228+
maxvalQ = powersOfQ[maxpowQ];
229+
if (maxvalQ.Evaled == Integer.Create(0))
230+
return null;
231+
}
232+
217233
var result = new Dictionary<EDecimal, Entity>();
218234
// possibly very long process
219235
while (maxpowP.GreaterThanOrEquals(maxpowQ))

‎Sources/Tests/UnitTests/Calculus/ExponentialOfALogarithmIntegralTest.cs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,53 @@ private static void DifferentiatesBack(string integrand)
5555
[InlineData("(3 - 3/(4*x^2))^3/e^(2*acoth(2*x))")]
5656
public void AnExponentialOfAnInverseHyperbolicCotangent(string integrand) => DifferentiatesBack(integrand);
5757

58+
/// <summary>
59+
/// With symbols for the coefficients, the substitution leaves coefficients that are zero
60+
/// as values and not as written. Under the root of the quotient,
61+
/// <c>e^(3 acoth(a x))/(c - a c x)^3</c> is <c>u^4 (a - a u^2)</c> over
62+
/// <c>(-(a - a u^2) - (u^2 + 1) a)^3</c>, which is <c>-8 a^3</c>, and the long division
63+
/// read it as a sextic with <c>-3 a^3 + 3 a^3</c> for its leading coefficient. Divided
64+
/// by that, the answer was NaN. <c>e^(2 acoth(a x)) sqrt(c - a c x)/x</c> leaves
65+
/// <c>((c - u^2)/c - 1)(u^2 - c)</c>, whose constant term <c>-(c/c - 1) c</c> is zero;
66+
/// read as a biquadratic, one of its roots in <c>u^2</c> was too, and the answer divided
67+
/// by its root. Rubi's 7.4.2, #225-#227 and #364-#365.
68+
/// https://github.com/asc-community/AngouriMath/issues/1665
69+
/// </summary>
70+
[Theory]
71+
[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 })]
72+
[InlineData("e^(3*acoth(a*x))/(c-a*c*x)^3", "a=0.71,c=-1.9", new[] { 1.6, -2.5 })]
73+
[InlineData("e^(3*acoth(a*x))/(c-a*c*x)^4", "a=1.37,c=2.11", new[] { 1.1, 2.3, -1.4 })]
74+
[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 })]
75+
[InlineData("e^(2*acoth(a*x))*sqrt(c-a*c*x)/x", "a=1.37,c=-2.11", new[] { 1.5, 3.0 })]
76+
[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 })]
77+
public void ACoefficientThatIsZeroAsAValueIsReadAsZero(string integrand, string pins, double[] points)
78+
{
79+
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0);
80+
Assert.DoesNotContain("integral(", integral.Stringize());
81+
Assert.DoesNotContain("NaN", integral.Stringize());
82+
Entity Pin(Entity e)
83+
{
84+
foreach (var pin in pins.Split(','))
85+
{
86+
var parts = pin.Split('=');
87+
e = e.Substitute(parts[0], double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture));
88+
}
89+
return e;
90+
}
91+
var derivative = Pin(integral).Differentiate("x");
92+
var original = Pin(integrand.ToEntity());
93+
foreach (var at in points)
94+
{
95+
var got = derivative.Substitute("x", at).EvalNumerical();
96+
var want = original.Substitute("x", at).EvalNumerical();
97+
Assert.False(want.IsNaN, $"the integrand {integrand} has no value at x = {at}");
98+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
99+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
100+
Assert.True(difference / scale < 1e-9,
101+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
102+
}
103+
}
104+
58105
/// <summary>
59106
/// The quotient of linears under the root leaves <c>(a (1 - u^2))^5</c> below the bar in
60107
/// <c>u</c>, which the rational reader does not read as <c>a^5 (1 - u^2)^5</c> until it
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath.Extensions;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.Common
12+
{
13+
/// <summary>
14+
/// The long division reads its divisor's degree off the divisor expanded, and a coefficient
15+
/// with a symbol in it can be zero as a value without being zero as written:
16+
/// <c>(-(a - a u^2) + -(u^2 + 1) a)^3</c> is <c>-8 a^3</c>, and expanded it leaves
17+
/// <c>-3 a^3 + 3 a^3</c> at <c>u^6</c>. The division took that for the leading coefficient
18+
/// and divided by it, and the quotient was NaN.
19+
/// https://github.com/asc-community/AngouriMath/issues/1665
20+
/// </summary>
21+
[Trait("Area", "Common")]
22+
public sealed class LongDivisionLeadingCoefficientTest
23+
{
24+
[Fact]
25+
public void ACoefficientThatIsZeroAsAValueIsNoDegree()
26+
{
27+
var u = MathS.Var("u");
28+
var dividend = "u^4 (a - a u^2)".ToEntity();
29+
// As the substitution writes it: with `- (u^2 + 1) a` the expansion collects to `-8 a^3`.
30+
var divisor = "(-(a - a u^2) + -(u^2 + 1) a)^3".ToEntity();
31+
var divided = Functions.TreeAnalyzer.PolynomialLongDivision(dividend, divisor, genericCase: true, inTermsOf: u);
32+
Assert.NotNull(divided);
33+
var (quotient, remainder) = divided!.Value;
34+
Assert.DoesNotContain("NaN", quotient.Stringize());
35+
Assert.DoesNotContain("NaN", remainder.Stringize());
36+
foreach (var (a, at) in new[] { (1.37, 0.4), (-0.83, 1.9), (2.71, -1.3) })
37+
{
38+
Entity Pinned(Entity e) => e.Substitute("a", a).Substitute(u, at);
39+
var got = Pinned(quotient + remainder).EvalNumerical();
40+
var want = Pinned(dividend / divisor).EvalNumerical();
41+
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()),
42+
$"the quotient and remainder are {got} at a = {a}, u = {at}, where the division is {want}");
43+
}
44+
}
45+
46+
[Theory]
47+
[InlineData("-3 * a^3 + 3 * a^3")]
48+
[InlineData("-(2 * 1/c * c - 1) + 1")]
49+
[InlineData("-(c/c - 1) * c")]
50+
public void IsZeroAsAValue(string coefficient)
51+
=> Assert.True(Functions.PartialFractions.IsZeroAsAValue(coefficient.ToEntity()));
52+
53+
[Theory]
54+
[InlineData("-3 * a^3 + 2 * a^3")]
55+
[InlineData("2 * 1/c * c")]
56+
[InlineData("a - b")]
57+
public void IsNotZeroAsAValue(string coefficient)
58+
=> Assert.False(Functions.PartialFractions.IsZeroAsAValue(coefficient.ToEntity()));
59+
}
60+
}

0 commit comments

Comments
 (0)