Ask Your Question
1

Expanding cos(a*(x+y))

asked 2015-12-31 01:58:16 +0100

pircks gravatar image

I'm having trouble finding out how to expand

a*(x+y)

when wrapped by cos(). The expression

expand(a*(x+y))

expands as expected, but

expand(cos(a*(x+y)))

, 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
3

answered 2015-12-31 09:20:17 +0100

slelievre gravatar image

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
cos
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

Stats

Asked: 2015-12-31 01:58:16 +0100

Seen: 640 times

Last updated: Dec 31 '15