MathGroup Archive 2011

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

Search the Archive

Re: Multiple use of Set on lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119846] Re: Multiple use of Set on lists
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sat, 25 Jun 2011 05:28:32 -0400 (EDT)
  • References: <itm0of$143$1@smc.vnet.net>

Here is a bit less exotic solution:

Set @@@ Append @@@
  Transpose[{Thread[Hold[cs] /. OwnValues[cs]], rands}]

You may try for example with

cs = {c[1/2][-(1/2)][1/2][-(1/2)][-1], c[1/2][-(1/2)][1/2][1/2][-1]}
rands = {1,2}

but I'd agree that it may be better to reformulate the problem.

Regards,
Leonid


On Tue, Jun 21, 2011 at 1:51 PM, Oleksandr Rasputinov <
oleksandr_rasputinov at hmamail.com> wrote:

> The problem is due to Set being HoldFirst but MapThread not having any
> corresponding Hold attributes. While the c's are being substituted for
> their values too early, this is not an issue with MapThread per se; rather
> it is a consequence of how attributes affect the order of evaluation in
> Mathematica.
>
> An ugly hack which solves the problem as stated is the following:
>
> SubValues[c] = Block[{c},
>  MapThread[Set, {cs, rands}];
>  SubValues[c]
> ];
>
> However, it may be better to reformulate the problem in such a way
> that this construct is not needed.
>
> On Jun 21, 12:38 am, Jonathan Frazer <J.Fra... at sussex.ac.uk> wrote:
> > Hello,
> >
> > I have a largish list of awkwardly "indexed variables", of the form cs =
> > {c[1/2][-(1/2)][1/2][-(1/2)][-1],
> c[1/2][-(1/2)][1/2][1/2][-1]......etc....
> > ....} which need to be assigned random values multiple times inside a Do
> loop. I was hoping I could generate a new list of random numbers each time
> then do something like
> >
> > MapThread[Set, {cs, rands}]
> >
> > Where "rands" would be the list of random numbers. I'm clearly
> misunderstanding how MapThread works though as this doesn't work. Rather
> than getting "c[1/2][-(1/2)][1/2][-(1/2)][-1]=new random number", instead
> "old random number=new random number" is what seems to be happening.
> >
> > As a last resort I thought I would just clear the variables at the
> beginning of the do loop but you can't use Clear to do this and I don't see
> how you can apply "=." (Unset) over a list either.
> >
> > Any suggestions?
> >
> > Many thanks,
> >
> > Jonny
>
>


  • Prev by Date: Re: what's in a name? (legal and conventional constructions of identifiers)
  • Next by Date: Re: parsing a strig
  • Previous by thread: Re: Multiple use of Set on lists
  • Next by thread: random financial portfolios