# Symply.jl for getting coefficients for all combination of the variables of a multivariable polynomial

**URL:** <https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [October 21, 2022, 11:10pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091 "2022-10-21T23:10:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 21, 2022, 11:10pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/1 "2022-10-21T23:10:19Z")

</div>

Dear all,

Does anyone know how to get coefficients for ALL combination of the variables of a multivariable polynomial using sympy.jl or other Julia package for symbolic computation?

Here is an example from MATLAB,

syms a b y  
[cxy, txy] = coeffs(a_x^2 + b_y, [y x], ‘All’)  
cxy =  
[0, 0, b]  
[a, 0, 0]  
txy =  
[x^2_y, x_y, y]  
[x^2, x, 1]

As you can see it returns all combination of the variables with 0 coefficients.

I know the Python package Sympy has a function “all\_coeffs”, but it cannot handle multivariable polynomials and is not available at sympy.jl as far as I know.

Cheers,  
Sean

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 21, 2022, 11:24pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/2 "2022-10-21T23:24:22Z")

</div>

The choice of appropriate CAS (computer algebra system) really depends on the type of math you will be working with. Nemo/Oscar in Julia is good for abstract math projects, Symbolics (or Sympy) is useful for generic symbolic manipulations over real/complex numbers.

In Nemo you can do what you want as follows:

```julia
julia> using Nemo # or the basic, unoptimized, pure-julia AbstractAlgebra

julia> R, (x,y) = PolynomialRing(RealField(), ["x", "y"])
(Multivariate Polynomial Ring in x, y over Real Field with 256 bits of precision and error bounds, AbstractAlgebra.Generic.MPoly{arb}[x, y])

julia> f = x*x*3+3*y*x;

julia> [coeff(f, [x,y], [i,j]) for i in 0:2 for j in 0:2]
9-element Vector{AbstractAlgebra.Generic.MPoly{arb}}:
 0
 0
 0
 0
 3.00000000000000000000000000000000000000000000000000000000000000000000000000000
 0
 3.00000000000000000000000000000000000000000000000000000000000000000000000000000
 0
 0

```

You can use a different field, to specify Rational numbers, or only Integers, or finite precision Floats. Here I am using arbitrary precision BigFloats (the default in RealField).

Check these pages:

- [Univariate polynomial functionality · AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/latest/polynomial/)
- [Sparse distributed multivariate polynomials · AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/latest/mpolynomial/#Multivariate-coefficients)
- [Real field · AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/latest/real/)

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 22, 2022, 12:06am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/3 "2022-10-22T00:06:05Z")

</div>

Thank you for the prompt and informative reply.  
I am dealing with derivatives, getting coefficients and terms and other manipulations of a 500\*500 symbolic matrix. The MATLAB code is way too slow for “gradient”, “coeffs”, and for loop for such a big symbolic matrix. That is why I am shifting to sympy.jl or symengine.jl.

I see that Nemo can deal with coefficients for multivariable polynomials. How do you get ALL the terms x^3y, x^2, xy, x, y, 1 using Nemo?  
Also, based on your experience, would Nemo be the candidate for the manipulation of 500\*500 symbolic matrix with polynomial elements? The MATLAB couldn’t return a result after 24 hours wall-clock time with 64 CPUs.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 22, 2022, 12:19am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/4 "2022-10-22T00:19:30Z")

</div>

If you are going to do code generation out of that matrix, I would probably suggest Symbolics.jl.

If you know that all you are going to do is polynomials, especially to high orders and of not too many variables, and only derivatives on them, then Nemo.jl probably would be the fastest. Especially if you know the polynomials would only be over integers or rationals.

Check this page for most available functionality in Nemo/Oscar/AbstractAlgebra: [Sparse distributed multivariate polynomials · AbstractAlgebra.jl](https://nemocas.github.io/AbstractAlgebra.jl/stable/mpolynomial/)

To get all terms, you probably need to make your own function using the `degrees` function to find the highest term in the polynomial and then the `coeff` function to extract all the coefficients.

If you use Symbolics.jl instead, the API is probably different.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [October 22, 2022, 1:33pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/5 "2022-10-22T13:33:02Z")

</div>

I am still not sure what “getting all the terms” mean. Given `f = 2*x^3 + x^2 * y + y`, what do you want it to return? Unfortunately I don’t speak MATLAB. Do you just want the list `[2*x^3, x^2 * y, y]`? Or do you want the coefficients of `f` for some fixed list of monomials like `[1, y, x, x*y, x^2, x^2*y]`? If so, for which monomials?

Nemo is happy with 500x500 matrices, depending on what one wants to do.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 22, 2022, 3:49pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/6 "2022-10-22T15:49:13Z")

</div>

Only now I am noticing that you seem to want polynomials with symbolic coefficients. This is not really the type of task most “abstract math” algebra systems are best at: for them polynomials are over a specific ring of coefficients, not over wildcard symbols. That enables all the interesting **fast** mathematical operations (e.g. in cryptography) that people care about.

