summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/Random.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/Random.hs')
-rw-r--r--packages/hmatrix/src/Numeric/Random.hs30
1 files changed, 29 insertions, 1 deletions
diff --git a/packages/hmatrix/src/Numeric/Random.hs b/packages/hmatrix/src/Numeric/Random.hs
index b4c6cde..7bf9e8b 100644
--- a/packages/hmatrix/src/Numeric/Random.hs
+++ b/packages/hmatrix/src/Numeric/Random.hs
@@ -16,13 +16,15 @@ module Numeric.Random (
16 RandDist(..), 16 RandDist(..),
17 randomVector, 17 randomVector,
18 gaussianSample, 18 gaussianSample,
19 uniformSample 19 uniformSample,
20 rand, randn
20) where 21) where
21 22
22import Numeric.GSL.Vector 23import Numeric.GSL.Vector
23import Data.Packed 24import Data.Packed
24import Data.Packed.Numeric 25import Data.Packed.Numeric
25import Numeric.LinearAlgebra.Algorithms 26import Numeric.LinearAlgebra.Algorithms
27import System.Random(randomIO)
26 28
27 29
28type Seed = Int 30type Seed = Int
@@ -55,3 +57,29 @@ uniformSample seed n rgs = m where
55 am = konst' 1 n `outer` a 57 am = konst' 1 n `outer` a
56 m = fromColumns (zipWith scale cs dat) `add` am 58 m = fromColumns (zipWith scale cs dat) `add` am
57 59
60-- | pseudorandom matrix with uniform elements between 0 and 1
61randm :: RandDist
62 -> Int -- ^ rows
63 -> Int -- ^ columns
64 -> IO (Matrix Double)
65randm d r c = do
66 seed <- randomIO
67 return (reshape c $ randomVector seed d (r*c))
68
69-- | pseudorandom matrix with uniform elements between 0 and 1
70rand :: Int -> Int -> IO (Matrix Double)
71rand = randm Uniform
72
73{- | pseudorandom matrix with normal elements
74
75>>> x <- randn 3 5
76>>> disp 3 x
773x5
780.386 -1.141 0.491 -0.510 1.512
790.069 -0.919 1.022 -0.181 0.745
800.313 -0.670 -0.097 -1.575 -0.583
81
82-}
83randn :: Int -> Int -> IO (Matrix Double)
84randn = randm Gaussian
85