Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example I have assigned some variables that could defined in terms of each other. Then I for some reason need to derive some formula using this variables. How to show the result of this computation without substituting the values and how then substitute them?

It should look something like this:


(*a^3 + 3 a^2 b + 3 a b^2 + b^3 + 3 a^2 c + 6 a b c + 3 b^2 c + 
 3 a c^2 + 3 b c^2 + c^3*)
(*a^3 + 3 a^2 b + 3 a b^2 + b^3 + 3 a^2 d + 6 a b d + 3 b^2 d + 
 3 a d^2 + 3 b d^2 + d^3 + 3 a^2 e + 6 a b e + 3 b^2 e + 6 a d e + 
 6 b d e + 3 d^2 e + 3 a e^2 + 3 b e^2 + 3 d e^2 + e^3*)
(*27 + 27 d + 9 d^2 + d^3 + 27 e + 18 d e + 3 d^2 e + 9 e^2 + 
 3 d e^2 + e^3*)
share|improve this question
up vote 10 down vote accepted

Solution using Block

The easiest option is probably to use Block, and wrap your result in Hold or HoldForm. So,

a = 1; b = 2; c = 3;

result = 
  Block[{a, b, c},
    Hold[Evaluate[Expand[(a + b + c)^3]]]

  Hold[a^3+3 a^2 b+3 a b^2+b^3+3 a^2 c+6 a b c+3 b^2 c+3 a c^2+3 b c^2+c^3]

The values will be automatically substituted once you call ReleaseHold:


(* 216 *)

Use metaprogramming to automate things

Here I will show how you can automate this step with some meta-programming. The following function returns all symbols used in building a given expression, and belonging to some pre-defined set of contexts (which I default to {"Global`"}:

SetAttributes[getHeldSymbols, HoldFirst];
getHeldSymbols[expr_, contexts_: {"Global`"}] :=      
      s_Symbol /; MemberQ[contexts, Context[s]] :> HoldComplete[s],
      {0, Infinity},
      Heads -> True

For example:

getHeldSymbols[Expand[(a + b + c)^3]]

(* {HoldComplete[a], HoldComplete[b], HoldComplete[c]}  *)

Now, the following code defines a dynamic environment where the values of defined symbols participating in building your expression have been "frozen":

SetAttributes[evalWithFrozenSyms, HoldAll];
evalWithFrozenSyms[expr_, unfrozen : {___Symbol} : {}, 
   wrapper_: Hold, getsymbolsF_: getHeldSymbols
] :=
   With[{frozen = 
        If[frozen === {}, expr, 
          frozen /. HoldComplete[{syms___}] :>
            Block[{syms}, wrapper[##] &@expr]

Now we can use it:


(* Hold[a^3+3 a^2 b+3 a b^2+b^3+3 a^2 c+6 a b c+3 b^2 c+3 a c^2+3 b c^2+c^3]  *)

if you want to release come symbols, list them in the second argument of evalWithFrozenSyms:


(* Hold[64+48 b+12 b^2+b^3] *)

Finally, per additional request of the OP - here is what you can do if you want to evaluate those symbols which may expand into other symbols, but not the "final" ones. We will have to use some heavy artillery now. Load the code from this answer on dependency graphs, and then here is another function:

symbolHasValue[held : HoldComplete[s_]] := depends[s] =!= {held}

and finally:

SetAttributes[evalWithSemiFrozenSyms, HoldAll];
evalWithSemiFrozenSyms[expr_, wrapper_: Hold] :=
  Thread[Select[depends[expr], symbolHasValue], HoldComplete] /.
     HoldComplete[{syms___Symbol}] :>
        evalWithFrozenSyms[expr, {syms}, wrapper, depends];

Here is an example of use:

ClearAll[a, b, c, d, e, f];
a = b + c; d = e + f;
b = 1; c = 2; e = 3; f = 4;

Now, you can see that symbols a and d can be expanded, while symbols b,c,e,f should be kept frozen. Here is an example then:


(* Hold[b^2 + 2 b c + c^2+ 2 b e +2 c e + e^2 + 2 b f + 2 c f + 2 e f + f^2] *)
share|improve this answer
Now what about making some kinda wrapper compute[Expand[(a + b + c)^3]] that will produce the same result without specifying the variables manually? – swish Dec 25 '12 at 14:46
@swish See my edit – Leonid Shifrin Dec 25 '12 at 14:49
Why not use HoldForm? Then it looks slightly nicer. – Rolf Mertig Dec 25 '12 at 14:51
@RolfMertig HoldForm is often invisible, when you forget about it, and can confuse. Besides, I have this as an option - the evalWithFrozenSym function accepts the wrapper head as a second optional argument. – Leonid Shifrin Dec 25 '12 at 14:53
One more good thing will be the ability to provide what symbols to actually evaluate. E.g. c=d+e and I want to substitute this definition in my computation but if d or e also defined somehow don't evaluate them if I didn't say so. And in the result also the ability to release only symbols I want. – swish Dec 25 '12 at 15:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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