Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store unevaluted function in list mathematica

Example:

list:={ Plus[1,1], Times[2,3] }

When looking at list, I get

{2,6}

I want to keep them unevaluated (as above) so that list returns

{ Plus[1,1], Times[2,3] }

Later I want to evaluate the functions in list sequence to get

{2,6}

The number of unevaluated functions in list is not known beforehand. Besides Plus, user defined functions like f[x_] may be stored in list

I hope the example is clear.

What is the best way to do this?

like image 781
nilo de roock Avatar asked Jun 06 '11 15:06

nilo de roock


2 Answers

The best way is to store them in Hold, not List, like so:

In[255]:= f[x_] := x^2;
lh = Hold[Plus[1, 1], Times[2, 3], f[2]]

Out[256]= Hold[1 + 1, 2 3, f[2]]

In this way, you have full control over them. At some point, you may call ReleaseHold to evaluate them:

In[258]:= ReleaseHold@lh

Out[258]= Sequence[2, 6, 4]

If you want the results in a list rather than Sequence, you may use just List@@lh instead. If you need to evaluate a specific one, simply use Part to extract it:

In[261]:= lh[[2]]

Out[261]= 6

If you insist on your construction, here is a way:

In[263]:= l:={Plus[1,1],Times[2,3],f[2]};
Hold[l]/.OwnValues[l]

Out[264]= Hold[{1+1,2 3,f[2]}]

EDIT

In case you have some functions/symbols with UpValues which can evaluate even inside Hold, you may want to use HoldComplete in place of Hold.

EDIT2

As pointed by @Mr.Wizard in another answer, sometimes you may find it more convenient to have Hold wrapped around individual items in your sequence. My comment here is that the usefulness of both forms is amplified once we realize that it is very easy to transform one into another and back. The following function will split the sequence inside Hold into a list of held items:

splitHeldSequence[Hold[seq___], f_: Hold] := List @@ Map[f, Hold[seq]]

for example,

In[274]:= splitHeldSequence[Hold[1 + 1, 2 + 2]]

Out[274]= {Hold[1 + 1], Hold[2 + 2]}

grouping them back into a single Hold is even easier - just Apply Join:

In[275]:= Join @@ {Hold[1 + 1], Hold[2 + 2]}

Out[275]= Hold[1 + 1, 2 + 2]

The two different forms are useful in diferrent circumstances. You can easily use things such as Union, Select, Cases on a list of held items without thinking much about evaluation. Once finished, you can combine them back into a single Hold, for example, to feed as unevaluated sequence of arguments to some function.

EDIT 3

Per request of @ndroock1, here is a specific example. The setup:

l = {1, 1, 1, 2, 4, 8, 3, 9, 27} 
S[n_] := Module[{}, l[[n]] = l[[n]] + 1; l] 
Z[n_] := Module[{}, l[[n]] = 0; l]

placing functions in Hold:

In[43]:= held = Hold[Z[1], S[1]]

Out[43]= Hold[Z[1], S[1]]

Here is how the exec function may look:

exec[n_] := MapAt[Evaluate, held, n]

Now,

In[46]:= {exec[1], exec[2]}

Out[46]= {Hold[{0, 1, 1, 2, 4, 8, 3, 9, 27}, S[1]],  Hold[Z[1], {1, 1, 1, 2, 4, 8, 3, 9, 27}]}

Note that the original variable held remains unchanged, since we operate on the copy. Note also that the original setup contains mutable state (l), which is not very idiomatic in Mathematica. In particular, the order of evaluations matter:

In[61]:= Reverse[{exec[2], exec[1]}]

Out[61]= {Hold[{0, 1, 1, 2, 4, 8, 3, 9, 27}, S[1]],  Hold[Z[1], {2, 1, 1, 2, 4, 8, 3, 9, 27}]}

Whether or not this is desired depends on the specific needs, I just wanted to point this out. Also, while the exec above is implemented according to the requested spec, it implicitly depends on a global variable l, which I consider a bad practice.

An alternative way to store functions suggested by @Mr.Wizard can be achieved e.g. like

In[63]:= listOfHeld = splitHeldSequence[held]

Out[63]= {Hold[Z1], Hold[S1]}

and here

In[64]:= execAlt[n_] := MapAt[ReleaseHold, listOfHeld, n]

In[70]:= l = {1, 1, 1, 2, 4, 8, 3, 9, 27} ;
{execAlt[1], execAlt[2]}

Out[71]= {{{0, 1, 1, 2, 4, 8, 3, 9, 27}, Hold[S[1]]}, {Hold[Z[1]], {1, 1, 1, 2, 4, 8, 3, 9, 27}}}

The same comments about mutability and dependence on a global variable go here as well. This last form is also more suited to query the function type:

getType[n_, lh_] := lh[[n]] /. {Hold[_Z] :> zType, Hold[_S] :> sType, _ :> unknownType}

for example:

In[172]:= getType[#, listOfHeld] & /@ {1, 2}

Out[172]= {zType, sType}
like image 165
Leonid Shifrin Avatar answered Sep 30 '22 14:09

Leonid Shifrin


The first thing that spings to mind is to not use List but rather use something like this:

 SetAttributes[lst, HoldAll];
 heldL=lst[Plus[1, 1], Times[2, 3]]

There will surely be lots of more erudite suggestions though!

like image 38
acl Avatar answered Sep 30 '22 16:09

acl