Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

What is the purpose of the Defer Command?

  • To: mathgroup at
  • Subject: [mg81868] What is the purpose of the Defer Command?
  • From: "David Park" <djmpark at>
  • Date: Fri, 5 Oct 2007 04:53:19 -0400 (EDT)

I do not understand the utility of the new Defer statement in Mathematica 
Version 6. Also, it seems to me to be similar to, but not as good as, the 
HoldTemporary command introduced by Ted Ersek on MathSource a few years ago.

The help for Defer says: "Defer[expr] yields an object that displays as the 
unevaluated form of expr, but which is evaluated if it is explicitly given 
as Mathematica input." What does 'given as Mathematica input' mean? The 
examples seem to only involve copying and pasting, which I don't consider a 
great method for doing mathematics, or evaluation in place.

I would like to understand how Defer might be used in expository notebooks 
to clarify some piece of mathematics. The problem is that it requires an 
interactive action, which would be invisible to a reader of a notebook.  I 
think the idea of 'modification in place' is poor in technical communication 
because it destroys the record of what was done.

(In the examples below, whenever an output resulted in an expression that 
copied as a box structure, I converted to InputForm to simplify the 

Here is a simple example:

y = Defer[1 + 1]
1 + y                                               giving

1 + 1

1 + (1 + 1)

I would prefer that the Defer expression would have evaluated in the second 
statement but I guess it is logical that it didn't. If I write:

1 + y

then select the y and Evaluate In Place I obtain the following, which must 
then be further evaluated to obtain 3.

1 + 1 + 1


A second example. I want to show an integral without evaluation and then the 
evaluated result. I have to write the following expression, then select the 
second line of output, evaluate in place, and then I obtain the result - but 
as an Input cell. This is certainly a place where HoldForm would be better.

Defer[Integrate[x^2 Exp[-x], {x, 0, 1}]]

Integrate[x^2/E^x, {x, 0, 1}]

2 - 5/\[ExponentialE]                        (which is an Input cell)

Here is third example. Defer does not evaluate and we obtain an error 

numb = Defer[2^67 - 1]
FactorInteger[numb]                                       giving

2^67 - 1

FactorInteger::"exact" :  "\"Argument \!\(\*SuperscriptBox[\"2\", \
\"67\"]\) - 1 in FactorInteger[\!\(\*SuperscriptBox[\"2\", \"67\"]\) \
- 1] is not an exact number\""

FactorInteger[2^67 - 1]

But it works if I copy and paste into FactorInteger.

Now, look at the behavior of Ted's MathSource package.


y = HoldTemporary[1 + 1]
1 + y                                                        giving

1 + 1


The expression is evaluated if it is an argument of some function.

HoldTemporary[Integrate[x^2 Exp[-x], {x, 0, 1}]]

Integrate[x^2/E^x, {x, 0, 1}]

2 - 5/\[ExponentialE]       (which is an Output cell)

numb = HoldTemporary[2^67 - 1]
FactorInteger[numb]                                            giving

2^67 - 1

{{193707721, 1}, {761838257287, 1}}

Much better. I might be missing the point, but I don't think that Defer is 
at all well designed.

There is another Hold that is very useful. This is one that holds an 
operation but evaluates the arguments. We have a HoldOp statement in the 
Tensorial package.



HoldOp[operation][expr] will prevent the given operation from being 
evaluated in expr. Nevertheless, other operations within expr will be 
evaluated. Operation may be a pattern, including alternatives, that 
represents heads of expressions. The HoldOp can be removed with ReleaseHold.

One reason we want the arguments to evaluate is that the arguments often 
contain tensor shortcut expressions and we want them evaluated to show the 
full tensor expression inside some operation. However, there are many other 

f[x_] := Sin[x] \[ExponentialE]^x

We would like f[x] to be evaluated inside the Integrate statement, but hold 
the actual itegration.

Integrate[f[x], {x, 0, \[Pi]}] // HoldOp[Integrate]
% // ReleaseHold 

HoldForm[Integrate[E^x*Sin[x], {x, 0, Pi}]]

1/2 (1 + \[ExponentialE]^\[Pi])

For exposition purposes we might want to keep the following expression in 
the input order.

\[Pi]  Sin[x] \[ExponentialE]^x // HoldOp[Times]
% // ReleaseHold 


\[ExponentialE]^x \[Pi] Sin[x]

Often we will have cases where some operation has automatic built-in rules, 
such as linear and Leibnizian breakouts with differentiation. Again, for 
exposition purposes, we might want to show the expression before these rules 
are applied.

g[x_] := x^2

D[a f[x] g[x], x] // HoldOp[D]
% // ReleaseHold                                    giving

HoldForm[D[a*E^x*x^2*Sin[x], x]]

a \[ExponentialE]^x x^2 Cos[x] + 2 a \[ExponentialE]^x x Sin[x] +
 a \[ExponentialE]^x x^2 Sin[x]

David Park
djmpark at

  • Prev by Date: Re: Functions with data hidden in them
  • Next by Date: Re: converting function from RasterArray to Raster
  • Previous by thread: Re: Fast alternative to Nest[.] or NestList[.]? (version 6.0)
  • Next by thread: Re: What is the purpose of the Defer Command?