Re: Indexed Slot in a Rule - How to do it??

• To: mathgroup at smc.vnet.net
• Subject: [mg100362] Re: [mg100289] Indexed Slot in a Rule - How to do it??
• From: Leonid Shifrin <lshifr at gmail.com>
• Date: Tue, 2 Jun 2009 06:41:06 -0400 (EDT)
• References: <200905311034.GAA16834@smc.vnet.net>

```Hi Alan,

Using anonymous pure  function semantics will probably be not the simplest
have global values (you must ensure it in your construction anyway) and that
you know which ones and how many of them you need in advance, here is a
possibility:

In[1] =
convertToFunction[eqs_, vars_List] := Function[Evaluate[vars], eqs];

In[2] = T = convertToFunction[{0, 5*x1, 1, 10*x3, 3*x2},{x1,x2,x3}]

Out[2] = Function[{x1, x2, x3}, {0, 5 x1, 1, 10 x3, 3 x2}]

In[3] = T[1, 2, 3]

Out[3] = {0, 5, 1, 30, 6}

This uses your variables x1, ... directly as function variables. The
<Evaluate> in the code is needed to force the binding to the variables
x1,etc. Note that the syntax is slightly different - you pass  a sequence of
arguments rather than a list.

If you really need slots for the anonimous function (#...), what is your
application - what is it that you want to do with them?

Regards,
Leonid

On Sun, May 31, 2009 at 3:34 AM, Alan <alan.stone at yahoo.com> wrote:

> Here's the problem.  Convert a vector (list) with symbol names into
> indexed slots.  I'm doing this so that I can turn the vector into
> function that takes a list.  Here's an example of the desired
> behavior:
>
> eqs = {0, 5*x1, 1, 10*x3, 3*x2};
>
> convert this to:
>
> indexedeqs = {0, 5*#[[1]], 1, 10*#[[3]], 3*#[[2]]};
>
> T[q_]:=indexedeqs;
>
> T[{1,2,3}] = {0,5,1,30,6}
>
> I can't for the life of me figure out how to do this.  I've been
> performing meta-Mathematica all week.  Here's the closest I've come:
>
> xvars={x1,x2,x3};
> MapIndexed[Rule[#1, #[[First@#2]]] &, xvars]
>
> But, this treats the second # literally, and puts in the current item
> from xvars.  I need a way for the second # to just be # without it
> really taking a value.
>
> So, I tried doing it as a string:
> xvars={x1,x2,x3};
> MapIndexed[Rule[#1, "#[[" <> ToString[First@#2] <> "]]"] &, xvars]
>
> And this creates a rule that looks correct, but, its right hand side
> of the rule is a string instead of an expression.
>
> I can't figure this out!!!  Any help is greatly appreciated.
>
> All the best,
> Alan
>
>

```

• Prev by Date: Speeding up code
• Next by Date: Re: Sidebar Tools
• Previous by thread: Re: Indexed Slot in a Rule - How to do it??
• Next by thread: Re: Perpendicular lines do not appear perpendicular