Skip to content

Commit d1d0296

Browse files
Answer a half-angle integrand through two roots where the cosine is negative too (#1959)
The half-angle rule beside a half-odd power of `1 ± sec` writes each root of a constant times the secant or cosine apart in t = tan(y/2). That is exact where cos(y) > 0. Beyond it each root turns its sign by its constant's, and two roots make the integrand real on both sides at once, so the answer said `provided cos(y) >= 0`: half of the integrand's domain, for every one of the 165 rows of Rubi's 4.5.4.2 that took the rule. Which roots turn is known from their constants. For an even number of roots, the turns multiply to (-1)^n s, where n is the number of roots of the cosine and s is the sign of the product of the constants, and the integrand is real just where s > 0. The answer now holds there as written, with `sgn(cos(y))` in front for an odd n, and says `provided a > 0 or cos(y) >= 0` only where s is not known. An odd number above one still says `provided cos(y) >= 0`. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 46a56fb commit d1d0296

4 files changed

Lines changed: 166 additions & 25 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,9 +4163,12 @@ cosine the whole is a rational function of `t` beside one root, of `1 - t^2` or
41634163
Two roots, which a half-odd power of the secant beside a whole one of `1 ± sec` makes, are
41644164
elliptic and still declined. The cosecant's the same way through the complement, and a root of
41654165
`1 - sec(y)` carries `sgn(tan(y/2))`, constant between its zeros. Each root written apart is exact
4166-
where `cos(y)` is positive; beyond it two roots can make the integrand real while each has turned
4167-
its sign on its own, so an answer through two says `provided cos(y) >= 0`
4168-
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
4166+
where `cos(y)` is positive; beyond it each turns its sign by its constant's. Two roots make the
4167+
integrand real on both sides at once, for a positive product of their constants, and then the turns
4168+
are as many as the roots of the cosine: an answer through two holds where the cosine is negative too,
4169+
with `sgn(cos(y))` in front for an odd number of roots of the cosine, and says
4170+
`provided a > 0 or cos(y) >= 0` where the product's sign is not known. Three say
4171+
`provided cos(y) >= 0` ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
41694172

41704173
| Input | Was (2.5.0) | Now |
41714174
|---|---|---|
@@ -4174,6 +4177,8 @@ its sign on its own, so an answer through two says `provided cos(y) >= 0`
41744177
| `"1/(sec(x)^(3/2)*sqrt(1 + sec(x)))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative through a root of `1 + tan(x/2)^2` |
41754178
| `"sqrt(a - a*sec(x))".ToEntity().Integrate("x")` | `integral(...)` | a logarithm in `tan(x/2)`, times `sgn(tan(x/2))` |
41764179
| `"sqrt(1 + csc(x))".ToEntity().Integrate("x")` | `integral(...)` | an arctangent in `tan(pi/4 - x/2)` |
4180+
| `"cos(x)^(3/2)/sqrt(1 + sec(x))".ToEntity().Integrate("x")` | `integral(...)` | a logarithm and a power in `tan(x/2)`, times `sgn(cos(x))` |
4181+
| `"cos(c+d*x)^(3/2)*(A+C*sec(c+d*x)^2)/sqrt(a+a*sec(c+d*x))".ToEntity().Integrate("x")` | `integral(...)` | the same, times `sgn(cos(c + d x))`, `provided a > 0 or cos(c + d x) >= 0` |
41774182

41784183
### An even power of the secant or the cosecant under a root is written in the tangent
41794184

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5361,6 +5361,14 @@ Entity Integral(ERational power)
53615361
/// chain in t.
53625362
/// https://github.com/asc-community/AngouriMath/issues/718
53635363
/// </para>
5364+
/// <para>
5365+
/// Two roots, as in <c>cos(y)^(3/2)/sqrt(a + a sec(y))</c>, are real together where the
5366+
/// cosine is negative too, and there each written apart may have turned its sign: the answer
5367+
/// said <c>provided cos(y) >= 0</c>, half of the integrand's domain, for every one of 165
5368+
/// rows of Rubi's 4.5.4.2. Which turn is known from the constants, and the answer carries
5369+
/// the cosine's sign where they leave an odd number turned.
5370+
/// https://github.com/asc-community/AngouriMath/issues/233
5371+
/// </para>
53645372
/// </remarks>
53655373
internal static Entity? SolveByTheHalfAngleTangentBesideAHalfOddPowerOfOnePlusASecant(Entity expr, Entity.Variable x, bool integrateByParts)
53665374
{
@@ -5408,6 +5416,9 @@ Entity Integral(ERational power)
54085416
var found = false;
54095417
var declined = false;
54105418
var roots = 0;
5419+
var rootsOfTheCompanion = 0;
5420+
// The product of the roots' constants: the sign of each radicand where t^2 < 1.
5421+
Entity constants = Number.Integer.One;
54115422
// Each half-odd power, assembled in t: of a (1 ± sec(y)), or of d sec(y) or d cos(y);
54125423
// for the cosecant, of a (1 ± csc(y)), d csc(y) or d sin(y).
54135424
var rewritten = expr.Replace(node =>
@@ -5423,6 +5434,7 @@ Entity Integral(ERational power)
54235434
if (ReadAsOnePlusMinusAFunction(radicand, secant, cosecant) is (var a, var plus, var isSecant) && isSecant == secantKind)
54245435
{
54255436
found = true;
5437+
constants = plus ? constants * a : constants * -a;
54265438
if (plus)
54275439
return MathS.Pow(2 * a, exponent) * MathS.Pow(oneMinus, (-exponent).InnerSimplified);
54285440
// (-2a t^2/(1 - t^2))^p is (-2a)^p |t|^(2p) (1 - t^2)^(-p), and 2p is odd.
@@ -5449,6 +5461,9 @@ Entity Integral(ERational power)
54495461
declined = true;
54505462
return node;
54515463
}
5464+
if (function == companion)
5465+
rootsOfTheCompanion++;
5466+
constants = constants * coefficient;
54525467
var (above, below) = function == primary ? (onePlus, oneMinus) : (oneMinus, onePlus);
54535468
return MathS.Pow(coefficient, exponent) * MathS.Pow(above, exponent) * MathS.Pow(below, (-exponent).InnerSimplified);
54545469
});
@@ -5500,12 +5515,36 @@ Entity Integral(ERational power)
55005515
if (answer.Nodes.Any(node => node == MathS.NaN))
55015516
return null;
55025517
// Each root written apart is exact inside t^2 < 1, where 1 - t^2 is positive, for any
5503-
// sign of the constants. Outside it a root whose radicand is negative there is
5504-
// imaginary, and one alone leaves the integrand complex, where the answer is nothing
5505-
// to be wrong about; but two can make it real again -- `(1 + sec(x))^(5/2) sqrt(cos(x))`
5506-
// is real where the cosine is negative -- and there each written apart may have
5507-
// turned its sign where the other did not. The answer says where it holds.
5508-
return roots < 2 ? answer : answer.Provided((secantKind ? MathS.Cos(argument) : MathS.Sin(argument)) >= Number.Integer.Zero);
5518+
// sign of the constants. Outside it `(P/(1 - t^2))^p` is `P^p (1 - t^2)^(-p)` but for a
5519+
// positive P, and `(P (1 - t^2))^p` is `P^p (1 - t^2)^p` but for a negative one, the two
5520+
// differing by e^(2 i pi p) = -1: where cos(y) < 0 a root of a ± a sec(y) or d sec(y)
5521+
// turns its sign if its constant is positive, and one of d cos(y) if its constant is
5522+
// negative. One root alone turns only where the integrand is complex, which is nothing
5523+
// to be wrong about. For an even number the turns multiply to (-1)^n s, for n roots of
5524+
// the cosine and s the sign of the product of the constants, and the integrand is real,
5525+
// on both sides at once, just where s is positive: `sqrt(sec(x)) sqrt(1 + sec(x))` holds
5526+
// as written where the cosine is negative, and `cos(x)^(3/2)/sqrt(1 + sec(x))` with the
5527+
// cosine's sign in front. The root of 1 - t^2 has cancelled then, so that the answer in
5528+
// t holds beyond t^2 < 1 too; for an odd number above one it has not, and the answer
5529+
// says where it holds, as it does where the product's sign is not known.
5530+
if (roots < 2)
5531+
return answer;
5532+
var withinTheHalfTurn = answer.Provided(companion >= Number.Integer.Zero);
5533+
if (!exponents[0].Numerator.Remainder(exponents[0].Denominator).IsZero)
5534+
return withinTheHalfTurn;
5535+
var turns = rootsOfTheCompanion % 2 == 1;
5536+
switch (constants.Evaled)
5537+
{
5538+
case Number.Real { IsFinite: true } product when !product.IsZero:
5539+
if (product < 0)
5540+
turns = !turns;
5541+
return turns ? MathS.Signum(companion) * answer : answer;
5542+
case Number:
5543+
return withinTheHalfTurn;
5544+
default:
5545+
return (turns ? MathS.Signum(companion) * answer : answer)
5546+
.Provided(constants > Number.Integer.Zero | companion >= Number.Integer.Zero);
5547+
}
55095548
}
55105549

55115550
/// <summary>

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,17 @@ public void AnExponentialOfAnInverseTangentBesideTheRootSaysWhereItHolds()
158158
/// <c>u = sqrt(t)</c> is <c>-2 u^2 (1 + 1/u^2)^(5/2)/sqrt(1 - u^4)</c>, whose power of a
159159
/// quotient is taken apart as <c>(1 + u^2)^(5/2)/|u|^5</c>. Where the cosine is negative the
160160
/// integrand is still real, a product of two imaginary factors, and the answer built for a
161-
/// real <c>u</c> was not its antiderivative there. Rubi's 4.5.3.1.
161+
/// real <c>u</c> was not its antiderivative there. Rubi's 4.5.3.1. The half-angle tangent
162+
/// answers it on both sides now, with the cosine's sign in front
163+
/// (<see cref="OnePlusASecantHalfPowerIntegralTest"/>); either way the answer is right where
164+
/// the cosine is negative or says nothing there.
162165
/// </summary>
163166
[Fact]
164-
public void TheSearchsEvenRootSaysWhereItHolds()
167+
public void TheSearchsEvenRootIsRightOrSaysNothingBeyondIt()
165168
{
166169
var integrand = "(1 + sec(x))^(5/2)*sqrt(cos(x))".ToEntity();
167170
var integral = integrand.Integrate("x");
168171
Assert.DoesNotContain("integral(", integral.Stringize());
169-
Assert.Contains("provided cos(x) >= 0", integral.Stringize());
170172
var derivative = integral.Substitute("C", 0).Differentiate("x");
171173
// Inside the condition the derivative is the integrand.
172174
foreach (var at in new[] { 0.3, 0.9, -0.7, 5.9 })
@@ -177,10 +179,12 @@ public void TheSearchsEvenRootSaysWhereItHolds()
177179
Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
178180
$"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}");
179181
}
180-
// Beyond it the integrand is real and the answer claims nothing.
182+
// Beyond it the integrand is real, and the answer is right or claims nothing.
181183
var outside = integrand.Substitute("x", 2.8).EvalNumerical();
182184
Assert.True(Math.Abs((double)outside.ImaginaryPart) < 1e-12, $"the integrand at x = 2.8 is {outside}, not real");
183-
Assert.True(derivative.Substitute("x", 2.8).Evaled.IsNaN, "the answer claims a derivative outside its condition");
185+
var beyond = derivative.Substitute("x", 2.8).EvalNumerical();
186+
Assert.True(beyond.IsNaN || Math.Abs((double)(beyond - outside).RealPart) + Math.Abs((double)(beyond - outside).ImaginaryPart)
187+
< 1e-9 * Math.Max(1, Math.Abs((double)outside.RealPart)), $"d/dx of the antiderivative is {beyond} at x = 2.8, where the integrand is {outside}");
184188
}
185189

186190
/// <summary>
@@ -190,17 +194,18 @@ public void TheSearchsEvenRootSaysWhereItHolds()
190194
/// product of roots below the bar was written with <c>sqrt(u^4) = u^2</c>, and the answer
191195
/// was negated wherever the cosine is negative. Rubi's 4.5.1.2. The same in <c>t</c>,
192196
/// <c>sqrt(t)/(sqrt(1/t + 1) sqrt(1 - t^2))</c>, is written over each linear once and
193-
/// answered on both sides (<see cref="EachLinearWrittenOnceIntegralTest"/>).
197+
/// answered on both sides (<see cref="EachLinearWrittenOnceIntegralTest"/>), and so is it in
198+
/// the half-angle tangent now (<see cref="OnePlusASecantHalfPowerIntegralTest"/>): the
199+
/// answer is right where the cosine is negative or says nothing there.
194200
/// https://github.com/asc-community/AngouriMath/issues/1581
195201
/// </summary>
196202
[Theory]
197-
[InlineData("sqrt(cos(x))/sqrt(1 + sec(x))", "provided cos(x) >= 0", new[] { 0.3, 0.9, -0.7 }, 2.4)]
198-
public void AnEvenRootTakenRealWithoutASignSaysWhereItHolds(string written, string condition, double[] inside, double outsideAt)
203+
[InlineData("sqrt(cos(x))/sqrt(1 + sec(x))", new[] { 0.3, 0.9, -0.7 }, 2.4)]
204+
public void AnEvenRootTakenRealWithoutASignIsRightOrSaysNothingBeyondIt(string written, double[] inside, double outsideAt)
199205
{
200206
var integrand = written.ToEntity();
201207
var integral = integrand.Integrate("x");
202208
Assert.DoesNotContain("integral(", integral.Stringize());
203-
Assert.Contains(condition, integral.Stringize());
204209
var derivative = integral.Substitute("C", 0).Differentiate("x");
205210
foreach (var at in inside)
206211
{
@@ -210,10 +215,12 @@ public void AnEvenRootTakenRealWithoutASignSaysWhereItHolds(string written, stri
210215
Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
211216
$"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}");
212217
}
213-
// Beyond it the integrand is real and the answer claims nothing.
218+
// Beyond it the integrand is real, and the answer is right or claims nothing.
214219
var outside = integrand.Substitute("x", outsideAt).EvalNumerical();
215220
Assert.True(Math.Abs((double)outside.ImaginaryPart) < 1e-12, $"the integrand at x = {outsideAt} is {outside}, not real");
216-
Assert.True(derivative.Substitute("x", outsideAt).Evaled.IsNaN, "the answer claims a derivative outside its condition");
221+
var beyond = derivative.Substitute("x", outsideAt).EvalNumerical();
222+
Assert.True(beyond.IsNaN || Math.Abs((double)(beyond - outside).RealPart) + Math.Abs((double)(beyond - outside).ImaginaryPart)
223+
< 1e-9 * Math.Max(1, Math.Abs((double)outside.RealPart)), $"d/dx of the antiderivative is {beyond} at x = {outsideAt}, where the integrand is {outside}");
217224
}
218225

219226
/// <summary>

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

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,106 @@ public void OnePlusASecant(string integrand)
8585
public void OneMinusASecant(string integrand)
8686
=> DifferentiatesBackPinned(integrand, new[] { 1.8, 2.2, 2.6, 3.0 });
8787

88+
/// <summary>
89+
/// d/dx of the antiderivative, the symbols pinned first, against the integrand at every
90+
/// point where the integrand is real: each of them must have a value there.
91+
/// </summary>
92+
private static void HoldsWhereverTheIntegrandIsReal(string integrand, double[] points, int compare, (string, double)[] pins)
93+
{
94+
var integral = integrand.ToEntity().Integrate("x");
95+
Assert.DoesNotContain("integral(", integral.Stringize());
96+
97+
var answer = integral;
98+
Entity original = integrand.ToEntity();
99+
foreach (var (name, value) in pins)
100+
{
101+
answer = answer.Substitute(name, value);
102+
original = original.Substitute(name, value);
103+
}
104+
var derivative = answer.Differentiate("x");
105+
var compared = 0;
106+
foreach (var at in points)
107+
{
108+
var want = original.Substitute("x", at).EvalNumerical();
109+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
110+
if (want.IsNaN || Math.Abs((double)want.ImaginaryPart) > 1e-12 * scale)
111+
continue;
112+
compared++;
113+
var got = derivative.Substitute("x", at).EvalNumerical();
114+
Assert.False(got.IsNaN, $"the antiderivative of {integrand} has no value at x = {at}, where the integrand is {want}");
115+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
116+
Assert.True(difference / scale < 1e-9,
117+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, "
118+
+ $"where the integrand is {want}");
119+
}
120+
Assert.True(compared >= compare, $"only {compared} points could be compared for {integrand}");
121+
}
122+
123+
/// <summary>
124+
/// On either side of the cosine's sign change, two quarter turns each: (-pi, -pi/2),
125+
/// (-pi/2, pi/2), (pi/2, pi) and (pi, 3pi/2), and the sine's the same way for the cosecant.
126+
/// </summary>
127+
private static readonly double[] BothSides = { -2.6, -2.0, -1.2, 0.3, 1.1, 1.8, 2.6, 3.6, 4.2 };
128+
88129
/// <summary>
89130
/// Two roots written apart are exact where <c>cos(y)</c> is positive, for any sign of the
90-
/// constants, and the answer says so: beyond it each may turn its sign where the other
91-
/// does not, while the integrand is real. Checked inside, where both roots of
92-
/// <c>1 - sec(x)</c> here are imaginary and their product real.
131+
/// constants. Where it is negative each turns its sign by its constant's, and where the
132+
/// integrand is real -- on both sides at once, for a positive product of the constants --
133+
/// the turns are as many as the roots of the cosine: the answer holds there as written, or
134+
/// with the cosine's sign in front. Rubi's
135+
/// <c>cos(c + d x)^(3/2) (A + C sec(c + d x)^2)/sqrt(a + a sec(c + d x))</c> said
136+
/// <c>provided cos(c + d x) >= 0</c>, where without it the answer was the integrand's
137+
/// negative.
138+
/// </summary>
139+
[Theory]
140+
[InlineData("(c - c*sec(x))^(7/2)/sqrt(a - a*sec(x))")]
141+
[InlineData("sqrt(sec(x))*sqrt(1 + sec(x))")]
142+
[InlineData("cos(x)^(3/2)/sqrt(1 + sec(x))")]
143+
[InlineData("sqrt(cos(x))*sqrt(a + a*sec(x))")]
144+
[InlineData("sec(x)^(3/2)*(A + B*sec(x))*sqrt(a + a*sec(x))")]
145+
[InlineData("cos(x)^(5/2)*(A + B*sec(x))*sqrt(a + a*sec(x))")]
146+
[InlineData("sqrt(-csc(x))*sqrt(a - a*csc(x))")]
147+
[InlineData("sin(x)^(3/2)/sqrt(1 + csc(x))")]
148+
[InlineData("sqrt(cos(x))/sqrt(1 + sec(x))")]
149+
[InlineData("(1 + sec(x))^(5/2)*sqrt(cos(x))")]
150+
public void TwoRootsHoldOnBothSides(string integrand)
151+
=> HoldsWhereverTheIntegrandIsReal(integrand, BothSides, BothSides.Length, Pins);
152+
153+
/// <summary>
154+
/// Rubi's 4.5.4.2, <c>(a + a sec)^m (d sec)^n (A + B sec + C sec^2)</c>, at
155+
/// <c>y = 0.9 + 1.3 x</c>: from <c>-2.48</c> to <c>6.36</c>, both signs of the cosine.
156+
/// </summary>
157+
[Theory]
158+
[InlineData("cos(c+d*x)^(3/2)*(A+C*sec(c+d*x)^2)/sqrt(a+a*sec(c+d*x))")]
159+
[InlineData("sec(c+d*x)^(3/2)*(A+B*sec(c+d*x)+C*sec(c+d*x)^2)*sqrt(a+a*sec(c+d*x))")]
160+
[InlineData("(a+a*sec(c+d*x))^(3/2)*(A+C*sec(c+d*x)^2)/sec(c+d*x)^(5/2)")]
161+
[InlineData("cos(c+d*x)^(5/2)*(A+B*sec(c+d*x)+C*sec(c+d*x)^2)/(a+a*sec(c+d*x))^(3/2)")]
162+
public void RubisSecantRowsHoldOnBothSides(string integrand)
163+
=> HoldsWhereverTheIntegrandIsReal(integrand, BothSides, BothSides.Length,
164+
new[] { ("a", 1.7), ("c", 0.9), ("d", 1.3), ("A", 0.6), ("B", 1.3), ("C", 0.8) });
165+
166+
/// <summary>
167+
/// The product of the constants of <c>sqrt(sec(x)) sqrt(a - a sec(x))</c> is <c>-a</c>,
168+
/// whose sign is not known: the integrand is real for a negative <c>a</c>, and then the
169+
/// answer holds on both sides, and complex everywhere for a positive one, where it says it
170+
/// holds on the cosine's positive side.
171+
/// </summary>
172+
[Fact]
173+
public void AProductOfConstantsOfUnknownSignSaysWhereItHolds()
174+
{
175+
const string integrand = "sqrt(sec(x))*sqrt(a - a*sec(x))";
176+
Assert.Contains("provided", integrand.ToEntity().Integrate("x").Stringize());
177+
HoldsWhereverTheIntegrandIsReal(integrand, BothSides, BothSides.Length, new[] { ("a", -1.7) });
178+
}
179+
180+
/// <summary>
181+
/// Three roots leave one of <c>1 - t^2</c> in the answer in t, which is not shown to hold
182+
/// beyond <c>t^2 < 1</c>, and the answer says so.
93183
/// </summary>
94184
[Fact]
95-
public void TwoRootsSayWhereTheyHold()
185+
public void ThreeRootsSayWhereTheyHold()
96186
{
97-
const string integrand = "(c - c*sec(x))^(7/2)/sqrt(a - a*sec(x))";
187+
const string integrand = "sqrt(cos(x))*sqrt(sec(x))*sqrt(1 + sec(x))";
98188
Assert.Contains("provided cos(x) >= 0", integrand.ToEntity().Integrate("x").Stringize());
99189
DifferentiatesBackPinned(integrand, new[] { 0.3, 0.7, 1.1, 1.4 });
100190
}

0 commit comments

Comments
 (0)