summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-11-14 11:31:13 +0000
committerAlberto Ruiz <aruiz@um.es>2009-11-14 11:31:13 +0000
commit6bdf5355a26da547b775f29926c131d539e86e7c (patch)
tree7f732fe9817f514be9fbcbb9399808b7d308da34
parentd404bcf2859f6b4f94d34dec205895019ed564e8 (diff)
randomVector
-rw-r--r--lib/Data/Packed/Vector.hs3
-rw-r--r--lib/Numeric/GSL/Vector.hs21
-rw-r--r--lib/Numeric/GSL/gsl-aux.c20
3 files changed, 42 insertions, 2 deletions
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 21f51e5..836f11a 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -21,7 +21,8 @@ module Data.Packed.Vector (
21 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, 21 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex,
22 mapVector, zipVector, 22 mapVector, zipVector,
23 fscanfVector, fprintfVector, freadVector, fwriteVector, 23 fscanfVector, fprintfVector, freadVector, fwriteVector,
24 foldLoop, foldVector, foldVectorG 24 foldLoop, foldVector, foldVectorG,
25 RandDist(..), randomVector
25) where 26) where
26 27
27import Data.Packed.Internal 28import Data.Packed.Internal
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs
index 0366744..2b21de2 100644
--- a/lib/Numeric/GSL/Vector.hs
+++ b/lib/Numeric/GSL/Vector.hs
@@ -17,7 +17,8 @@ module Numeric.GSL.Vector (
17 FunCodeS(..), toScalarR, 17 FunCodeS(..), toScalarR,
18 FunCodeV(..), vectorMapR, vectorMapC, 18 FunCodeV(..), vectorMapR, vectorMapC,
19 FunCodeSV(..), vectorMapValR, vectorMapValC, 19 FunCodeSV(..), vectorMapValR, vectorMapValC,
20 FunCodeVV(..), vectorZipR, vectorZipC 20 FunCodeVV(..), vectorZipR, vectorZipC,
21 RandDist(..), randomVector
21) where 22) where
22 23
23import Data.Packed.Internal.Common 24import Data.Packed.Internal.Common
@@ -146,3 +147,21 @@ vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) ->
146vectorZipC = vectorZipAux c_vectorZipC 147vectorZipC = vectorZipAux c_vectorZipC
147 148
148foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV 149foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV
150
151-----------------------------------------------------------------------
152
153data RandDist = Uniform -- ^ uniform distribution in [0,1)
154 | Gaussian -- ^ normal distribution with mean zero and standard deviation one
155 deriving Enum
156
157-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed.
158randomVector :: Int -- ^ seed
159 -> RandDist -- ^ distribution
160 -> Int -- ^ vector size
161 -> Vector Double
162randomVector seed dist n = unsafePerformIO $ do
163 r <- createVector n
164 app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector"
165 return r
166
167foreign import ccall safe "random_vector" c_random_vector :: CInt -> CInt -> TV
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c
index d129aeb..0c71ca1 100644
--- a/lib/Numeric/GSL/gsl-aux.c
+++ b/lib/Numeric/GSL/gsl-aux.c
@@ -20,6 +20,8 @@
20#include <gsl/gsl_multimin.h> 20#include <gsl/gsl_multimin.h>
21#include <gsl/gsl_multiroots.h> 21#include <gsl/gsl_multiroots.h>
22#include <gsl/gsl_complex_math.h> 22#include <gsl/gsl_complex_math.h>
23#include <gsl/gsl_rng.h>
24#include <gsl/gsl_randist.h>
23#include <string.h> 25#include <string.h>
24#include <stdio.h> 26#include <stdio.h>
25 27
@@ -777,3 +779,21 @@ int rootj(int method, int f(int, double*, int, double*),
777 gsl_multiroot_fdfsolver_free(s); 779 gsl_multiroot_fdfsolver_free(s);
778 OK 780 OK
779} 781}
782
783//////////////////////////////////////////////////////
784
785#define RAN(C,F) case C: { for(k=0;k<rn;k++) { rp[k]= F(gen); }; OK }
786
787int random_vector(int seed, int code, RVEC(r)) {
788 DEBUGMSG("random_vector")
789 static gsl_rng * gen = NULL;
790 if (!gen) { gen = gsl_rng_alloc (gsl_rng_mt19937);}
791 gsl_rng_set (gen, seed);
792 int k;
793 switch (code) {
794 RAN(0,gsl_rng_uniform)
795 RAN(1,gsl_ran_ugaussian)
796 default: ERROR(BAD_CODE);
797 }
798}
799#undef RAN