# SymEngine: dropping terms in a polynomial with near-zero coefficients

**URL:** <https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391>\
**Category:** General Usage\
**Created:** [November 10, 2018, 5:45pm UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391 "2018-11-10T17:45:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [November 10, 2018, 5:45pm UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/1 "2018-11-10T17:45:31Z")

</div>

I’ve been using SymEngine to get a symbolic polynomial representation of a function and have been impressed by its speed and amazed that it even works seemlessly with `BigFloat`. However, at least partially because the function I care about is based on floating point parameters parsed from a file, which were serialized with limited precision, I end up with a lot of terms with coefficients like

```julia
2.632918509595140993295845084408890582769689363958590871803066577611530482365e-116

```

I’d like to drop those terms. Is there a simple way to do that?

---

<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:** [November 11, 2018, 10:42am UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/2 "2018-11-11T10:42:30Z")

</div>

This might help:

EDIT: No, `zchop` on an expression returned by `SymEngine` throws a `StackOverflow`. This is a bug in `ZChop`.

> **[GitHub - jlapeyre/ZChop.jl: replace small numbers with zero](https://github.com/jlapeyre/ZChop.jl)**
>
> replace small numbers with zero. Contribute to jlapeyre/ZChop.jl development by creating an account on GitHub.

---

<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:** [November 11, 2018, 11:12am UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/3 "2018-11-11T11:12:49Z")

</div>

But, this doesn’t look good to me:

```julia
julia> using SymEngine;

julia> a, b = symbols("a b");

julia> expr = a * b
a*b

julia> typeof(expr)
Basic

julia> Base.isiterable(Basic)
true

julia> for x in expr
       global z = x
       println(x)
       end
a*b

julia> z === expr
true

```

This is consistent with iteration over objects whose type is a subtype of `Number`. It follows from

```julia
julia> supertype(Basic)
Number

```

Even so, it’s counterintutive.

---

<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:** [November 11, 2018, 12:04pm UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/4 "2018-11-11T12:04:01Z")

</div>

> [@jlapeyre](#):
>
> No, `zchop` on an expression returned by `SymEngine` throws a `StackOverflow` . This is a bug in `ZChop`

I fixed this bug on the master branch of `ZChop`. `zchop(x::Number)` now returns `x` if `x` is neither `Real` nor `Complex`.

Unfortunately this does not provide a solution to the question. And I was unable to find documents or examples related to accessing parts of expressions in `SymEngine`.

---

<div class="post-metadata">

**Author:** ![isuruf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isuruf/32/106_2.png) [@isuruf](https://discourse.julialang.org/u/isuruf)\
**Post date:** [November 11, 2018, 6:00pm UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/5 "2018-11-11T18:00:22Z")

</div>

Here’s a prototype that you can improve for your needs. (I’d like to get rid of if/elseif/else after implementing [Add a method to create a object from class id and args · Issue #1478 · symengine/symengine · GitHub](https://github.com/symengine/symengine/issues/1478))

```julia
function chop_expr(x::Basic)
  cls = SymEngine.get_symengine_class(x)
  args = [chop_expr(arg) for arg in get_args(x)]
  if cls == :Add
     return sum(args)
  elseif cls == :Mul
     return prod(args)
  elseif cls == :Pow
     return args[1]^args[2]
  elseif cls in [:RealDouble, :RealMPFR, :ComplexDouble, :ComplexMPFR]
     y = N(x)
     # Do chopping here
     if abs(y) <= 1e-100
        y = 0
     end
     return Basic(y)
  else
     return x
  end
end

```

Btw, if all you are using are polynomials, you’ll find that SymEngine is slow for polynomials and a specialized polynomial library will be much faster.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [November 11, 2018, 7:14pm UTC](https://discourse.julialang.org/t/symengine-dropping-terms-in-a-polynomial-with-near-zero-coefficients/17391/6 "2018-11-11T19:14:57Z")

</div>

Great, thank you very much. @jlapeyre, thanks as well.
