summaryrefslogtreecommitdiff
path: root/examples/pinv.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pinv.hs')
-rw-r--r--examples/pinv.hs13
1 files changed, 6 insertions, 7 deletions
diff --git a/examples/pinv.hs b/examples/pinv.hs
index 7de50b8..6f093b4 100644
--- a/examples/pinv.hs
+++ b/examples/pinv.hs
@@ -1,20 +1,19 @@
1import Numeric.LinearAlgebra 1import Numeric.LinearAlgebra
2import Graphics.Plot
3import Text.Printf(printf) 2import Text.Printf(printf)
4 3
5expand :: Int -> Vector Double -> Matrix Double 4expand :: Int -> Vector R -> Matrix R
6expand n x = fromColumns $ map (x^) [0 .. n] 5expand n x = fromColumns $ map (x^) [0 .. n]
7 6
8polynomialModel :: Vector Double -> Vector Double -> Int 7polynomialModel :: Vector R -> Vector R -> Int
9 -> (Vector Double -> Vector Double) 8 -> (Vector R -> Vector R)
10polynomialModel x y n = f where 9polynomialModel x y n = f where
11 f z = expand n z <> ws 10 f z = expand n z #> ws
12 ws = expand n x <\> y 11 ws = expand n x <\> y
13 12
14main = do 13main = do
15 [x,y] <- (toColumns . readMatrix) `fmap` readFile "data.txt" 14 [x,y] <- toColumns <$> loadMatrix "data.txt"
16 let pol = polynomialModel x y 15 let pol = polynomialModel x y
17 let view = [x, y, pol 1 x, pol 2 x, pol 3 x] 16 let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
18 putStrLn $ " x y p 1 p 2 p 3" 17 putStrLn $ " x y p 1 p 2 p 3"
19 putStrLn $ format " " (printf "%.2f") $ fromColumns view 18 putStrLn $ format " " (printf "%.2f") $ fromColumns view
20 mplot view 19