module Main where trans_f init inv inv splitAt ak where inv map 0-x ak

 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
module Main where
trans_f m v k = ((\(q, w) -> (init (inv q)) ++ (inv w)) (splitAt (k+1) a) ,b/(ak)) where
inv e = map (\x -> (0-x)/(ak)) e
a = m!!k
b = v!!k
ak = a!!k
eval_f x (a,b) k = foldr (+) b (zipWith (*) a (x' x k )) where
x' a k = (\(q, w) -> (init q) ++ (w)) (splitAt (k+1) a)
prep_iter u z = map (trans_f u z) [0..3]
gsi u z x = zipWith (eval_f x) (prep_iter u z) [0..3]
gsi_list d = iterate (gsi mA bv) d
gsi_list2 = gsi_list [0,0,0,0]
norm u = maximum $ map abs u
v_sub x y = zipWith (-) x y
gsi_solve n e = if norm(x1 `v_sub` x2) < e then (x2, n+1) else gsi_solve (n+1) e where
x1 = gsi_list2!!n
x2 = gsi_list2!!(n+1)
------------------
mA = [[5.94,-0.02,-3.4,2.08],[0.49,5.58,1.32,2.25],[-0.71,-0.3,7.03,1.5],[2.28,3.35,2.45,10.39]]
bv = [2.3,7.47,-2.83,11.48]
(x,n) = gsi_solve 0 0.0001
------------------
main = do
putStrLn "Solution="
print x
putStrLn "N of iterations="
print n
putStrLn "Iterations:"
print $ take n (gsi_list2)
--------------------------------------
Colored with http://dumpz.org