summaryrefslogtreecommitdiff
path: root/examples/pinv.hs
blob: 7de50b85216ebf6a1cb7d1ec6e1f070615ac6ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import Numeric.LinearAlgebra
import Graphics.Plot
import Text.Printf(printf)

expand :: Int -> Vector Double -> Matrix Double
expand n x = fromColumns $ map (x^) [0 .. n]

polynomialModel :: Vector Double -> Vector Double -> Int
                -> (Vector Double -> Vector Double)
polynomialModel x y n = f where
    f z = expand n z <> ws
    ws  = expand n x <\> y

main = do
    [x,y] <- (toColumns . readMatrix) `fmap` readFile "data.txt"
    let pol = polynomialModel x y
    let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
    putStrLn $ "  x      y      p 1    p 2    p 3"
    putStrLn $ format "  " (printf "%.2f") $ fromColumns view
    mplot view