Skip to content

Commit c11661b

Browse files
An antiderivative built through an even root says where the root is real (#1431)
The linear-radical substitution u = (a x + b)^(1/q) and the general substitution for an even root both take a power of u out of a radical on the strength of u being a non-negative real, which it is only where the radical is. Beyond the radicand's zero u is imaginary and the integrand can still be real: Rubi's x sqrt(c - a c x)/e^(3 atanh(a x)) above a x = 1 is the product of two imaginary factors, and the answer built for a real u was 14% off its derivative there and shipped as an answer (family 7, one wrong after #1430 made the row reachable). An answer that used the step now carries provided a x + b >= 0: NaN beyond it, which is no claim, and unchanged where it holds. An answer the step did not touch is as it was. #718 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent dbbe2a1 commit c11661b

3 files changed

Lines changed: 92 additions & 12 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,24 @@ Every spelling but `lcm` was a parse error in 2.5.0, the congruence node being n
557557
| `forall n in ZZ : 6 divides n^3 + 5 n` | `UnhandledParseException` | `True` |
558558
| `exists x, y in ZZ : 3 x^2 - 5 y^2 = 1` | `UnhandledParseException` | `False` |
559559

560+
### An antiderivative built through an even root says where the root is real
561+
562+
The linear-radical substitution, `u = (a x + b)^(1/q)`, and the general substitution for an even
563+
root both take a power of `u` out of a radical on the strength of `u` being a non-negative real —
564+
`sqrt(u^2 (1 + u))` as `u sqrt(1 + u)` — which it is only where the radical is. Beyond the
565+
radicand's zero `u` is imaginary, and the integrand can still be real there: Rubi's
566+
`x sqrt(c - a c x) / e^(3 atanh(a x))` above `a x = 1` is the product of two imaginary factors,
567+
and the answer built for a real `u` is not its antiderivative — 14% off at `x = 0.59` with
568+
`a = 3.1`, `c = 0.7`, and it shipped as an answer. An answer that used the step now carries the
569+
condition, `provided a x + b >= 0`: `NaN` beyond it, which is no claim, and unchanged where it
570+
holds. An answer the step did not touch — `x/sqrt(1 + x)`, `1/(sqrt(x) - x^(-1/3))` — is as it
571+
was ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
572+
573+
| Input | Was (2.5.0) | Now |
574+
|---|---|---|
575+
| `"x * sqrt(c - a*c*x) / e^(3 * atanh(a*x))".Integrate("x")` | `integral(…)` — left unevaluated, with `e` evaluated to a hundred digits inside it | an antiderivative in `sqrt(c - a c x)`, `provided c - a * c * x >= 0` |
576+
| `"1/sqrt(x + x^(3/2))".Integrate("x")` | `integral(1 / sqrt(x + x ^ (3/2)), x)` — left unevaluated | `2 * sqrt(1 + sqrt(x)) / (1/2) + C provided x >= 0` |
577+
560578
### `binomial(n, k)` is a function
561579

562580
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,10 +5169,16 @@ static bool IsTwice(Entity argument, Entity.Variable x)
51695169
/// two substitutions at once and is declined rather than half-rewritten.
51705170
/// </para>
51715171
/// <para>
5172-
/// <b>No condition is owed by the rewrite.</b> <c>a</c> is non-zero, since a zero
5172+
/// <b>No condition is owed by the rewrite itself.</b> <c>a</c> is non-zero, since a zero
51735173
/// coefficient is not a linear expression in <c>x</c> and the reader below rejects it,
51745174
/// and <c>u^q = a*x + b</c> is invertible wherever the radical it came from is defined.
5175-
/// The answer inherits the radical's own domain and adds nothing.
5175+
/// The answer inherits the radical's own domain and adds nothing -- <b>except where the
5176+
/// even-root step below has read <c>u</c> as a non-negative real</b>, which it is only
5177+
/// where the radical is real. Beyond that, <c>u</c> is imaginary and the integrand can
5178+
/// still be real: <c>x sqrt(c - a c x) / e^(3 atanh(a x))</c> above <c>a x = 1</c> is the
5179+
/// product of two imaginary factors, and the answer built for a real <c>u</c> is not its
5180+
/// antiderivative there (Rubi 7.3.6). So the answer says what it used: it is given
5181+
/// <c>provided a x + b &gt;= 0</c> exactly when that step changed the integrand.
51765182
/// </para>
51775183
/// https://github.com/asc-community/AngouriMath/issues/718
51785184
/// </remarks>
@@ -5304,13 +5310,24 @@ node is Powf(var radical, Number.Rational e) && e is not Number.Integer
53045310
integrand = inner;
53055311
// For an even q the principal root is not negative wherever it is real, and a root
53065312
// holding a power of u gives that power up: `1/sqrt(t + t^(3/2))` under `u = sqrt(t)`
5307-
// is `2u/sqrt(u^2 + u^3)`, a root of a cubic, and is `2/sqrt(1 + u)`.
5313+
// is `2u/sqrt(u^2 + u^3)`, a root of a cubic, and is `2/sqrt(1 + u)`. Where the
5314+
// radical is not real, u is not a non-negative real and the identities do not hold,
5315+
// so an answer that used them is conditioned on the radicand -- unless the caller
5316+
// knows its variable to be non-negative, as an exponential is.
5317+
// https://github.com/asc-community/AngouriMath/issues/718
5318+
Entity? condition = null;
53085319
if (q % 2 == 0)
5309-
integrand = FactorANonnegativeVariableOutOfRadicals(integrand, u);
5320+
{
5321+
var factored = FactorANonnegativeVariableOutOfRadicals(integrand, u);
5322+
if (factored != integrand && !variableIsNonnegative)
5323+
condition = radicalBase >= Number.Integer.Zero;
5324+
integrand = factored;
5325+
}
53105326

5311-
return Integration.ComputeIndefiniteIntegral(integrand, u, integrateByParts) is { } result
5312-
? result.Substitute(u, MathS.Pow(radicalBase, Number.Rational.Create(1, q)))
5313-
: null;
5327+
if (Integration.ComputeIndefiniteIntegral(integrand, u, integrateByParts) is not { } result)
5328+
return null;
5329+
var back = result.Substitute(u, MathS.Pow(radicalBase, Number.Rational.Create(1, q)));
5330+
return condition is null ? back : back.Provided(condition);
53145331
}
53155332

53165333
/// <summary>
@@ -13787,11 +13804,16 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab
1378713804
&& Integration.ComputeIndefiniteIntegral(factored, uSub, integrateByParts) is { } forPositiveU
1378813805
&& !forPositiveU.Nodes.Any(node => node == MathS.NaN))
1378913806
{
13790-
// An even root is not negative, and what holds for u > 0 is the answer as
13791-
// it stands; `sqrt(x^(1/3))` under `1/(sqrt(x) - x^(-1/3))` was extended by
13792-
// parity it did not need, to an answer with a sign function in it.
13793-
if (u is Powf(_, Number.Rational root) && root.ERational.Denominator.IsEven)
13794-
return Functions.PartialFractions.Bare(forPositiveU).Substitute(uSub, u);
13807+
// An even root is not negative where it is real, and what holds for u > 0
13808+
// is the answer as it stands there; `sqrt(x^(1/3))` under
13809+
// `1/(sqrt(x) - x^(-1/3))` was extended by parity it did not need, to an
13810+
// answer with a sign function in it. Where the radicand is negative the root
13811+
// is imaginary and the identities the factoring used do not hold, so the
13812+
// answer is conditioned on the radicand, as the linear-radical substitution
13813+
// conditions its own. https://github.com/asc-community/AngouriMath/issues/718
13814+
if (u is Powf(var radicand, Number.Rational root) && root.ERational.Denominator.IsEven)
13815+
return Functions.PartialFractions.Bare(forPositiveU).Substitute(uSub, u)
13816+
.Provided(radicand >= Number.Integer.Zero);
1379513817
if (ExtendedByParity(integrandInU, forPositiveU, uSub) is { } onBothSides)
1379613818
return onBothSides.Substitute(uSub, u);
1379713819
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ private static void DifferentiatesBack(string integrand)
7777
[InlineData("x/sqrt(1 + x^2 + (1 + x^2)^(3/2))")]
7878
public void APowerOfTheRootLeavesTheRadical(string integrand) => DifferentiatesBack(integrand);
7979

80+
/// <summary>
81+
/// That step reads <c>u</c> as a non-negative real, which it is only where the radical
82+
/// is real, and the answer says so: <c>1/sqrt(x + x^(3/2))</c> comes back
83+
/// <c>provided x &gt;= 0</c>. Beyond the radicand's zero <c>u</c> is imaginary, and the
84+
/// integrand can still be real there -- Rubi's <c>x sqrt(c - a c x) / e^(3 atanh(a x))</c>
85+
/// above <c>a x = 1</c> is the product of two imaginary factors -- while the answer built
86+
/// for a real <c>u</c> is not its antiderivative: with <c>a = 3.1</c>, <c>c = 0.7</c> at
87+
/// <c>x = 0.59</c> its derivative was 14% off, and it shipped as an answer. Now it is
88+
/// <c>NaN</c> there, which is no claim, and right where the condition holds.
89+
/// https://github.com/asc-community/AngouriMath/issues/718
90+
/// </summary>
91+
[Fact]
92+
public void TheEvenRootStepSaysWhereItHolds()
93+
{
94+
var conditioned = "1/sqrt(x + x^(3/2))".ToEntity().Integrate("x");
95+
Assert.Contains("provided x >= 0", conditioned.Stringize());
96+
97+
var integrand = "x * sqrt(c - a*c*x) / e^(3 * atanh(a*x))".ToEntity();
98+
var integral = integrand.Integrate("x");
99+
Assert.DoesNotContain("integral(", integral.Stringize());
100+
Assert.Contains("provided c - a * c * x >= 0", integral.Stringize());
101+
var pinned = integral.Substitute("C", 0).Substitute("a", 3.1).Substitute("c", 0.7);
102+
var derivative = pinned.Differentiate("x");
103+
var original = integrand.Substitute("a", 3.1).Substitute("c", 0.7);
104+
// Inside the condition, a x < 1: the derivative is the integrand.
105+
foreach (var at in new[] { 0.05, 0.17, 0.31 })
106+
{
107+
var got = derivative.Substitute("x", at).EvalNumerical();
108+
var want = original.Substitute("x", at).EvalNumerical();
109+
Assert.False(got.IsNaN, $"no answer at x = {at}, inside the condition");
110+
Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
111+
$"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}");
112+
}
113+
// Beyond it the integrand is real, 3.1 * 0.59 being above 1, and the answer claims
114+
// nothing rather than something wrong.
115+
var outside = original.Substitute("x", 0.59).EvalNumerical();
116+
Assert.True(Math.Abs((double)outside.ImaginaryPart) < 1e-12, $"the integrand at x = 0.59 is {outside}, not real");
117+
Assert.True(derivative.Substitute("x", 0.59).Evaled.IsNaN, "the answer claims a derivative outside its condition");
118+
}
119+
80120
/// <summary>
81121
/// A polynomial over a square root of something linear. None of these had an
82122
/// antiderivative, and each is a first-year exercise.

0 commit comments

Comments
 (0)