1 {-# LANGUAGE ExistentialQuantification #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE NoMonomorphismRestriction #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeFamilies #-}
8 {-# LANGUAGE RebindableSyntax #-}
10 -- | Boxed matrices; that is, boxed m-vectors of boxed n-vectors. We
11 -- assume that the underlying representation is
12 -- Data.Vector.Fixed.Boxed.Vec for simplicity. It was tried in
13 -- generality and failed.
18 import Data.List (intercalate)
20 import Data.Vector.Fixed (
36 import qualified Data.Vector.Fixed as V (
47 import Data.Vector.Fixed.Cont ( Arity, arity )
48 import Linear.Vector ( Vec, delete, element_sum )
49 import Normed ( Normed(..) )
51 import NumericPrelude hiding ( (*), abs )
52 import qualified NumericPrelude as NP ( (*) )
53 import qualified Algebra.Absolute as Absolute ( C )
54 import Algebra.Absolute ( abs )
55 import qualified Algebra.Additive as Additive ( C )
56 import qualified Algebra.Algebraic as Algebraic ( C )
57 import Algebra.Algebraic ( root )
58 import qualified Algebra.Ring as Ring ( C )
59 import qualified Algebra.Module as Module ( C )
60 import qualified Algebra.RealRing as RealRing ( C )
61 import qualified Algebra.ToRational as ToRational ( C )
62 import qualified Algebra.Transcendental as Transcendental ( C )
63 import qualified Prelude as P ( map )
65 -- | Our main matrix type.
66 data Mat m n a = (Arity m, Arity n) => Mat (Vec m (Vec n a))
68 -- Type synonyms for n-by-n matrices.
69 type Mat1 a = Mat N1 N1 a
70 type Mat2 a = Mat N2 N2 a
71 type Mat3 a = Mat N3 N3 a
72 type Mat4 a = Mat N4 N4 a
73 type Mat5 a = Mat N5 N5 a
75 -- | Type synonym for row vectors expressed as 1-by-n matrices.
76 type Row n a = Mat N1 n a
78 -- Type synonyms for 1-by-n row "vectors".
79 type Row1 a = Row N1 a
80 type Row2 a = Row N2 a
81 type Row3 a = Row N3 a
82 type Row4 a = Row N4 a
83 type Row5 a = Row N5 a
85 -- | Type synonym for column vectors expressed as n-by-1 matrices.
86 type Col n a = Mat n N1 a
88 -- Type synonyms for n-by-1 column "vectors".
89 type Col1 a = Col N1 a
90 type Col2 a = Col N2 a
91 type Col3 a = Col N3 a
92 type Col4 a = Col N4 a
93 type Col5 a = Col N5 a
95 -- We need a big column for Gaussian quadrature.
96 type N10 = S (S (S (S (S N5))))
97 type Col10 a = Col N10 a
100 instance (Eq a) => Eq (Mat m n a) where
101 -- | Compare a row at a time.
105 -- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int
106 -- >>> let m2 = fromList [[1,2],[3,4]] :: Mat2 Int
107 -- >>> let m3 = fromList [[5,6],[7,8]] :: Mat2 Int
113 (Mat rows1) == (Mat rows2) =
114 V.and $ V.zipWith comp rows1 rows2
116 -- Compare a row, one column at a time.
117 comp row1 row2 = V.and (V.zipWith (==) row1 row2)
120 instance (Show a) => Show (Mat m n a) where
121 -- | Display matrices and vectors as ordinary tuples. This is poor
122 -- practice, but these results are primarily displayed
123 -- interactively and convenience trumps correctness (said the guy
124 -- who insists his vector lengths be statically checked at
129 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
134 "(" ++ (intercalate "," (V.toList row_strings)) ++ ")"
136 row_strings = V.map show_vector rows
138 "(" ++ (intercalate "," element_strings) ++ ")"
141 element_strings = P.map show v1l
144 -- | Convert a matrix to a nested list.
145 toList :: Mat m n a -> [[a]]
146 toList (Mat rows) = map V.toList (V.toList rows)
148 -- | Create a matrix from a nested list.
149 fromList :: (Arity m, Arity n) => [[a]] -> Mat m n a
150 fromList vs = Mat (V.fromList $ map V.fromList vs)
153 -- | Unsafe indexing.
154 (!!!) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> a
155 (!!!) (Mat rows) (i, j) = (rows ! i) ! j
159 (!!?) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> Maybe a
161 | i < 0 || j < 0 = Nothing
162 | i > (nrows matrix) - 1 = Nothing
163 | j > (ncols matrix) - 1 = Nothing
164 | otherwise = Just $ matrix !!! (i,j)
167 -- | The number of rows in the matrix.
168 nrows :: forall m n a. (Arity m) => Mat m n a -> Int
169 nrows _ = arity (undefined :: m)
172 -- | The number of columns in the first row of the
173 -- matrix. Implementation stolen from Data.Vector.Fixed.length.
174 ncols :: forall m n a. (Arity n) => Mat m n a -> Int
175 ncols _ = arity (undefined :: n)
178 -- | Return the @i@th row of @m@ as a matrix. Unsafe.
179 row :: (Arity m, Arity n) => Mat m n a -> Int -> Row n a
183 lambda _ j = m !!! (i, j)
186 -- | Return the @j@th column of @m@ as a matrix. Unsafe.
187 column :: (Arity m, Arity n) => Mat m n a -> Int -> Col m a
191 lambda i _ = m !!! (i, j)
194 -- | Transpose @m@; switch it's columns and its rows. This is a dirty
195 -- implementation, but I don't see a better way.
197 -- TODO: Don't cheat with fromList.
201 -- >>> let m = fromList [[1,2], [3,4]] :: Mat2 Int
205 transpose :: (Arity m, Arity n) => Mat m n a -> Mat n m a
209 lambda i j = matrix !!! (j,i)
212 -- | Is @m@ symmetric?
216 -- >>> let m1 = fromList [[1,2], [2,1]] :: Mat2 Int
220 -- >>> let m2 = fromList [[1,2], [3,1]] :: Mat2 Int
224 symmetric :: (Eq a, Arity m) => Mat m m a -> Bool
229 -- | Construct a new matrix from a function @lambda@. The function
230 -- @lambda@ should take two parameters i,j corresponding to the
231 -- entries in the matrix. The i,j entry of the resulting matrix will
232 -- have the value returned by lambda i j.
236 -- >>> let lambda i j = i + j
237 -- >>> construct lambda :: Mat3 Int
238 -- ((0,1,2),(1,2,3),(2,3,4))
240 construct :: forall m n a. (Arity m, Arity n)
241 => (Int -> Int -> a) -> Mat m n a
242 construct lambda = Mat $ generate make_row
244 make_row :: Int -> Vec n a
245 make_row i = generate (lambda i)
248 -- | Create an identity matrix with the right dimensions.
252 -- >>> identity_matrix :: Mat3 Int
253 -- ((1,0,0),(0,1,0),(0,0,1))
254 -- >>> identity_matrix :: Mat3 Double
255 -- ((1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0))
257 identity_matrix :: (Arity m, Ring.C a) => Mat m m a
259 construct (\i j -> if i == j then (fromInteger 1) else (fromInteger 0))
262 -- | Given a positive-definite matrix @m@, computes the
263 -- upper-triangular matrix @r@ with (transpose r)*r == m and all
264 -- values on the diagonal of @r@ positive.
268 -- >>> let m1 = fromList [[20,-1], [-1,20]] :: Mat2 Double
270 -- ((4.47213595499958,-0.22360679774997896),(0.0,4.466542286825459))
271 -- >>> (transpose (cholesky m1)) * (cholesky m1)
272 -- ((20.000000000000004,-1.0),(-1.0,20.0))
274 cholesky :: forall m n a. (Algebraic.C a, Arity m, Arity n)
275 => (Mat m n a) -> (Mat m n a)
276 cholesky m = construct r
279 r i j | i == j = sqrt(m !!! (i,j) - sum [(r k i)^2 | k <- [0..i-1]])
281 (((m !!! (i,j)) - sum [(r k i) NP.* (r k j) | k <- [0..i-1]]))/(r i i)
285 -- | Returns True if the given matrix is upper-triangular, and False
286 -- otherwise. The parameter @epsilon@ lets the caller choose a
291 -- >>> let m = fromList [[1,1],[1e-12,1]] :: Mat2 Double
292 -- >>> is_upper_triangular m
294 -- >>> is_upper_triangular' 1e-10 m
299 -- 1. Don't cheat with lists.
301 is_upper_triangular' :: (Ord a, Ring.C a, Absolute.C a, Arity m, Arity n)
302 => a -- ^ The tolerance @epsilon@.
305 is_upper_triangular' epsilon m =
308 results = [[ test i j | i <- [0..(nrows m)-1]] | j <- [0..(ncols m)-1] ]
310 test :: Int -> Int -> Bool
313 -- use "less than or equal to" so zero is a valid epsilon
314 | otherwise = abs (m !!! (i,j)) <= epsilon
317 -- | Returns True if the given matrix is upper-triangular, and False
318 -- otherwise. A specialized version of 'is_upper_triangular\'' with
323 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
324 -- >>> is_upper_triangular m
327 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
328 -- >>> is_upper_triangular m
333 -- 1. The Ord constraint is too strong here, Eq would suffice.
335 is_upper_triangular :: (Ord a, Ring.C a, Absolute.C a, Arity m, Arity n)
337 is_upper_triangular = is_upper_triangular' 0
340 -- | Returns True if the given matrix is lower-triangular, and False
341 -- otherwise. This is a specialized version of 'is_lower_triangular\''
342 -- with @epsilon = 0@.
346 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
347 -- >>> is_lower_triangular m
350 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
351 -- >>> is_lower_triangular m
354 is_lower_triangular :: (Ord a,
361 is_lower_triangular = is_upper_triangular . transpose
364 -- | Returns True if the given matrix is lower-triangular, and False
365 -- otherwise. The parameter @epsilon@ lets the caller choose a
370 -- >>> let m = fromList [[1,1e-12],[1,1]] :: Mat2 Double
371 -- >>> is_lower_triangular m
373 -- >>> is_lower_triangular' 1e-12 m
376 is_lower_triangular' :: (Ord a,
381 => a -- ^ The tolerance @epsilon@.
384 is_lower_triangular' epsilon = (is_upper_triangular' epsilon) . transpose
387 -- | Returns True if the given matrix is triangular, and False
392 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
393 -- >>> is_triangular m
396 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
397 -- >>> is_triangular m
400 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
401 -- >>> is_triangular m
404 is_triangular :: (Ord a,
411 is_triangular m = is_upper_triangular m || is_lower_triangular m
414 -- | Return the (i,j)th minor of m.
418 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
419 -- >>> minor m 0 0 :: Mat2 Int
421 -- >>> minor m 1 1 :: Mat2 Int
432 minor (Mat rows) i j = m
434 rows' = delete rows i
435 m = Mat $ V.map ((flip delete) j) rows'
438 class (Eq a, Ring.C a) => Determined p a where
439 determinant :: (p a) -> a
441 instance (Eq a, Ring.C a) => Determined (Mat (S Z) (S Z)) a where
442 determinant (Mat rows) = (V.head . V.head) rows
448 Determined (Mat (S n) (S n)) a)
449 => Determined (Mat (S (S n)) (S (S n))) a where
450 -- | The recursive definition with a special-case for triangular matrices.
454 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
459 | is_triangular m = product [ m !!! (i,i) | i <- [0..(nrows m)-1] ]
460 | otherwise = determinant_recursive
464 det_minor i j = determinant (minor m i j)
466 determinant_recursive =
467 sum [ (-1)^(toInteger j) NP.* (m' 0 j) NP.* (det_minor 0 j)
468 | j <- [0..(ncols m)-1] ]
472 -- | Matrix multiplication.
476 -- >>> let m1 = fromList [[1,2,3], [4,5,6]] :: Mat N2 N3 Int
477 -- >>> let m2 = fromList [[1,2],[3,4],[5,6]] :: Mat N3 N2 Int
482 (*) :: (Ring.C a, Arity m, Arity n, Arity p)
486 (*) m1 m2 = construct lambda
489 sum [(m1 !!! (i,k)) NP.* (m2 !!! (k,j)) | k <- [0..(ncols m1)-1] ]
493 instance (Ring.C a, Arity m, Arity n) => Additive.C (Mat m n a) where
495 (Mat rows1) + (Mat rows2) =
496 Mat $ V.zipWith (V.zipWith (+)) rows1 rows2
498 (Mat rows1) - (Mat rows2) =
499 Mat $ V.zipWith (V.zipWith (-)) rows1 rows2
501 zero = Mat (V.replicate $ V.replicate (fromInteger 0))
504 instance (Ring.C a, Arity m, Arity n, m ~ n) => Ring.C (Mat m n a) where
505 -- The first * is ring multiplication, the second is matrix
510 instance (Ring.C a, Arity m, Arity n) => Module.C a (Mat m n a) where
511 -- We can multiply a matrix by a scalar of the same type as its
513 x *> (Mat rows) = Mat $ V.map (V.map (NP.* x)) rows
516 instance (Algebraic.C a,
519 => Normed (Mat (S m) N1 a) where
520 -- | Generic p-norms for vectors in R^n that are represented as nx1
525 -- >>> let v1 = vec2d (3,4)
531 norm_p p (Mat rows) =
532 (root p') $ sum [fromRational' (toRational x)^p' | x <- xs]
535 xs = concat $ V.toList $ V.map V.toList rows
537 -- | The infinity norm.
541 -- >>> let v1 = vec3d (1,5,2)
545 norm_infty (Mat rows) =
546 fromRational' $ toRational $ V.maximum $ V.map V.maximum rows
549 -- | Compute the Frobenius norm of a matrix. This essentially treats
550 -- the matrix as one long vector containing all of its entries (in
551 -- any order, it doesn't matter).
555 -- >>> let m = fromList [[1, 2, 3],[4,5,6],[7,8,9]] :: Mat3 Double
556 -- >>> frobenius_norm m == sqrt 285
559 -- >>> let m = fromList [[1, -1, 1],[-1,1,-1],[1,-1,1]] :: Mat3 Double
560 -- >>> frobenius_norm m == 3
563 frobenius_norm :: (Algebraic.C a, Ring.C a) => Mat m n a -> a
564 frobenius_norm (Mat rows) =
565 sqrt $ element_sum $ V.map row_sum rows
567 -- | Square and add up the entries of a row.
568 row_sum = element_sum . V.map (^2)
571 -- Vector helpers. We want it to be easy to create low-dimension
572 -- column vectors, which are nx1 matrices.
574 -- | Convenient constructor for 2D vectors.
578 -- >>> import Roots.Simple
579 -- >>> let fst m = m !!! (0,0)
580 -- >>> let snd m = m !!! (1,0)
581 -- >>> let h = 0.5 :: Double
582 -- >>> let g1 m = 1.0 + h NP.* exp(-((fst m)^2))/(1.0 + (snd m)^2)
583 -- >>> let g2 m = 0.5 + h NP.* atan((fst m)^2 + (snd m)^2)
584 -- >>> let g u = vec2d ((g1 u), (g2 u))
585 -- >>> let u0 = vec2d (1.0, 1.0)
586 -- >>> let eps = 1/(10^9)
587 -- >>> fixed_point g eps u0
588 -- ((1.0728549599342185),(1.0820591495686167))
590 vec1d :: (a) -> Col1 a
591 vec1d (x) = Mat (mk1 (mk1 x))
593 vec2d :: (a,a) -> Col2 a
594 vec2d (x,y) = Mat (mk2 (mk1 x) (mk1 y))
596 vec3d :: (a,a,a) -> Col3 a
597 vec3d (x,y,z) = Mat (mk3 (mk1 x) (mk1 y) (mk1 z))
599 vec4d :: (a,a,a,a) -> Col4 a
600 vec4d (w,x,y,z) = Mat (mk4 (mk1 w) (mk1 x) (mk1 y) (mk1 z))
602 vec5d :: (a,a,a,a,a) -> Col5 a
603 vec5d (v,w,x,y,z) = Mat (mk5 (mk1 v) (mk1 w) (mk1 x) (mk1 y) (mk1 z))
605 -- Since we commandeered multiplication, we need to create 1x1
606 -- matrices in order to multiply things.
607 scalar :: a -> Mat1 a
608 scalar x = Mat (mk1 (mk1 x))
610 dot :: (RealRing.C a, n ~ N1, m ~ S t, Arity t)
614 v1 `dot` v2 = ((transpose v1) * v2) !!! (0, 0)
617 -- | The angle between @v1@ and @v2@ in Euclidean space.
621 -- >>> let v1 = vec2d (1.0, 0.0)
622 -- >>> let v2 = vec2d (0.0, 1.0)
623 -- >>> angle v1 v2 == pi/2.0
626 angle :: (Transcendental.C a,
638 theta = (recip norms) NP.* (v1 `dot` v2)
639 norms = (norm v1) NP.* (norm v2)
642 -- | Retrieve the diagonal elements of the given matrix as a \"column
643 -- vector,\" i.e. a m-by-1 matrix. We require the matrix to be
644 -- square to avoid ambiguity in the return type which would ideally
645 -- have dimension min(m,n) supposing an m-by-n matrix.
649 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
653 diagonal :: (Arity m) => Mat m m a -> Col m a
657 lambda i _ = matrix !!! (i,i)
660 -- | Given a square @matrix@, return a new matrix of the same size
661 -- containing only the on-diagonal entries of @matrix@. The
662 -- off-diagonal entries are set to zero.
666 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
667 -- >>> diagonal_part m
668 -- ((1,0,0),(0,5,0),(0,0,9))
670 diagonal_part :: (Arity m, Ring.C a)
673 diagonal_part matrix =
676 lambda i j = if i == j then matrix !!! (i,j) else 0
679 -- | Given a square @matrix@, return a new matrix of the same size
680 -- containing only the on-diagonal and below-diagonal entries of
681 -- @matrix@. The above-diagonal entries are set to zero.
685 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
687 -- ((1,0,0),(4,5,0),(7,8,9))
689 lt_part :: (Arity m, Ring.C a)
695 lambda i j = if i >= j then matrix !!! (i,j) else 0
698 -- | Given a square @matrix@, return a new matrix of the same size
699 -- containing only the below-diagonal entries of @matrix@. The on-
700 -- and above-diagonal entries are set to zero.
704 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
705 -- >>> lt_part_strict m
706 -- ((0,0,0),(4,0,0),(7,8,0))
708 lt_part_strict :: (Arity m, Ring.C a)
711 lt_part_strict matrix =
714 lambda i j = if i > j then matrix !!! (i,j) else 0
717 -- | Given a square @matrix@, return a new matrix of the same size
718 -- containing only the on-diagonal and above-diagonal entries of
719 -- @matrix@. The below-diagonal entries are set to zero.
723 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
725 -- ((1,2,3),(0,5,6),(0,0,9))
727 ut_part :: (Arity m, Ring.C a)
730 ut_part = transpose . lt_part . transpose
733 -- | Given a square @matrix@, return a new matrix of the same size
734 -- containing only the above-diagonal entries of @matrix@. The on-
735 -- and below-diagonal entries are set to zero.
739 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
740 -- >>> ut_part_strict m
741 -- ((0,2,3),(0,0,6),(0,0,0))
743 ut_part_strict :: (Arity m, Ring.C a)
746 ut_part_strict = transpose . lt_part_strict . transpose
749 -- | Compute the trace of a square matrix, the sum of the elements
750 -- which lie on its diagonal. We require the matrix to be
751 -- square to avoid ambiguity in the return type which would ideally
752 -- have dimension min(m,n) supposing an m-by-n matrix.
756 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
760 trace :: (Arity m, Ring.C a) => Mat m m a -> a
762 let (Mat rows) = diagonal matrix
764 element_sum $ V.map V.head rows
767 -- | Zip together two column matrices.
771 -- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int
772 -- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int
774 -- (((1,1)),((1,2)),((1,3)))
776 colzip :: Arity m => Col m a -> Col m a -> Col m (a,a)
780 lambda i j = (c1 !!! (i,j), c2 !!! (i,j))
783 -- | Zip together two column matrices using the supplied function.
787 -- >>> let c1 = fromList [[1],[2],[3]] :: Col3 Integer
788 -- >>> let c2 = fromList [[4],[5],[6]] :: Col3 Integer
789 -- >>> colzipwith (^) c1 c2
792 colzipwith :: Arity m
800 lambda i j = f (c1 !!! (i,j)) (c2 !!! (i,j))
803 -- | Map a function over a matrix of any dimensions.
807 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
811 map2 :: (a -> b) -> Mat m n a -> Mat m n b
818 -- | Fold over the entire matrix passing the coordinates @i@ and @j@
819 -- (of the row/column) to the accumulation function.
823 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
824 -- >>> ifoldl2 (\i j cur _ -> cur + i + j) 0 m
827 ifoldl2 :: forall a b m n.
828 (Int -> Int -> b -> a -> b)
832 ifoldl2 f initial (Mat rows) =
833 V.ifoldl row_function initial rows
835 -- | The order that we need this in (so that @g idx@ makes sense)
836 -- is a little funny. So that we don't need to pass weird
837 -- functions into ifoldl2, we swap the second and third
838 -- arguments of @f@ calling the result @g@.
839 g :: Int -> b -> Int -> a -> b
842 row_function :: b -> Int -> Vec n a -> b
843 row_function rowinit idx r = V.ifoldl (g idx) rowinit r