Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import List
  4. import Monad
  5.  
  6. main = run cgs >> putStrLn "\n" >> run mgs where
  7.     run f = sequence $ liftM (run' f) ks
  8.    ks = [1e-1,1e-5,1e-10]
  9.    
  10.    run' f k = mprint $ res where
  11.         q = f [[1,k,0,0,0],[1,0,k,0,0],[1,0,0,k,0],[1,0,0,0,k]]
  12.         res = (transpose q ?* q) ?- ident (length q)
  13.    
  14.     --print matrix row by row.
  15.     mprint xs = sequence (liftM print $ transpose xs) >> putStrLn "\n"
  16.  
  17. --------------------------------------------------------------------------------------- vector utils
  18. infix  7 .& -- inner product
  19. infixl 5 .- -- vector subtraction
  20. infixl 6 .* -- vector scaling
  21.  
  22. (.&) xs ys = foldl1 (+) $ zipWith (*) xs ys
  23. (.*) xs y = map (*y) xs
  24. (.-) = zipWith (-)
  25.  
  26. norm xs = sqrt $ xs .& xs
  27. unit xs = xs .* 1/norm xs
  28.  
  29. ----------------------------------------------------------------------------------------matrix utils
  30. infixl 6 ?* -- matrix multiplication
  31. infixl 5 ?- -- matrix subtraction
  32.  
  33. (?*) xs ys = map m [0..length(ys)-1] where
  34.     xs' = transpose xs
  35.    m i = map (yi .&) xs' where yi = ys !! i
  36. (?-) = zipWith (.-)
  37.  
  38. ident n = [ map (delta i) [1..n] | i <- [1..n] ] where
  39.     delta i j = if(i==j) then 1 else 0
  40.  
  41. ----------------------------------------------------------classical gram schmidt... functional style
  42. cgs as = map q [0..length(as)-1] where
  43.     v i = foldl (.-) ai $ map (\j -> q j .* (ai .& q j)) [0..i-1]
  44.         where ai = as !! i
  45.     q i = unit $ v i
  46.    
  47. -----------------------------------------------------------modified gram schmidt... functional style
  48. mgs as = map q [1..length(as)] where
  49.     v i 1 = as !! (i-1)
  50.     v j i = v j (i-1) .- q (i-1) .* (v j (i-1) .& q (i-1))
  51.     q i = unit $ v i i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement