Ask Your Question
3

collect multiple variables and simplify their coefficients

asked 2011-10-09 16:50:44 +0100

nablaoperator gravatar image

Hi there!

In Mathematica you can do the following: Collect[expr, {var1, var2, var3}, Simplify]

This transforms

expr = var1expr11 + var2expr22 + var1var2expr12 + ...

to

var1Simplify[expr11] + var2Simplify[expr22] + var1var2Simplify[expr12] + ...

I was wondering if there is an easy way to do this in Sage.

So my questions are:

1) How can you collect multiple variables?

2) How can you manipulate their coefficients? (each coefficient is independent)

3*) How can you print the result in a pretty way? (e.g. collecting the expression by coefficients and printing each term in a new line)

Thanks for your help!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2011-10-10 07:35:52 +0100

Jason Grout gravatar image

Here are two helpful functions:

sage: var('x,y,z')
(x, y, z)
sage: expr=x^2*y+2*x*y*z+54*z-3*x*z
sage: expr.collect(x)
x^2*y + (2*y*z - 3*z)*x + 54*z
sage: expr.coefficients(x)
[[54*z, 0], [2*y*z - 3*z, 1], [y, 2]]
edit flag offensive delete link more

Comments

Okay, thank you! It seems I have to do it manually then.

nablaoperator gravatar imagenablaoperator ( 2011-10-10 08:02:21 +0100 )edit

Can we do that in a matrix too?

Apoorv gravatar imageApoorv ( 2021-03-25 11:42:54 +0100 )edit
1

answered 2012-06-25 10:44:01 +0100

pang gravatar image

updated 2012-06-28 08:54:28 +0100

maxima allows to collect multiple terms, so it is possible to do so in Sage using:

sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2
sage: fm = f.maxima_methods()
sage: fm.collectterms(x,y)
x^2*y^2*z^2 + 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2
sage: fm.collectterms(x)
x^2*y^2*z^2 + (4*y + z)*x + 20*y^2 + 21*y*z + 4*z^2

from there you could use the operands() method to simplify each term separately. Is this enough?

Can you assume that your expression is polynomial in x and y?

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: 2011-10-09 16:50:44 +0100

Seen: 8,814 times

Last updated: Jun 28 '12