diff options
Diffstat (limited to 'examples/pinv.hs')
-rw-r--r-- | examples/pinv.hs | 13 |
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 @@ | |||
1 | import Numeric.LinearAlgebra | 1 | import Numeric.LinearAlgebra |
2 | import Graphics.Plot | ||
3 | import Text.Printf(printf) | 2 | import Text.Printf(printf) |
4 | 3 | ||
5 | expand :: Int -> Vector Double -> Matrix Double | 4 | expand :: Int -> Vector R -> Matrix R |
6 | expand n x = fromColumns $ map (x^) [0 .. n] | 5 | expand n x = fromColumns $ map (x^) [0 .. n] |
7 | 6 | ||
8 | polynomialModel :: Vector Double -> Vector Double -> Int | 7 | polynomialModel :: Vector R -> Vector R -> Int |
9 | -> (Vector Double -> Vector Double) | 8 | -> (Vector R -> Vector R) |
10 | polynomialModel x y n = f where | 9 | polynomialModel 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 | ||
14 | main = do | 13 | main = 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 | |