# Why the integral of |x| is not exact? with SymPy

**URL:** <https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705>\
**Category:** General Usage\
**Tags:** package\
**Created:** [November 23, 2022, 1:07pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705 "2022-11-23T13:07:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 1:07pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/1 "2022-11-23T13:07:31Z")

</div>

Dear all,

I have tried `SymPy` to compute indefinite integral of |x|, this is the code:

```julia
using SymPy

# we can also use @vars x y z
x = symbols("x")
integrate(abs(x))

```

but the result is like this at terminal, different from the textbook solution:  
 ![Capture d’écran_2022-11-23_20-04-32](https://global.discourse-cdn.com/julialang/original/3X/2/6/26d4deca35a88d09a4f3f97bb5b78664d9d9098c.png)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 23, 2022, 2:03pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/2 "2022-11-23T14:03:29Z")

</div>

Please post code and REPL output as text. And what do `include("plot1.jl")` and `include("plot.jl")` have to do with anything?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 23, 2022, 2:26pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/3 "2022-11-23T14:26:47Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> ```julia
> x = symbols("x")
> integrate(abs(x))
> 
> ```

You need to add an assumption, as is implicit in the textbook:

```julia
julia> @syms x y::real
(x, y)

julia> integrate(abs(x),x)
⌠       
⎮ │x│ dx
⌡       

julia> integrate(abs(y),y)
⎧ 2            
⎪-y             
⎪──── for y ≤ 0
⎪ 2             
⎨               
⎪ 2            
⎪ y             
⎪ ── otherwise
⎩ 2             

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 2:37pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/4 "2022-11-23T14:37:01Z")

</div>

Thanks a lot! I do not know in Julia code I have to do that… put another line for y

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 23, 2022, 2:38pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/5 "2022-11-23T14:38:57Z")

</div>

It is testing the code I name it `plot.jl`, and `plot1.jl` is for other code. Sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [November 23, 2022, 2:39pm UTC](https://discourse.julialang.org/t/why-the-integral-of-x-is-not-exact-with-sympy/90705/6 "2022-11-23T14:39:03Z")

</div>

The point was to show you needed the assumption (::real did that, though you could do it with keywords to symbols).
