# Convert polynomial to symbolic expression

Dear all,

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

``````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:

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

but I get the following error:

``````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!

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

``````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
``````
3 Likes

Thanks so much! Sorry for the incomplete code

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:

``````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)))
``````
1 Like