# SymPy.jl analogue of \`as\_dict\` and \`gens\`

**URL:** <https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972>\
**Category:** General Usage\
**Tags:** package, jump, python\
**Created:** [January 4, 2020, 6:05pm UTC](https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972 "2020-01-04T18:05:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdp](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@tdp](https://discourse.julialang.org/u/tdp)\
**Post date:** [January 4, 2020, 6:05pm UTC](https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972/1 "2020-01-04T18:05:40Z")

</div>

Hi,  
What are the SymPy.jl analogues for `as_dict` and `gens`? See below for a minimal piece of code I would like to translate from SymPy to SymPy.jl. This is part of a larger project I am translating from Python to Julia which involves sum of squares of polynomials. Thanks in advance for any help!

```julia
from sympy import poly

poly = poly('5*y **7 + 3*x** 2 + 2*x*y**2 + 1')
print(poly.as_dict()) # {(0, 0): 1, (0, 7): 5, (1, 2): 2, (2, 0): 3}
print(poly.gens) # (x, y)

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [January 4, 2020, 9:30pm UTC](https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972/2 "2020-01-04T21:30:48Z")

</div>

> [@tdp](#):
>
> poly = poly(‘5_y\*\*7 + 3_x **2 + 2_x_y** 2 + 1’)

It is quite similar:

```julia
julia> p = sympy.poly("5*y^7 + 3*x^2 + 2*x*y^2 + 1")
Poly(3*x **2 + 2*x*y** 2 + 5*y**7 + 1, x, y, domain='ZZ')

julia> p.as_dict()
Dict{Any,Any} with 4 entries:
  (0, 0) => 1
  (1, 2) => 2
  (2, 0) => 3
  (0, 7) => 5

julia> p.gens
(x, y)

```

---

<div class="post-metadata">

**Author:** ![tdp](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@tdp](https://discourse.julialang.org/u/tdp)\
**Post date:** [January 5, 2020, 3:20am UTC](https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972/3 "2020-01-05T03:20:47Z")

</div>

Thank you for your solution. I was constructing my polynomials like so:

```julia
using SymPy
@vars x y
p = 5 * y^7 + 3 * x^2 + 2 * x*y^2 + 1

```

for which both `p.as_dict` and `p.gens` give errors.

This leads me to another question: **why does the last line in the code below return `false`?**

```julia
using SymPy
@vars x y
p = 5 * y^7 + 3 * x^2 + 2 * x*y^2 + 1

typeof(p) == typeof(sympy.poly(p)) # true
p == sympy.poly(p) # false

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [January 5, 2020, 4:09am UTC](https://discourse.julialang.org/t/sympy-jl-analogue-of-as-dict-and-gens/32972/4 "2020-01-05T04:09:21Z")

</div>

The Poly class in SymPy is different than a symbolic expression, that happens to represent a polynomial. You’ll need the constructor. The typeof will be the same (Sym objects), but they aren’t identical.
