Ask Your Question
1

Factorization of polynomial with root

asked 2025-09-04 00:51:41 +0200

azerbajdzan gravatar image

updated 2025-09-04 15:38:03 +0200

Can Sagemath factor polynomial expressions with square root or in general any root?

For example this expression:

$$-x^3+5 x^2+3 x+3+\sqrt{x^2+1} \left(2 x^3+x^2+3\right)$$

-x^3 + 5*x^2 + 3*x + 3 + sqrt(x^2 + 1)*(2*x^3 + x^2 + 3)

The results should be in the form (assuming two factors):

(f1(x) + f2(x)*sqrt(x^2+1)) * (g1(x) + g2(x)*sqrt(x^2+1))

where f1(x), f2(x), g1(x), g2(x) are normal polynomials without any roots.

$\sqrt{x^2+1}$ is like an extension but I do not know the right terminology how to name it.

edit retag flag offensive close merge delete

Comments

Why does it not display one of the Latex lines properly? Even editing does not work properly.

azerbajdzan gravatar imageazerbajdzan ( 2025-09-04 00:58:29 +0200 )edit

Your expression is already factorized, isn't it? Think of f1(x)=1 and f2(x)=0.

Max Alekseyev gravatar imageMax Alekseyev ( 2025-09-04 19:11:12 +0200 )edit

@MaxAlekseyev Yes, it is in the same way like x^2 - 1 is already factored because it is (1) * (x^2 - 1). But I am interested in nontrivial factorization. For this case it would be (x+1) * (x-1). For OP case it would be something with nonzero polynomials.

azerbajdzan gravatar imageazerbajdzan ( 2025-09-05 11:28:35 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2025-09-05 17:26:41 +0200

Max Alekseyev gravatar image

updated 2025-09-05 22:36:44 +0200

I don't think Sage has a routine for that, and furthermore such quasi-polynomials may not form a UFD. So, our best shot could be using the method of undetermined coefficients and solving the resulting system of polynomial equations. For example, thinking of a "nontrivial factorization" as one with the degrees of polynomials in factors strictly smaller than those in the given expression, we can try something like this:

R = PolynomialRing(QQ,3*4,'c') # polynomial ring of undetermined coefficients
c = R.gens()
K.<x> = R[]  # ring of polynomials in x
f1 = K(c[:3])      # = c2*x^2 + c1*x + c0
f2 = K(c[3:6]) 
g1 = K(c[6:9])
g2 = K(c[9:])
# define the system of equations as an ideal:
J = R.ideal( (f1*g1 + f2*g2*(x^2+1) - (-x^3 + 5*x^2 + 3*x + 3)).coefficients() + (f1*g2 + f2*g1 - (2*x^3 + x^2 + 3)).coefficients() + [c[0]-1] )
print( J.variety() )     # solving
edit flag offensive delete link more

Comments

On http://aleph.sagemath.org the last line produced error: ValueError: The dimension of the ideal is 1, but it should be 0.

azerbajdzan gravatar imageazerbajdzan ( 2025-09-05 21:48:02 +0200 )edit

Nevertheless I upvoted as the method works if I avoid using variety. Also you probably erroneously swapped g1 and f2 in your ideal.

azerbajdzan gravatar imageazerbajdzan ( 2025-09-05 22:29:27 +0200 )edit

Dimension 1 is, in fact, expected as we can multiply one factor by a constant, and divide the other by the same constant without affecting the product. I've added a "normalizing" equation c[0] - 1 to eliminate this issue.

Max Alekseyev gravatar imageMax Alekseyev ( 2025-09-05 22:32:41 +0200 )edit

I do not see the swap of g1 and f2 - can you elaborate?

Max Alekseyev gravatar imageMax Alekseyev ( 2025-09-05 22:35:58 +0200 )edit

I think f1*g1 + f2*g2*(x^2+1) should be f1*f2 + g1*g2*(x^2+1).

azerbajdzan gravatar imageazerbajdzan ( 2025-09-05 22:51:11 +0200 )edit
0

answered 2025-09-06 00:33:51 +0200

azerbajdzan gravatar image

updated 2025-09-06 00:46:56 +0200

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-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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-09-04 00:51:41 +0200

Seen: 12,824 times

Last updated: Sep 06