diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-25 07:32:56 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-25 07:32:56 +0000 |
commit | 1871acb835b4fc164bcff3f6e7467884b87fbd0f (patch) | |
tree | ac1028d40778bbae532c3915276b5af21ba5f5cb /lib/Data/Packed/Internal | |
parent | 3d5d6f06598aac00906c93ac5358e68697c47fc7 (diff) |
l.a. algorithms, etc.
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 18 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 6 |
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 | -} | ||
96 | reshape :: (Field t) => Int -> Vector t -> Matrix t | 105 | reshape :: (Field t) => Int -> Vector t -> Matrix t |
97 | reshape c v = matrixFromVector RowMajor c v | 106 | reshape 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 | ||
141 | liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | 150 | liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t |
142 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes | 151 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes |
143 | |||
144 | ------------------------------------------------------------------ | 152 | ------------------------------------------------------------------ |
145 | 153 | ||
146 | dotL a b = sum (zipWith (*) a b) | 154 | dotL a b = sum (zipWith (*) a b) |
@@ -200,6 +208,14 @@ multiplyD order a b | |||
200 | 208 | ||
201 | outer' u v = dat (outer u v) | 209 | outer' 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 | -} | ||
203 | outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t | 219 | outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t |
204 | outer u v = multiply RowMajor r c | 220 | outer 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 | |||
48 | toList :: Storable a => Vector a -> [a] | 48 | toList :: Storable a => Vector a -> [a] |
49 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) | 49 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) |
50 | 50 | ||
51 | n # l = if length l == n then fromList l else error "# with wrong size" | 51 | n |> l = if length l == n then fromList l else error "|> with wrong size" |
52 | 52 | ||
53 | at' :: Storable a => Vector a -> Int -> a | 53 | at' :: Storable a => Vector a -> Int -> a |
54 | at' v n = unsafePerformIO $ peekElemOff (ptr v) n | 54 | at' 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 | ||
60 | instance (Show a, Storable a) => (Show (Vector a)) where | 60 | instance (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 |
64 | subVector :: Storable t => Int -- ^ index of the starting element | 64 | subVector :: 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 | ||
132 | liftVector f = fromList . map f . toList | ||
133 | liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) | ||