From: janitor048 on
Hi there!

I need a somewhat larger list of substitution rules along the line of

subs = { name1 -> SomeFunction[name1], name2 ->
SomeFunction[name2], ... , nameN -> SomeFunction[nameN]}

Obviously typing that by hand can become a little annoying. So I would
like to have that list created dynamically, via Table[ ... ] or
something the like.
The problem is, I can't figure out the correct syntax for this task.
"name1" etc. are just, well, names (variable names to be specific).
Unfortunately I haven't been able to find a solution on the web so
far. Any suggestions?

Cheers,
Oliver

From: Albert Retey on
Am 14.04.2010 04:42, schrieb janitor048:
> Hi there!
>
> I need a somewhat larger list of substitution rules along the line of
>
> subs = { name1 -> SomeFunction[name1], name2 ->
> SomeFunction[name2], ... , nameN -> SomeFunction[nameN]}
>
> Obviously typing that by hand can become a little annoying. So I would
> like to have that list created dynamically, via Table[ ... ] or
> something the like.
> The problem is, I can't figure out the correct syntax for this task.
> "name1" etc. are just, well, names (variable names to be specific).
> Unfortunately I haven't been able to find a solution on the web so
> far. Any suggestions?

It's actually quite simple, so I'm not sure whether I got what your real
problem is:

namelist = {a, b, c, d}

subs = # -> SomeFunction[#] & /@ namelist

If you want to evaluate the rhs only when the lhs matches you can also use:

subs = # :> SomeFunction[#] & /@ namelist

hth,

albert

From: telefunkenvf14 on
On Apr 13, 9:42 pm, janitor048 <janitor...(a)googlemail.com> wrote:
> Hi there!
>
> I need a somewhat larger list of substitution rules along the line of
>
> subs = { name1 -> SomeFunction[name1], name2 ->
> SomeFunction[name2], ... , nameN -> SomeFunction[nameN]}
>
> Obviously typing that by hand can become a little annoying. So I would
> like to have that list created dynamically, via Table[ ... ] or
> something the like.
> The problem is, I can't figure out the correct syntax for this task.
> "name1" etc. are just, well, names (variable names to be specific).
> Unfortunately I haven't been able to find a solution on the web so
> far. Any suggestions?
>
> Cheers,
> Oliver

I think Thread[] is what you want. David Park's website includes a
notebook with a notebook on evaluation control, and examples like:

2 a + 5 d + 3 x
%/. Thread[{a, d, x} -> {8, 3, 4}]

or

2 a + 5 d + 3 x
% /. Thread[Rule[{a, d, x}, {8, 3, 4}]]

-RG

From: janitor048 on
On Apr 14, 11:15 am, telefunkenvf14 <rgo...(a)gmail.com> wrote:
> On Apr 13, 9:42 pm, janitor048 <janitor...(a)googlemail.com> wrote:
>
>
>
> > Hi there!
>
> > I need a somewhat larger list of substitution rules along the line of
>
> > subs = { name1 -> SomeFunction[name1], name2 ->
> > SomeFunction[name2], ... , nameN -> SomeFunction[nameN]}
>
> > Obviously typing that by hand can become a little annoying. So I would
> > like to have that list created dynamically, via Table[ ... ] or
> > something the like.
> > The problem is, I can't figure out the correct syntax for this task.
> > "name1" etc. are just, well, names (variable names to be specific).
> > Unfortunately I haven't been able to find a solution on the web so
> > far. Any suggestions?
>
> > Cheers,
> > Oliver
>
> I think Thread[] is what you want. David Park's website includes a
> notebook with a notebook on evaluation control, and examples like:
>
> 2 a + 5 d + 3 x
> %/. Thread[{a, d, x} -> {8, 3, 4}]
>
> or
>
> 2 a + 5 d + 3 x
> % /. Thread[Rule[{a, d, x}, {8, 3, 4}]]
>
> -RG

Thanks a lot for all your answers!
Actually I received one more answer via "reply to author" that goes
along the line of the suggestion by Albert.

I'm afraid I couldn't make myself exactly clear. I really needed a way
to create a list of names (with pattern "string" plus "number")
dynamically and then to create a list of subsitution rules from this
list.
So this is the solution I've come up with by now (with help from all
your suggestions):

indicesToBeReplaced = ToExpression[ Table["li" <> ToString[i], {i, 1,
10} ] ]
indicesReplacementRules = (# -> ComplexIndex[#]) & /@
indicesToBeReplaced

This gives me a list
{li1 -> ComplexIndex[li1], li2 -> ComplexIndex[li2], li3 ->
ComplexIndex[li3], ...}
as needed.

Probably this is far from the most elegant or shortest solution, but
it works.. :-)


Cheers,
Oliver

From: Ingolf Dahl on
You first define your list of names, say by

namelist =
Table[ToExpression[StringJoin["name", ToString[i]]], {i, 1, 10}]

then you make your rule list by

subs = (#1 -> SomeFunction[#1]) & /@ namelist

Look at "Map" (for /@) and "Function" (for #1 and &) in the Mathematica help
if you do not recognize the notation

Best regards

Ingolf Dahl
Sweden
ingolf.dahl(a)telia.com


-----Original Message-----
From: janitor048 [mailto:janitor048(a)googlemail.com]
Sent: den 19 april 2010 10:10
Subject: How to dynamically build a list of rules

Hi there!

I need a somewhat larger list of substitution rules along the line of

subs = { name1 -> SomeFunction[name1], name2 ->
SomeFunction[name2], ... , nameN -> SomeFunction[nameN]}

Obviously typing that by hand can become a little annoying. So I would
like to have that list created dynamically, via Table[ ... ] or
something the like.
The problem is, I can't figure out the correct syntax for this task.
"name1" etc. are just, well, names (variable names to be specific).
Unfortunately I haven't been able to find a solution on the web so
far. Any suggestions?

Cheers,
Oliver


 | 
Pages: 1
Prev: Equation problem
Next: NIntegrate Confusion