summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-04-07 08:18:31 +0000
committerAlberto Ruiz <aruiz@um.es>2010-04-07 08:18:31 +0000
commitc562a040612f89f9866f3173f91a954513befcb8 (patch)
tree363777aae045465bae2c73db71d9d74cd0ebe9a6
parentbc854c679c77cbdc97ee2e24383322550109118d (diff)
toBlocks
-rw-r--r--CHANGES2
-rw-r--r--lib/Data/Packed/Internal/Vector.hs17
-rw-r--r--lib/Data/Packed/Matrix.hs25
-rw-r--r--lib/Data/Packed/Vector.hs2
4 files changed, 43 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index 69902ef..d37d271 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
10.9.2.0 10.9.2.0
2======= 2=======
3 3
4- toBlocks, toBlocksEvery
5
4- cholSolve, mbCholSH 6- cholSolve, mbCholSH
5 7
6- GSL Nonlinear Least-Squares fitting using Levenberg-Marquardt. 8- GSL Nonlinear Least-Squares fitting using Levenberg-Marquardt.
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 04232a2..9ca8e58 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -16,7 +16,7 @@
16module Data.Packed.Internal.Vector ( 16module Data.Packed.Internal.Vector (
17 Vector, dim, 17 Vector, dim,
18 fromList, toList, (|>), 18 fromList, toList, (|>),
19 join, (@>), safe, at, at', subVector, 19 join, (@>), safe, at, at', subVector, takesV,
20 mapVector, zipVector, 20 mapVector, zipVector,
21 foldVector, foldVectorG, foldLoop, 21 foldVector, foldVectorG, foldLoop,
22 createVector, vec, 22 createVector, vec,
@@ -222,6 +222,21 @@ join as = unsafePerformIO $ do
222 joiner cs 0 (advancePtr p n) 222 joiner cs 0 (advancePtr p n)
223 223
224 224
225{- | Extract consecutive subvectors of the given sizes.
226
227@> takesV [3,4] (linspace 10 (1,10))
228[3 |> [1.0,2.0,3.0],4 |> [4.0,5.0,6.0,7.0]]@
229
230-}
231takesV :: Storable t => [Int] -> Vector t -> [Vector t]
232takesV ms w | sum ms > dim w = error $ "takesV " ++ show ms ++ " on dim = " ++ (show $ dim w)
233 | otherwise = go ms w
234 where go [] _ = []
235 go (n:ns) v = subVector 0 n v
236 : go ns (subVector n (dim v - n) v)
237
238---------------------------------------------------------------
239
225-- | transforms a complex vector into a real vector with alternating real and imaginary parts 240-- | transforms a complex vector into a real vector with alternating real and imaginary parts
226asReal :: Vector (Complex Double) -> Vector Double 241asReal :: Vector (Complex Double) -> Vector Double
227asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) } 242asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) }
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index e204a83..d91a089 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -22,7 +22,8 @@ module Data.Packed.Matrix (
22 (@@>), 22 (@@>),
23 asRow, asColumn, 23 asRow, asColumn,
24 fromRows, toRows, fromColumns, toColumns, 24 fromRows, toRows, fromColumns, toColumns,
25 fromBlocks, repmat, 25 fromBlocks, toBlocks, toBlocksEvery,
26 repmat,
26 flipud, fliprl, 27 flipud, fliprl,
27 subMatrix, takeRows, dropRows, takeColumns, dropColumns, 28 subMatrix, takeRows, dropRows, takeColumns, dropColumns,
28 extractRows, 29 extractRows,
@@ -413,3 +414,25 @@ compat' m1 m2 = rows m1 == 1 && cols m1 == 1
413 || rows m2 == 1 && cols m2 == 1 414 || rows m2 == 1 && cols m2 == 1
414 || rows m1 == rows m2 && cols m1 == cols m2 415 || rows m1 == rows m2 && cols m1 == cols m2
415 416
417------------------------------------------------------------
418
419toBlockRows [r] m | r == rows m = [m]
420toBlockRows rs m = map (reshape (cols m)) (takesV szs (flatten m))
421 where szs = map (* cols m) rs
422
423toBlockCols [c] m | c == cols m = [m]
424toBlockCols cs m = map trans . toBlockRows cs . trans $ m
425
426-- | Partition a matrix into blocks with the given numbers of rows and columns.
427-- The remaining rows and columns are discarded.
428toBlocks :: (Element t) => [Int] -> [Int] -> Matrix t -> [[Matrix t]]
429toBlocks rs cs m = map (toBlockCols cs) . toBlockRows rs $ m
430
431-- | Fully partition a matrix into blocks of the same size. If the dimensions are not
432-- a multiple of the given size the last blocks will be smaller.
433toBlocksEvery :: (Element t) => Int -> Int -> Matrix t -> [[Matrix t]]
434toBlocksEvery r c m = toBlocks rs cs m where
435 (qr,rr) = rows m `divMod` r
436 (qc,rc) = cols m `divMod` c
437 rs = replicate qr r ++ if rr > 0 then [rr] else []
438 cs = replicate qc c ++ if rc > 0 then [rc] else []
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 6f1096f..472240c 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -16,7 +16,7 @@ module Data.Packed.Vector (
16 Vector, 16 Vector,
17 fromList, (|>), toList, buildVector, 17 fromList, (|>), toList, buildVector,
18 dim, (@>), 18 dim, (@>),
19 subVector, join, 19 subVector, takesV, join,
20 constant, linspace, 20 constant, linspace,
21 vecdisp, 21 vecdisp,
22 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, 22 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex,