-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inverses.hs
85 lines (66 loc) · 3.1 KB
/
Inverses.hs
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
-- Inverses Module
--
-- This stores all the fundamental things for manipulating matrices
-- This includes things like determinants and inverse matrices
--
-- Jacob Buete 2014
module Inverses (
det_two, -- :: Matrix -> Float
determinant, -- :: Matrix -> Float
cofactor_matrix, -- :: Matrix -> Matrix
adjoint_matrix, -- :: Matrix -> Matrix
inverse_matrix, -- :: Matrix -> Matrix
) where
import Matrices
import Algebra
remove_at :: Int -> [a] -> [a]
remove_at k list = case list of
[] -> error "List not that long"
x:xs
| k == 1 -> xs
| otherwise -> x:remove_at (k-1) xs
find_cofactor :: Int -> Int -> Matrix -> Matrix
find_cofactor n m matrix = map (remove_at m) (remove_at n matrix)
det_two :: Matrix -> Float
det_two matrix = (head top)*(last bottom) - (head bottom)*(last top)
where
top = head matrix
bottom = last matrix
element_at :: Int -> Row -> Float
element_at k row = case row of
[] -> error "Nope"
x:xs
| k == 1 -> x
| otherwise -> element_at (k-1) xs
determinant :: Matrix -> Float
determinant matrix
| length matrix == 2 = det_two matrix
| is_singleton matrix = head (head matrix)
| otherwise = find_det (length matrix) matrix
where
det_element :: Int -> Matrix -> Float
det_element m matrix
| m`mod`2 == 0 = (-element_at m (head matrix)) * determinant (find_cofactor 1 m matrix)
| otherwise = (element_at m (head matrix)) * determinant (find_cofactor 1 m matrix)
find_det :: Int -> Matrix -> Float
find_det m matrix
| m == 0 = 0
| otherwise = (det_element m matrix) + find_det (m-1) matrix
cofactor_matrix :: Matrix -> Matrix
cofactor_matrix matrix = cofactor_aux (length matrix) (length (head matrix))
where
cofactor_row :: Int -> Int -> Row
cofactor_row n m
| m == 0 = []
| (m+n) `mod` 2 == 1 = cofactor_row n (m-1) ++ [-determinant (find_cofactor n m matrix)]
| otherwise = cofactor_row n (m-1) ++ [determinant (find_cofactor n m matrix)]
cofactor_aux :: Int -> Int -> Matrix
cofactor_aux n m
| n == 0 = []
| otherwise = cofactor_aux (n-1) m ++ [cofactor_row n m]
adjoint_matrix :: Matrix -> Matrix
adjoint_matrix matrix = transpose_matrix (cofactor_matrix matrix)
inverse_matrix :: Matrix -> Matrix
inverse_matrix matrix
| is_singleton matrix = [[(1/head (head matrix))]]
| otherwise = scale_matrix (1/(determinant matrix)) (adjoint_matrix matrix)