diff options
-rw-r--r-- | README | 3 | ||||
-rw-r--r-- | examples/pinv.hs | 20 | ||||
-rw-r--r-- | examples/pinv1.hs | 18 | ||||
-rw-r--r-- | examples/pinv2.hs | 26 |
4 files changed, 22 insertions, 45 deletions
@@ -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 | ||
109 | Unfortunately the lapack dll supplied by the R system does not include zgels_ and zgelss_, | 109 | Unfortunately the lapack dll supplied by the R system does not include zgels_ and zgelss_, |
110 | so the functions depending on them will produced a "non supported" runtime error. | 110 | so the functions depending on them (linearSolveLS and linearSolveSVD for complex data) |
111 | will 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 @@ | |||
1 | import Numeric.LinearAlgebra | ||
2 | import Graphics.Plot | ||
3 | import Text.Printf(printf) | ||
4 | |||
5 | expand :: Int -> Vector Double -> Matrix Double | ||
6 | expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n] | ||
7 | |||
8 | polynomialModel :: Vector Double -> Vector Double -> Int | ||
9 | -> (Vector Double -> Vector Double) | ||
10 | polynomialModel x y n = f where | ||
11 | f z = expand n z <> ws | ||
12 | ws = expand n x <\> y | ||
13 | |||
14 | main = 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 | ||
2 | import Numeric.LinearAlgebra | ||
3 | |||
4 | |||
5 | prepSyst :: Int -> Matrix Double -> (Matrix Double, Vector Double) | ||
6 | prepSyst n d = (a,b) where | ||
7 | [x,b] = toColumns d | ||
8 | a = fromColumns $ 1+0*x : map (x^) [1 .. n] | ||
9 | |||
10 | main = 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 | ||
2 | import Numeric.LinearAlgebra | ||
3 | import Graphics.Plot | ||
4 | |||
5 | expand :: Int -> Vector Double -> Matrix Double | ||
6 | expand n x = fromColumns $ constant 1 (dim x): map (x^) [1 .. n] | ||
7 | |||
8 | polynomialModel :: Matrix Double -> Int -> (Vector Double -> Vector Double) | ||
9 | polynomialModel 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 | |||
15 | mk fv = f where | ||
16 | f x = fv (fromList [x]) @> 0 | ||
17 | |||
18 | main = 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) | ||