First time here? Check out the FAQ!

Ask Your Question
1

Expanding cos(a*(x+y))

asked 9 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 9 years ago

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.

Preview: (hide)
link

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: 9 years ago

Seen: 860 times

Last updated: Dec 31 '15