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

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: [mg119768] Re: Multiple use of Set on lists
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 21 Jun 2011 05:52:53 -0400 (EDT)

On 6/20/11 at 7:38 PM, J.Frazer at sussex.ac.uk (Jonathan Frazer) wrote:

>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?

You have made it difficult to see the problem since you've not
posted enough code so that someone can see what you are doing.
But I will try.

Usually, the problem with creating random numbers you seem to be
describing is caused by using Set rather than SetDelayed. When
you use Set the call to Random gets evaluated immediately, the
results get assigned to a variable and Random is never called
again when using the variable. In contrast, using SetDelayed,
Random gets called every time the variable in the assignment is
evaluated. This is usually what you want when assigning Random
to a variable.

It is trivial to set a variable to a list of random values.
There is no need to use either MapThread or a Do loop to achieve this.

If you want a fixed set of random values assigned to a variable,
simply do

c = RandomReal[1, {n}]

where n is the number of random values you want. Here, I've
assumed a uniform distribution from 0 to 1. Other distributions
could be used instead.

If you want c to have a different set of random values every
time it is used, do

c := RandomReal[1,{n}]

However, for a set of random values this probably isn't what you
want. That is if you used this definition doing things like
Variance[c] would give different results every time. Probably
not what you want.

I can't see any reason to use Clear within a Do loop. If I
needed new values to be assigned, I would simply assign the new
values to the variables. No need to Clear first. And if I needed
the variable to behave as if it had no assignment, I would
localize it using Block.

Basically, it seems to me Clear is meant to be used
interactively rather than via a program.

=46inally, while both c[1][2] and c[1,2] can be thought of as
indexed variables they are different from usual variables.

When you do c[1][2] = 0, you will find c has SubValues, no
OwnValues and no DownValues.

When you do c[1,2]=0, you will find c has no Subvalues, no
OwnValues and does have DownValues.

And for the usual variable assignment c= 0, you will find c has
no Subvalues, has OwnValues and no DownValues.

These differences will impact usage of c.



  • Prev by Date: Re: Why doesn't TrueQ return True here?
  • Next by Date: Re: Graphics not displayed
  • Previous by thread: Re: Convenient way to add inline formatting to usage Messages
  • Next by thread: Re: Multiple use of Set on lists