MathGroup Archive 1998

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

Search the Archive

Re: Re: Compile and NormalDistribution

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15226] Re: [mg15220] Re: Compile and NormalDistribution
  • From: jtischer at col2.telecom.com.co (Cl-Jurgen Tischer)
  • Date: Wed, 23 Dec 1998 01:04:03 -0500
  • Organization: Universidad del Valle
  • References: <75f9jo$7mi@smc.vnet.net> <199812220901.EAA29138@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul,
as to speed, 

In[1]:=  Timing[tmp=gaussianArray[128] ;] Out[1]= {1.43 Second,Null}

In[2]:= First[Timing[tmp = Table[gaussian[0, 1], {180}, {180}]; ]]
Out[2]= 1.92 Second

with a 300Mhz PII, 128M Ram, clone. But since it's for the fun of it, I
offer a (moderate) speedup:

In[3]:= fu=Compile[{{mu, _Real}, {sigma,
_Real},{n,_Integer},{m,_Integer}},
    Table[mu + E^(2\[Pi] Random[] I) sigma Sqrt[-2
Log[Random[]]],{n},{m}]]

In[4]:= First[Timing[tmp=fu[0,1,180,180];]]

Out[4]= 0.66 Second

Jurgen

Paul Abbott wrote:
> 
> Andrew Watson wrote:
> 
> > In[1]:= <<Statistics`ContinuousDistributions`
> >
> > gaussianArray[n_] :=
> >         RandomArray[NormalDistribution[0.0, 1.0],{n,n}]  +
> >          I  RandomArray[NormalDistribution[0.0, 1.0],{n,n}]
> >
> > In[6]:= Timing[tmp=gaussianArray[128] ;]
> >
> > Out[6]= {1.26667 Second,Null}
> 
> You must have a very fast computer. This is much slower on my PowerMac!
> 
> > At 11:11 PM -0800 12/17/98, Brett Patterson wrote:
> > >
> > >I am trying to speed up the generation of a large array of random
> > >complex Gaussian numbers (e.g. 360 x 360).
> 
> Some comments. If you look at Statistics`NormalDistribution` you can see
> how Random[] and RandomArray[] works for NormalDistribution[]:
> 
> normal = Compile[{{mu, _Real}, {sigma, _Real},
>         {q1, _Real}, {q2, _Real}},
>         mu + sigma Sqrt[-2 Log[q1]] Cos[2Pi q2] ]
> 
> NormalDistribution/: Random[NormalDistribution[mu_:0, sigma_:1]] :=
>         normal[mu, sigma, Random[], Random[]]
> 
> normalpair = Compile[{{mu, _Real}, {sigma, _Real},
>         {q1, _Real}, {q2, _Real}},
>         mu + sigma Sqrt[-2 Log[q1]] {Cos[2Pi q2], Sin[2Pi q2]}]
> 
> NormalDistribution/: RandomArray[
>         NormalDistribution[mu_:0, sigma_:1], dim_] :=
>           Module[{n, array},
>                 n = If[VectorQ[dim], Apply[Times, dim], dim];
>                 array = Flatten[Table[normalpair[mu, sigma, Random[], Random[]],
>                  {Quotient[n, 2]}]];
>     If[OddQ[n],
>        AppendTo[array, normal[mu, sigma, Random[], Random[]] ]  ];
>     If[VectorQ[dim] && Length[dim] > 1,
>        Fold[Partition[#1, #2]&, array, Reverse[Drop[dim, 1]] ],
>        array  ]
>   ] /;
>   (IntegerQ[dim] && dim > 0) || VectorQ[dim, (IntegerQ[#] && # > 0)&]
> 
> The key points are that:
> 
> [1] Random[NormalDistribution[]] is already compiled. [2] normalpair
> generates a _pair_ of random numbers.
> 
> We can easily modify normalpair to generate complex Gaussian numbers:
> 
> In[1]:= gaussian = Compile[{{mu, _Real}, {sigma, _Real}},
>    mu + E^(2 Pi I Random[]) sigma Sqrt[-2 Log[Random[]]]]
> 
> In[2]:= First[Timing[tmp = Table[gaussian[0, 1], {180}, {180}]; ]]
> Out[2]= 6.4 Second
> 
> Cheers,
>         Paul
> 
> ____________________________________________________________________
> Paul Abbott                                   Phone: +61-8-9380-2734
> Department of Physics                           Fax: +61-8-9380-1014
> The University of Western Australia            Nedlands WA  6907
> mailto:paul at physics.uwa.edu.au  AUSTRALIA
> http://www.physics.uwa.edu.au/~paul
> 
>             God IS a weakly left-handed dice player
> ____________________________________________________________________



  • Prev by Date: Q: extra carriage returns in OutputForm[] output?
  • Next by Date: Re: cov matrix of sample means, variances and covariances
  • Previous by thread: Re: Compile and NormalDistribution
  • Next by thread: Re: Re: Compile and NormalDistribution