polynomial division, irrational coefficients

51 views
Skip to first unread message

Albert Schueller

unread,
Nov 16, 2018, 8:08:11 AM11/16/18
to sympy
Given this expression in the symbol 'h': expression = (16*sqrt(3)*h**3 - 36*h**2 + 9)/(6*(4*sqrt(3)*h**2 - 12*h + 3*sqrt(3)))

which simplifies to the expression: 2*h/3 + sqrt(3)/6

I can't figure out how to get sympy to do this reduction. If 'n' is the numerator and 'd' is the denominator, then

n,d = fraction(expression)
div(n,d)

gives

(2*h/3, 12*h**2 - 12*sqrt(3)*h + 9)

Adding, domain=RR, gives:

 (0.666666666666667*h + 0.288675134594813, 0)

which is the floating point representation of the reduced function, but it is inexact. The presence of the sqrt(3) is the issue. Is there a way to get sympy to do this reduction and keep the sqrt(3), i.e. stay in closed form, not floating point?

Thanks,

Albert 

Kalevi Suominen

unread,
Nov 16, 2018, 8:27:08 AM11/16/18
to sympy
The polynomial expressions should first be converted to SymPy polynomials setting the keyword `extension` to indicate that the algebraic number `sqrt(3)` shall be considered as a coefficient. Then the quotient of the polynomials can be computed, using polynomial methods, and finally converted back to an expression if desired.

>>> p = Poly(16*sqrt(3)*h**3 - 36*h**2 + 9, extension=True)
>>> q = Poly(6*(4*sqrt(3)*h**2 - 12*h + 3*sqrt(3)), extension=True)
>>> p.quo(q)
Poly(2/3*h + sqrt(3)/6, h, domain='QQ<sqrt(3)>')
>>> _.as_expr()
2*h/3 + sqrt(3)/6

Kalevi Suominen
Reply all
Reply to author
Forward
0 new messages