summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-25 07:32:56 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-25 07:32:56 +0000
commit1871acb835b4fc164bcff3f6e7467884b87fbd0f (patch)
treeac1028d40778bbae532c3915276b5af21ba5f5cb /lib/Data/Packed/Internal
parent3d5d6f06598aac00906c93ac5358e68697c47fc7 (diff)
l.a. algorithms, etc.
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs18
-rw-r--r--lib/Data/Packed/Internal/Vector.hs6
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 9309d1d..dd33943 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -93,6 +93,15 @@ createMatrix order r c = do
93 p <- createVector (r*c) 93 p <- createVector (r*c)
94 return (matrixFromVector order c p) 94 return (matrixFromVector order c p)
95 95
96{- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns.
97
98@\> reshape 4 ('fromList' [1..12])
99(3><4)
100 [ 1.0, 2.0, 3.0, 4.0
101 , 5.0, 6.0, 7.0, 8.0
102 , 9.0, 10.0, 11.0, 12.0 ]@
103
104-}
96reshape :: (Field t) => Int -> Vector t -> Matrix t 105reshape :: (Field t) => Int -> Vector t -> Matrix t
97reshape c v = matrixFromVector RowMajor c v 106reshape c v = matrixFromVector RowMajor c v
98 107
@@ -140,7 +149,6 @@ liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes
140 149
141liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t 150liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
142liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes 151liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes
143
144------------------------------------------------------------------ 152------------------------------------------------------------------
145 153
146dotL a b = sum (zipWith (*) a b) 154dotL a b = sum (zipWith (*) a b)
@@ -200,6 +208,14 @@ multiplyD order a b
200 208
201outer' u v = dat (outer u v) 209outer' u v = dat (outer u v)
202 210
211{- | Outer product of two vectors.
212
213@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]
214(3><3)
215 [ 5.0, 2.0, 3.0
216 , 10.0, 4.0, 6.0
217 , 15.0, 6.0, 9.0 ]@
218-}
203outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t 219outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t
204outer u v = multiply RowMajor r c 220outer u v = multiply RowMajor r c
205 where r = matrixFromVector RowMajor 1 u 221 where r = matrixFromVector RowMajor 1 u
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 25e848d..f1addf4 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -48,7 +48,7 @@ fromList l = unsafePerformIO $ do
48toList :: Storable a => Vector a -> [a] 48toList :: Storable a => Vector a -> [a]
49toList v = unsafePerformIO $ peekArray (dim v) (ptr v) 49toList v = unsafePerformIO $ peekArray (dim v) (ptr v)
50 50
51n # l = if length l == n then fromList l else error "# with wrong size" 51n |> l = if length l == n then fromList l else error "|> with wrong size"
52 52
53at' :: Storable a => Vector a -> Int -> a 53at' :: Storable a => Vector a -> Int -> a
54at' v n = unsafePerformIO $ peekElemOff (ptr v) n 54at' v n = unsafePerformIO $ peekElemOff (ptr v) n
@@ -58,7 +58,7 @@ at v n | n >= 0 && n < dim v = at' v n
58 | otherwise = error "vector index out of range" 58 | otherwise = error "vector index out of range"
59 59
60instance (Show a, Storable a) => (Show (Vector a)) where 60instance (Show a, Storable a) => (Show (Vector a)) where
61 show v = (show (dim v))++" # " ++ show (toList v) 61 show v = (show (dim v))++" |> " ++ show (toList v)
62 62
63-- | creates a Vector taking a number of consecutive toList from another Vector 63-- | creates a Vector taking a number of consecutive toList from another Vector
64subVector :: Storable t => Int -- ^ index of the starting element 64subVector :: Storable t => Int -- ^ index of the starting element
@@ -129,3 +129,5 @@ constant x n | isReal id x = scast $ constantR (scast x) n
129 | isComp id x = scast $ constantC (scast x) n 129 | isComp id x = scast $ constantC (scast x) n
130 | otherwise = constantG x n 130 | otherwise = constantG x n
131 131
132liftVector f = fromList . map f . toList
133liftVector2 f u v = fromList $ zipWith f (toList u) (toList v)