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.