Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

Re: Dont Understand

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15895] Re: Dont Understand
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 17 Feb 1999 23:33:41 -0500
  • References: <7a2bm8$1ts@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Don McKenzie Paul wrote in message <7a2bm8$1ts at smc.vnet.net>...
>
>I'm not an expert in Mathematica and would appreciate comments on why
>init1 returns a list with all zeros replaced by -1, but init does not.
>Its probably very simple but its confused me.
>
>Don
>
>init[n_Integer]:=Module[
>                    {s=Table[Random[Integer],{n n}]},
>                    s/.{0->-1};
>                    Partition[s,n]
>                    ]
>
>init1[n_Integer]:=Module[
>                    {s=Table[Random[Integer],{n n}]},
>                    s/.{0->-1}
>                    ]
>
>
>--
>
>Prof. Don McKenzie Paul               tel.  (1203) 523603 Department of
>Physics                 fax.  (1203) 692016 University of Warwick
>email  phrje at csv.warwick.ac.uk COVENTRY CV4 7AL
>UK
>
>

Don:

The problem has nothing to do with Module - which is in fact not needed.

A) With

>init[n_Integer]:=Module[
>                    {s=Table[Random[Integer],{n n}]},
>                    s/.{0->-1};
>                    Partition[s,n]
>                    ]

s/.{0->-1} give s with 0 replaced by -1, but the value of the symbol s is
unchange and this unchange value is used in Partition[s,n}

Two ways out:

1) to illustrate the problem

init[n_Integer]:=Module[
                       {s=Table[Random[Integer],{n n}]},
                       s =  s/.{0->-1};
                       Partition[s,n]
                      ]

2) better Mathematic (functional) code

init[n_Integer]:= Partition[Table[Random[Integer],{n n}]/. {0->-1}, n]

3) Avoiding Partition

init[n_Integer]:= Table[Random[Integer],{n},{n}]/. 0->-1


B) With

init1[n_Integer]:=Module[
                    {s=Table[Random[Integer],{n n}]},
                    s/.{0->-1}
                    ]
s  is operated on to give the result.

We get the same result with

init1[n_Integer]:= Table[Random[Integer],{n n}]/.{0->-1}


Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester, UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565













  • Prev by Date: Re: PlotVectorField Package (easy question?)
  • Next by Date: Re: 3D List Plots
  • Previous by thread: Re: Dont Understand
  • Next by thread: Re: Dont Understand