summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-11 11:10:33 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-11 11:10:33 +0000
commit6859c5712a85950b5bc3de3fe8352f4592bc273b (patch)
treede9008ebc9c25792237b4df5a4aa8e60057cf55a /lib/Data
parent84a5ee4fb1b2185eabf64b761279b4da313bd207 (diff)
added generic konst
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Random.hs10
1 files changed, 4 insertions, 6 deletions
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs
index b30b299..c34f212 100644
--- a/lib/Data/Packed/Random.hs
+++ b/lib/Data/Packed/Random.hs
@@ -22,10 +22,8 @@ module Data.Packed.Random (
22import Numeric.GSL.Vector 22import Numeric.GSL.Vector
23import Data.Packed 23import Data.Packed
24import Numeric.Container 24import Numeric.Container
25import Data.Packed.Internal(constantD)
26import Numeric.LinearAlgebra.Algorithms 25import Numeric.LinearAlgebra.Algorithms
27 26
28constant k v = constantD k v
29 27
30-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate 28-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
31-- Gaussian distribution. 29-- Gaussian distribution.
@@ -36,7 +34,7 @@ gaussianSample :: Int -- ^ seed
36 -> Matrix Double -- ^ result 34 -> Matrix Double -- ^ result
37gaussianSample seed n med cov = m where 35gaussianSample seed n med cov = m where
38 c = dim med 36 c = dim med
39 meds = constant 1 n `outer` med 37 meds = konst 1 n `outer` med
40 rs = reshape c $ randomVector seed Gaussian (c * n) 38 rs = reshape c $ randomVector seed Gaussian (c * n)
41 m = rs `mXm` cholSH cov `add` meds 39 m = rs `mXm` cholSH cov `add` meds
42 40
@@ -52,7 +50,7 @@ uniformSample seed n rgs = m where
52 cs = zipWith subtract as bs 50 cs = zipWith subtract as bs
53 d = dim a 51 d = dim a
54 dat = toRows $ reshape n $ randomVector seed Uniform (n*d) 52 dat = toRows $ reshape n $ randomVector seed Uniform (n*d)
55 am = constant 1 n `outer` a 53 am = konst 1 n `outer` a
56 m = fromColumns (zipWith scale cs dat) `add` am 54 m = fromColumns (zipWith scale cs dat) `add` am
57 55
58------------ utilities ------------------------------- 56------------ utilities -------------------------------
@@ -62,7 +60,7 @@ meanCov :: Matrix Double -> (Vector Double, Matrix Double)
62meanCov x = (med,cov) where 60meanCov x = (med,cov) where
63 r = rows x 61 r = rows x
64 k = 1 / fromIntegral r 62 k = 1 / fromIntegral r
65 med = constant k r `vXm` x 63 med = konst k r `vXm` x
66 meds = constant 1 r `outer` med 64 meds = konst 1 r `outer` med
67 xc = x `sub` meds 65 xc = x `sub` meds
68 cov = flip scale (trans xc `mXm` xc) (recip (fromIntegral (r-1))) 66 cov = flip scale (trans xc `mXm` xc) (recip (fromIntegral (r-1)))