Ask Your Question
0

How do I iterate through factors of a monomial?

asked 2012-06-10 11:46:35 +0200

Ilya gravatar image

updated 2012-06-10 11:49:18 +0200

I am (very naively) trying to implement a certain non-linear map of polynomial rings. I can get the monomials in a polynomial by simply iterating through a polynomial:

>>> R.<x,y,z>=QQ[]
>>> list(x*y^3 + 2*x*y)
[(1, x*y^3), (2, x*y)]

How do I now iterate through the factors of each monomial, i.e. convert x*y^3 into something like [(x, 1), (y, 3)] or even just [x, y, y, y]?

Also, what is the correct way to do the opposite conversion, i.e. [x, y, y, y] to x*y^3?

I hope you won't be offended by such newbie questions. I couldn't find this in any of the tutorials easily (did I miss the right one?) Thank you!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-06-10 14:42:53 +0200

achrzesz gravatar image
R.<x,y,z>=QQ[]
[u for u in factor(x*y^3)]
#[(x, 1), (y, 3)]
edit flag offensive delete link more

Comments

1

`list(factor(x*y^3))` also works.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-11 02:43:42 +0200 )edit

This works, thank you!

Ilya gravatar imageIlya ( 2012-06-11 07:51:19 +0200 )edit
3

answered 2012-06-10 14:44:42 +0200

Eviatar Bach gravatar image

updated 2012-06-10 15:35:32 +0200

Not offended at all! I didn't know about this myself until now.

This works:

sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3

And, if you want to go from the factor list to the expression:

sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3

Hope this helps!

edit flag offensive delete link more

Comments

1

I think something like `mul(t^m for t,m in a)` is simpler.

DSM gravatar imageDSM ( 2012-06-10 15:16:53 +0200 )edit

Yes, you're right. I changed it.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-10 15:35:50 +0200 )edit

Thank you very much, don't know how long I'd be looking for it without your help. However, I have to do `list((x*y^3).factor())`. For some reason, `(x*y^3).factor_list()` gives an error ('sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingu\ lar' object has no attribute 'factor_list').

Ilya gravatar imageIlya ( 2012-06-11 07:49:41 +0200 )edit

I see it has to do with `R.<x,y,z>=QQ[]`. Looks like a bug.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-11 14:48:06 +0200 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-06-10 11:46:35 +0200

Seen: 1,003 times

Last updated: Jun 10 '12