Skip to content

Commit 02e2f3d

Browse files
An answer that took an even root real without reading its sign says where it holds (#1594)
The substitution for an even root, u = t^(1/q), conditions its answer on the radicand only where the answer below read a sign or a modulus of u. The rules below can take u real without either: SolveByCombiningRadicals writes sqrt(1 + 1/u^2) sqrt(1 - u^4) as sqrt(u^4 - u^8 + u^2 - u^6)/u^2, which reads sqrt(u^4) as u^2, the other root wherever u is imaginary. Where the radicand is negative u is imaginary, and the integrand can still be real there beside a second imaginary root: sqrt(cos(x))/sqrt(a + a sec(x)) under t = cos(x) and u = sqrt(t) came back negated wherever the cosine is negative. Where the answer below read no sign and the integrand holds a second even root, the answer is now differentiated and compared with the integrand at the default sampled points where the integrand is real; where it differs, it carries provided t >= 0, as the answers that read a sign already do. Measured with work/intbench against dd72825, 3 s a problem: a seeded twentieth of the problems holding half-odd powers of two different bases, the only ones the check reaches, 273 statable: no verdict moves, same time. Closes #1581. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3a1e247 commit 02e2f3d

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,23 @@ Rubi's 4.5.3.1:640 was wrong on that region
765765
|---|---|---|
766766
| `"(1+sec(x))^(5/2)*sqrt(cos(x))".Integrate("x")` | `integral((1 + sec(x)) ^ (5/2) * sqrt(cos(x)), x)` — left unevaluated | an antiderivative with `sgn(sin(x))` in it, `provided cos(x) >= 0` |
767767

768+
### An answer that took an even root real without reading its sign says where it holds
769+
770+
The same step's answer is checked where the rules below read no sign of `u` and still took it real:
771+
`SolveByCombiningRadicals` writes `sqrt(1 + 1/u^2) sqrt(1 - u^4)` as
772+
`sqrt(u^4 - u^8 + u^2 - u^6)/u^2`, which reads `sqrt(u^4)` as `u^2`, the other root wherever `u` is
773+
imaginary. Where the integrand holds a second even root, as it must to be real where the radicand
774+
is negative, the answer is differentiated and compared with the integrand where the integrand is
775+
real. Where it is not the antiderivative there, it now carries `provided t >= 0` as well.
776+
`sqrt(cos(x))/sqrt(a + a sec(x))` and six of its kin in Rubi's 4.5.1.2 and 4.5.3.1 were answered
777+
negated wherever the cosine is negative; the half-angle rule above answers those seven first now,
778+
and this is the same answer reached through the even root
779+
([#1581](https://github.com/asc-community/AngouriMath/issues/1581)).
780+
781+
| Input | Was (2.5.0) | Now |
782+
|---|---|---|
783+
| `"sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))".Integrate("x")` | `integral(...)` | an antiderivative in `sqrt(x)`, `provided x >= 0` |
784+
768785
### The inverse hyperbolic tangent's answer is given on its domain, and says so
769786

770787
`1/((1 - a^2 x^2)^(5/2) artanh(a x))` was left unevaluated, although the substitution `a x = tanh(u)`

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,19 @@ internal static bool DerivativeHoldsAtSampledPoints(Entity antiderivative, Entit
18691869
return HoldsAtSampledPoints(Bare(pinnedAntiderivative).Differentiate(x), pinnedIntegrand, x);
18701870
}
18711871

1872+
/// <summary>
1873+
/// Whether the derivative of <paramref name="antiderivative"/> differs from
1874+
/// <paramref name="integrand"/> at one of the default points where the integrand is
1875+
/// real, as <see cref="DiffersWhereReal"/> tells, with the symbols pinned before the
1876+
/// differentiation, as <see cref="DerivativeHoldsAtSampledPoints"/> pins them.
1877+
/// </summary>
1878+
internal static bool DerivativeDiffersWhereReal(Entity antiderivative, Entity integrand, Variable x)
1879+
{
1880+
using var _ = MathS.Settings.DowncastingEnabled.Set(false);
1881+
var (pinnedAntiderivative, pinnedIntegrand) = Pinned(antiderivative, integrand, x);
1882+
return DiffersWhereReal(Bare(pinnedAntiderivative).Differentiate(x), pinnedIntegrand, x);
1883+
}
1884+
18721885
/// <summary>
18731886
/// The two with every symbol but <paramref name="x"/> pinned to a fixed value:
18741887
/// distinct values, off the integers, so that no two factors coincide by accident and

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19089,6 +19089,20 @@ private static Entity SimplifiedWithoutTheImaginaryUnit(Entity written, Entity i
1908919089
/// Attempts to solve an integral using u-substitution.
1909019090
/// Looks for patterns where f(g(x)) * g'(x) can be integrated as F(g(x)).
1909119091
/// </summary>
19092+
/// <summary>
19093+
/// Whether <paramref name="expr"/> holds an even root of something in <paramref name="x"/>
19094+
/// other than <paramref name="radicand"/>: the only way it can be real where the radicand is
19095+
/// negative and its own root imaginary.
19096+
/// </summary>
19097+
private static bool HoldsAnEvenRootBesides(Entity expr, Entity radicand, Entity.Variable x)
19098+
{
19099+
foreach (var node in expr.Nodes)
19100+
if (node is Powf(var @base, Number.Rational power) && power.ERational.Denominator.IsEven
19101+
&& @base != radicand && @base.ContainsNode(x))
19102+
return true;
19103+
return false;
19104+
}
19105+
1909219106
internal static Entity? SolveBySubstitution(Entity expr, Entity.Variable x, bool integrateByParts = true)
1909319107
{
1909419108
// A rational function over written linear factors with symbols in their
@@ -19325,6 +19339,17 @@ private static Entity SimplifiedWithoutTheImaginaryUnit(Entity written, Entity i
1932519339
// radicand, as the factoring below conditions its own.
1932619340
if (asTheRoot != resultInU)
1932719341
return Functions.PartialFractions.Bare(asTheRoot).Substitute(uSub, u).Provided(rootOf >= Number.Integer.Zero);
19342+
// And a rule below can take u real without reading its sign: `sqrt(u^4)`
19343+
// written as `u^2` is the other root wherever u is imaginary. That matters
19344+
// only where the integrand is real with the radicand negative, which takes
19345+
// a second even root imaginary beside this one, so there the answer is
19346+
// checked where the integrand is real: `sqrt(cos(x))/sqrt(1 + sec(x))` under
19347+
// `t = cos(x)` and `u = sqrt(t)` came back negated wherever the cosine is
19348+
// negative. https://github.com/asc-community/AngouriMath/issues/1581
19349+
var answer = resultInU.Substitute(uSub, u);
19350+
if (HoldsAnEvenRootBesides(expr, rootOf, x) && Functions.PartialFractions.DerivativeDiffersWhereReal(answer, expr, x))
19351+
return Functions.PartialFractions.Bare(answer).Provided(rootOf >= Number.Integer.Zero);
19352+
return answer;
1932819353
}
1932919354
// Substitute back: replace u with g(x)
1933019355
return resultInU.Substitute(uSub, u);

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,38 @@ public void TheSearchsEvenRootSaysWhereItHolds()
148148
Assert.True(derivative.Substitute("x", 2.8).Evaled.IsNaN, "the answer claims a derivative outside its condition");
149149
}
150150

151+
/// <summary>
152+
/// And where the rules below read no sign at all, the answer is checked where the radicand
153+
/// is negative. <c>sqrt(cos(x))/sqrt(1 + sec(x))</c> is real where the cosine is negative,
154+
/// two of its roots imaginary there; under <c>t = cos(x)</c> and <c>u = sqrt(t)</c> the
155+
/// product of roots below the bar was written with <c>sqrt(u^4) = u^2</c>, and the answer
156+
/// was negated wherever the cosine is negative. Rubi's 4.5.1.2, and the same in <c>t</c>.
157+
/// https://github.com/asc-community/AngouriMath/issues/1581
158+
/// </summary>
159+
[Theory]
160+
[InlineData("sqrt(cos(x))/sqrt(1 + sec(x))", "provided cos(x) >= 0", new[] { 0.3, 0.9, -0.7 }, 2.4)]
161+
[InlineData("sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))", "provided x >= 0", new[] { 0.2, 0.5, 0.8 }, -0.6)]
162+
public void AnEvenRootTakenRealWithoutASignSaysWhereItHolds(string written, string condition, double[] inside, double outsideAt)
163+
{
164+
var integrand = written.ToEntity();
165+
var integral = integrand.Integrate("x");
166+
Assert.DoesNotContain("integral(", integral.Stringize());
167+
Assert.Contains(condition, integral.Stringize());
168+
var derivative = integral.Substitute("C", 0).Differentiate("x");
169+
foreach (var at in inside)
170+
{
171+
var got = derivative.Substitute("x", at).EvalNumerical();
172+
var want = integrand.Substitute("x", at).EvalNumerical();
173+
Assert.False(got.IsNaN, $"no answer at x = {at}, inside the condition");
174+
Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
175+
$"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}");
176+
}
177+
// Beyond it the integrand is real and the answer claims nothing.
178+
var outside = integrand.Substitute("x", outsideAt).EvalNumerical();
179+
Assert.True(Math.Abs((double)outside.ImaginaryPart) < 1e-12, $"the integrand at x = {outsideAt} is {outside}, not real");
180+
Assert.True(derivative.Substitute("x", outsideAt).Evaled.IsNaN, "the answer claims a derivative outside its condition");
181+
}
182+
151183
/// <summary>
152184
/// A polynomial over a square root of something linear. None of these had an
153185
/// antiderivative, and each is a first-year exercise.

0 commit comments

Comments
 (0)