# How to receive the result of the integral $\int\frac{1}{x}dx$ is $\ln|x|$?

When I input \[Integral]1/x \[DifferentialD]x in Mathematica, I get Log[x]. How can I make the result of the integral $\displaystyle \int\dfrac{1}{x}dx$ to be $\ln|x|$?

-
Could you specify in more detail what you want to achieve? –  sebhofer Oct 19 '12 at 8:52

The result you're after is only correct in the reals. Since Mathematica generally assumes that everything is complex, I'm not sure if there is a simple way to make it return the result you want.

You can go backwards and check that, for $x\in\mathbb{R}$, $\frac{d}{dx}\log|x|=\frac1x$:

D[Log[Abs[x]], x]
FullSimplify[%, x \[Element] Reals]

(*
==> Abs'[x]/Abs[x]
==> 1/x
*)


(aside: FullSimplify[Abs'[x], x \[Element] Reals] == Sign[x])

However, in general, the antiderivative of $x^{-1}$ is defined to be $\log(x)$

Integrate[1/x, x]
(*
==> Log[x]
*)


Note that $\log(x) = \log|x| + i\arg(x) = \log|x| + i(\theta+2n\pi)$, where $n$ is the winding number (see [1]) and for real $x$, $\arg(x)=m\pi$ is "locally constant". That is, if you assume that $x$ is real and non-zero, then the only difference between $\log(x)$ and $\log(-x)$ is a purely imaginary constant ($\pm i \pi$):

Simplify[Log[x] - Log[-x], #] & /@ {x > 0, x < 0}

(*
==> {-I Pi, I Pi}
*)


There was an interesting discussion about this (mainly focused on pedagogy) at the n-Category Café earlier this year.

So, in summary, I don't think there is a good way or a good reason to force the absolute value into Integrate's ouput...

-
That was a long non-answer... sorry! –  Simon Oct 19 '12 at 10:28
Hello, stranger! –  Mr.Wizard Oct 19 '12 at 11:32
@Mr.Wizard: Cheers! It's been a busy year... –  Simon Oct 20 '12 at 10:41

We can 'hack' Mathematica to get what we want.

In[1]:= Unprotect[Integrate];
ClearAttributes[Integrate, Protected];
Integrate[f_, x_Symbol] := With[{a = Cancel[x f]}, (a Log[Abs[x]]) /; FreeQ[a, x]]
Protect[Integrate];

In[5]:= Integrate[1/x, x]

Out[5]= Log[Abs[x]]


Edit

Here's a more general example:

In[6]:= Integrate[f_, x_Symbol] := Block[{useSystemIntegrate = True},
Simplify[Integrate[f, x] /. Log[expr_] :> Log[Abs[expr]], Element[x, Reals]]
] /; ! TrueQ[useSystemIntegrate]

In[7]:= Integrate[(a-2x-2a^2*x-x^2+4a*x^2-2x^3)/((a-x^2)(1+a^2-2a*x+x^2)), x]

Out[7]= -ArcTan[a - x] + Log[Abs[a - x^2]]

In[8]:= Assuming[a > 0, Integrate[(2 x)/(a + x^2), x]]

Out[8]= Log[a + x^2]

-