# Convert polynomial to symbolic expression

**URL:** <https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 28, 2022, 9:29am UTC](https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908 "2022-09-28T09:29:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@Luca](https://discourse.julialang.org/u/Luca)\
**Post date:** [September 28, 2022, 9:29am UTC](https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908/1 "2022-09-28T09:29:34Z")

</div>

Dear all,

I’m using LinearAlgebra and Symbolics package. I have a polynomial created by

```julia
for ivar in 1:num_vars
    push!(variables, "x[" * string(ivar) * "]")
end
global ZZ = RealField
global R, = PolynomialRing(ZZ, variables)
global C = MPolyBuildCtx(R)
push_term!(C, ZZ(coefficient), exponent);
global objFunct = finish(C)

```

Now I would like to use Symbolics to compute derivative of `sin(objFunct)`. I implemented:

```julia
Symbolics.@variables x[1:num_vars]
f = objFunct
f = sin(f)

```

but I get the following error:

```julia
ERROR: LoadError: MethodError: no method matching sin(::AbstractAlgebra.Generic.MPoly{BigFloat})

```

What I have to do? I tried to convert `objFunct` to String and this String to expression, but it does not work. All your suggestions are really appreciated. Thanks very much!

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 28, 2022, 11:27am UTC](https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908/2 "2022-09-28T11:27:56Z")

</div>

Please post code that works if it is copied and pasted.

```julia
using AbstractAlgebra, Symbolics

R, (x, y) = PolynomialRing(ZZ, ["x", "y"])

C = MPolyBuildCtx(R)
push_term!(C, ZZ(3), [1, 2]);
push_term!(C, ZZ(2), [1, 4]);

f = finish(C) # 2xy^4 + 3xy^2

exponents = f.exps
coeffic = f.coeffs

# Symbolics.jl expression 
symbolics_vars = Symbolics.variable.(f.parent.S) |> reverse
symb_f = sum(coeffic[i] * reduce(*, symbolics_vars.^(exponents[:, i])) for i in axes(exponents, 2))
# symb_f = 3y^2*x+2y^4*x

# work with symb_f  
Symbolics.gradient(sin(symb_f), symbolics_vars)

```

---

<div class="post-metadata">

**Author:** ![Luca](https://avatars.discourse-cdn.com/v4/letter/l/51bf81/32.png) [@Luca](https://discourse.julialang.org/u/Luca)\
**Post date:** [September 28, 2022, 11:46am UTC](https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908/3 "2022-09-28T11:46:33Z")

</div>

Thanks so much! Sorry for the incomplete code

---

<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:** [September 29, 2022, 6:34am UTC](https://discourse.julialang.org/t/convert-polynomial-to-symbolic-expression/87908/4 "2022-09-29T06:34:26Z")

</div>

It is a shame that `f(symbolics_vars...)` resp. `evaluate(f, symbolics_vars)` is not supported (the blame is on us). Anyway, the following is a version of your code which uses the official interface of accessing exponents and coefficients, which should be more reliable:

```julia
using AbstractAlgebra, Symbolics

R, (x, y) = PolynomialRing(ZZ, ["x", "y"])

C = MPolyBuildCtx(R)
push_term!(C, ZZ(3), [1, 2]);
push_term!(C, ZZ(2), [1, 4]);

f = finish(C) # 2xy^4 + 3xy^2

symbolics_vars = Symbolics.variable.(symbols(parent(f))) |> reverse

symb_f = sum(c * reduce(*, symbolics_vars.^e) for (c, e) in zip(coefficients(f), exponent_vectors(f)))

```
