Obtaining coefficients of polynomial with SymEngine.jl

I am trying to obtain a dictionary that contains monomials (keys) and coefficients (values) for a multi-variate polynomial in SymEngine. This function exists in Symbolics.jl

Symbolics.polynomial_coeffs

but I would rather use SymEngine because of speed of differentiation issues (see Symbolics.jl: Speed of derivative computations vs. SymEngine.jl)

This functionality is I believe built in SymEngine, but I cannot find a way to access it in Julia.
In Python, with the SymEngine wrapper, we can use

import symengine as se
x = se.symarray("x", 3)
p = 6*x[0]*x[1]*x[2]+2*x[2]**2
p.as_coefficients_dict()

which returns

defaultdict(int, {1: 0, x_2**2: 2, x_1*x_0*x_2: 6})

Is there a way to do the same in Julia?

you could call yourself a function with something like this


using SymEngine

@vars a

pol1=1+2*a+3*(a*a)+4*a^3


subs.(diff.(pol1,a,0:3),a=>0)./factorial.(0:3)

This would be ungodly slow for univariate polynomials, and far worse for multi-variate ones.

SymEngine keeps an internal representation as a dictionary, I just cannot find much in terms of documentation on the package SymEngine.jl to know how to access it.

I had no idea that using symbolic systems there was also a need for speed.
However the function that finds the coefficients should not be much slower than diff() and subs()

I remember needing that too a while ago and I then went looking for it in the SymEngine repo. IIRC then the relevant function from SymEngine was/is not yet available via their C interface and one would need to make a PR in the C++ project first.
I ended up solving my problem in another way, so I did not follow up on that. But it would be great to add that functionality one day.

1 Like

I see – do I understand correctly that the Julia wrappers are based on the C wrappers to the C++ project, but the Python wrappers are not based on the C wrappers?
If the C wrappers the Julia implementation are based on are these

then obtaining the dictionary of coefficients is not yet implemented, as far as I can tell. Thanks, this clarified things quite a bit.