From 0198366bba7a5f2d67338633f9eb90889ffc31b2 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 21 Sep 2007 18:28:08 +0000 Subject: add examples --- examples/pinv2.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/pinv2.hs (limited to 'examples/pinv2.hs') diff --git a/examples/pinv2.hs b/examples/pinv2.hs new file mode 100644 index 0000000..c1038d1 --- /dev/null +++ b/examples/pinv2.hs @@ -0,0 +1,26 @@ +-- MSE polynomial model using the pseudoinverse +import LinearAlgebra +import Graphics.Plot + +expand :: Int -> Vector Double -> Matrix Double +expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n] + +polynomialModel :: Matrix Double -> Int -> (Vector Double -> Vector Double) +polynomialModel d n = f where + f z = expand n z <> ws + ws = pinv a <> b + [x,b] = toColumns d + a = expand n x + +mk fv = f where + f x = fv (fromList [x]) @> 0 + +main = do + d <- readMatrix `fmap` readFile "data.txt" + let [x,y] = toColumns d + let pol = polynomialModel d + let view = [x, y, pol 1 x, pol 2 x, pol 3 x] + dispR 2 $ fromColumns view + mplot view + let f = mk (pol 2) + print (f 2.5) -- cgit v1.2.3