From 5d15d765c0204854b587b03232b20336ddf91ced Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 16 May 2014 09:28:57 +0200 Subject: move rand, randn --- packages/hmatrix/src/Numeric/HMatrix.hs | 2 ++ packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs | 27 ------------------- packages/hmatrix/src/Numeric/Random.hs | 30 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 28 deletions(-) (limited to 'packages/hmatrix/src/Numeric') diff --git a/packages/hmatrix/src/Numeric/HMatrix.hs b/packages/hmatrix/src/Numeric/HMatrix.hs index c8742c4..36fcf70 100644 --- a/packages/hmatrix/src/Numeric/HMatrix.hs +++ b/packages/hmatrix/src/Numeric/HMatrix.hs @@ -134,4 +134,6 @@ import Numeric.Vector() import Numeric.Container import Numeric.LinearAlgebra.Algorithms import Numeric.LinearAlgebra.Util +import Numeric.Random + diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs b/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs index 7d134bf..fb2d54c 100644 --- a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs @@ -22,7 +22,6 @@ module Numeric.LinearAlgebra.Util( col, (&), (¦), (——), (#), (?), (¿), - rand, randn, cross, norm, unitary, @@ -60,7 +59,6 @@ import Numeric.LinearAlgebra.Algorithms hiding (i) import Numeric.Matrix() import Numeric.Vector() -import System.Random(randomIO) import Numeric.LinearAlgebra.Util.Convolution import Graphics.Plot @@ -77,31 +75,6 @@ disp :: Int -> Matrix Double -> IO () disp n = putStrLn . dispf n --- | pseudorandom matrix with uniform elements between 0 and 1 -randm :: RandDist - -> Int -- ^ rows - -> Int -- ^ columns - -> IO (Matrix Double) -randm d r c = do - seed <- randomIO - return (reshape c $ randomVector seed d (r*c)) - --- | pseudorandom matrix with uniform elements between 0 and 1 -rand :: Int -> Int -> IO (Matrix Double) -rand = randm Uniform - -{- | pseudorandom matrix with normal elements - ->>> x <- randn 3 5 ->>> disp 3 x -3x5 -0.386 -1.141 0.491 -0.510 1.512 -0.069 -0.919 1.022 -0.181 0.745 -0.313 -0.670 -0.097 -1.575 -0.583 - --} -randn :: Int -> Int -> IO (Matrix Double) -randn = randm Gaussian {- | create a real diagonal matrix from a list 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 ( RandDist(..), randomVector, gaussianSample, - uniformSample + uniformSample, + rand, randn ) where import Numeric.GSL.Vector import Data.Packed import Data.Packed.Numeric import Numeric.LinearAlgebra.Algorithms +import System.Random(randomIO) type Seed = Int @@ -55,3 +57,29 @@ uniformSample seed n rgs = m where am = konst' 1 n `outer` a m = fromColumns (zipWith scale cs dat) `add` am +-- | pseudorandom matrix with uniform elements between 0 and 1 +randm :: RandDist + -> Int -- ^ rows + -> Int -- ^ columns + -> IO (Matrix Double) +randm d r c = do + seed <- randomIO + return (reshape c $ randomVector seed d (r*c)) + +-- | pseudorandom matrix with uniform elements between 0 and 1 +rand :: Int -> Int -> IO (Matrix Double) +rand = randm Uniform + +{- | pseudorandom matrix with normal elements + +>>> x <- randn 3 5 +>>> disp 3 x +3x5 +0.386 -1.141 0.491 -0.510 1.512 +0.069 -0.919 1.022 -0.181 0.745 +0.313 -0.670 -0.097 -1.575 -0.583 + +-} +randn :: Int -> Int -> IO (Matrix Double) +randn = randm Gaussian + -- cgit v1.2.3