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

MathGroup Archive 2007

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

Search the Archive

Re: "dereference" variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82074] Re: [mg82033] "dereference" variable
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Thu, 11 Oct 2007 00:22:00 -0400 (EDT)
  • References: <22397821.1192019277612.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

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 evaluat=
es  =

the sum to a product, and m3 passes the Sum unevaluated.

Bobby

On Wed, 10 Oct 2007 03:22:56 -0500, Todd Johnson <johnsong at ics.uci.edu> =
 =

wrote:

> I tried posting this on the Wolfram.com student forums, but got no  =

> response. A friend suggested that I try this forum as well.
>
> I have a Mathematica question I'm hoping someone here can help with,  =

> because it seems fundamental, but it's been bothering me for a long  =

> time. My basic question is "is there a way pass a local variable to a =
 =

> function in a state between 'unevaluated' and 'evaluated,' which might=
  =

> correspond to something like 'dereferenced.'"
>
> Here is a bit of code I've written which demonstrates the problem:
>
> Clear[L, M, P, Q, k]
> SetAttributes[L, HoldFirst];
>
> L[Sum[x_, it_]] := Print["here."];
> L[Times[x_, y_]] := Print["there."];
> L[x_Symbol] := Print["nowhere."];
>
> M[x_] :=
> Module[{s},
> s = Sum[x, {i, 1, 20}];
> L[s]
> ];
> P[x_] :=
> Module[{s},
> s = Unevaluated[Sum[x, {i, 1, 20}]];
> L[s]
> ];
> Q[x_] :=
> Module[{s},
> s = Unevaluated[Sum[x, {i, 1, 20}]];
> L[Evaluate[s]]
> ];
>
> Let me try to explain/motivate this. Suppose that L is some function  =

> which cares about the difference between summation and multiplication,=
  =

> and uses pattern matching to tell the difference. Suppose further that=
 L  =

> receives input from some other function. I've shown three ways of  =

> writing that function: M, P, and Q. Here are the three functions, with=
  =

> their corresponding outputs:
>
> In: M[k]
> Out: nowhere.
>
> In: P[k]
> Out: nowhere.
>
> In: Q[k]
> Out: there.
>
> None of these is what I want, which is "here," and which can be achiev=
ed  =

> by passing input directly to L:
>
> In: L[Sum[k, {i, 1, 20}]]
> Out: here.
>
> Why don't the three functions M, P, and Q work? M and P leave "s"  =

> completely unevaluated, so that what L actually receives is a value li=
ke  =

> "s$123456". Q, on the other hand, evaluates "s" entirely, and arrives =
at  =

> "20*k". You may wonder, "but wait, that is what s evaluates to, why  =

> can't your code deal with that?" The simplest answer is that sometimes=
  =

> Mathematica seems to decide erroneously that the summation can be turn=
ed  =

> into multiplication, which leaves me with dummy variables from  =

> summations lying around unbound. Also, this is a problem at other time=
s,  =

> such as when there is an appreciable difference between "x[[i]]-x[[i]]=
"  =

> and "0", because I really mean "x[[i]]" to stand in for some vector of=
  =

> as-yet-unknown length, so that it minus itself is a zero vector, which=
  =

> is different from plain 0.
>
> So, does anyone know of a way to write a function which gets the  =

> expression out of "s", but doesn't evaluate it completely?
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Re: How much memory is needed?
  • Next by Date: Re: "dereference" variable
  • Previous by thread: Re: "dereference" variable
  • Next by thread: Re: Re: "dereference" variable