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> 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

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.

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.