# Conversion from symbolic expression to polynomial stuck

I have a very long symbolic expression with six variables:

(E_mu, E_xi3, ISP, T, V_mu, V_xi3, Z, m, mu, n_I, n_P, xi_1, xi_3)

I call the expression AN. I want to convert it in a polynomial of two variables (mu and xi_3). I tried the following command:

AP = AN.polynomial(None,ring=SR['mu,xi_3'])


This works for simple expressions but the command remains stuck for the given expression. My aim is to get the monomials of the polynomial. mu and xi_3 are random variables and I want to compute the expectation of AN (E[AN]). Thus, I could substitute the expectations in the expression.

AN is a numerator, so it is not a fraction. Moreover, I also tried simply_rational but it did not help to convert.

Is there anyway to convert easily ?

EDIT : https://framabin.org/p/?2417942055fc9fc9#Oenpb+BI567QXYcVCzjO4zkLRC0byQaNdhi2XW/CUkY= (I put the symbolic expression in a framabin.)

edit retag close merge delete

1

Could you please provide the (construction of the) symbolic expression ?

https://framabin.org/p/?2417942055fc9fc9#Oenpb+BI567QXYcVCzjO4zkLRC0byQaNdhi2XW/CUkY= (I added a framabin where you can read the symbolic expression.)

Sort by » oldest newest most voted Since the whole expression is a polynomial with rational coefficients, the following works:

sage: R = PolynomialRing(PolynomialRing(QQ, names='E_mu, E_xi3, ISP, T, V_mu, V_xi3, Z, m, n_I, n_P, xi_1'), names='mu, xi_3')
sage: map(SR, R(AN).monomials())
[mu^2, mu*xi_3, mu, xi_3, 1]
sage: map(SR, R(AN).coefficients())
[T^2*m^6 + 4*T^2*m^5*n_I + 4*T^2*m^4*n_I^2 + 2*T^2*m^5*n_P + 6*T^2*m^4*n_I*n_P + 4*T^2*m^3*n_I^2*n_P + T^2*m^4*n_P^2 + 2*T^2*m^3*n_I*n_P^2 + T^2*m^2*n_I^2*n_P^2 + 2*T*V_mu*m^4*n_I + 2*T*V_xi3*m^4*n_I + 4*T*V_mu*m^3*n_I^2 + 4*T*V_xi3*m^3*n_I^2 + ...


For expressions involving fractions you would want to replace the inner PolynomialRing by wrapping it in FractionField(...), like this:

sage: R = PolynomialRing(FractionField(PolynomialRing(QQ, names='E_mu, E_xi3, ISP, T, V_mu, V_xi3, Z, m, n_I, n_P, xi_1')), names='mu, xi_3')

more

https://doc.sagemath.org/html/en/reference/rings/sage/rings/fraction_field_element.html (I found the page about the Fraction Field Elements.)

AN is my numerator and AD is my denominator of my symbolic expression A. There is not the values of interest (mu and xi_3) in the denominator AD. How should I wrap the whole in the Fraction Field ?

BTW, thank you very much :)

I added the precise instruction to my answer. You should then be able to do R(A) etc. The coefficients of the polynomial R(A) will then be Fraction Field Elements. You're welcome :)