Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: What is the purpose of the Defer Command?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81916] Re: What is the purpose of the Defer Command?
  • From: Vince Virgilio <blueschi at gmail.com>
  • Date: Sat, 6 Oct 2007 04:46:33 -0400 (EDT)
  • References: <fe4uhp$155$1@smc.vnet.net>

On Oct 5, 5:01 am, "David Park" <djmp... at comcast.net> wrote:
> 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
> posting.)
>
> 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
>
> 3
>
> 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}]]
> %
> giving
>
> 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
> message.
>
> 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.
>
> Needs["Enhancements`HoldTemporary`"]
>
> y = HoldTemporary[1 + 1]
> 1 + y                                                        giving
>
> 1 + 1
>
> 3
>
> The expression is evaluated if it is an argument of some function.
>
> HoldTemporary[Integrate[x^2 Exp[-x], {x, 0, 1}]]
> Identity[%]
> giving
>
> 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.
>
> Needs["TensorCalculus4V6`Tensorial`"]
>
> ?HoldOp
>
> 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
> uses.
>
> 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
> giving
>
> 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
> giving
>
> HoldForm[Pi*Sin[x]*E^x]
>
> \[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
> djmp... at comcast.nethttp://home.comcast.net/~djmpark/

Perhaps Defer is a kind of better-behaved Unevaluated:

1. That disappears even when not an argument to another function
2. And that terminates infinite evaluation immediately.

?

Vince Virgilio



  • Prev by Date: Re: What is the purpose of the Defer Command?
  • Next by Date: Re: How to help Mathematica take an integral
  • Previous by thread: Re: What is the purpose of the Defer Command?
  • Next by thread: Re: Re: What is the purpose of the Defer Command?