Re: "dereference" variable
- To: mathgroup at smc.vnet.net
- Subject: [mg82121] Re: "dereference" variable
- From: Yaroslav Bulatov <yaroslavvb at gmail.com>
- Date: Fri, 12 Oct 2007 03:04:18 -0400 (EDT)
- References: <22397821.1192019277612.JavaMail.root@m35>
If I understand your problem correctly, you want a kind of partial evaluation, evaluate L[s] to L[Sum..] and stop argument evaluation at that point Here's a couple of approaches s := 1 + 2; SetAttributes[f, HoldFirst]; f[s_] := Head[Unevaluated[s]] ReleaseHold[Hold[f[s]] /. OwnValues[s]] Or s = Hold[1 + 2]; f[s_] := (Head /@ (Unevaluated @@ # &) /@ {s}) // First f[s] The basic idea is that you could put the expression inside Hold, use Replace or Map to bring the expression in the right form without interference from evaluator, then get rid of Hold by calling ReleaseHold (first example) or replacing it with Unevaluated (second example) Robby Villegas gives some other techniques for doing partial evaluation http://library.wolfram.com/infocenter/Conferences/377/ Yaroslav On Oct 10, 9:41 pm, Todd Johnson <johns... at ics.uci.edu> wrote: > That is true. But, as I understand it, this only works if you have the > luxury of typing in "type[Sum[x, {i, 1, 20}]]"; if all you have is a > local variable which has been assigned the as-yet-unevaluated expression > "Sum[x, {i, 1, 20}]", then that solution doesn't seem to work. > > todd. > > DrMajorBob wrote: > > Possibly this will make it clearer: > > > Clear[type, m, p, q, k] > > SetAttributes[type, HoldFirst]; > > > type[arg : Sum[x_, it_]] = {"Sum", HoldForm[arg], arg}; > > type[arg : Times[x_, y_]] = {"Times", HoldForm[arg], arg}; > > type[x_Symbol] = {"Symbol", HoldForm[x], x}; > > > m1[x_] := Module[{s = Sum[x, {i, 1, 20}]}, type[s]] > > m2[x_] := type[Evaluate@Sum[x, {i, 1, 20}]] > > m3[x_] := type[Sum[x, {i, 1, 20}]] > > > m1[k] > > {Symbol,s$1748,20 k} > > > m2[k] > > {Times,20 k,20 k} > > > m3[k] > > {Sum,\!\( > > \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(20\)]k\),20 k} > > > m1 passes s as a symbol (setting s beforehand has no effect), m2 > > evaluates the sum to a product, and m3 passes the Sum unevaluated. > > > Bobby