summaryrefslogtreecommitdiff
path: root/examples/pinv.hs
blob: cab8fc6f7799ab18f3088c7ee4ada1946c2b9a60 (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 $ constant 1 (dim x): map (x^) [1 .. 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