Ask Your Question

Expanding cos(a*(x+y))

asked 2015-12-30 18:58:16 -0500

pircks gravatar image

I'm having trouble finding out how to expand


when wrapped by cos(). The expression


expands as expected, but


, leaves the expression unchanged. I would prefer a solution that scales for more complex expressions

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-12-31 02:20:17 -0500

Here is an exploration of your question. First define a few expressions.

sage: a, x, y = SR.var('a x y')
sage: expr1 = a * (x + y)
sage: expr1
a*(x + y)
sage: expr2 = cos(expr1)
sage: expr2
cos(a*(x + y))

Trying to expand naively.

sage: expand(expr1)
a*x + a*y
sage: expand(expr2)
cos(a*(x + y))

In the case of expr2 the following does what you want:

sage: expr2.full_simplify()
cos(a*x + a*y)

Not sure how it would scale for more complex expressions though.

Let's see how to explore the expression tree.

From an expression, you can get the operator and the operands as follows.

sage: op = expr2.operator()
sage: op
sage: ops = expr2.operands()
sage: ops
[a*(x + y)]

Then you can form back the expression as follows.

sage: op(*ops)
cos(a*(x + y))

So you could expand the operands and then apply the operator to the expanded operands as follows:

sage: op(*[expr.expand() for expr in ops])
cos(a*x + a*y)

From there, one could imagine writing a recursive function to explore and expand the expression tree.

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


Asked: 2015-12-30 18:58:16 -0500

Seen: 36 times

Last updated: Dec 31 '15