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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module Numeric.Sparse(
GMatrix(..), CSR(..), mkCSR, fromCSR,
mkSparse, mkDiagR, mkDense,
AssocMatrix,
toDense,
gmXv, (!#>)
)where
import Data.Packed.Numeric
import qualified Data.Vector.Storable as V
import Data.Function(on)
import Control.Arrow((***))
import Control.Monad(when)
import Data.List(groupBy, sort)
import Foreign.C.Types(CInt(..))
import Data.Packed.Development
import System.IO.Unsafe(unsafePerformIO)
import Foreign(Ptr)
import Text.Printf(printf)
infixl 0 ~!~
c ~!~ msg = when c (error msg)
type AssocMatrix = [((Int,Int),Double)]
data CSR = CSR
{ csrVals :: Vector Double
, csrCols :: Vector CInt
, csrRows :: Vector CInt
, csrNRows :: Int
, csrNCols :: Int
} deriving Show
data CSC = CSC
{ cscVals :: Vector Double
, cscRows :: Vector CInt
, cscCols :: Vector CInt
, cscNRows :: Int
, cscNCols :: Int
} deriving Show
mkCSR :: AssocMatrix -> CSR
mkCSR sm' = CSR{..}
where
sm = sort sm'
rws = map ((fromList *** fromList)
. unzip
. map ((succ.fi.snd) *** id)
)
. groupBy ((==) `on` (fst.fst))
$ sm
rszs = map (fi . dim . fst) rws
csrRows = fromList (scanl (+) 1 rszs)
csrVals = vjoin (map snd rws)
csrCols = vjoin (map fst rws)
csrNRows = dim csrRows - 1
csrNCols = fromIntegral (V.maximum csrCols)
{- | General matrix with specialized internal representations for
dense, sparse, diagonal, banded, and constant elements.
>>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)]
>>> m
SparseR {gmCSR = CSR {csrVals = fromList [1.0,2.0],
csrCols = fromList [1000,2000],
csrRows = fromList [1,2,3],
csrNRows = 2,
csrNCols = 2000},
nRows = 2,
nCols = 2000}
>>> let m = mkDense (mat 2 [1..4])
>>> m
Dense {gmDense = (2><2)
[ 1.0, 2.0
, 3.0, 4.0 ], nRows = 2, nCols = 2}
-}
data GMatrix
= SparseR
{ gmCSR :: CSR
, nRows :: Int
, nCols :: Int
}
| SparseC
{ gmCSC :: CSC
, nRows :: Int
, nCols :: Int
}
| Diag
{ diagVals :: Vector Double
, nRows :: Int
, nCols :: Int
}
| Dense
{ gmDense :: Matrix Double
, nRows :: Int
, nCols :: Int
}
-- | Banded
deriving Show
mkDense :: Matrix Double -> GMatrix
mkDense m = Dense{..}
where
gmDense = m
nRows = rows m
nCols = cols m
mkSparse :: AssocMatrix -> GMatrix
mkSparse = fromCSR . mkCSR
fromCSR :: CSR -> GMatrix
fromCSR csr = SparseR {..}
where
gmCSR @ CSR {..} = csr
nRows = csrNRows
nCols = csrNCols
mkDiagR r c v
| dim v <= min r c = Diag{..}
| otherwise = error $ printf "mkDiagR: incorrect sizes (%d,%d) [%d]" r c (dim v)
where
nRows = r
nCols = c
diagVals = v
type IV t = CInt -> Ptr CInt -> t
type V t = CInt -> Ptr Double -> t
type SMxV = V (IV (IV (V (V (IO CInt)))))
gmXv :: GMatrix -> Vector Double -> Vector Double
gmXv SparseR { gmCSR = CSR{..}, .. } v = unsafePerformIO $ do
dim v /= nCols ~!~ printf "gmXv (CSR): incorrect sizes: (%d,%d) x %d" nRows nCols (dim v)
r <- createVector nRows
app5 c_smXv vec csrVals vec csrCols vec csrRows vec v vec r "CSRXv"
return r
gmXv SparseC { gmCSC = CSC{..}, .. } v = unsafePerformIO $ do
dim v /= nCols ~!~ printf "gmXv (CSC): incorrect sizes: (%d,%d) x %d" nRows nCols (dim v)
r <- createVector nRows
app5 c_smTXv vec cscVals vec cscRows vec cscCols vec v vec r "CSCXv"
return r
gmXv Diag{..} v
| dim v == nCols
= vjoin [ subVector 0 (dim diagVals) v `mul` diagVals
, konst 0 (nRows - dim diagVals) ]
| otherwise = error $ printf "gmXv (Diag): incorrect sizes: (%d,%d) [%d] x %d"
nRows nCols (dim diagVals) (dim v)
gmXv Dense{..} v
| dim v == nCols
= mXv gmDense v
| otherwise = error $ printf "gmXv (Dense): incorrect sizes: (%d,%d) x %d"
nRows nCols (dim v)
{- | general matrix - vector product
>>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)]
>>> m !#> vector [1..2000]
fromList [1000.0,4000.0]
-}
infixr 8 !#>
(!#>) :: GMatrix -> Vector Double -> Vector Double
(!#>) = gmXv
--------------------------------------------------------------------------------
foreign import ccall unsafe "smXv"
c_smXv :: SMxV
foreign import ccall unsafe "smTXv"
c_smTXv :: SMxV
--------------------------------------------------------------------------------
toDense :: AssocMatrix -> Matrix Double
toDense asm = assoc (r+1,c+1) 0 asm
where
(r,c) = (maximum *** maximum) . unzip . map fst $ asm
instance Transposable CSR CSC
where
tr (CSR vs cs rs n m) = CSC vs cs rs m n
instance Transposable CSC CSR
where
tr (CSC vs rs cs n m) = CSR vs rs cs m n
instance Transposable GMatrix GMatrix
where
tr (SparseR s n m) = SparseC (tr s) m n
tr (SparseC s n m) = SparseR (tr s) m n
tr (Diag v n m) = Diag v m n
tr (Dense a n m) = Dense (tr a) m n
|