开发者

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 de开发者_Go百科fined functions like f[x_] may be stored in list

I hope the example is clear.

What is the best way to do this?


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}


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!


You can also use Hold on every element that you want held:

a = {Hold[2 + 2], Hold[2*3]}

You can use HoldForm on either the elements or the list, if you want the appearance of the list without Hold visible:

b = {HoldForm[2 + 2], HoldForm[2*3]}

c = HoldForm@{2 + 2, 2*3}
   {2 + 2, 2 * 3}

And you can recover the evaluated form with ReleaseHold:

a // ReleaseHold
b // ReleaseHold
c // ReleaseHold

Out[8]= {4, 6}

Out[9]= {4, 6}

Out[10]= {4, 6}

The form Hold[2+2, 2*3] or that of a and b above are good because you can easily add terms with e.g. Append. For b type is it logically:

Append[b, HoldForm[8/4]]

For Hold[2+2, 2*3]:

Hold[2+2, 2*3] ~Join~ Hold[8/4]


Another way:

lh = Function[u, Hold@u, {HoldAll, Listable}];
k = lh@{2 + 2, Sin[Pi]}
(*
->{Hold[2 + 2], Hold[Sin[\[Pi]]]}
*)
ReleaseHold@First@k
(*
-> 4
*)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