If you just want expressions over many symbols where only addition, multiplication, and exponentiation by an integer are used, then Symbolics is probably going to be easier to use. But that might not actually use a special-purpose “polynomial” datastructure, rather a generic “symbolic expression tree” datastructure.

The whole “symbolic coefficients in my polynomial” kinda throws a wrench in the common tools (at least if you want to be fast – if you do not care about speed, there is always a solution). Maybe you can share a bit more about the actual problem you want to solve and why you need symbolic coefficients in your polynomials?

---

<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:** [October 22, 2022, 5:45pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/7 "2022-10-22T17:45:48Z")

</div>

I don’t know how well this scales, but if you want to try SymPy for this task, I think this is doing what you want:

```julia
using SymPy

@syms x[1:10]
Σ = zero(Sym)
for i ∈ 1:9
    Σ += i * x[i] * x[i+1] # some arbitrary polynomial expression
end

p = Σ.as_poly()
xs = p.gens
ds = p.as_dict()

d = Dict( prod(filter(!iszero, k .* xs)) => v for (k, v) ∈ ds)

```

---

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [October 23, 2022, 8:16am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/8 "2022-10-23T08:16:47Z")

</div>

> I know the Python package Sympy has a function “all\_coeffs”, but it cannot handle multivariable polynomials

Sympy.py can handle multivariate polynomials in the manner you require.  
Please refer to the link below for an example.

> and is not available at sympy.jl as far as I know.

Have used Sympy.py for a number of years, recently switched to the all-Julia package Symbolics.jl for symbolic computation. Speed of execution was one deciding factor.

Suggest reading

> <https://github.com/JuliaSymbolics/Symbolics.jl/issues/699>
>
> Hello, 
> 
> Please find below a Sympy MWE that describes my question in the title…:
> 
> \`\`\`
> import sympy as sp
> 
> def main():
> 
> x, y = sp.symbols('x, y')
> a22, a21, a12, a11, a10, a01, a00 = sp.symbols('a22, a21, a12, a11, a10, a01, a00')
> b60, b53, b32, b21, b10, b00 = sp.symbols('b60, a53, a32, b21, b10, b00')
> 
> expr\_1 = a22\*x\*\*2\*y\*\*2 + a21\*x\*\*2\*y + a12\*x\*y\*\*2 + a11\*x\*y + a10\*x + a01\*y + a00
> expr\_2 = b60\*x\*\*6 + b53\*x\*\*5\*y\*\*3 + b32\*x\*\*3\*y\*\*2 + b21\*x\*\*2\*y + b10\*x + b00
> 
> expr = expr\_1 + expr\_2
> 
> poly\_coeffs = sp.poly(expr, \[x, y\]).coeffs(order='grevlex')\[::-1\]
> 
> poly\_monoms = sp.poly(expr, \[x, y\]).monoms(order='grevlex')\[::-1\]
> 
> print(f'expr = {expr} \\n')
> print(f'expr coefficients: {len(poly\_coeffs)} {poly\_coeffs} \\n')
> print(f'expr monomials: {len(poly\_monoms)} {poly\_monoms} \\n')
> 
> 
> if \_\_name\_\_ == "\_\_main\_\_":
> main()
> \`\`\`
> 
> gives the results
> 
> \`\`\`
> expr = a00 + a01\*y + a10\*x + a11\*x\*y + a12\*x\*y\*\*2 + a21\*x\*\*2\*y + a22\*x\*\*2\*y\*\*2 + a32\*x\*\*3\*y\*\*2 + a53\*x\*\*5\*y\*\*3 + b00 + b10\*x + b21\*x\*\*2\*y + b60\*x\*\*6 
> 
> expr coefficients: 10 \[a00 + b00, a01, a10 + b10, a11, a12, a21 + b21, a22, a32, b60, a53\] 
> 
> expr monomial degrees: 10 \[(0, 0), (0, 1), (1, 0), (1, 1), (1, 2), (2, 1), (2, 2), (3, 2), (6, 0), (5, 3)\] 
> \`\`\`
> 
> Are similar functions currently available in Symbolics.jl?
> A search of Symbolics.jl issues did turn up the following discussion:
> 
> https://github.com/JuliaSymbolics/Symbolics.jl/pull/677
> 
> However, a search for "coefficient" in the Symbolics.jl documentation only gave a link to Gröbner bases.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [October 23, 2022, 9:11am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/9 "2022-10-23T09:11:22Z")

</div>

If it is just about getting all the non-zero terms, you can do this as follows in AbstractAlgebra/Nemo:

```julia
julia> using Nemo; # using AbstractAlgebra would also work

julia> Qxy, (x, y) = PolynomialRing(QQ, ["x", "y"])
(Multivariate Polynomial Ring in x, y over Rational Field, fmpq_mpoly[x, y])

julia> f = sum(rand(-2:2) * x^rand(0:2) * y^rand(0:2) for i in 1:10)
x^2*y - x^2 + 2*x*y^2 + y^2 - 4*y

julia> collect(coefficients(f))
5-element Vector{fmpq}:
 1
 -1
 2
 1
 -4

