# factor() issue with second degre polynomes

Hello, In a sagenb worksheet, I'am trying to factor two 2° polynomes R and H as follow:

from sage.misc.latex import pretty_print
x=var('x')
R(x)=9*(x-5)**2-4
f3=R.factor()
H(x)=2*(x+5)**2-4
f4=H.factor()
pretty_print('R(x)=',f3)
pretty_print('H(x)=',f4)


I get :

R(x)=(3x-17)(3x-13) as waited
but H(x)=2x**2+20x+46


I can develop the factorized H as:

test=(sqrt(2)*(x+5)-2)*(sqrt(2)*(x+5)+2)


which yields:

2x**2+20x+46


Is there a kind of "radical_factor()" ? Thank you

edit retag close merge delete

Sort by » oldest newest most voted

If you don't know the roots, you can use H.roots(). For example:

sage: H=2*(x+5)**2-4
sage: H.roots()
[(-sqrt(2) - 5, 1), (sqrt(2) - 5, 1)]
sage: prod((x-_[0])^_[1] for _ in H.roots())
(x - sqrt(2) + 5)*(x + sqrt(2) + 5)


Note that this has leading coefficient 1, not 2, so it's not equal to H. This could be repaired by multiplying by

sage: H.simplify_full().leading_coefficient(x)
2

more

I propose a small modification prod((x-_[0])^_[1] for _ in H.roots())

( 2012-09-22 09:49:30 -0500 )edit

achrzesz: that's a good idea. I'll edit my answer.

( 2012-09-22 11:43:49 -0500 )edit

If p is a simple polynomial with variable x, then sometimes the following (too) simple function can work

def rad_factor(p):
s=solve(p,x,multiplicities=True)
z=zip(map(lambda x:x.rhs(),s[0]),s[1])
return p.leading_coeff(x)*prod([(x-y[0])^y[1] for y in z])


Or using the suggestion by John Palmieri

def rad_factor(p):
return p.leading_coeff(x)*(prod((x-_[0])^_[1] for _ in p.roots()))

more
sage: R.<x>=PolynomialRing(QQ[sqrt(2)],'x')
sage: R(2*x**2+20*x+46).factor()
(2) * (x - sqrt2 + 5) * (x + sqrt2 + 5)

more

Thank you. So I have to know "before" that the roots contain a sqrt(2).

( 2012-09-22 06:53:50 -0500 )edit

You can obtain such information from solve

( 2012-09-22 06:56:32 -0500 )edit

Thank you again.

( 2012-09-22 07:47:26 -0500 )edit