summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README3
-rw-r--r--examples/pinv.hs20
-rw-r--r--examples/pinv1.hs18
-rw-r--r--examples/pinv2.hs26
4 files changed, 22 insertions, 45 deletions
diff --git a/README b/README
index cd54fcd..e2b7aa3 100644
--- a/README
+++ b/README
@@ -107,4 +107,5 @@ INSTALLATION ON WINDOWS
107 They are required to run the programs and ghci. 107 They are required to run the programs and ghci.
108 108
109Unfortunately the lapack dll supplied by the R system does not include zgels_ and zgelss_, 109Unfortunately the lapack dll supplied by the R system does not include zgels_ and zgelss_,
110so the functions depending on them will produced a "non supported" runtime error. 110so the functions depending on them (linearSolveLS and linearSolveSVD for complex data)
111will produce a "non supported" runtime error.
diff --git a/examples/pinv.hs b/examples/pinv.hs
new file mode 100644
index 0000000..cab8fc6
--- /dev/null
+++ b/examples/pinv.hs
@@ -0,0 +1,20 @@
1import Numeric.LinearAlgebra
2import Graphics.Plot
3import Text.Printf(printf)
4
5expand :: Int -> Vector Double -> Matrix Double
6expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n]
7
8polynomialModel :: Vector Double -> Vector Double -> Int
9 -> (Vector Double -> Vector Double)
10polynomialModel x y n = f where
11 f z = expand n z <> ws
12 ws = expand n x <\> y
13
14main = do
15 [x,y] <- (toColumns . readMatrix) `fmap` readFile "data.txt"
16 let pol = polynomialModel x y
17 let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
18 putStrLn $ " x y p 1 p 2 p 3"
19 putStrLn $ format " " (printf "%.2f") $ fromColumns view
20 mplot view
diff --git a/examples/pinv1.hs b/examples/pinv1.hs
deleted file mode 100644
index 301eac8..0000000
--- a/examples/pinv1.hs
+++ /dev/null
@@ -1,18 +0,0 @@
1-- initial check for the polynomial model example
2import Numeric.LinearAlgebra
3
4
5prepSyst :: Int -> Matrix Double -> (Matrix Double, Vector Double)
6prepSyst n d = (a,b) where
7 [x,b] = toColumns d
8 a = fromColumns $ 1+0*x : map (x^) [1 .. n]
9
10main = do
11 dat <- readMatrix `fmap` readFile "data.txt"
12 let (a,b) = prepSyst 3 dat
13 putStr "Coefficient matrix:\n"
14 dispR 3 a
15 putStr "Desired values:\n"
16 print b
17 putStr "\nLeast Squares Solution:\n"
18 print $ pinv a <> b -- equivalent to: linearSolveLSR a (fromColumns [b])
diff --git a/examples/pinv2.hs b/examples/pinv2.hs
deleted file mode 100644
index 7423540..0000000
--- a/examples/pinv2.hs
+++ /dev/null
@@ -1,26 +0,0 @@
1-- MSE polynomial model using the pseudoinverse
2import Numeric.LinearAlgebra
3import Graphics.Plot
4
5expand :: Int -> Vector Double -> Matrix Double
6expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n]
7
8polynomialModel :: Matrix Double -> Int -> (Vector Double -> Vector Double)
9polynomialModel d n = f where
10 f z = expand n z <> ws
11 ws = pinv a <> b
12 [x,b] = toColumns d
13 a = expand n x
14
15mk fv = f where
16 f x = fv (fromList [x]) @> 0
17
18main = do
19 d <- readMatrix `fmap` readFile "data.txt"
20 let [x,y] = toColumns d
21 let pol = polynomialModel d
22 let view = [x, y, pol 1 x, pol 2 x, pol 3 x]
23 dispR 2 $ fromColumns view
24 mplot view
25 let f = mk (pol 2)
26 print (f 2.5)