summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HSSL.cabal6
-rw-r--r--examples/pru.hs27
-rw-r--r--examples/tests.hs20
-rw-r--r--lib/Data/Packed/Internal/Common.hs84
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs109
-rw-r--r--lib/Data/Packed/Internal/Tensor.hs12
-rw-r--r--lib/Data/Packed/Internal/Vector.hs62
-rw-r--r--lib/Data/Packed/Matrix.hs72
-rw-r--r--lib/Data/Packed/Vector.hs41
-rw-r--r--lib/LAPACK.hs3
10 files changed, 252 insertions, 184 deletions
diff --git a/HSSL.cabal b/HSSL.cabal
index 28fe130..6622fa7 100644
--- a/HSSL.cabal
+++ b/HSSL.cabal
@@ -18,10 +18,10 @@ Extensions: ForeignFunctionInterface
18--ghc-options: -Wall 18--ghc-options: -Wall
19ghc-options: -O 19ghc-options: -O
20hs-source-dirs: lib 20hs-source-dirs: lib
21Exposed-modules: Data.Packed.Internal.Vector, 21Exposed-modules: Data.Packed.Internal, Data.Packed.Internal.Common,
22 Data.Packed.Internal.Matrix, 22 Data.Packed.Internal.Vector, Data.Packed.Vector,
23 Data.Packed.Internal.Matrix, Data.Packed.Matrix,
23 Data.Packed.Internal.Tensor, 24 Data.Packed.Internal.Tensor,
24 LAPACK.Internal,
25 LAPACK 25 LAPACK
26Other-modules: 26Other-modules:
27C-sources: lib/Data/Packed/Internal/aux.c, 27C-sources: lib/Data/Packed/Internal/aux.c,
diff --git a/examples/pru.hs b/examples/pru.hs
index 10789d2..a90aa6f 100644
--- a/examples/pru.hs
+++ b/examples/pru.hs
@@ -1,9 +1,11 @@
1--{-# OPTIONS_GHC #-} 1--{-# OPTIONS_GHC #-}
2--module Main where 2--module Main where
3 3
4import Data.Packed.Internal
4import Data.Packed.Internal.Vector 5import Data.Packed.Internal.Vector
5import Data.Packed.Internal.Matrix 6import Data.Packed.Internal.Matrix
6import Data.Packed.Internal.Tensor 7import Data.Packed.Internal.Tensor
8import Data.Packed.Matrix
7import LAPACK 9import LAPACK
8 10
9import Complex 11import Complex
@@ -11,17 +13,6 @@ import Numeric(showGFloat)
11import Data.List(transpose,intersperse,sort) 13import Data.List(transpose,intersperse,sort)
12import Foreign.Storable 14import Foreign.Storable
13 15
14r >< c = f where
15 f l | dim v == r*c = matrixFromVector RowMajor c v
16 | otherwise = error $ "inconsistent list size = "
17 ++show (dim v) ++"in ("++show r++"><"++show c++")"
18 where v = fromList l
19
20r >|< c = f where
21 f l | dim v == r*c = matrixFromVector ColumnMajor c v
22 | otherwise = error $ "inconsistent list size = "
23 ++show (dim v) ++"in ("++show r++"><"++show c++")"
24 where v = fromList l
25 16
26vr = fromList [1..15::Double] 17vr = fromList [1..15::Double]
27vc = fromList (map (\x->x :+ (x+1)) [1..15::Double]) 18vc = fromList (map (\x->x :+ (x+1)) [1..15::Double])
@@ -49,8 +40,10 @@ cf = mulF af bc
49r = mulC cc (trans cf) 40r = mulC cc (trans cf)
50 41
51rd = (2><2) 42rd = (2><2)
52 [ 43492.0, 50572.0 43 [ 27736.0, 65356.0
53 , 102550.0, 119242.0 ] 44 , 65356.0, 154006.0 ]
45
46
54 47
55main = do 48main = do
56 print $ r |=| rd 49 print $ r |=| rd
@@ -77,7 +70,6 @@ e i n = fromList [ delta k i | k <- [1..n]]
77 70
78diagl = diag.fromList 71diagl = diag.fromList
79 72
80ident n = diag (constant n 1)
81 73
82tensorFromVector idx v = T {dims = [(dim v,idx)], ten = v} 74tensorFromVector idx v = T {dims = [(dim v,idx)], ten = v}
83tensorFromMatrix idxr idxc m = T {dims = [(rows m,idxr),(cols m,idxc)], ten = cdat m} 75tensorFromMatrix idxr idxc m = T {dims = [(rows m,idxr),(cols m,idxc)], ten = cdat m}
@@ -107,10 +99,3 @@ pru = do
107 print $ normal t2 99 print $ normal t2
108 print $ foldl part t2 [("j'",0),("p",1),("r",1)] 100 print $ foldl part t2 [("j'",0),("p",1),("r",1)]
109 101
110
111names t = sort $ map (snd.snd) (dims t)
112
113normal t = tridx (names t) t
114
115contractions t1 t2 = [ contraction t1 n1 t2 n2 | n1 <- names t1, n2 <- names t2, compatIdx t1 n1 t2 n2 ]
116
diff --git a/examples/tests.hs b/examples/tests.hs
index b075704..9f0ae2a 100644
--- a/examples/tests.hs
+++ b/examples/tests.hs
@@ -4,7 +4,9 @@
4 4
5----------------------------------------------------------------------------- 5-----------------------------------------------------------------------------
6 6
7import Data.Packed.Internal.Vector 7import Data.Packed.Internal
8import Data.Packed.Vector
9import Data.Packed.Matrix
8import Data.Packed.Internal.Matrix 10import Data.Packed.Internal.Matrix
9import LAPACK 11import LAPACK
10import Test.QuickCheck 12import Test.QuickCheck
@@ -31,18 +33,6 @@ nullspaceProp tol m = cr > 0 ==> m <> nt ~~ zeros
31 33
32-} 34-}
33 35
34r >< c = f where
35 f l | dim v == r*c = matrixFromVector RowMajor c v
36 | otherwise = error $ "inconsistent list size = "
37 ++show (dim v) ++"in ("++show r++"><"++show c++")"
38 where v = fromList l
39
40r >|< c = f where
41 f l | dim v == r*c = matrixFromVector ColumnMajor c v
42 | otherwise = error $ "inconsistent list size = "
43 ++show (dim v) ++"in ("++show r++"><"++show c++")"
44 where v = fromList l
45
46ac = (2><3) [1 .. 6::Double] 36ac = (2><3) [1 .. 6::Double]
47bc = (3><4) [7 .. 18::Double] 37bc = (3><4) [7 .. 18::Double]
48 38
@@ -82,8 +72,8 @@ cf = mulF af bc
82r = mulC cc (trans cf) 72r = mulC cc (trans cf)
83 73
84rd = (2><2) 74rd = (2><2)
85 [ 43492.0, 50572.0 75 [ 27736.0, 65356.0
86 , 102550.0, 119242.0 :: Double] 76 , 65356.0, 154006.0 ::Double]
87 77
88instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where 78instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where
89 arbitrary = do 79 arbitrary = do
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs
new file mode 100644
index 0000000..dddd269
--- /dev/null
+++ b/lib/Data/Packed/Internal/Common.hs
@@ -0,0 +1,84 @@
1{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
2-----------------------------------------------------------------------------
3-- |
4-- Module : Data.Packed.Internal.Common
5-- Copyright : (c) Alberto Ruiz 2007
6-- License : GPL-style
7--
8-- Maintainer : Alberto Ruiz <aruiz@um.es>
9-- Stability : provisional
10-- Portability : portable (uses FFI)
11--
12-- Common tools
13--
14-----------------------------------------------------------------------------
15
16module Data.Packed.Internal.Common where
17
18import Foreign
19import Complex
20import Control.Monad(when)
21import Debug.Trace
22import Data.List(transpose,intersperse)
23import Data.Typeable
24import Data.Maybe(fromJust)
25
26debug x = trace (show x) x
27
28data Vector t = V { dim :: Int
29 , fptr :: ForeignPtr t
30 , ptr :: Ptr t
31 } deriving Typeable
32
33----------------------------------------------------------------------
34instance (Storable a, RealFloat a) => Storable (Complex a) where --
35 alignment x = alignment (realPart x) --
36 sizeOf x = 2 * sizeOf (realPart x) --
37 peek p = do --
38 [re,im] <- peekArray 2 (castPtr p) --
39 return (re :+ im) --
40 poke p (a :+ b) = pokeArray (castPtr p) [a,b] --
41----------------------------------------------------------------------
42
43on f g = \x y -> f (g x) (g y)
44
45partit :: Int -> [a] -> [[a]]
46partit _ [] = []
47partit n l = take n l : partit n (drop n l)
48
49-- | obtains the common value of a property of a list
50common :: (Eq a) => (b->a) -> [b] -> Maybe a
51common f = commonval . map f where
52 commonval :: (Eq a) => [a] -> Maybe a
53 commonval [] = Nothing
54 commonval [a] = Just a
55 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing
56
57xor a b = a && not b || b && not a
58
59(//) :: x -> (x -> y) -> y
60infixl 0 //
61(//) = flip ($)
62
63errorCode 1000 = "bad size"
64errorCode 1001 = "bad function code"
65errorCode 1002 = "memory problem"
66errorCode 1003 = "bad file"
67errorCode 1004 = "singular"
68errorCode 1005 = "didn't converge"
69errorCode n = "code "++show n
70
71check msg ls f = do
72 err <- f
73 when (err/=0) (error (msg++": "++errorCode err))
74 mapM_ (touchForeignPtr . fptr) ls
75 return ()
76
77class (Storable a, Typeable a) => Field a where
78instance (Storable a, Typeable a) => Field a where
79
80isReal w x = typeOf (undefined :: Double) == typeOf (w x)
81isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x)
82
83scast :: forall a . forall b . (Typeable a, Typeable b) => a -> b
84scast = fromJust . cast
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index bae56f1..2c0acdf 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -1,4 +1,4 @@
1{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} 1{-# OPTIONS_GHC -fglasgow-exts #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3-- | 3-- |
4-- Module : Data.Packed.Internal.Matrix 4-- Module : Data.Packed.Internal.Matrix
@@ -15,18 +15,16 @@
15 15
16module Data.Packed.Internal.Matrix where 16module Data.Packed.Internal.Matrix where
17 17
18import Data.Packed.Internal.Common
18import Data.Packed.Internal.Vector 19import Data.Packed.Internal.Vector
19 20
20import Foreign hiding (xor) 21import Foreign hiding (xor)
21import Complex 22import Complex
22import Control.Monad(when) 23import Control.Monad(when)
23import Debug.Trace
24import Data.List(transpose,intersperse) 24import Data.List(transpose,intersperse)
25import Data.Typeable 25import Data.Typeable
26import Data.Maybe(fromJust) 26import Data.Maybe(fromJust)
27 27
28debug x = trace (show x) x
29
30 28
31data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) 29data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq)
32 30
@@ -39,7 +37,7 @@ data Matrix t = M { rows :: Int
39 , order :: MatrixOrder 37 , order :: MatrixOrder
40 } deriving Typeable 38 } deriving Typeable
41 39
42xor a b = a && not b || b && not a 40
43 41
44fortran m = order m == ColumnMajor 42fortran m = order m == ColumnMajor
45 43
@@ -57,25 +55,12 @@ type t ::> s = Mt t s
57 55
58mat d m f = f (rows m) (cols m) (ptr (d m)) 56mat d m f = f (rows m) (cols m) (ptr (d m))
59 57
58toLists m = partit (cols m) . toList . cdat $ m
59
60instance (Show a, Storable a) => (Show (Matrix a)) where 60instance (Show a, Storable a) => (Show (Matrix a)) where
61 show m = (sizes++) . dsp . map (map show) . toLists $ m 61 show m = (sizes++) . dsp . map (map show) . toLists $ m
62 where sizes = "("++show (rows m)++"><"++show (cols m)++")\n" 62 where sizes = "("++show (rows m)++"><"++show (cols m)++")\n"
63 63
64partit :: Int -> [a] -> [[a]]
65partit _ [] = []
66partit n l = take n l : partit n (drop n l)
67
68-- | obtains the common value of a property of a list
69common :: (Eq a) => (b->a) -> [b] -> Maybe a
70common f = commonval . map f where
71 commonval :: (Eq a) => [a] -> Maybe a
72 commonval [] = Nothing
73 commonval [a] = Just a
74 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing
75
76
77toLists m = partit (cols m) . toList . cdat $ m
78
79dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp 64dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp
80 where 65 where
81 mt = transpose as 66 mt = transpose as
@@ -146,62 +131,6 @@ transdata c1 d c2 | isReal baseOf d = scast $ transdataR c1 (scast d) c2
146--{-# RULES "transdataR" transdata=transdataR #-} 131--{-# RULES "transdataR" transdata=transdataR #-}
147--{-# RULES "transdataC" transdata=transdataC #-} 132--{-# RULES "transdataC" transdata=transdataC #-}
148 133
149-----------------------------------------------------------------------------
150
151-- | creates a Matrix from a list of vectors
152fromRows :: Field t => [Vector t] -> Matrix t
153fromRows vs = case common dim vs of
154 Nothing -> error "fromRows applied to [] or to vectors with different sizes"
155 Just c -> reshape c (join vs)
156
157-- | extracts the rows of a matrix as a list of vectors
158toRows :: Storable t => Matrix t -> [Vector t]
159toRows m = toRows' 0 where
160 v = cdat m
161 r = rows m
162 c = cols m
163 toRows' k | k == r*c = []
164 | otherwise = subVector k c v : toRows' (k+c)
165
166-- | Creates a matrix from a list of vectors, as columns
167fromColumns :: Field t => [Vector t] -> Matrix t
168fromColumns m = trans . fromRows $ m
169
170-- | Creates a list of vectors from the columns of a matrix
171toColumns :: Field t => Matrix t -> [Vector t]
172toColumns m = toRows . trans $ m
173
174-- | creates a matrix from a vertical list of matrices
175joinVert :: Field t => [Matrix t] -> Matrix t
176joinVert ms = case common cols ms of
177 Nothing -> error "joinVert on matrices with different number of columns"
178 Just c -> reshape c $ join (map cdat ms)
179
180-- | creates a matrix from a horizontal list of matrices
181joinHoriz :: Field t => [Matrix t] -> Matrix t
182joinHoriz ms = trans. joinVert . map trans $ ms
183
184-- | creates a complex vector from vectors with real and imaginary parts
185toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double)
186toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i]
187
188-- | obtains the complex conjugate of a complex vector
189conj :: Vector (Complex Double) -> Vector (Complex Double)
190conj v = asComplex $ cdat $ reshape 2 (asReal v) `mulC` diag (fromList [1,-1])
191 where mulC = multiply RowMajor
192
193comp v = toComplex (v,constant (dim v) 0)
194
195------------------------------------------------------------------------------
196
197-- | Reverse rows
198flipud :: Field t => Matrix t -> Matrix t
199flipud m = fromRows . reverse . toRows $ m
200
201-- | Reverse columns
202fliprl :: Field t => Matrix t -> Matrix t
203fliprl m = fromColumns . reverse . toColumns $ m
204
205----------------------------------------------------------------- 134-----------------------------------------------------------------
206 135
207liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes 136liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes
@@ -330,13 +259,25 @@ diagG v = reshape c $ fromList $ [ l!!(i-1) * delta k i | k <- [1..c], i <- [1..
330 delta i j | i==j = 1 259 delta i j | i==j = 1
331 | otherwise = 0 260 | otherwise = 0
332 261
333diagRect s r c 262-- | creates a Matrix from a list of vectors
334 | dim s < min r c = error "diagRect" 263fromRows :: Field t => [Vector t] -> Matrix t
335 | r == c = diag s 264fromRows vs = case common dim vs of
336 | r < c = trans $ diagRect s c r 265 Nothing -> error "fromRows applied to [] or to vectors with different sizes"
337 | r > c = joinVert [diag s , zeros (r-c,c)] 266 Just c -> reshape c (join vs)
338 where zeros (r,c) = reshape c $ constant (r*c) 0
339 267
340takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] 268-- | extracts the rows of a matrix as a list of vectors
269toRows :: Storable t => Matrix t -> [Vector t]
270toRows m = toRows' 0 where
271 v = cdat m
272 r = rows m
273 c = cols m
274 toRows' k | k == r*c = []
275 | otherwise = subVector k c v : toRows' (k+c)
341 276
342ident n = diag (constant n 1) 277-- | Creates a matrix from a list of vectors, as columns
278fromColumns :: Field t => [Vector t] -> Matrix t
279fromColumns m = trans . fromRows $ m
280
281-- | Creates a list of vectors from the columns of a matrix
282toColumns :: Field t => Matrix t -> [Vector t]
283toColumns m = toRows . trans $ m
diff --git a/lib/Data/Packed/Internal/Tensor.hs b/lib/Data/Packed/Internal/Tensor.hs
index 67dcb09..123270d 100644
--- a/lib/Data/Packed/Internal/Tensor.hs
+++ b/lib/Data/Packed/Internal/Tensor.hs
@@ -1,4 +1,3 @@
1--{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
2----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
3-- | 2-- |
4-- Module : Data.Packed.Internal.Tensor 3-- Module : Data.Packed.Internal.Tensor
@@ -9,15 +8,17 @@
9-- Stability : provisional 8-- Stability : provisional
10-- Portability : portable (uses FFI) 9-- Portability : portable (uses FFI)
11-- 10--
12-- Fundamental types 11-- basic tensor operations
13-- 12--
14----------------------------------------------------------------------------- 13-----------------------------------------------------------------------------
15 14
16module Data.Packed.Internal.Tensor where 15module Data.Packed.Internal.Tensor where
17 16
17import Data.Packed.Internal
18import Data.Packed.Internal.Vector 18import Data.Packed.Internal.Vector
19import Data.Packed.Internal.Matrix 19import Data.Packed.Internal.Matrix
20import Foreign.Storable 20import Foreign.Storable
21import Data.List(sort)
21 22
22data IdxTp = Covariant | Contravariant deriving (Show,Eq) 23data IdxTp = Covariant | Contravariant deriving (Show,Eq)
23 24
@@ -99,3 +100,10 @@ compatIdxAux (n1,(t1,_)) (n2, (t2,_)) = t1 /= t2 && n1 == n2
99compatIdx t1 n1 t2 n2 = compatIdxAux d1 d2 where 100compatIdx t1 n1 t2 n2 = compatIdxAux d1 d2 where
100 d1 = head $ snd $ fst $ findIdx n1 t1 101 d1 = head $ snd $ fst $ findIdx n1 t1
101 d2 = head $ snd $ fst $ findIdx n2 t2 102 d2 = head $ snd $ fst $ findIdx n2 t2
103
104names t = sort $ map (snd.snd) (dims t)
105
106normal t = tridx (names t) t
107
108contractions t1 t2 = [ contraction t1 n1 t2 n2 | n1 <- names t1, n2 <- names t2, compatIdx t1 n1 t2 n2 ]
109
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 4836bdb..125df1e 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -1,4 +1,4 @@
1{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} 1{-# OPTIONS_GHC -fglasgow-exts #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3-- | 3-- |
4-- Module : Data.Packed.Internal.Vector 4-- Module : Data.Packed.Internal.Vector
@@ -9,70 +9,16 @@
9-- Stability : provisional 9-- Stability : provisional
10-- Portability : portable (uses FFI) 10-- Portability : portable (uses FFI)
11-- 11--
12-- Fundamental types 12-- Vector implementation
13-- 13--
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15 15
16module Data.Packed.Internal.Vector where 16module Data.Packed.Internal.Vector where
17 17
18import Data.Packed.Internal.Common
18import Foreign 19import Foreign
19import Complex 20import Complex
20import Control.Monad(when) 21import Control.Monad(when)
21import Debug.Trace
22import Data.List(transpose,intersperse)
23import Data.Typeable
24import Data.Maybe(fromJust)
25
26debug x = trace (show x) x
27
28----------------------------------------------------------------------
29instance (Storable a, RealFloat a) => Storable (Complex a) where --
30 alignment x = alignment (realPart x) --
31 sizeOf x = 2 * sizeOf (realPart x) --
32 peek p = do --
33 [re,im] <- peekArray 2 (castPtr p) --
34 return (re :+ im) --
35 poke p (a :+ b) = pokeArray (castPtr p) [a,b] --
36----------------------------------------------------------------------
37
38on f g = \x y -> f (g x) (g y)
39
40(//) :: x -> (x -> y) -> y
41infixl 0 //
42(//) = flip ($)
43
44errorCode 1000 = "bad size"
45errorCode 1001 = "bad function code"
46errorCode 1002 = "memory problem"
47errorCode 1003 = "bad file"
48errorCode 1004 = "singular"
49errorCode 1005 = "didn't converge"
50errorCode n = "code "++show n
51
52check msg ls f = do
53 err <- f
54 when (err/=0) (error (msg++": "++errorCode err))
55 mapM_ (touchForeignPtr . fptr) ls
56 return ()
57
58class (Storable a, Typeable a) => Field a where
59instance (Storable a, Typeable a) => Field a where
60
61isReal w x = typeOf (undefined :: Double) == typeOf (w x)
62isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x)
63baseOf v = (v `at` 0)
64
65scast :: forall a . forall b . (Typeable a, Typeable b) => a -> b
66scast = fromJust . cast
67
68
69
70----------------------------------------------------------------------
71
72data Vector t = V { dim :: Int
73 , fptr :: ForeignPtr t
74 , ptr :: Ptr t
75 } deriving Typeable
76 22
77type Vc t s = Int -> Ptr t -> s 23type Vc t s = Int -> Ptr t -> s
78infixr 5 :> 24infixr 5 :>
@@ -81,6 +27,8 @@ type t :> s = Vc t s
81vec :: Vector t -> (Vc t s) -> s 27vec :: Vector t -> (Vc t s) -> s
82vec v f = f (dim v) (ptr v) 28vec v f = f (dim v) (ptr v)
83 29
30baseOf v = (v `at` 0)
31
84createVector :: Storable a => Int -> IO (Vector a) 32createVector :: Storable a => Int -> IO (Vector a)
85createVector n = do 33createVector n = do
86 when (n <= 0) $ error ("trying to createVector of dim "++show n) 34 when (n <= 0) $ error ("trying to createVector of dim "++show n)
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 8d1c8b6..6ea76c0 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -1 +1,71 @@
1 1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Matrix
4-- Copyright : (c) Alberto Ruiz 2007
5-- License : GPL-style
6--
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional
9-- Portability : portable
10--
11-- Matrices
12--
13-----------------------------------------------------------------------------
14
15module Data.Packed.Matrix (
16 Matrix(rows,cols), Field,
17 toLists, (><), (>|<),
18 trans,
19 reshape,
20 fromRows, toRows, fromColumns, toColumns,
21 joinVert, joinHoriz,
22 flipud, fliprl,
23 liftMatrix, liftMatrix2,
24 multiply,
25 subMatrix, diag, takeDiag, diagRect, ident
26) where
27
28import Data.Packed.Internal
29
30-- | creates a matrix from a vertical list of matrices
31joinVert :: Field t => [Matrix t] -> Matrix t
32joinVert ms = case common cols ms of
33 Nothing -> error "joinVert on matrices with different number of columns"
34 Just c -> reshape c $ join (map cdat ms)
35
36-- | creates a matrix from a horizontal list of matrices
37joinHoriz :: Field t => [Matrix t] -> Matrix t
38joinHoriz ms = trans. joinVert . map trans $ ms
39
40-- | Reverse rows
41flipud :: Field t => Matrix t -> Matrix t
42flipud m = fromRows . reverse . toRows $ m
43
44-- | Reverse columns
45fliprl :: Field t => Matrix t -> Matrix t
46fliprl m = fromColumns . reverse . toColumns $ m
47
48------------------------------------------------------------
49
50diagRect s r c
51 | dim s < min r c = error "diagRect"
52 | r == c = diag s
53 | r < c = trans $ diagRect s c r
54 | r > c = joinVert [diag s , zeros (r-c,c)]
55 where zeros (r,c) = reshape c $ constant (r*c) 0
56
57takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
58
59ident n = diag (constant n 1)
60
61r >< c = f where
62 f l | dim v == r*c = matrixFromVector RowMajor c v
63 | otherwise = error $ "inconsistent list size = "
64 ++show (dim v) ++"in ("++show r++"><"++show c++")"
65 where v = fromList l
66
67r >|< c = f where
68 f l | dim v == r*c = matrixFromVector ColumnMajor c v
69 | otherwise = error $ "inconsistent list size = "
70 ++show (dim v) ++"in ("++show r++"><"++show c++")"
71 where v = fromList l
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 8d1c8b6..992301a 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -1 +1,40 @@
1 1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Vector
4-- Copyright : (c) Alberto Ruiz 2007
5-- License : GPL-style
6--
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional
9-- Portability : portable
10--
11-- Vectors
12--
13-----------------------------------------------------------------------------
14
15module Data.Packed.Vector (
16 Vector(dim), Field,
17 fromList, toList,
18 at,
19 subVector, join,
20 constant,
21 toComplex, comp,
22 conj,
23 dot
24) where
25
26import Data.Packed.Internal
27import Complex
28
29-- | creates a complex vector from vectors with real and imaginary parts
30toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double)
31toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i]
32
33-- | obtains the complex conjugate of a complex vector
34conj :: Vector (Complex Double) -> Vector (Complex Double)
35conj v = asComplex $ cdat $ reshape 2 (asReal v) `mulC` diag (fromList [1,-1])
36 where mulC = multiply RowMajor
37
38comp v = toComplex (v,constant (dim v) 0)
39
40
diff --git a/lib/LAPACK.hs b/lib/LAPACK.hs
index e6249a1..1531cfb 100644
--- a/lib/LAPACK.hs
+++ b/lib/LAPACK.hs
@@ -21,8 +21,11 @@ module LAPACK (
21 linearSolveSVDR, linearSolveSVDC, 21 linearSolveSVDR, linearSolveSVDC,
22) where 22) where
23 23
24import Data.Packed.Internal
24import Data.Packed.Internal.Vector 25import Data.Packed.Internal.Vector
25import Data.Packed.Internal.Matrix 26import Data.Packed.Internal.Matrix
27import Data.Packed.Vector
28import Data.Packed.Matrix
26import Complex 29import Complex
27import Foreign 30import Foreign
28 31