Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This is rewritten and slightly adapted version of Max Alekseyev's answer with printed results.

Instead of this answer upvote his answer.

I have to decrease max degree of f2, g1, g2 compared to his answer as computing J.variety() would take probably too much time and I was not patient enough. But was lucky that it worked even with decreased degrees (which of course does not have to work always).

R = PolynomialRing(QQ,3*4,'c') # polynomial ring of undetermined coefficients
c = R.gens()
K.<x> = R[]  # ring of polynomials in x

expr=-x^3 + 5*x^2 + 3*x + 3 + sqrt(x^2 + 1)*(2*x^3 + x^2 + 3)
kk=(expr).coefficients(sqrt(x^2 + 1))
k1=K(kk[0][0])
k2=K(kk[1][0])

f1 = K(c[:3])      # = c2*x^2 + c1*x + c0
f2 = K(c[3:5]) 
g1 = K(c[5:7])
g2 = K(c[7:])
# define the system of equations as an ideal:
J = R.ideal( (f1*g1 + f2*g2*(x^2+1) - (k1)).coefficients() + (f1*g2 + f2*g1 - (k2)).coefficients() + [c[0]-1] )
s=J.variety()[0]
factor1=f1.map_coefficients(lambda z: z.subs(s)) + f2.map_coefficients(lambda z: z.subs(s))*sqrt(x^2+1)
factor2=g1.map_coefficients(lambda z: z.subs(s)) + g2.map_coefficients(lambda z: z.subs(s))*sqrt(x^2+1)
print("first factor:", factor1)
print("second factor:", factor2)
print("factorization", expr==factor1*factor2)
print("\nVerification that expanded factorization is same as original expression")
print((factor1*factor2).expand().collect(sqrt(x^2 + 1)))
print(expr)

first factor: 2*x^2 + x + sqrt(x^2 + 1) + 1
second factor: sqrt(x^2 + 1)*x - x + 3
factorization -x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3 == (2*x^2 + x + sqrt(x^2 + 1) + 1)*(sqrt(x^2 + 1)*x - x + 3)

Verification that expanded factorization is same as original expression
-x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3
-x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3

This is rewritten and slightly adapted version of Max Alekseyev's answer with printed results.

Instead of this answer upvote his answer.

I have to decrease max degree of f2, g1, g2 compared to his answer as computing J.variety() would take probably too much time and I was not patient enough. But was lucky that it worked even with decreased degrees (which of course does not have to work always).

R = PolynomialRing(QQ,3*4,'c') PolynomialRing(QQ,3*4-3,'c') # polynomial ring of undetermined coefficients
c = R.gens()
K.<x> = R[]  # ring of polynomials in x

expr=-x^3 + 5*x^2 + 3*x + 3 + sqrt(x^2 + 1)*(2*x^3 + x^2 + 3)
kk=(expr).coefficients(sqrt(x^2 + 1))
k1=K(kk[0][0])
k2=K(kk[1][0])

f1 = K(c[:3])      # = c2*x^2 + c1*x + c0
f2 = K(c[3:5]) 
g1 = K(c[5:7])
g2 = K(c[7:])
# define the system of equations as an ideal:
J = R.ideal( (f1*g1 + f2*g2*(x^2+1) - (k1)).coefficients() + (f1*g2 + f2*g1 - (k2)).coefficients() + [c[0]-1] )
s=J.variety()[0]
factor1=f1.map_coefficients(lambda z: z.subs(s)) + f2.map_coefficients(lambda z: z.subs(s))*sqrt(x^2+1)
factor2=g1.map_coefficients(lambda z: z.subs(s)) + g2.map_coefficients(lambda z: z.subs(s))*sqrt(x^2+1)
print("first factor:", factor1)
print("second factor:", factor2)
print("factorization", expr==factor1*factor2)
print("\nVerification that expanded factorization is same as original expression")
print((factor1*factor2).expand().collect(sqrt(x^2 + 1)))
print(expr)

first factor: 2*x^2 + x + sqrt(x^2 + 1) + 1
second factor: sqrt(x^2 + 1)*x - x + 3
factorization -x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3 == (2*x^2 + x + sqrt(x^2 + 1) + 1)*(sqrt(x^2 + 1)*x - x + 3)

Verification that expanded factorization is same as original expression
-x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3
-x^3 + 5*x^2 + (2*x^3 + x^2 + 3)*sqrt(x^2 + 1) + 3*x + 3