summaryrefslogtreecommitdiff
path: root/examples/pinv1.hs
blob: 76fa0a9e90100b9ee194a7739620973cc83992aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- initial check for the polynomial model example
import LinearAlgebra


prepSyst :: Int -> Matrix Double -> (Matrix Double, Vector Double)
prepSyst n d = (a,b) where
    [x,b] = toColumns d
    a = fromColumns $ 1+0*x : map (x^) [1 .. n]

main = do
    dat <- readMatrix `fmap` readFile "data.txt"
    let (a,b) = prepSyst 3 dat
    putStr "Coefficient matrix:\n"
    dispR 3 a
    putStr "Desired values:\n"
    print b
    putStr "\nLeast Squares Solution:\n"
    print $ pinv a <> b -- equivalent to: linearSolveLSR a (fromColumns [b])