I was playing with the Expand command and found something that puzzled me. Using Trace did not clarify the situation.

expr=Exp[x^2] (-2 Exp[-x^2] + 4 Exp[-x^2] x^2);
Expand[expr, Exp[x^2]]
(* expr is left unchanged *)
Expand[expr, Exp[-x^2]]
(* - 2 + 4 x^2 - it worked! *)
Expand[-2 Exp[-x^2] + 4 Exp[-x^2] x^2, Exp[-x^2]]
(* -2 Exp[-x^2] + 4 Exp[-x^2] x^2 is left unchanged *)

What exactly happened when the second call to Expand above successfully simplified expr?

Note: Simplify also does the job, but I am trying to figure out Expand.

Thanks for the help.

I would like to clarify my question.

I reasoned that Mathematica would evaluate the second call to Expand above in two steps:

Step 1) Leave Exp[x^2] aside and Expand the remainder of expr with argument Exp[-x^2].

Step 2) Evaluate the resulting expression.

However, the third call above to Expand does not change its argument, and Step 1 is identical to the third call. So Step 1 should leave expr unchanged and therefore in Step 2 expr would be evaluated. But expr evaluates to itself, so Step 2 also should not change expr. So neither of the two steps in the second call should change expr. But the second call does change expr. That is what I do not understand. My hypothesis of a two-step evaluation must be wrong, but I do not know why.

  • 1
    $\begingroup$ The docs say: "Expand[expr, patt] leaves unexpanded any parts of expr that are free of the pattern patt" So the first one is leaving everything in the parenthesis unchanged (because it doesn't have positive powers of `Exp[x^2]'). The second one works just as if there were no second argument: i.e., like Expand[expr]. $\endgroup$ – bill s Sep 26 '16 at 18:35
  • $\begingroup$ @ bill s Thanks. But why does the second one works as if there was no second argument? $\endgroup$ – Soldalma Sep 26 '16 at 19:57

Expand[expr, patt], according to the documentation, works as follows:

leaves unexpanded any parts of expr that are free of the pattern patt.

Consider first an example from the docs:

(* Leave parts free of 1+x unexpanded: *)
Expand[(1 + x)^2 + (2 + x)^2, 1 + x]

1 + 2 x + x^2 + (2 + x)^2

That means "if there is 1+x, expand; if there is no 1+x, don't expand".


Expand[expr, Exp[x^2]]

means "if there is Exp[x^2], expand; leave the rest unchanged". Exp[x^2], as it is, is present only outside of the bracket - it has no way to be expanded further (like there is no way to further expand a single x); the bracket is free of it, so it's left unexpanded.

On the other hand,

Expand[expr, Exp[-x^2]]

says "if there is Exp[-x^2], expand; leave the rest unchanged". This occurs in the bracket, and what's outside the bracket is free of it. Hence, obviously Exp[x^2] is undexpanded (again: there's even no way to expand it by itself), but the bracket isn't free of Exp[-x^2], hence it is expanded.

Finally, as a summary and final example, consider

Expand[expr, y]

which is left unexpanded, because the whole expr is free of y.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.