# Obtaining coefficients of polynomial with SymEngine.jl

**URL:** <https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371>\
**Category:** General Usage\
**Tags:** question, package, symbolic\
**Created:** [January 5, 2024, 4:56am UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371 "2024-01-05T04:56:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MAB](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@MAB](https://discourse.julialang.org/u/MAB)\
**Post date:** [January 5, 2024, 4:56am UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/1 "2024-01-05T04:56:35Z")

</div>

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`](https://docs.juliahub.com/Symbolics/eABRO/5.5.0/autodocs/#Symbolics.polynomial_coeffs-Tuple%7BAny,%20Any%7D)

but I would rather use SymEngine because of speed of differentiation issues (see [Symbolics.jl: Speed of derivative computations vs. SymEngine.jl](https://discourse.julialang.org/t/symbolics-jl-speed-of-derivative-computations-vs-symengine-jl/68803))

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 5, 2024, 6:41pm UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/2 "2024-01-05T18:41:08Z")

</div>

you could call yourself a function with something like this

```julia

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)

```

---

<div class="post-metadata">

**Author:** ![MAB](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@MAB](https://discourse.julialang.org/u/MAB)\
**Post date:** [January 5, 2024, 8:15pm UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/3 "2024-01-05T20:15:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 5, 2024, 8:24pm UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/4 "2024-01-05T20:24:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 5, 2024, 8:31pm UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/5 "2024-01-05T20:31:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MAB](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@MAB](https://discourse.julialang.org/u/MAB)\
**Post date:** [January 5, 2024, 11:11pm UTC](https://discourse.julialang.org/t/obtaining-coefficients-of-polynomial-with-symengine-jl/108371/6 "2024-01-05T23:11:56Z")

</div>

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

> <https://github.com/symengine/symengine/blob/master/symengine/cwrapper.h>

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