Truncate a polynomial expression up to a given order using SymPy in Julia
The snippet can be accessed without any authentication.
Authored by
LEJAY Antoine
Edited
truncate_polynomial.jl 300 B
using SymPy
# Keep only terms of a given orders in a polynomial expression
function truncate_upto(ex::Sym, x::Sym, order::Int)
if ex.func.__name__ != "Add"
error("The main expression should be an addition")
end
return ex.func(ex.args[findall(y->degree(y,x) ≤ order,ex.args)]...)
end
Please register or sign in to comment