0

I'd like to get:

$$\int \frac{1}{x} dx = \log |x|$$

in Mathematica. I don't know how impose that $x$ is real. If I run:

Integrate[1/x,x]

Mathematica gives me $\log (x)$ without absolute value because it considers $x$ as a complex number.

Thank you in advance for your help.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Gennaro Arguzzi
  • 959
  • 6
  • 16

1 Answers1

2

You can define your own function to do so:

realIntegrate[f_, x_Symbol] := 
  Simplify[Integrate[f, x] /. Log[expr_] :> Log[Abs[expr]], x ∈ Reals]

realIntegrate[1/x, x]

(* Log[Abs[x]] *)

realIntegrate[2x/(x^2 - 1), x]

(* Log[Abs[x^2 - 1]] *)

realIntegrate[2x/(x^2 + 1), x]

(* Log[x^2 + 1] *)

Or if you need Integrate to do this, you can overload its definition:

Unprotect[Integrate];

Integrate[f_, x_Symbol] /; !TrueQ[$flag] := 
  Block[{$flag = True},
    realIntegrate[f, x]
  ]

Protect[Integrate];

Integrate[1/x, x]

(* Log[Abs[x]] *)

Integrate[2x/(x^2 - 1), x]

(* Log[Abs[x^2 - 1]] *)

Integrate[2x/(x^2 + 1), x]

(* Log[x^2 + 1] *)
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136