sympy: how to evaluate integral of absolute value
<p>When I try to simplify the following integral in <code>sympy</code>, it will not evaluate, i.e. the output is $\int_{-1}^1 |z| dz$ while the output I expect is 1.</p>

<pre><code>z = symbols('z', real=True)
a = integrate(abs(z), (z, -1, 1))
simplify(a)
</code></pre>

<p>Similar integral without the absolute value on <code>z</code> will evaluate.</p>

<p>How can I get <code>sympy</code> to evaluate this integral?</p>

<p><code>integrate</code> already does all it can to evaluate an integral. If you get an <code>Integral</code> object back, that means it couldn't evaluate it. The only thing that might help is rewriting the integrand in a way that SymPy can recognize. </p>

<p>Looking at <a href="https://github.com/sympy/sympy/issues/8430" rel="nofollow">this issue</a>, it looks like a workaround is to rewrite it as Heaviside:</p>

<pre><code>In [201]: z = symbols('z', real=True)

In [202]: a = integrate(abs(z).rewrite(Heaviside), (z, -1, 1))

In [203]: a
Out[203]: 1
</code></pre>

python sympy integral absolute-value