1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
-----------------------------------------------------------------------------
-- |
-- Module : Data.Packed.Matrix
-- Copyright : (c) Alberto Ruiz 2007
-- License : GPL-style
--
-- Maintainer : Alberto Ruiz <aruiz@um.es>
-- Stability : provisional
-- Portability : portable
--
-- Matrices
--
-----------------------------------------------------------------------------
module Data.Packed.Matrix (
Matrix(rows,cols), Field,
toLists, (><), (>|<),
trans,
reshape,
fromRows, toRows, fromColumns, toColumns,
joinVert, joinHoriz,
flipud, fliprl,
liftMatrix, liftMatrix2,
multiply,
subMatrix, diag, takeDiag, diagRect, ident
) where
import Data.Packed.Internal
-- | creates a matrix from a vertical list of matrices
joinVert :: Field t => [Matrix t] -> Matrix t
joinVert ms = case common cols ms of
Nothing -> error "joinVert on matrices with different number of columns"
Just c -> reshape c $ join (map cdat ms)
-- | creates a matrix from a horizontal list of matrices
joinHoriz :: Field t => [Matrix t] -> Matrix t
joinHoriz ms = trans. joinVert . map trans $ ms
-- | Reverse rows
flipud :: Field t => Matrix t -> Matrix t
flipud m = fromRows . reverse . toRows $ m
-- | Reverse columns
fliprl :: Field t => Matrix t -> Matrix t
fliprl m = fromColumns . reverse . toColumns $ m
------------------------------------------------------------
diagRect s r c
| dim s < min r c = error "diagRect"
| r == c = diag s
| r < c = trans $ diagRect s c r
| r > c = joinVert [diag s , zeros (r-c,c)]
where zeros (r,c) = reshape c $ constant (r*c) 0
takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
ident n = diag (constant n 1)
r >< c = f where
f l | dim v == r*c = matrixFromVector RowMajor c v
| otherwise = error $ "inconsistent list size = "
++show (dim v) ++"in ("++show r++"><"++show c++")"
where v = fromList l
r >|< c = f where
f l | dim v == r*c = matrixFromVector ColumnMajor c v
| otherwise = error $ "inconsistent list size = "
++show (dim v) ++"in ("++show r++"><"++show c++")"
where v = fromList l
|