9
$\begingroup$

I need to evaluate the result returned from an external package, which contains Defer in it, but do this in a script without user interaction.

I am not able to figure how to do this, without actually using the mouse and hitting Shift + Return in the notebook itself. But I need to do this in a program (script) that runs outside the notebook environment.

Here is an example:

r = Defer[Integrate][Cos[x], x]

Mathematica graphics

The out cell there has the Defer in it. I want to now evaluate r. The only way is by moving the mouse to the Out cell and hit Enter, which now changes the Out cell to an In cell and evaluate what was there resulting in:

Mathematica graphics

How do I do the above in a script? I tried Evaluate[r] and Evaluate[ReleaseHold[r]] and all sorts of things. But I can't get rid of the Defer. (There is no RemoveDefer function). The only way is to do evaluate in place which is what the pointing on the cell and hitting Enter does.

How do I remove Defer from a returned expression and evaluate the result in non-interactive script (i.e. programmatically)?

I am using version 10.1 on Windows 7.

$\endgroup$
4
  • 2
    $\begingroup$ r /. Defer -> Identity ? $\endgroup$ – ilian Jul 20 '15 at 0:46
  • 2
    $\begingroup$ How about r/.Defer->Identity ? Wow, @ilian was 10 seconds faster! $\endgroup$ – Jens Jul 20 '15 at 0:46
  • $\begingroup$ Indeed, a photo-finish! $\endgroup$ – ilian Jul 20 '15 at 0:47
  • $\begingroup$ @ilian and jens thanks! Yes, this did the trick. I spend long time searching and never saw this trick. Please feel free to post this as answer if you like. $\endgroup$ – Nasser Jul 20 '15 at 0:49
8
$\begingroup$

As suggested in the comments by both me and Jens

r = Defer[Integrate][Cos[x], x];
r /. Defer -> Identity
Sin[x]
$\endgroup$
1
  • 3
    $\begingroup$ I wish Mathematica documentation for Defer would mention such method. Nothing there that mentions how to evaluate Defer expressions other than evaluate in place method. $\endgroup$ – Nasser Jul 20 '15 at 1:18
10
$\begingroup$

Defer is a special head that behaves like Hold, but it has an additional rule for output: it disappears from the output box expression.

There is nothing special about how it is handled as input -- all the "magic" takes place during output formatting. To emulate this process, simply apply a ToBoxes ToExpression pair:

r = Defer[Integrate][Cos[x], x];

r // ToBoxes // ToExpression

Sin[x]

This is superior to a simple replacement of all instances of Defer with Identity as it replicates the output-input process, whereas replacement does not:

x = {Defer};
x /. Defer -> Identity
x // ToBoxes // ToExpression
{Identity} 

{Defer}

One could instead match the head Defer, i.e. /. Defer[x_] :> x, but if the desire is to replicate output and re-input I believe box conversion is the appropriate method. See for example:

$\endgroup$
1
  • $\begingroup$ Thanks for fixing my stupid typos @Peter ! $\endgroup$ – Mr.Wizard Jul 20 '15 at 9:04

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.