5
$\begingroup$

14.2 appears to have trouble with the Cauchy principal value of piecewise defined functions; Integrate is off while NIntegrate works:

In[1]:= Integrate[
 Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], {x, -5, 5}, 
 PrincipalValue -> True]

Out[1]= -\[Infinity]

In[2]:= NIntegrate[
 Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], {x, -5, 0, 5}, 
 Method -> "PrincipalValue"]

Out[2]= 5.

Am I doing anything wrong? If not: is this a known issue, are there workarounds?

$\endgroup$
2
  • 1
    $\begingroup$ Are you sure the numerical result is correct? Anyway, without looking too much into it, you can get same result from Integrate as follows. f[x_] := Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}]; Integrate[f[x], {x, -5, 5}, GenerateConditions -> False] which now gives 5. Screen shot i.sstatic.net/Kukb0VGy.png but using GenerateConditions -> False made Integrate skip the extra checking on poles and divergence? and that is why it gave same answer as NIntegrate $\endgroup$ Commented 2 days ago
  • 1
    $\begingroup$ Fyi, I tried this in Maple, and it gave 5 using CauchyPrincipalValue. So it looks like there is an issue with Integrate here, assuming Maple and NIntegrate are both correct. Here is the code I used in Maple f:= x->piecewise(x>0,1/x,x<0,1+1/x): int(f(x), x=-5..5, 'CauchyPrincipalValue') and it gave 5 screen shot i.sstatic.net/JpYLx9L2.png using Maple 2025.2 on windows 10 $\endgroup$ Commented 2 days ago

3 Answers 3

4
$\begingroup$

$Version ("12.2.0 for Microsoft Windows (64-bit) (December 12, 2020)")

If you split the integrand, Integrate gives the correct result

Integrate[Piecewise[{{1/x, x > 0}, { 1/x, x < 0}}], {x, -5, 5},PrincipalValue -> True] + 
Integrate[1, {x, -5, 0 }]
(* 5 *)

more simple

Integrate[ 1/x , {x, -5, 5}, PrincipalValue -> True] + 
Integrate[1, {x, -5, 0 }]
(* 5 *)

without Piecewise

 Integrate[ 1/x + UnitStep[-x], {x, -5, 5}, PrincipalValue -> True] 
 (* 5 *)

Hope it helps!

$\endgroup$
4
$\begingroup$

Another workaround from my answer to Integration Help. The trick is to integrate the average of a function $f(x)$ and its reflection $f(a+b-x)$ in the center of the integration interval $[a,b]$. I noticed many years ago that it often helps. The asymmetry in the integrand seemed to call out for trial.

ClearAll[symmetrizeIntegrate];
SetAttributes[symmetrizeIntegrate, HoldAll];
symmetrizeIntegrate[Integrate[f_, {x_, a_, b_}, opts___]] := 
  Integrate[(f + (f /. x -> a + b - x))/2, {x, a, b}, opts];

Integrate[Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], {x, -5, 5}, 
  PrincipalValue -> True] // symmetrizeIntegrate

(*  5  *)

In response to OP's comment: For a nonsymmetric relationship between integral and interval, subtract an integral that is zero:

Integrate[
 Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}] - 
  Piecewise[{{1/x, -1 < x < 1}}], {x, -3, 5}, PrincipalValue -> True]

(* 3 - Log[3] + Log[5]  *)
$\endgroup$
5
  • $\begingroup$ Could it be consequence of using PiecewiseExpand by Integrate before integration? I do not know if it was used but if yes, then it leads to Piecewise[{{1/2, x > 0 || x < 0}}, 0] which has no singularities and therefore no problems. $\endgroup$ Commented yesterday
  • $\begingroup$ @azerbajdzan I see. Thanks. It probably does use it (why not?). Or it simplifies the left side and right side of x == 0 separately, effectively removing the piecewise. But if it did that, you would think the OP's integral wouldn't be buggy. $\endgroup$ Commented yesterday
  • $\begingroup$ "The limit of the symmetrized function at 0 is finite" but the same hold for OP function: Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}] /. x -> 0 - it returns 0. $\endgroup$ Commented yesterday
  • $\begingroup$ @azerbajdzan The limit at $0$ is $\pm\infty$: Limit[Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], x -> 0, Direction -> "FromAbove"] and Limit[Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], x -> 0, Direction -> "FromBelow"] $\endgroup$ Commented yesterday
  • 1
    $\begingroup$ I believe your trick only works in this example because the singularity happens to be in the middle of the interval. For instance, Integrate[Piecewise[{{1/x, x > 0}, {1 + 1/x, x < 0}}], {x, -3, 5}, PrincipalValue -> True] // symmetrizeIntegrate won't work. $\endgroup$ Commented yesterday
1
$\begingroup$

Just for illustration:

f[x_] := Piecewise[{{1 + 1/x, x < 0}, {1/x, x > 0}}]
g[u_] := Integrate[f[x], {x, u, 5}] + Integrate[f[x], {x, -5, -u}]
h[u_, a_, b_] := 
 Integrate[f[x], {x, u, b}] + Integrate[f[x], {x, a, -u}]

Approximation:

ListLogLinearPlot[

Table[{u, g[u]}, {u, PowerRange[1/10, 1/1000000, 1/Sqrt[10]]}]]

enter image description here

Symbolic:

Trace[Limit[Assuming[{0 < e < 1}, FullSimplify[g[e]]], e -> 0]]
Trace[Limit[Assuming[{0 < e < 1}, FullSimplify[h[e, -3, 5]]], e -> 0]]
Limit[Assuming[{0 < e < 1}, FullSimplify[h[e, -3, 2]]], e -> 0]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.