]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Linear/Matrix.hs
src/Linear/Matrix.hs: add support for zero-length columns/matrices.
[numerical-analysis.git] / src / Linear / Matrix.hs
1 {-# LANGUAGE ExistentialQuantification #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE NoMonomorphismRestriction #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeFamilies #-}
8 {-# LANGUAGE RebindableSyntax #-}
9
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.
14 --
15 module Linear.Matrix
16 where
17
18 import Data.List (intercalate)
19
20 import Data.Vector.Fixed (
21 (!),
22 generate,
23 mk0,
24 mk1,
25 mk2,
26 mk3,
27 mk4,
28 mk5 )
29 import qualified Data.Vector.Fixed as V (
30 and,
31 foldl,
32 fromList,
33 head,
34 ifoldl,
35 ifoldr,
36 imap,
37 map,
38 replicate,
39 reverse,
40 toList,
41 zipWith )
42 import Data.Vector.Fixed.Cont ( Arity, arity )
43 import Linear.Vector ( Vec, delete )
44 import Naturals
45 import Normed ( Normed(..) )
46
47 -- We want the "max" that works on Ord, not the one that only works on
48 -- Bool/Integer from the Lattice class!
49 import NumericPrelude hiding ( (*), abs, max)
50 import qualified NumericPrelude as NP ( (*) )
51 import qualified Algebra.Absolute as Absolute ( C )
52 import Algebra.Absolute ( abs )
53 import qualified Algebra.Additive as Additive ( C )
54 import qualified Algebra.Algebraic as Algebraic ( C )
55 import Algebra.Algebraic ( root )
56 import qualified Algebra.Field as Field ( C )
57 import qualified Algebra.Ring as Ring ( C )
58 import qualified Algebra.Module as Module ( C )
59 import qualified Algebra.RealRing as RealRing ( C )
60 import qualified Algebra.ToRational as ToRational ( C )
61 import qualified Algebra.Transcendental as Transcendental ( C )
62 import qualified Prelude as P ( map, max)
63
64 -- | Our main matrix type.
65 data Mat m n a = (Arity m, Arity n) => Mat (Vec m (Vec n a))
66
67 -- Type synonyms for n-by-n matrices.
68 type Mat0 a = Mat Z Z a
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
74 type Mat6 a = Mat N6 N6 a
75 type Mat7 a = Mat N7 N7 a
76
77 -- * Type synonyms for 1-by-n row "vectors".
78
79 -- | Type synonym for row vectors expressed as 1-by-n matrices.
80 type Row n a = Mat N1 n a
81
82 type Row1 a = Row N1 a
83 type Row2 a = Row N2 a
84 type Row3 a = Row N3 a
85 type Row4 a = Row N4 a
86 type Row5 a = Row N5 a
87
88 -- * Type synonyms for n-by-1 column "vectors".
89
90 -- | Type synonym for column vectors expressed as n-by-1 matrices.
91 type Col n a = Mat n N1 a
92
93 type Col0 a = Col Z a
94 type Col1 a = Col N1 a
95 type Col2 a = Col N2 a
96 type Col3 a = Col N3 a
97 type Col4 a = Col N4 a
98 type Col5 a = Col N5 a
99 type Col6 a = Col N6 a
100 type Col7 a = Col N7 a
101 type Col8 a = Col N8 a
102 type Col9 a = Col N9 a
103 type Col10 a = Col N10 a
104 type Col11 a = Col N11 a
105 type Col12 a = Col N12 a
106 type Col13 a = Col N13 a
107 type Col14 a = Col N14 a
108 type Col15 a = Col N15 a
109 type Col16 a = Col N16 a
110 type Col17 a = Col N17 a
111 type Col18 a = Col N18 a
112 type Col19 a = Col N19 a
113 type Col20 a = Col N20 a
114 type Col21 a = Col N21 a
115 type Col22 a = Col N22 a
116 type Col23 a = Col N23 a
117 type Col24 a = Col N24 a
118 type Col25 a = Col N25 a
119 type Col26 a = Col N26 a
120 type Col27 a = Col N27 a
121 type Col28 a = Col N28 a
122 type Col29 a = Col N29 a
123 type Col30 a = Col N30 a
124 type Col31 a = Col N31 a
125 type Col32 a = Col N32 a
126
127
128 instance (Eq a) => Eq (Mat m n a) where
129 -- | Compare a row at a time.
130 --
131 -- Examples:
132 --
133 -- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int
134 -- >>> let m2 = fromList [[1,2],[3,4]] :: Mat2 Int
135 -- >>> let m3 = fromList [[5,6],[7,8]] :: Mat2 Int
136 -- >>> m1 == m2
137 -- True
138 -- >>> m1 == m3
139 -- False
140 --
141 (Mat rows_one) == (Mat rows_two) =
142 V.and $ V.zipWith comp rows_one rows_two
143 where
144 -- Compare a row, one column at a time.
145 comp row1 row2 = V.and (V.zipWith (==) row1 row2)
146
147
148 instance (Show a) => Show (Mat m n a) where
149 -- | Display matrices and vectors as ordinary tuples. This is poor
150 -- practice, but these results are primarily displayed
151 -- interactively and convenience trumps correctness (said the guy
152 -- who insists his vector lengths be statically checked at
153 -- compile-time).
154 --
155 -- Examples:
156 --
157 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
158 -- >>> show m
159 -- ((1,2),(3,4))
160 --
161 show (Mat rows) =
162 "(" ++ (intercalate "," (V.toList row_strings)) ++ ")"
163 where
164 row_strings = V.map show_vector rows
165 show_vector v1 =
166 "(" ++ (intercalate "," element_strings) ++ ")"
167 where
168 v1l = V.toList v1
169 element_strings = P.map show v1l
170
171
172 -- | Convert a matrix to a nested list.
173 toList :: Mat m n a -> [[a]]
174 toList (Mat rows) = map V.toList (V.toList rows)
175
176
177 -- | Create a matrix from a nested list.
178 fromList :: (Arity m, Arity n) => [[a]] -> Mat m n a
179 fromList vs = Mat (V.fromList $ map V.fromList vs)
180
181
182 -- | Unsafe indexing. Much faster than the safe indexing.
183 (!!!) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> a
184 (!!!) (Mat rows) (i, j) = (rows ! i) ! j
185
186
187 -- | Safe indexing.
188 --
189 -- Examples:
190 --
191 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
192 -- >>> m !!? (-1,-1)
193 -- Nothing
194 -- >>> m !!? (-1,0)
195 -- Nothing
196 -- >>> m !!? (-1,1)
197 -- Nothing
198 -- >>> m !!? (0,-1)
199 -- Nothing
200 -- >>> m !!? (0,0)
201 -- Just 1
202 -- >>> m !!? (0,1)
203 -- Just 2
204 -- >>> m !!? (1,-1)
205 -- Nothing
206 -- >>> m !!? (1,0)
207 -- Just 3
208 -- >>> m !!? (1,1)
209 -- Just 4
210 -- >>> m !!? (2,-1)
211 -- Nothing
212 -- >>> m !!? (2,0)
213 -- Nothing
214 -- >>> m !!? (2,1)
215 -- Nothing
216 -- >>> m !!? (2,2)
217 -- Nothing
218 --
219 (!!?) :: (Arity m, Arity n) => Mat m n a -> (Int, Int) -> Maybe a
220 (!!?) matrix idx =
221 ifoldl2 f Nothing matrix
222 where
223 f k l found cur = if (k,l) == idx then (Just cur) else found
224
225
226 -- | The number of rows in the matrix.
227 nrows :: forall m n a. (Arity m) => Mat m n a -> Int
228 nrows _ = arity (undefined :: m)
229
230
231 -- | The number of columns in the first row of the
232 -- matrix. Implementation stolen from Data.Vector.Fixed.length.
233 ncols :: forall m n a. (Arity n) => Mat m n a -> Int
234 ncols _ = arity (undefined :: n)
235
236
237 -- | Return the @i@th row of @m@ as a matrix. Unsafe.
238 row :: (Arity m, Arity n) => Mat m n a -> Int -> Row n a
239 row m i =
240 construct lambda
241 where
242 lambda _ j = m !!! (i, j)
243
244
245 -- | Return the @j@th column of @m@ as a matrix. Unsafe.
246 column :: (Arity m, Arity n) => Mat m n a -> Int -> Col m a
247 column m j =
248 construct lambda
249 where
250 lambda i _ = m !!! (i, j)
251
252
253 -- | Transpose @m@; switch it's columns and its rows. This is a dirty
254 -- implementation, but I don't see a better way.
255 --
256 -- TODO: Don't cheat with fromList.
257 --
258 -- Examples:
259 --
260 -- >>> let m = fromList [[1,2], [3,4]] :: Mat2 Int
261 -- >>> transpose m
262 -- ((1,3),(2,4))
263 --
264 transpose :: (Arity m, Arity n) => Mat m n a -> Mat n m a
265 transpose matrix =
266 construct lambda
267 where
268 lambda i j = matrix !!! (j,i)
269
270
271 -- | Is @m@ symmetric?
272 --
273 -- Examples:
274 --
275 -- >>> let m1 = fromList [[1,2], [2,1]] :: Mat2 Int
276 -- >>> symmetric m1
277 -- True
278 --
279 -- >>> let m2 = fromList [[1,2], [3,1]] :: Mat2 Int
280 -- >>> symmetric m2
281 -- False
282 --
283 symmetric :: (Eq a, Arity m) => Mat m m a -> Bool
284 symmetric m =
285 m == (transpose m)
286
287
288 -- | Construct a new matrix from a function @lambda@. The function
289 -- @lambda@ should take two parameters i,j corresponding to the
290 -- entries in the matrix. The i,j entry of the resulting matrix will
291 -- have the value returned by lambda i j.
292 --
293 -- Examples:
294 --
295 -- >>> let lambda i j = i + j
296 -- >>> construct lambda :: Mat3 Int
297 -- ((0,1,2),(1,2,3),(2,3,4))
298 --
299 construct :: forall m n a. (Arity m, Arity n)
300 => (Int -> Int -> a) -> Mat m n a
301 construct lambda = Mat $ generate make_row
302 where
303 make_row :: Int -> Vec n a
304 make_row i = generate (lambda i)
305
306
307 -- | Create an identity matrix with the right dimensions.
308 --
309 -- Examples:
310 --
311 -- >>> identity_matrix :: Mat3 Int
312 -- ((1,0,0),(0,1,0),(0,0,1))
313 -- >>> identity_matrix :: Mat3 Double
314 -- ((1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0))
315 --
316 identity_matrix :: (Arity m, Ring.C a) => Mat m m a
317 identity_matrix =
318 construct (\i j -> if i == j then (fromInteger 1) else (fromInteger 0))
319
320
321 -- | Given a positive-definite matrix @m@, computes the
322 -- upper-triangular matrix @r@ with (transpose r)*r == m and all
323 -- values on the diagonal of @r@ positive.
324 --
325 -- Examples:
326 --
327 -- >>> let m1 = fromList [[20,-1], [-1,20]] :: Mat2 Double
328 -- >>> let r = cholesky m1
329 -- >>> frobenius_norm ((transpose r)*r - m1) < 1e-10
330 -- True
331 -- >>> is_upper_triangular r
332 -- True
333 --
334 -- >>> let k1 = [6, -3, 0, 0, 0, 0, 0] :: [Double]
335 -- >>> let k2 = [-3, 10.5, -7.5, 0, 0, 0, 0] :: [Double]
336 -- >>> let k3 = [0, -7.5, 12.5, 0, 0, 0, 0] :: [Double]
337 -- >>> let k4 = [0, 0, 0, 6, 0, 0, 0] :: [Double]
338 -- >>> let k5 = [0, 0, 0, 0, 6, 0, 0] :: [Double]
339 -- >>> let k6 = [0, 0, 0, 0, 0, 6, 0] :: [Double]
340 -- >>> let k7 = [0, 0, 0, 0, 0, 0, 15] :: [Double]
341 -- >>> let big_K = fromList [k1,k2,k3,k4,k5,k6,k7] :: Mat N7 N7 Double
342 --
343 -- >>> let e1 = [2.449489742783178,0,0,0,0,0,0] :: [Double]
344 -- >>> let e2 = [-1.224744871391589,3,0,0,0,0,0] :: [Double]
345 -- >>> let e3 = [0,-5/2,5/2,0,0,0,0] :: [Double]
346 -- >>> let e4 = [0,0,0,2.449489742783178,0,0,0] :: [Double]
347 -- >>> let e5 = [0,0,0,0,2.449489742783178,0,0] :: [Double]
348 -- >>> let e6 = [0,0,0,0,0,2.449489742783178,0] :: [Double]
349 -- >>> let e7 = [0,0,0,0,0,0,3.872983346207417] :: [Double]
350 -- >>> let expected = fromList [e1,e2,e3,e4,e5,e6,e7] :: Mat N7 N7 Double
351 --
352 -- >>> let r = cholesky big_K
353 -- >>> frobenius_norm (r - (transpose expected)) < 1e-12
354 -- True
355 --
356 cholesky :: forall m a. (Algebraic.C a, Arity m)
357 => (Mat m m a) -> (Mat m m a)
358 cholesky m = ifoldl2 f zero m
359 where
360 f :: Int -> Int -> (Mat m m a) -> a -> (Mat m m a)
361 f i j cur_R _ = set_idx cur_R (i,j) (r cur_R i j)
362
363 r :: (Mat m m a) -> Int -> Int -> a
364 r cur_R i j
365 | i == j = sqrt(m !!! (i,j) - sum [(cur_R !!! (k,i))^2 | k <- [0..i-1]])
366 | i < j = (((m !!! (i,j))
367 - sum [(cur_R !!! (k,i)) NP.* (cur_R !!! (k,j))
368 | k <- [0..i-1]]))/(cur_R !!! (i,i))
369 | otherwise = 0
370
371
372
373 -- | Returns True if the given matrix is upper-triangular, and False
374 -- otherwise. The parameter @epsilon@ lets the caller choose a
375 -- tolerance.
376 --
377 -- Examples:
378 --
379 -- >>> let m = fromList [[1,1],[1e-12,1]] :: Mat2 Double
380 -- >>> is_upper_triangular m
381 -- False
382 -- >>> is_upper_triangular' 1e-10 m
383 -- True
384 --
385 is_upper_triangular' :: forall m n a.
386 (Ord a, Ring.C a, Absolute.C a, Arity m, Arity n)
387 => a -- ^ The tolerance @epsilon@.
388 -> Mat m n a
389 -> Bool
390 is_upper_triangular' epsilon matrix =
391 ifoldl2 f True matrix
392 where
393 f :: Int -> Int -> Bool -> a -> Bool
394 f _ _ False _ = False
395 f i j True x
396 | i <= j = True
397 -- use "less than or equal to" so zero is a valid epsilon
398 | otherwise = abs x <= epsilon
399
400
401 -- | Returns True if the given matrix is upper-triangular, and False
402 -- otherwise. We don't delegate to the general
403 -- 'is_upper_triangular'' here because it imposes additional
404 -- typeclass constraints throughout the library.
405 --
406 -- Examples:
407 --
408 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
409 -- >>> is_upper_triangular m
410 -- False
411 --
412 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
413 -- >>> is_upper_triangular m
414 -- True
415 --
416 is_upper_triangular :: forall m n a.
417 (Eq a, Ring.C a, Arity m, Arity n)
418 => Mat m n a -> Bool
419 is_upper_triangular matrix =
420 ifoldl2 f True matrix
421 where
422 f :: Int -> Int -> Bool -> a -> Bool
423 f _ _ False _ = False
424 f i j True x
425 | i <= j = True
426 | otherwise = x == 0
427
428
429
430 -- | Returns True if the given matrix is lower-triangular, and False
431 -- otherwise.
432 --
433 -- Examples:
434 --
435 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
436 -- >>> is_lower_triangular m
437 -- True
438 --
439 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
440 -- >>> is_lower_triangular m
441 -- False
442 --
443 is_lower_triangular :: (Eq a,
444 Ring.C a,
445 Arity m,
446 Arity n)
447 => Mat m n a
448 -> Bool
449 is_lower_triangular = is_upper_triangular . transpose
450
451
452 -- | Returns True if the given matrix is lower-triangular, and False
453 -- otherwise. The parameter @epsilon@ lets the caller choose a
454 -- tolerance.
455 --
456 -- Examples:
457 --
458 -- >>> let m = fromList [[1,1e-12],[1,1]] :: Mat2 Double
459 -- >>> is_lower_triangular m
460 -- False
461 -- >>> is_lower_triangular' 1e-12 m
462 -- True
463 --
464 is_lower_triangular' :: (Ord a,
465 Ring.C a,
466 Absolute.C a,
467 Arity m,
468 Arity n)
469 => a -- ^ The tolerance @epsilon@.
470 -> Mat m n a
471 -> Bool
472 is_lower_triangular' epsilon = (is_upper_triangular' epsilon) . transpose
473
474
475 -- | Returns True if the given matrix is triangular, and False
476 -- otherwise.
477 --
478 -- Examples:
479 --
480 -- >>> let m = fromList [[1,0],[1,1]] :: Mat2 Int
481 -- >>> is_triangular m
482 -- True
483 --
484 -- >>> let m = fromList [[1,2],[0,3]] :: Mat2 Int
485 -- >>> is_triangular m
486 -- True
487 --
488 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
489 -- >>> is_triangular m
490 -- False
491 --
492 is_triangular :: (Ord a,
493 Ring.C a,
494 Absolute.C a,
495 Arity m,
496 Arity n)
497 => Mat m n a
498 -> Bool
499 is_triangular m = is_upper_triangular m || is_lower_triangular m
500
501
502 -- | Delete the @i@th row and @j@th column from the matrix. The name
503 -- \"preminor\" is made up, but is meant to signify that this is
504 -- usually used in the computationof a minor. A minor is simply the
505 -- determinant of a preminor in that case.
506 --
507 -- Examples:
508 --
509 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
510 -- >>> preminor m 0 0 :: Mat2 Int
511 -- ((5,6),(8,9))
512 -- >>> preminor m 1 1 :: Mat2 Int
513 -- ((1,3),(7,9))
514 --
515 preminor :: (Arity m, Arity n)
516 => Mat (S m) (S n) a
517 -> Int
518 -> Int
519 -> Mat m n a
520 preminor (Mat rows) i j = m
521 where
522 rows' = delete rows i
523 m = Mat $ V.map ((flip delete) j) rows'
524
525
526 -- | Compute the i,jth minor of a @matrix@.
527 --
528 -- Examples:
529 --
530 -- >>> let m1 = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Double
531 -- >>> minor m1 1 1
532 -- -12.0
533 --
534 minor :: (Arity m, Determined (Mat m m) a)
535 => Mat (S m) (S m) a
536 -> Int
537 -> Int
538 -> a
539 minor matrix i j = determinant (preminor matrix i j)
540
541 class (Eq a, Ring.C a) => Determined p a where
542 determinant :: (p a) -> a
543
544 instance (Eq a, Ring.C a) => Determined (Mat (S Z) (S Z)) a where
545 determinant = unscalar
546
547 instance (Ord a,
548 Ring.C a,
549 Absolute.C a,
550 Arity n,
551 Determined (Mat (S n) (S n)) a)
552 => Determined (Mat (S (S n)) (S (S n))) a where
553 -- | The recursive definition with a special-case for triangular matrices.
554 --
555 -- Examples:
556 --
557 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
558 -- >>> determinant m
559 -- -1
560 --
561 determinant m
562 | is_triangular m = product [ m !!! (i,i) | i <- [0..(nrows m)-1] ]
563 | otherwise = determinant_recursive
564 where
565 m' i j = m !!! (i,j)
566
567 determinant_recursive =
568 sum [ (-1)^(toInteger j) NP.* (m' 0 j) NP.* (minor m 0 j)
569 | j <- [0..(ncols m)-1] ]
570
571
572
573 -- | Matrix multiplication.
574 --
575 -- Examples:
576 --
577 -- >>> let m1 = fromList [[1,2,3], [4,5,6]] :: Mat N2 N3 Int
578 -- >>> let m2 = fromList [[1,2],[3,4],[5,6]] :: Mat N3 N2 Int
579 -- >>> m1 * m2
580 -- ((22,28),(49,64))
581 --
582 infixl 7 *
583 (*) :: (Ring.C a, Arity m, Arity n, Arity p)
584 => Mat (S m) (S n) a
585 -> Mat (S n) (S p) a
586 -> Mat (S m) (S p) a
587 (*) m1 m2 = construct lambda
588 where
589 lambda i j = (transpose $ row m1 i) `dot` (column m2 j)
590
591
592
593 instance (Ring.C a, Arity m, Arity n) => Additive.C (Mat m n a) where
594
595 (Mat rows_one) + (Mat rows_two) =
596 Mat $ V.zipWith (V.zipWith (+)) rows_one rows_two
597
598 (Mat rows_one) - (Mat rows_two) =
599 Mat $ V.zipWith (V.zipWith (-)) rows_one rows_two
600
601 zero = Mat (V.replicate $ V.replicate (fromInteger 0))
602
603
604 instance (Ring.C a, Arity m, Arity n, m ~ n) => Ring.C (Mat (S m) (S n) a) where
605 -- The first * is ring multiplication, the second is matrix
606 -- multiplication.
607 m1 * m2 = m1 * m2
608
609
610 instance (Ring.C a, Arity m, Arity n) => Module.C a (Mat m n a) where
611 -- We can multiply a matrix by a scalar of the same type as its
612 -- elements.
613 x *> (Mat rows) = Mat $ V.map (V.map (NP.* x)) rows
614
615
616 instance (Absolute.C a,
617 Algebraic.C a,
618 ToRational.C a,
619 Arity m)
620 => Normed (Col m a) where
621 -- | Generic p-norms for vectors in R^m that are represented as m-by-1
622 -- matrices.
623 --
624 -- Examples:
625 --
626 -- >>> let v1 = vec2d (3,4)
627 -- >>> norm_p 1 v1
628 -- 7.0
629 -- >>> norm_p 2 v1
630 -- 5.0
631 --
632 -- >>> let v1 = vec2d (-1,1) :: Col2 Double
633 -- >>> norm_p 1 v1 :: Double
634 -- 2.0
635 --
636 -- >>> let v1 = vec0d :: Col0 Double
637 -- >>> norm v1
638 -- 0.0
639 --
640 norm_p p (Mat rows) =
641 (root p') $ sum [fromRational' (toRational $ abs x)^p' | x <- xs]
642 where
643 p' = toInteger p
644 xs = concat $ V.toList $ V.map V.toList rows
645
646 -- | The infinity norm.
647 --
648 -- Examples:
649 --
650 -- >>> let v1 = vec3d (1,5,2)
651 -- >>> norm_infty v1
652 -- 5
653 --
654 norm_infty (Mat rows) =
655 fromRational' $ toRational
656 $ (V.foldl P.max 0) $ V.map (V.foldl P.max 0) rows
657
658
659 -- | Compute the Frobenius norm of a matrix. This essentially treats
660 -- the matrix as one long vector containing all of its entries (in
661 -- any order, it doesn't matter).
662 --
663 -- Examples:
664 --
665 -- >>> let m = fromList [[1, 2, 3],[4,5,6],[7,8,9]] :: Mat3 Double
666 -- >>> frobenius_norm m == sqrt 285
667 -- True
668 --
669 -- >>> let m = fromList [[1, -1, 1],[-1,1,-1],[1,-1,1]] :: Mat3 Double
670 -- >>> frobenius_norm m == 3
671 -- True
672 --
673 frobenius_norm :: (Arity m, Arity n, Algebraic.C a, Ring.C a)
674 => Mat m n a
675 -> a
676 frobenius_norm matrix =
677 sqrt $ element_sum2 $ squares
678 where
679 squares = map2 (^2) matrix
680
681
682 -- Vector helpers. We want it to be easy to create low-dimension
683 -- column vectors, which are nx1 matrices.
684
685 -- | Convenient constructor for 2D vectors.
686 --
687 -- Examples:
688 --
689 -- >>> import Roots.Simple
690 -- >>> let fst m = m !!! (0,0)
691 -- >>> let snd m = m !!! (1,0)
692 -- >>> let h = 0.5 :: Double
693 -- >>> let g1 m = 1.0 + h NP.* exp(-((fst m)^2))/(1.0 + (snd m)^2)
694 -- >>> let g2 m = 0.5 + h NP.* atan((fst m)^2 + (snd m)^2)
695 -- >>> let g u = vec2d ((g1 u), (g2 u))
696 -- >>> let u0 = vec2d (1.0, 1.0)
697 -- >>> let eps = 1/(10^9)
698 -- >>> fixed_point g eps u0
699 -- ((1.0728549599342185),(1.0820591495686167))
700 --
701 vec0d :: Col0 a
702 vec0d = Mat mk0
703
704 vec1d :: (a) -> Col1 a
705 vec1d (x) = Mat (mk1 (mk1 x))
706
707 vec2d :: (a,a) -> Col2 a
708 vec2d (x,y) = Mat (mk2 (mk1 x) (mk1 y))
709
710 vec3d :: (a,a,a) -> Col3 a
711 vec3d (x,y,z) = Mat (mk3 (mk1 x) (mk1 y) (mk1 z))
712
713 vec4d :: (a,a,a,a) -> Col4 a
714 vec4d (w,x,y,z) = Mat (mk4 (mk1 w) (mk1 x) (mk1 y) (mk1 z))
715
716 vec5d :: (a,a,a,a,a) -> Col5 a
717 vec5d (v,w,x,y,z) = Mat (mk5 (mk1 v) (mk1 w) (mk1 x) (mk1 y) (mk1 z))
718
719
720 -- Since we commandeered multiplication, we need to create 1x1
721 -- matrices in order to multiply things.
722 scalar :: a -> Mat1 a
723 scalar x = Mat (mk1 (mk1 x))
724
725 -- Get the scalar value out of a 1x1 matrix.
726 unscalar :: Mat1 a -> a
727 unscalar (Mat rows) = V.head $ V.head rows
728
729
730 dot :: (Ring.C a, Arity m)
731 => Col (S m) a
732 -> Col (S m) a
733 -> a
734 v1 `dot` v2 = element_sum2 $ zipwith2 (NP.*) v1 v2
735
736
737 -- | The angle between @v1@ and @v2@ in Euclidean space.
738 --
739 -- Examples:
740 --
741 -- >>> let v1 = vec2d (1.0, 0.0)
742 -- >>> let v2 = vec2d (0.0, 1.0)
743 -- >>> angle v1 v2 == pi/2.0
744 -- True
745 --
746 angle :: (Transcendental.C a,
747 RealRing.C a,
748 m ~ S t,
749 Arity t,
750 ToRational.C a)
751 => Col m a
752 -> Col m a
753 -> a
754 angle v1 v2 =
755 acos theta
756 where
757 theta = (recip norms) NP.* (v1 `dot` v2)
758 norms = (norm v1) NP.* (norm v2)
759
760
761 -- | Retrieve the diagonal elements of the given matrix as a \"column
762 -- vector,\" i.e. a m-by-1 matrix. We require the matrix to be
763 -- square to avoid ambiguity in the return type which would ideally
764 -- have dimension min(m,n) supposing an m-by-n matrix.
765 --
766 -- Examples:
767 --
768 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
769 -- >>> diagonal m
770 -- ((1),(5),(9))
771 --
772 diagonal :: (Arity m) => Mat m m a -> Col m a
773 diagonal matrix =
774 construct lambda
775 where
776 lambda i _ = matrix !!! (i,i)
777
778
779 -- | Given a square @matrix@, return a new matrix of the same size
780 -- containing only the on-diagonal entries of @matrix@. The
781 -- off-diagonal entries are set to zero.
782 --
783 -- Examples:
784 --
785 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
786 -- >>> diagonal_part m
787 -- ((1,0,0),(0,5,0),(0,0,9))
788 --
789 diagonal_part :: (Arity m, Ring.C a)
790 => Mat m m a
791 -> Mat m m a
792 diagonal_part matrix =
793 construct lambda
794 where
795 lambda i j = if i == j then matrix !!! (i,j) else 0
796
797
798 -- | Given a square @matrix@, return a new matrix of the same size
799 -- containing only the on-diagonal and below-diagonal entries of
800 -- @matrix@. The above-diagonal entries are set to zero.
801 --
802 -- Examples:
803 --
804 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
805 -- >>> lt_part m
806 -- ((1,0,0),(4,5,0),(7,8,9))
807 --
808 lt_part :: (Arity m, Ring.C a)
809 => Mat m m a
810 -> Mat m m a
811 lt_part matrix =
812 construct lambda
813 where
814 lambda i j = if i >= j then matrix !!! (i,j) else 0
815
816
817 -- | Given a square @matrix@, return a new matrix of the same size
818 -- containing only the below-diagonal entries of @matrix@. The on-
819 -- and above-diagonal entries are set to zero.
820 --
821 -- Examples:
822 --
823 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
824 -- >>> lt_part_strict m
825 -- ((0,0,0),(4,0,0),(7,8,0))
826 --
827 lt_part_strict :: (Arity m, Ring.C a)
828 => Mat m m a
829 -> Mat m m a
830 lt_part_strict matrix =
831 construct lambda
832 where
833 lambda i j = if i > j then matrix !!! (i,j) else 0
834
835
836 -- | Given a square @matrix@, return a new matrix of the same size
837 -- containing only the on-diagonal and above-diagonal entries of
838 -- @matrix@. The below-diagonal entries are set to zero.
839 --
840 -- Examples:
841 --
842 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
843 -- >>> ut_part m
844 -- ((1,2,3),(0,5,6),(0,0,9))
845 --
846 ut_part :: (Arity m, Ring.C a)
847 => Mat m m a
848 -> Mat m m a
849 ut_part = transpose . lt_part . transpose
850
851
852 -- | Given a square @matrix@, return a new matrix of the same size
853 -- containing only the above-diagonal entries of @matrix@. The on-
854 -- and below-diagonal entries are set to zero.
855 --
856 -- Examples:
857 --
858 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
859 -- >>> ut_part_strict m
860 -- ((0,2,3),(0,0,6),(0,0,0))
861 --
862 ut_part_strict :: (Arity m, Ring.C a)
863 => Mat m m a
864 -> Mat m m a
865 ut_part_strict = transpose . lt_part_strict . transpose
866
867
868 -- | Compute the trace of a square matrix, the sum of the elements
869 -- which lie on its diagonal. We require the matrix to be
870 -- square to avoid ambiguity in the return type which would ideally
871 -- have dimension min(m,n) supposing an m-by-n matrix.
872 --
873 -- Examples:
874 --
875 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
876 -- >>> trace m
877 -- 15
878 --
879 trace :: (Arity m, Ring.C a) => Mat m m a -> a
880 trace = element_sum2 . diagonal
881
882
883
884 -- | Zip together two matrices.
885 --
886 -- TODO: don't cheat with construct (map V.zips instead).
887 --
888 -- Examples:
889 --
890 -- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int
891 -- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int
892 -- >>> zip2 m1 m2
893 -- (((1,1)),((1,2)),((1,3)))
894 --
895 -- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int
896 -- >>> let m2 = fromList [[1,1],[1,1]] :: Mat2 Int
897 -- >>> zip2 m1 m2
898 -- (((1,1),(2,1)),((3,1),(4,1)))
899 --
900 zip2 :: (Arity m, Arity n) => Mat m n a -> Mat m n b -> Mat m n (a,b)
901 zip2 m1 m2 =
902 construct lambda
903 where
904 lambda i j = (m1 !!! (i,j), m2 !!! (i,j))
905
906
907 -- | Zip together three matrices.
908 --
909 -- TODO: don't cheat with construct (map V.zips instead).
910 --
911 -- Examples:
912 --
913 -- >>> let m1 = fromList [[1],[1],[1]] :: Col3 Int
914 -- >>> let m2 = fromList [[1],[2],[3]] :: Col3 Int
915 -- >>> let m3 = fromList [[4],[5],[6]] :: Col3 Int
916 -- >>> zip2three m1 m2 m3
917 -- (((1,1,4)),((1,2,5)),((1,3,6)))
918 --
919 -- >>> let m1 = fromList [[1,2],[3,4]] :: Mat2 Int
920 -- >>> let m2 = fromList [[1,1],[1,1]] :: Mat2 Int
921 -- >>> let m3 = fromList [[8,2],[6,3]] :: Mat2 Int
922 -- >>> zip2three m1 m2 m3
923 -- (((1,1,8),(2,1,2)),((3,1,6),(4,1,3)))
924 --
925 zip2three :: (Arity m, Arity n)
926 => Mat m n a
927 -> Mat m n a
928 -> Mat m n a
929 -> Mat m n (a,a,a)
930 zip2three m1 m2 m3 =
931 construct lambda
932 where
933 lambda i j = (m1 !!! (i,j), m2 !!! (i,j), m3 !!! (i,j))
934
935
936 -- | Zip together two matrices using the supplied function.
937 --
938 -- Examples:
939 --
940 -- >>> let c1 = fromList [[1],[2],[3]] :: Col3 Integer
941 -- >>> let c2 = fromList [[4],[5],[6]] :: Col3 Integer
942 -- >>> zipwith2 (^) c1 c2
943 -- ((1),(32),(729))
944 --
945 zipwith2 :: (Arity m, Arity n)
946 => (a -> b -> c)
947 -> Mat m n a
948 -> Mat m n b
949 -> Mat m n c
950 zipwith2 f c1 c2 =
951 construct lambda
952 where
953 lambda i j = f (c1 !!! (i,j)) (c2 !!! (i,j))
954
955
956 -- | Map a function over a matrix of any dimensions.
957 --
958 -- Examples:
959 --
960 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
961 -- >>> map2 (^2) m
962 -- ((1,4),(9,16))
963 --
964 map2 :: (a -> b) -> Mat m n a -> Mat m n b
965 map2 f (Mat rows) =
966 Mat $ V.map g rows
967 where
968 g = V.map f
969
970
971 -- | Fold over the entire matrix passing the coordinates @i@ and @j@
972 -- (of the row/column) to the accumulation function. The fold occurs
973 -- from top-left to bottom-right.
974 --
975 -- Examples:
976 --
977 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
978 -- >>> ifoldl2 (\i j cur _ -> cur + i + j) 0 m
979 -- 18
980 --
981 ifoldl2 :: forall a b m n.
982 (Int -> Int -> b -> a -> b)
983 -> b
984 -> Mat m n a
985 -> b
986 ifoldl2 f initial (Mat rows) =
987 V.ifoldl row_function initial rows
988 where
989 -- | The order that we need this in (so that @g idx@ makes sense)
990 -- is a little funny. So that we don't need to pass weird
991 -- functions into ifoldl2, we swap the second and third
992 -- arguments of @f@ calling the result @g@.
993 g :: Int -> b -> Int -> a -> b
994 g w x y = f w y x
995
996 row_function :: b -> Int -> Vec n a -> b
997 row_function rowinit idx r = V.ifoldl (g idx) rowinit r
998
999
1000 -- | Left fold over the entries of a matrix (top-left to bottom-right).
1001 --
1002 foldl2 :: forall a b m n.
1003 (b -> a -> b)
1004 -> b
1005 -> Mat m n a
1006 -> b
1007 foldl2 f initial matrix =
1008 -- Use the index fold but ignore the index arguments.
1009 let g _ _ = f in ifoldl2 g initial matrix
1010
1011
1012 -- | Fold over the entire matrix passing the coordinates @i@ and @j@
1013 -- (of the row/column) to the accumulation function. The fold occurs
1014 -- from bottom-right to top-left.
1015 --
1016 -- The order of the arguments in the supplied function are different
1017 -- from those in V.ifoldr; we keep them similar to ifoldl2.
1018 --
1019 -- Examples:
1020 --
1021 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
1022 -- >>> ifoldr2 (\i j cur _ -> cur + i + j) 0 m
1023 -- 18
1024 --
1025 ifoldr2 :: forall a b m n.
1026 (Int -> Int -> b -> a -> b)
1027 -> b
1028 -> Mat m n a
1029 -> b
1030 ifoldr2 f initial (Mat rows) =
1031 V.ifoldr row_function initial rows
1032 where
1033 -- | Swap the order of arguments in @f@ so that it agrees with the
1034 -- @f@ passed to ifoldl2.
1035 g :: Int -> Int -> a -> b -> b
1036 g w x y z = f w x z y
1037
1038 row_function :: Int -> Vec n a -> b -> b
1039 row_function idx r rowinit = V.ifoldr (g idx) rowinit r
1040
1041
1042 -- | Map a function over a matrix of any dimensions, passing the
1043 -- coordinates @i@ and @j@ to the function @f@.
1044 --
1045 -- Examples:
1046 --
1047 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
1048 -- >>> imap2 (\i j _ -> i+j) m
1049 -- ((0,1),(1,2))
1050 --
1051 imap2 :: (Int -> Int -> a -> b) -> Mat m n a -> Mat m n b
1052 imap2 f (Mat rows) =
1053 Mat $ V.imap g rows
1054 where
1055 g i = V.imap (f i)
1056
1057
1058 -- | Reverse the order of elements in a matrix.
1059 --
1060 -- Examples:
1061 --
1062 -- >>> let m1 = fromList [[1,2,3]] :: Row3 Int
1063 -- >>> reverse2 m1
1064 -- ((3,2,1))
1065 --
1066 -- >>> let m1 = vec3d (1,2,3 :: Int)
1067 -- >>> reverse2 m1
1068 -- ((3),(2),(1))
1069 --
1070 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
1071 -- >>> reverse2 m
1072 -- ((9,8,7),(6,5,4),(3,2,1))
1073 --
1074 reverse2 :: (Arity m, Arity n) => Mat m n a -> Mat m n a
1075 reverse2 (Mat rows) = Mat $ V.reverse $ V.map V.reverse rows
1076
1077
1078 -- | Unsafely set the (i,j) element of the given matrix.
1079 --
1080 -- Examples:
1081 --
1082 -- >>> let m = fromList [[1,2,3],[4,5,6],[7,8,9]] :: Mat3 Int
1083 -- >>> set_idx m (1,1) 17
1084 -- ((1,2,3),(4,17,6),(7,8,9))
1085 --
1086 set_idx :: forall m n a.
1087 (Arity m, Arity n)
1088 => Mat m n a
1089 -> (Int, Int)
1090 -> a
1091 -> Mat m n a
1092 set_idx matrix (i,j) newval =
1093 imap2 updater matrix
1094 where
1095 updater :: Int -> Int -> a -> a
1096 updater k l existing =
1097 if k == i && l == j
1098 then newval
1099 else existing
1100
1101
1102 -- | Compute the i,jth cofactor of the given @matrix@. This simply
1103 -- premultiplues the i,jth minor by (-1)^(i+j).
1104 cofactor :: (Arity m, Determined (Mat m m) a)
1105 => Mat (S m) (S m) a
1106 -> Int
1107 -> Int
1108 -> a
1109 cofactor matrix i j =
1110 (-1)^(toInteger i + toInteger j) NP.* (minor matrix i j)
1111
1112
1113 -- | Compute the inverse of a matrix using cofactor expansion
1114 -- (generalized Cramer's rule).
1115 --
1116 -- Examples:
1117 --
1118 -- >>> let m1 = fromList [[37,22],[17,54]] :: Mat2 Double
1119 -- >>> let e1 = [54/1624, -22/1624] :: [Double]
1120 -- >>> let e2 = [-17/1624, 37/1624] :: [Double]
1121 -- >>> let expected = fromList [e1, e2] :: Mat2 Double
1122 -- >>> let actual = inverse m1
1123 -- >>> frobenius_norm (actual - expected) < 1e-12
1124 -- True
1125 --
1126 inverse :: (Arity m,
1127 Determined (Mat (S m) (S m)) a,
1128 Determined (Mat m m) a,
1129 Field.C a)
1130 => Mat (S m) (S m) a
1131 -> Mat (S m) (S m) a
1132 inverse matrix =
1133 (1 / (determinant matrix)) *> (transpose $ construct lambda)
1134 where
1135 lambda i j = cofactor matrix i j
1136
1137
1138
1139 -- | Retrieve the rows of a matrix as a column matrix. If the given
1140 -- matrix is m-by-n, the result would be an m-by-1 column whose
1141 -- entries are 1-by-n row matrices.
1142 --
1143 -- Examples:
1144 --
1145 -- >>> let m = fromList [[1,2],[3,4]] :: Mat2 Int
1146 -- >>> (rows2 m) !!! (0,0)
1147 -- ((1,2))
1148 -- >>> (rows2 m) !!! (1,0)
1149 -- ((3,4))
1150 --
1151 rows2 :: (Arity m, Arity n)
1152 => Mat m n a
1153 -> Col m (Row n a)
1154 rows2 (Mat rows) =
1155 Mat $ V.map (mk1. Mat . mk1) rows
1156
1157
1158
1159 -- | Sum the elements of a matrix.
1160 --
1161 -- Examples:
1162 --
1163 -- >>> let m = fromList [[1,-1],[3,4]] :: Mat2 Int
1164 -- >>> element_sum2 m
1165 -- 7
1166 --
1167 element_sum2 :: (Arity m, Arity n, Additive.C a) => Mat m n a -> a
1168 element_sum2 = foldl2 (+) zero