# Can't chop using SymPy in Julia

**URL:** <https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 3, 2017, 12:13am UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487 "2017-05-03T00:13:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [May 3, 2017, 12:13am UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/1 "2017-05-03T00:13:16Z")

</div>

I’m using SymPy in Julia and can’t use the chop function. As a result of my calculations I have a number like

> 20.663702825333246 + 2.6469779601696886e-23im

If I try to:

> chop(ans)

I get this error:

> MethodError: no method matching chop(::Complex{Float64})  
> Closest candidates are:  
> chop(::AbstractString) at strings/util.jl:39

Why isn’t it working, and how should I do it? (I’m working on Julia v0.5)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 3, 2017, 12:36am UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/2 "2017-05-03T00:36:47Z")

</div>

What do you want to do, i.e. what should `chop` do?

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [May 3, 2017, 12:56am UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/3 "2017-05-03T00:56:04Z")

</div>

As you can read from the error message, `chop` works for strings, not numbers. Is `round` what you are looking for?

```julia
julia> round(20.663702825333246 + 2.6469779601696886e-23im, 10)
20.6637028253 + 0.0im

```

From the [manual](https://docs.julialang.org/en/stable/stdlib/math/?highlight=round#Base.round):

```julia
round(x, digits) rounds to the specified number of digits after the decimal place (or before if negative).

```

---

<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:** [May 3, 2017, 2:32am UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/4 "2017-05-03T02:32:52Z")

</div>

But `round` isn’t defined properly for `Sym` Objects … Not sure what the OP wants, but perhaps `round(N(x))` or `x[:round]()` until this is addressed.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [May 3, 2017, 3:26pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/5 "2017-05-03T15:26:15Z")

</div>

In SymPy chop: “Chops off small real or imaginary parts, or converts numbers close to zero to exact zeros.” ([http://docs.sympy.org/0.7.1/modules/mpmath/general.html](http://docs.sympy.org/0.7.1/modules/mpmath/general.html)) `round` kinda does the thing, that I want, if we don’t find better solution, I will go with that.

---

<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:** [May 3, 2017, 4:14pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/6 "2017-05-03T16:14:50Z")

</div>

It isn’t pretty, and I doubt it should be nicer, but you can try `SymPy.mpmath[:chop](x,tol=1e9)`, say

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 3, 2017, 4:23pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/7 "2017-05-03T16:23:40Z")

</div>

It is unclear why you need a symbolic library, but if I understand you requirements correctly, it should be easy to implement in Julia:

```julia
numchop{T <: Real}(x::T, δ = eps(T)) = abs(x) > δ ? x : zero(T)

function numchop{T <: Real}(x::Complex{T}, δ = eps(T))
    ichop = numchop(imag(x), δ)
    rchop = numchop(real(x), δ)
    if ichop == zero(T)
        rchop
    else
        complex(rchop, ichop)
    end
end

```

Note that of course it is not type stable.

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [May 3, 2017, 4:35pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/8 "2017-05-03T16:35:27Z")

</div>

The `SymPy.mpmath[:chop](x,tol=1e9)` doesn’t work, it returns a `Complex{BigFloat}` with a bunch of zeros at the end of both the real and imaginary parts. But the `numchop` function works well, thank you for your help!

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [July 4, 2017, 9:15pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/9 "2017-07-04T21:15:03Z")

</div>

This is pretty late, but one of the first modules I wrote is [ZChop.jl](https://github.com/jlapeyre/ZChop.jl) I think it does what you want. It is similar to Mathematica’s Chop.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [July 4, 2017, 9:34pm UTC](https://discourse.julialang.org/t/cant-chop-using-sympy-in-julia/3487/10 "2017-07-04T21:34:53Z")

</div>

Try explicity wrapping the complex number with PyObject, as so `PyObject(1 + 1 im)` before calling the python function.
