Skip to content

Commit 5e34439

Browse files
committed
Indented code with hindent
1 parent f957b3c commit 5e34439

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

chapters/matrix_methods/gaussian_elimination/code/haskell/gaussianElimination.hs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,61 @@ import Data.List (intercalate)
44
type Matrix a = Array (Int, Int) a
55

66
cols :: Matrix a -> [Int]
7-
cols m = let ((_, c1), (_, cn)) = bounds m in [c1..cn]
7+
cols m =
8+
let ((_, c1), (_, cn)) = bounds m
9+
in [c1 .. cn]
810

911
swapRows :: Int -> Int -> Matrix a -> Matrix a
1012
swapRows r1 r2 m
11-
| r1 == r2 = m
12-
| otherwise = m // concat [ [((r2, c), m!(r1, c)), ((r1, c), m!(r2, c))]
13-
| c <- cols m]
13+
| r1 == r2 = m
14+
| otherwise =
15+
m // concat [[((r2, c), m ! (r1, c)), ((r1, c), m ! (r2, c))] | c <- cols m]
1416

1517
multRow :: (Num a) => Int -> a -> Matrix a -> Matrix a
16-
multRow r a m = m // [((r, k), a * m!(r, k)) | k <- cols m ]
18+
multRow r a m = m // [((r, k), a * m ! (r, k)) | k <- cols m]
1719

18-
combRows :: (Eq a, Fractional a) => (Int, Int) -> a -> Int -> Matrix a -> Matrix a
20+
combRows ::
21+
(Eq a, Fractional a) => (Int, Int) -> a -> Int -> Matrix a -> Matrix a
1922
combRows (r, c) a t m
20-
| m!(t, c) == 0 = m
21-
| otherwise = m // [((t, k), a * m!(t, k)/(m!(t, c)) - m!(r, k))
22-
| k <- cols m ]
23+
| m ! (t, c) == 0 = m
24+
| otherwise =
25+
m // [((t, k), a * m ! (t, k) / (m ! (t, c)) - m ! (r, k)) | k <- cols m]
2326

2427
toEchelon :: (Ord a, Fractional a) => Matrix a -> Matrix a
2528
toEchelon mat = go (r1, c1) mat
2629
where
27-
((r1, c1), (rn, cn)) = bounds mat
28-
go (r, c) m
29-
| c == cn = m
30-
| pivot == 0 = go (r, c + 1) m
31-
| otherwise = go (r + 1, c + 1)
32-
$ foldr (combRows (r, c) pivot) (swapRows r target m) [r+1..rn]
33-
where (pivot, target) = maximum [ (m!(k, c), k) | k <- [r..rn]]
30+
((r1, c1), (rn, cn)) = bounds mat
31+
go (r, c) m
32+
| c == cn = m
33+
| pivot == 0 = go (r, c + 1) m
34+
| otherwise =
35+
go (r + 1, c + 1) $
36+
foldr (combRows (r, c) pivot) (swapRows r target m) [r + 1 .. rn]
37+
where
38+
(pivot, target) = maximum [(m ! (k, c), k) | k <- [r .. rn]]
3439

3540
toReducedEchelon :: (Fractional a, Eq a) => Matrix a -> Matrix a
3641
toReducedEchelon mat = foldr go mat (echelonPath (r1, c1))
3742
where
38-
((r1, c1), (rn, cn)) = bounds mat
39-
echelonPath (r, c)
40-
| r > rn || c>=cn = []
41-
| mat!(r, c) == 0 = echelonPath (r, c + 1)
42-
| otherwise = (r, c) : echelonPath (r + 1, c + 1)
43-
go (r, c) m = foldr (combRows (r, c) 1) (multRow r (1/(m!(r, c))) m) [r1..r-1]
44-
43+
((r1, c1), (rn, cn)) = bounds mat
44+
echelonPath (r, c)
45+
| r > rn || c >= cn = []
46+
| mat ! (r, c) == 0 = echelonPath (r, c + 1)
47+
| otherwise = (r, c) : echelonPath (r + 1, c + 1)
48+
go (r, c) m =
49+
foldr (combRows (r, c) 1) (multRow r (1 / (m ! (r, c))) m) [r1 .. r - 1]
4550

4651
printM :: (Show a) => Matrix a -> String
4752
printM m =
48-
let ((r1, c1), (rn, cn)) = bounds m
49-
in unlines [ intercalate "\t" [ take 7 $ show $ m!(r, c) | c <- [c1..cn] ]
50-
| r <- [r1..rn] ]
53+
let ((r1, c1), (rn, cn)) = bounds m
54+
in unlines
55+
[ intercalate "\t" [take 7 $ show $ m ! (r, c) | c <- [c1 .. cn]]
56+
| r <- [r1 .. rn]
57+
]
5158

5259
main :: IO ()
5360
main = do
54-
let m = listArray ((1,1),(3,4)) [2,3,4,6,1,2,3,4,3,-4,0,10]
61+
let m = listArray ((1, 1), (3, 4)) [2, 3, 4, 6, 1, 2, 3, 4, 3, -4, 0, 10]
5562
putStrLn "Original Matrix:"
5663
putStrLn $ printM m
5764
putStrLn "Echelon form"

0 commit comments

Comments
 (0)