julia> collect(exponent_vectors(f))
5-element Vector{Vector{Int64}}:
 [2, 1]
 [2, 0]
 [1, 2]
 [0, 2]
 [0, 1]

```

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 24, 2022, 4:48am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/10 "2022-10-24T04:48:13Z")

</div>

Indeed, I want polynomials with symbolic coefficients. For simplicity, let me share a TOY version of my problem. Considering solving an equation of d **x** /dt = A **x** , where **x** is a symbolic vector with polynomial elements,  
**x** = [a1_x1^2+a2_x2^2+a3_x3^2+a4_x1_x2+a5_x2_x3+1, b1_x1^2+b2_x2^2+b3_x3^2+b4_x1_x2+b5_x2_x3+1,  
c1_x1^2+c2_x2^2+c3_x3^2+c4_x1_x2+c5_x2\*x3+1 ]

1. First, I need to get ALL the symbolic coefficients, a1,…a5, b1,…b5, and c1, …, c5 and the terms x1^2_x2^2_x3^2, …, 1.
2. Then I define another vector **y** =[y1, y2, y3, …,y9] such that y1=x1^2, y2=x2^2, y3=x3^2, y4=x1\*x2, …, y9=1.
3. I construct a new linear equation of **y** using the chain rule , d **y** /dt=(d **y** /d **x** )\*(d **x** /dt) = B **y** , where B = (d **y** /d **x** ).

My goal is to obtain a symbolic matrix B, this means I need to derive d **y** /d **x** symbolically. The main operations are calculating the symbolic derivatives and obtaining the coefficients.

My real problem deals with a much larger **x** and higher order.  
The parallel MATLAB is way too slow, so I am switching to Julia symbolic computation.  
@Krastanov, @thofma, speed (parallelisation) is exactly what I need.

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 24, 2022, 4:52am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/11 "2022-10-24T04:52:37Z")

</div>

Thanks. I am also experimenting Symbolics.jl. Speed and parallelisation are exactly what I need. I’ve now posted my exact problem below (sorry I don’t know how to point you to my post, hope you can see it), any tips will be highly appreciated.

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 24, 2022, 5:03am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/12 "2022-10-24T05:03:44Z")

</div>

Thanks. This seems to leave out coefficients of first order, i.e., x₁, x₁₀, x₂, x₃, x₄, x₅, x₆, x₇, x₈, x₉.  
I need to try out the scaling. The speed is what I am after.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 24, 2022, 5:07am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/13 "2022-10-24T05:07:10Z")

</div>

Because of the need for symbolic coefficients, I would suggest using Audrius-St suggestion above with Symbolics.jl. Its general purpose symbolic expression structure would not be as fast as the polynomial structures from Nemo, but it would probably be necessary in order to deal with the unconstrained coefficients.

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 24, 2022, 5:10am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/14 "2022-10-24T05:10:25Z")

</div>

My coefficients are all symbolic. Hope that works with PolynomialRing. I’ve used sympy.jl to derive my vector f. I guess I need to convert f to PolynomialRing and then perform collect(coefficients(f)) and collect(exponent\_vectors(f)).

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 25, 2022, 1:34am UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/15 "2022-10-25T01:34:42Z")

</div>

I am switching to Symbolics.jl now.  
In your GitHub post,  
How do you get all the terms as a vector, i.e., [x **2\*y** 2, x **2_y, x_y** 2, x\*y, x, y, 1]?  
Same for the expr\_coeffs as [a22, a21, a12, a11, a10, a01, a00].  
I’ve got my matrix using Julia and now trying to get the symbolic coefficients and the corresponding terms in the form of a vector, respectively.

---

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [October 25, 2022, 7:53pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/16 "2022-10-25T19:53:17Z")

</div>

```julia
# test_symbols_vector.jl

using Symbolics

begin
    # [x2*y2, x2y, xy2, x*y, x, y, 1]
    @variables x, y, x2, y2, x2y, xy2

    variables_vec = Vector{Num}(undef, 7)

    # Below you would instead loop over the indices of the matrix

    variables_vec[1] = x2*y2
    variables_vec[2] = x2y
    variables_vec[3] = xy2
    variables_vec[4] = x*y
    variables_vec[5] = x
    variables_vec[6] = y
    variables_vec[7] = 1

    @show variables_vec

end

```

Output:

```julia
variables_vec = Num[x2*y2, x2y, xy2, x*y, x, y, 1]
7-element Vector{Num}:
 x2*y2
   x2y
   xy2
   x*y
     x
     y
     1

```

---

<div class="post-metadata">

**Author:** ![xyli](https://avatars.discourse-cdn.com/v4/letter/x/d78d45/32.png) [@xyli](https://discourse.julialang.org/u/xyli)\
**Post date:** [October 25, 2022, 10:01pm UTC](https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091/17 "2022-10-25T22:01:10Z")

</div>

Thanks. I just figured it out. The terms and coefficients can be collected from the returned dictionary “coeffs” from  
Julia\> coeffs, \_ = polynomial\_coeffs()
