Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Re: Help with Speeding up a For loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99014] Re: [mg98916] Re: Help with Speeding up a For loop
  • From: dh <dh at metrohm.com>
  • Date: Fri, 24 Apr 2009 03:45:41 -0400 (EDT)
  • References: <gsivhv$9f6$1@smc.vnet.net> <200904220907.FAA13170@smc.vnet.net> <49EF3785.7010901@wolfram.com>

Hi Darren,
you are right of course.
This was easy, but I hope you will not mind if I will ask you 
occasionally a question I can not get an answer. There are not too many 
knowledgeable people around.
Daniel

Darren Glosemeyer wrote:
> A smaller additional speedup can be had by using one call to RandomReal 
> to get the normals, e.g.
> 
> Resolution = RandomReal[NormalDistribution[0, Sigma], Length[Ideal]] + 
> Ideal
> 
> This uses the fact that if z follows NormalDistribution[0,sigma], mu+z 
> follows NormalDistribution[mu,sigma]. This is more efficient because it 
> is faster to get a bunch of random numbers in one call than to get them 
> one at a time and the addition of Ideal is comparatively very fast.
> 
> Darren Glosemeyer
> Wolfram Research
> 
> dh wrote:
>> Hi Adam,
>>
>> first note that indices in Mathematica start at 1, not 0.
>>
>> Then I set the start value of Ideal and Resolution to an empty list.
>>
>> With this changes, your loop can be replaced by:
>>
>>
>>
>> Ideal = Pick[Eb1,
>>
>>     Thread@Less[k, Re[(E0 - Eb1)^2*Sqrt[1 - m^2/(E0 - Eb1)^2]]] ];
>>
>> Resolution = RandomReal[NormalDistribution[#, Sigma]] & /@ Ideal;
>>
>>
>>
>> For n=10^7, this takes 7.5 sec.
>>
>> Daniel
>>
>>
>>
>> Adam Dally wrote:
>>
>>  
>>> I am using an Intel MacBook with OS X 10.5.6.
>>>     
>>
>>  
>>  
>>> I am trying to create 2 lists: "Ideal" and "Resolution".  This is 
>>> basically
>>>     
>>
>>  
>>> a "Monte Carlo Integration" technique. Ideal should simulate the curve.
>>>     
>>
>>  
>>> Resolution should simulate the curve convoluted with a normal 
>>> distribution.
>>>     
>>
>>  
>>> I want to do this for n=10 000 000 or more, but it takes far too long 
>>> right
>>>     
>>
>>  
>>> now. I can do n=100 000 in about 1 minute, but 1 000 000 takes more 
>>> than an
>>>     
>>
>>  
>>> hour. I haven't waited long enough for 10 000 000 to finish (it has 
>>> been 5
>>>     
>>
>>  
>>> days).
>>>     
>>
>>  
>>  
>>> Thank you,
>>>     
>>
>>  
>>> Adam Dally
>>>     
>>
>>  
>>  
>>> Here is the code:
>>>     
>>
>>  
>>  
>>> ClearAll[E0, Eb1, m, DeltaE, Sigma, k, n, y3, Ideal, Resolution, i,
>>>     
>>
>>  
>>> normalizer, maxE, minE]
>>>     
>>
>>  
>>> Eb1 = 0; k = 0; n = 10000; E0 = 2470; m = 0.2; DeltaE = 50; Sigma = 
>>> 5; maxE
>>>     
>>
>>  
>>> = E0 - m; minE = E0 - DeltaE; Resolution = {Eb1}; Ideal = {Eb1};  
>>> (*Setup
>>>     
>>
>>  
>>> all constants, lists and ranges*)
>>>     
>>
>>  
>>  
>>> Eb1 = RandomReal[{minE, maxE}, n];  (*create a list of 'n' random Eb1
>>>     
>>
>>  
>>> values*)
>>>     
>>
>>  
>>> k = -RandomReal[TriangularDistribution[{-2470, 0}, -0.1], n]; (*create a
>>>     
>>
>>  
>>> list of 'n' random k values; triangle distribution gives more successful
>>>     
>>
>>  
>>> results*)
>>>     
>>
>>  
>>  
>>> For[i = 0, i < n, i++,
>>>     
>>
>>  
>>  
>>>  If[k[[i]] < Re[(E0 - Eb1[[i]])^2*Sqrt[1 - m^2/(E0 - Eb1[[i]])^2]], 
>>> (*check
>>>     
>>
>>  
>>> if the {k,Eb1} value is under the curve*)
>>>     
>>
>>  
>>>      AppendTo[Ideal, Eb1[[i]]];  (*Keep events under curve in 'Ideal'*)
>>>     
>>
>>  
>>>      y3 = Eb1[[i]]; (*cast element to a number*)
>>>     
>>
>>  
>>>      Eb1[[i]] = RandomReal[NormalDistribution[y3, Sigma], 1]; (*choose a
>>>     
>>
>>  
>>> random number from a normal distribution about that point*)
>>>     
>>
>>  
>>>      AppendTo[Resolution, Eb1[[i]]]; ]] (*Keep that event in 
>>> 'Resolution'*)
>>>     
>>
>>  
>>  
>>
>>   
> 
> 
> 
> 


-- 

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh at metrohm.com>
Internet:<http://www.metrohm.com>



  • Prev by Date: Re: Change Figure according to Session Time
  • Next by Date: Re: 3D Agent Problem
  • Previous by thread: Re: Re: Help with Speeding up a For loop
  • Next by thread: Re: Help with Speeding up a For loop