1 {-# LANGUAGE ScopedTypeVariables #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE TypeFamilies #-}
11 import Data.Vector.Fixed (
16 import qualified Data.Vector.Fixed as V (
22 import Data.Vector.Fixed.Internal (arity)
24 type Mat v w a = Vn v (Vn w a)
25 type Mat2 a = Mat Vec2D Vec2D a
26 type Mat3 a = Mat Vec3D Vec3D a
27 type Mat4 a = Mat Vec4D Vec4D a
29 -- | Convert a matrix to a nested list.
30 toList :: (Vector v (Vn w a), Vector w a) => Mat v w a -> [[a]]
31 toList m = map V.toList (V.toList m)
33 -- | Create a matrix from a nested list.
34 fromList :: (Vector v (Vn w a), Vector w a) => [[a]] -> Mat v w a
35 fromList vs = V.fromList $ map V.fromList vs
39 (!!!) :: (Vector v (Vn w a), Vector w a) => Mat v w a -> (Int, Int) -> a
40 (!!!) m (i, j) = (row m i) ! j
43 (!!?) :: (Vector v (Vn w a), Vector w a) => Mat v w a
47 | i < 0 || j < 0 = Nothing
48 | i > V.length m = Nothing
49 | otherwise = if j > V.length (row m j)
51 else Just $ (row m j) ! j
54 -- | The number of rows in the matrix.
55 nrows :: forall v w a. (Vector v (Vn w a), Vector w a) => Mat v w a -> Int
58 -- | The number of columns in the first row of the
59 -- matrix. Implementation stolen from Data.Vector.Fixed.length.
60 ncols :: forall v w a. (Vector v (Vn w a), Vector w a) => Mat v w a -> Int
61 ncols _ = arity (undefined :: Dim w)
63 -- | Return the @i@th row of @m@. Unsafe.
64 row :: (Vector v (Vn w a), Vector w a) => Mat v w a
70 -- | Return the @j@th column of @m@. Unsafe.
71 column :: (Vector v a, Vector v (Vn w a), Vector w a) => Mat v w a
80 -- | Transpose @m@; switch it's columns and its rows. This is a dirty
81 -- implementation.. it would be a little cleaner to use imap, but it
82 -- doesn't seem to work.
84 -- TODO: Don't cheat with fromList.
88 -- >>> let m = fromList [[1,2], [3,4]] :: Mat2 Int
92 transpose :: (Vector v (Vn w a),
98 transpose m = V.fromList column_list
100 column_list = [ column m i | i <- [0..(ncols m)-1] ]
102 -- | Is @m@ symmetric?
106 -- >>> let m1 = fromList [[1,2], [2,1]] :: Mat2 Int
110 -- >>> let m2 = fromList [[1,2], [3,1]] :: Mat2 Int
114 symmetric :: (Vector v (Vn w a),
125 -- | Construct a new matrix from a function @lambda@. The function
126 -- @lambda@ should take two parameters i,j corresponding to the
127 -- entries in the matrix. The i,j entry of the resulting matrix will
128 -- have the value returned by lambda i j.
130 -- TODO: Don't cheat with fromList.
134 -- >>> let lambda i j = i + j
135 -- >>> construct lambda :: Mat3 Int
136 -- ((0,1,2),(1,2,3),(2,3,4))
138 construct :: forall v w a.
143 construct lambda = rows
145 -- The arity trick is used in Data.Vector.Fixed.length.
146 imax = (arity (undefined :: Dim v)) - 1
147 jmax = (arity (undefined :: Dim w)) - 1
148 row' i = V.fromList [ lambda i j | j <- [0..jmax] ]
149 rows = V.fromList [ row' i | i <- [0..imax] ]
151 -- | Given a positive-definite matrix @m@, computes the
152 -- upper-triangular matrix @r@ with (transpose r)*r == m and all
153 -- values on the diagonal of @r@ positive.
157 -- >>> let m1 = fromList [[20,-1], [-1,20]] :: Mat2 Double
159 -- ((4.47213595499958,-0.22360679774997896),(0.0,4.466542286825459))
160 -- >>> (transpose (cholesky m1)) `mult` (cholesky m1)
161 -- ((20.000000000000004,-1.0),(-1.0,20.0))
163 cholesky :: forall a v w.
169 cholesky m = construct r
172 r i j | i == j = sqrt(m !!! (i,j) - sum [(r k i)**2 | k <- [0..i-1]])
174 (((m !!! (i,j)) - sum [(r k i)*(r k j) | k <- [0..i-1]]))/(r i i)
177 -- | Matrix multiplication. Our 'Num' instance doesn't define one, and
178 -- we need additional restrictions on the result type anyway.
182 -- >>> let m1 = fromList [[1,2,3], [4,5,6]] :: Mat Vec2D Vec3D Int
183 -- >>> let m2 = fromList [[1,2],[3,4],[5,6]] :: Mat Vec3D Vec2D Int
196 mult m1 m2 = construct lambda
199 sum [(m1 !!! (i,k)) * (m2 !!! (k,j)) | k <- [0..(ncols m1)-1] ]