summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2013-03-19 10:27:29 -0700
committerAlberto Ruiz <aruiz@um.es>2013-03-19 10:27:29 -0700
commit55db8511c95a4e5ef0904940dff2a9a36696c8f6 (patch)
treec1bc4545d7e68e1be5deda703b3d45f948daa9e5
parent55edd1062e41e58c702e8049849e44333c8c83d7 (diff)
parent167ee54f28248d55da72bcb22c3335f8448efcfa (diff)
Merge pull request #35 from bgamari/master
A few fixes
-rw-r--r--lib/Data/Packed/Matrix.hs30
1 files changed, 15 insertions, 15 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index ca96c50..c1a9b24 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -46,7 +46,6 @@ import Data.Array
46 46
47import Data.List(transpose,intersperse) 47import Data.List(transpose,intersperse)
48import Foreign.Storable(Storable) 48import Foreign.Storable(Storable)
49import Control.Arrow((***))
50 49
51------------------------------------------------------------------- 50-------------------------------------------------------------------
52 51
@@ -158,7 +157,7 @@ adaptBlocks ms = ms' where
158 157
159----------------------------------------------------------- 158-----------------------------------------------------------
160 159
161-- | Reverse rows 160-- | Reverse rows
162flipud :: Element t => Matrix t -> Matrix t 161flipud :: Element t => Matrix t -> Matrix t
163flipud m = fromRows . reverse . toRows $ m 162flipud m = fromRows . reverse . toRows $ m
164 163
@@ -273,7 +272,7 @@ Hilbert matrix of order N:
273buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a 272buildMatrix :: Element a => Int -> Int -> ((Int, Int) -> a) -> Matrix a
274buildMatrix rc cc f = 273buildMatrix rc cc f =
275 fromLists $ map (map f) 274 fromLists $ map (map f)
276 $ map (\ ri -> map (\ ci -> (ri, ci)) [0 .. (cc - 1)]) [0 .. (rc - 1)] 275 $ map (\ ri -> map (\ ci -> (ri, ci)) [0 .. (cc - 1)]) [0 .. (rc - 1)]
277 276
278----------------------------------------------------- 277-----------------------------------------------------
279 278
@@ -284,7 +283,7 @@ fromArray2D m = (r><c) (elems m)
284 c = c1-c0+1 283 c = c1-c0+1
285 284
286 285
287-- | rearranges the rows of a matrix according to the order given in a list of integers. 286-- | rearranges the rows of a matrix according to the order given in a list of integers.
288extractRows :: Element t => [Int] -> Matrix t -> Matrix t 287extractRows :: Element t => [Int] -> Matrix t -> Matrix t
289extractRows l m = fromRows $ extract (toRows m) l 288extractRows l m = fromRows $ extract (toRows m) l
290 where extract l' is = [l'!!i |i<-is] 289 where extract l' is = [l'!!i |i<-is]
@@ -352,9 +351,9 @@ toBlocksEvery r c m = toBlocks rs cs m where
352 351
353------------------------------------------------------------------- 352-------------------------------------------------------------------
354 353
355mk c g = \k v -> g ((fromIntegral *** fromIntegral) (divMod k c)) v 354mk c g = \k v -> g (divMod k c) v
356 355
357{- | 356{- |
358 357
359@ghci> mapMatrixWithIndexM_ (\\(i,j) v -> printf \"m[%.0f,%.0f] = %.f\\n\" i j v :: IO()) ((2><3)[1 :: Double ..]) 358@ghci> mapMatrixWithIndexM_ (\\(i,j) v -> printf \"m[%.0f,%.0f] = %.f\\n\" i j v :: IO()) ((2><3)[1 :: Double ..])
360m[0,0] = 1 359m[0,0] = 1
@@ -367,8 +366,8 @@ m[1,2] = 6@
367mapMatrixWithIndexM_ 366mapMatrixWithIndexM_
368 :: (Element a, Num a, 367 :: (Element a, Num a,
369 Functor f, Monad f) => 368 Functor f, Monad f) =>
370 ((a, a) -> a -> f ()) -> Matrix a -> f () 369 ((Int, Int) -> a -> f ()) -> Matrix a -> f ()
371mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m 370mapMatrixWithIndexM_ g m = mapVectorWithIndexM_ (mk c g) . flatten $ m
372 where 371 where
373 c = cols m 372 c = cols m
374 373
@@ -381,11 +380,11 @@ Just (3><3)
381 , 20.0, 21.0, 122.0 ]@ 380 , 20.0, 21.0, 122.0 ]@
382-} 381-}
383mapMatrixWithIndexM 382mapMatrixWithIndexM
384 :: (Foreign.Storable.Storable t, 383 :: (Foreign.Storable.Storable t,
385 Element a, Num a, 384 Element a, Num a,
386 Functor f, Monad f) => 385 Functor f, Monad f) =>
387 ((a, a) -> a -> f t) -> Matrix a -> f (Matrix t) 386 ((Int, Int) -> a -> f t) -> Matrix a -> f (Matrix t)
388mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m 387mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flatten $ m
389 where 388 where
390 c = cols m 389 c = cols m
391 390
@@ -396,11 +395,12 @@ mapMatrixWithIndexM g m = fmap (reshape c) . mapVectorWithIndexM (mk c g) . flat
396 , 10.0, 111.0, 12.0 395 , 10.0, 111.0, 12.0
397 , 20.0, 21.0, 122.0 ]@ 396 , 20.0, 21.0, 122.0 ]@
398 -} 397 -}
399mapMatrixWithIndex :: (Foreign.Storable.Storable t, 398mapMatrixWithIndex :: (Foreign.Storable.Storable t,
400 Element a, Num a) => 399 Element a, Num a) =>
401 ((a, a) -> a -> t) -> Matrix a -> Matrix t 400 ((Int, Int) -> a -> t) -> Matrix a -> Matrix t
402mapMatrixWithIndex g = head . mapMatrixWithIndexM (\a b -> [g a b]) 401mapMatrixWithIndex g m = reshape c $ mapVectorWithIndex (mk c g) $ flatten m
402 where
403 c = cols m
403 404
404mapMatrix :: (Storable a, Storable b) => (a -> b) -> Matrix a -> Matrix b 405mapMatrix :: (Storable a, Storable b) => (a -> b) -> Matrix a -> Matrix b
405mapMatrix f = liftMatrix (mapVector f) 406mapMatrix f = liftMatrix (mapVector f)
406