summaryrefslogtreecommitdiff
path: root/packages/hmatrix
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix')
-rw-r--r--packages/hmatrix/hmatrix.cabal15
-rw-r--r--packages/hmatrix/src/Numeric/Container.hs23
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Fitting.hs2
-rw-r--r--packages/hmatrix/src/Numeric/GSL/IO.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs)6
-rw-r--r--packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs38
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Random.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs)10
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Vector.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs)43
-rw-r--r--packages/hmatrix/src/Numeric/LinearAlgebra.hs20
8 files changed, 56 insertions, 101 deletions
diff --git a/packages/hmatrix/hmatrix.cabal b/packages/hmatrix/hmatrix.cabal
index a61c64f..83a2188 100644
--- a/packages/hmatrix/hmatrix.cabal
+++ b/packages/hmatrix/hmatrix.cabal
@@ -1,5 +1,5 @@
1Name: hmatrix 1Name: hmatrix-gsl
2Version: 0.16.0.1 2Version: 0.16.0.0
3License: GPL 3License: GPL
4License-file: LICENSE 4License-file: LICENSE
5Author: Alberto Ruiz 5Author: Alberto Ruiz
@@ -73,7 +73,7 @@ flag debugnan
73 73
74library 74library
75 75
76 Build-Depends: base, hmatrix-base, array, vector, 76 Build-Depends: base, hmatrix, array, vector,
77 process, random 77 process, random
78 78
79 79
@@ -90,13 +90,12 @@ library
90 Numeric.GSL.Fitting, 90 Numeric.GSL.Fitting,
91 Numeric.GSL.ODE, 91 Numeric.GSL.ODE,
92 Numeric.GSL, 92 Numeric.GSL,
93 Numeric.LinearAlgebra, 93 Numeric.GSL.LinearAlgebra,
94 Numeric.Container,
95 Graphics.Plot 94 Graphics.Plot
96 other-modules: Numeric.GSL.Internal, 95 other-modules: Numeric.GSL.Internal,
97 Numeric.GSL.LinearAlgebra, 96 Numeric.GSL.Vector,
98 Numeric.LinearAlgebra.IO, 97 Numeric.GSL.IO,
99 Numeric.LinearAlgebra.Random 98 Numeric.GSL.Random
100 99
101 100
102 C-sources: src/Numeric/GSL/gsl-aux.c 101 C-sources: src/Numeric/GSL/gsl-aux.c
diff --git a/packages/hmatrix/src/Numeric/Container.hs b/packages/hmatrix/src/Numeric/Container.hs
deleted file mode 100644
index 2b61d90..0000000
--- a/packages/hmatrix/src/Numeric/Container.hs
+++ /dev/null
@@ -1,23 +0,0 @@
1-----------------------------------------------------------------------------
2{- |
3Module : Numeric.Container
4Copyright : (c) Alberto Ruiz 2006-14
5License : GPL
6Maintainer : Alberto Ruiz
7Stability : provisional
8
9-}
10-----------------------------------------------------------------------------
11
12module Numeric.Container (
13 module Data.Packed.Numeric,
14 module Numeric.LinearAlgebra.IO,
15 module Numeric.LinearAlgebra.Random,
16 meanCov
17) where
18
19import Data.Packed.Numeric hiding (saveMatrix, loadMatrix)
20import Numeric.LinearAlgebra.IO
21import Numeric.LinearAlgebra.Random hiding (Seed)
22import Numeric.LinearAlgebra.Util(meanCov)
23
diff --git a/packages/hmatrix/src/Numeric/GSL/Fitting.hs b/packages/hmatrix/src/Numeric/GSL/Fitting.hs
index 0a92373..93fb281 100644
--- a/packages/hmatrix/src/Numeric/GSL/Fitting.hs
+++ b/packages/hmatrix/src/Numeric/GSL/Fitting.hs
@@ -116,7 +116,7 @@ err (model,deriv) dat vsol = zip sol errs where
116 dof = length dat - (rows cov) 116 dof = length dat - (rows cov)
117 chi = norm2 (fromList $ cost (resMs model) dat sol) 117 chi = norm2 (fromList $ cost (resMs model) dat sol)
118 js = fromLists $ jacobian (resDs deriv) dat sol 118 js = fromLists $ jacobian (resDs deriv) dat sol
119 cov = inv $ trans js <> js 119 cov = inv $ trans js <.> js
120 errs = toList $ scalar c * sqrt (takeDiag cov) 120 errs = toList $ scalar c * sqrt (takeDiag cov)
121 121
122 122
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs b/packages/hmatrix/src/Numeric/GSL/IO.hs
index d2278ee..0d6031a 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra/IO.hs
+++ b/packages/hmatrix/src/Numeric/GSL/IO.hs
@@ -1,6 +1,6 @@
1----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
2-- | 2-- |
3-- Module : Numeric.LinearAlgebra.IO 3-- Module : Numeric.GSL.IO
4-- Copyright : (c) Alberto Ruiz 2007-14 4-- Copyright : (c) Alberto Ruiz 2007-14
5-- License : GPL 5-- License : GPL
6-- Maintainer : Alberto Ruiz 6-- Maintainer : Alberto Ruiz
@@ -8,14 +8,14 @@
8-- 8--
9----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
10 10
11module Numeric.LinearAlgebra.IO ( 11module Numeric.GSL.IO (
12 saveMatrix, 12 saveMatrix,
13 fwriteVector, freadVector, fprintfVector, fscanfVector, 13 fwriteVector, freadVector, fprintfVector, fscanfVector,
14 fileDimensions, loadMatrix, fromFile 14 fileDimensions, loadMatrix, fromFile
15) where 15) where
16 16
17import Data.Packed 17import Data.Packed
18import Numeric.GSL.LinearAlgebra 18import Numeric.GSL.Vector
19import System.Process(readProcess) 19import System.Process(readProcess)
20 20
21 21
diff --git a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
index 8bd0cc2..17e2258 100644
--- a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
+++ b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
@@ -9,13 +9,13 @@
9----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
10 10
11module Numeric.GSL.LinearAlgebra ( 11module Numeric.GSL.LinearAlgebra (
12 randomVector, 12 RandDist(..), randomVector,
13 saveMatrix, 13 saveMatrix,
14 fwriteVector, freadVector, fprintfVector, fscanfVector 14 fwriteVector, freadVector, fprintfVector, fscanfVector,
15 fileDimensions, loadMatrix, fromFile
15) where 16) where
16 17
17import Data.Packed 18import Data.Packed
18import Numeric.LinearAlgebra.Base(RandDist(..))
19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) 19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM)
20 20
21import Foreign.Marshal.Alloc(free) 21import Foreign.Marshal.Alloc(free)
@@ -23,11 +23,16 @@ import Foreign.Ptr(Ptr)
23import Foreign.C.Types 23import Foreign.C.Types
24import Foreign.C.String(newCString) 24import Foreign.C.String(newCString)
25import System.IO.Unsafe(unsafePerformIO) 25import System.IO.Unsafe(unsafePerformIO)
26import System.Process(readProcess)
26 27
27fromei x = fromIntegral (fromEnum x) :: CInt 28fromei x = fromIntegral (fromEnum x) :: CInt
28 29
29----------------------------------------------------------------------- 30-----------------------------------------------------------------------
30 31
32data RandDist = Uniform -- ^ uniform distribution in [0,1)
33 | Gaussian -- ^ normal distribution with mean zero and standard deviation one
34 deriving Enum
35
31-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. 36-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed.
32randomVector :: Int -- ^ seed 37randomVector :: Int -- ^ seed
33 -> RandDist -- ^ distribution 38 -> RandDist -- ^ distribution
@@ -35,10 +40,10 @@ randomVector :: Int -- ^ seed
35 -> Vector Double 40 -> Vector Double
36randomVector seed dist n = unsafePerformIO $ do 41randomVector seed dist n = unsafePerformIO $ do
37 r <- createVector n 42 r <- createVector n
38 app1 (c_random_vector_GSL (fi seed) ((fi.fromEnum) dist)) vec r "randomVectorGSL" 43 app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector"
39 return r 44 return r
40 45
41foreign import ccall unsafe "random_vector_GSL" c_random_vector_GSL :: CInt -> CInt -> TV 46foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV
42 47
43-------------------------------------------------------------------------------- 48--------------------------------------------------------------------------------
44 49
@@ -105,3 +110,26 @@ type PD = Ptr Double --
105type TV = CInt -> PD -> IO CInt -- 110type TV = CInt -> PD -> IO CInt --
106type TM = CInt -> CInt -> PD -> IO CInt -- 111type TM = CInt -> CInt -> PD -> IO CInt --
107 112
113--------------------------------------------------------------------------------
114
115{- | obtains the number of rows and columns in an ASCII data file
116 (provisionally using unix's wc).
117-}
118fileDimensions :: FilePath -> IO (Int,Int)
119fileDimensions fname = do
120 wcres <- readProcess "wc" ["-w",fname] ""
121 contents <- readFile fname
122 let tot = read . head . words $ wcres
123 c = length . head . dropWhile null . map words . lines $ contents
124 if tot > 0
125 then return (tot `div` c, c)
126 else return (0,0)
127
128-- | Loads a matrix from an ASCII file formatted as a 2D table.
129loadMatrix :: FilePath -> IO (Matrix Double)
130loadMatrix file = fromFile file =<< fileDimensions file
131
132-- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
133fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
134fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c)
135
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs b/packages/hmatrix/src/Numeric/GSL/Random.hs
index fa125a0..2872b17 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra/Random.hs
+++ b/packages/hmatrix/src/Numeric/GSL/Random.hs
@@ -1,6 +1,6 @@
1----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
2-- | 2-- |
3-- Module : Numeric.LinearAlgebra.Random 3-- Module : Numeric.GSL.Random
4-- Copyright : (c) Alberto Ruiz 2009-14 4-- Copyright : (c) Alberto Ruiz 2009-14
5-- License : GPL 5-- License : GPL
6-- 6--
@@ -11,7 +11,7 @@
11-- 11--
12----------------------------------------------------------------------------- 12-----------------------------------------------------------------------------
13 13
14module Numeric.LinearAlgebra.Random ( 14module Numeric.GSL.Random (
15 Seed, 15 Seed,
16 RandDist(..), 16 RandDist(..),
17 randomVector, 17 randomVector,
@@ -20,9 +20,9 @@ module Numeric.LinearAlgebra.Random (
20 rand, randn 20 rand, randn
21) where 21) where
22 22
23import Numeric.GSL.LinearAlgebra 23import Numeric.GSL.Vector
24import Data.Packed.Numeric 24import Numeric.Container
25import Numeric.LinearAlgebra.Base(Seed,RandDist(..),cholSH) 25import Numeric.LinearAlgebra(Seed,RandDist(..),cholSH)
26import System.Random(randomIO) 26import System.Random(randomIO)
27 27
28 28
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs b/packages/hmatrix/src/Numeric/GSL/Vector.hs
index 1fde621..af79f32 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra/GSL.hs
+++ b/packages/hmatrix/src/Numeric/GSL/Vector.hs
@@ -1,6 +1,6 @@
1----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
2-- | 2-- |
3-- Module : Numeric.LinearAlgebra.GSL 3-- Module : Numeric.GSL.Vector
4-- Copyright : (c) Alberto Ruiz 2007-14 4-- Copyright : (c) Alberto Ruiz 2007-14
5-- License : GPL 5-- License : GPL
6-- Maintainer : Alberto Ruiz 6-- Maintainer : Alberto Ruiz
@@ -8,32 +8,26 @@
8-- 8--
9----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
10 10
11module Numeric.LinearAlgebra.GSL ( 11module Numeric.GSL.Vector (
12 RandDist(..), randomVector, 12 randomVector,
13 saveMatrix, 13 saveMatrix,
14 fwriteVector, freadVector, fprintfVector, fscanfVector, 14 fwriteVector, freadVector, fprintfVector, fscanfVector
15 fileDimensions, loadMatrix, fromFile
16) where 15) where
17 16
18import Data.Packed 17import Data.Packed
18import Numeric.LinearAlgebra(RandDist(..))
19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) 19import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM)
20 20
21import Data.Complex
22import Foreign.Marshal.Alloc(free) 21import Foreign.Marshal.Alloc(free)
23import Foreign.Ptr(Ptr) 22import Foreign.Ptr(Ptr)
24import Foreign.C.Types 23import Foreign.C.Types
25import Foreign.C.String(newCString) 24import Foreign.C.String(newCString)
26import System.IO.Unsafe(unsafePerformIO) 25import System.IO.Unsafe(unsafePerformIO)
27import System.Process(readProcess)
28 26
29fromei x = fromIntegral (fromEnum x) :: CInt 27fromei x = fromIntegral (fromEnum x) :: CInt
30 28
31----------------------------------------------------------------------- 29-----------------------------------------------------------------------
32 30
33data RandDist = Uniform -- ^ uniform distribution in [0,1)
34 | Gaussian -- ^ normal distribution with mean zero and standard deviation one
35 deriving Enum
36
37-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed. 31-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed.
38randomVector :: Int -- ^ seed 32randomVector :: Int -- ^ seed
39 -> RandDist -- ^ distribution 33 -> RandDist -- ^ distribution
@@ -41,10 +35,10 @@ randomVector :: Int -- ^ seed
41 -> Vector Double 35 -> Vector Double
42randomVector seed dist n = unsafePerformIO $ do 36randomVector seed dist n = unsafePerformIO $ do
43 r <- createVector n 37 r <- createVector n
44 app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector" 38 app1 (c_random_vector_GSL (fi seed) ((fi.fromEnum) dist)) vec r "randomVectorGSL"
45 return r 39 return r
46 40
47foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV 41foreign import ccall unsafe "random_vector_GSL" c_random_vector_GSL :: CInt -> CInt -> TV
48 42
49-------------------------------------------------------------------------------- 43--------------------------------------------------------------------------------
50 44
@@ -111,26 +105,3 @@ type PD = Ptr Double --
111type TV = CInt -> PD -> IO CInt -- 105type TV = CInt -> PD -> IO CInt --
112type TM = CInt -> CInt -> PD -> IO CInt -- 106type TM = CInt -> CInt -> PD -> IO CInt --
113 107
114--------------------------------------------------------------------------------
115
116{- | obtains the number of rows and columns in an ASCII data file
117 (provisionally using unix's wc).
118-}
119fileDimensions :: FilePath -> IO (Int,Int)
120fileDimensions fname = do
121 wcres <- readProcess "wc" ["-w",fname] ""
122 contents <- readFile fname
123 let tot = read . head . words $ wcres
124 c = length . head . dropWhile null . map words . lines $ contents
125 if tot > 0
126 then return (tot `div` c, c)
127 else return (0,0)
128
129-- | Loads a matrix from an ASCII file formatted as a 2D table.
130loadMatrix :: FilePath -> IO (Matrix Double)
131loadMatrix file = fromFile file =<< fileDimensions file
132
133-- | Loads a matrix from an ASCII file (the number of rows and columns must be known in advance).
134fromFile :: FilePath -> (Int,Int) -> IO (Matrix Double)
135fromFile filename (r,c) = reshape c `fmap` fscanfVector filename (r*c)
136
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/LinearAlgebra.hs
deleted file mode 100644
index 71ed808..0000000
--- a/packages/hmatrix/src/Numeric/LinearAlgebra.hs
+++ /dev/null
@@ -1,20 +0,0 @@
1-----------------------------------------------------------------------------
2{- |
3Module : Numeric.LinearAlgebra
4Copyright : (c) Alberto Ruiz 2006-14
5License : GPL
6Maintainer : Alberto Ruiz
7Stability : provisional
8
9-}
10-----------------------------------------------------------------------------
11
12module Numeric.LinearAlgebra (
13 module Numeric.Container,
14 module Numeric.LinearAlgebra.Algorithms,
15) where
16
17import Numeric.Container
18import Numeric.LinearAlgebra.Algorithms
19import Numeric.LinearAlgebra.Util()
20