diff options
author | Alberto Ruiz <aruiz@um.es> | 2009-06-05 18:53:07 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2009-06-05 18:53:07 +0000 |
commit | 49a3d719221cd9484a64688ffcdbeb13cb8e55a0 (patch) | |
tree | fbe5161936acb1f49d1337c2da1f7f93139c089a | |
parent | bbc54bf2573ea3631ee436507807dae6c4353bcc (diff) |
check dim in root function
-rw-r--r-- | INSTALL | 2 | ||||
-rw-r--r-- | configure.hs | 4 | ||||
-rw-r--r-- | examples/root.hs | 14 | ||||
-rw-r--r-- | lib/Numeric/GSL/Root.hs | 8 |
4 files changed, 12 insertions, 16 deletions
@@ -41,7 +41,7 @@ INSTALLATION ON WINDOWS ---------------------------------------- | |||
41 | change to: build-type: Simple | 41 | change to: build-type: Simple |
42 | 42 | ||
43 | line 160: extra-libraries: | 43 | line 160: extra-libraries: |
44 | add: extra-libraries: libgls-0 blas lapack | 44 | add: extra-libraries: libgsl-0 blas lapack |
45 | 45 | ||
46 | line 161: extra-lib-dirs: | 46 | line 161: extra-lib-dirs: |
47 | add: extra-lib-dirs: c:\ghc\ghc-6.10.3\bin | 47 | add: extra-lib-dirs: c:\ghc\ghc-6.10.3\bin |
diff --git a/configure.hs b/configure.hs index 2764dbc..7aa296e 100644 --- a/configure.hs +++ b/configure.hs | |||
@@ -36,7 +36,7 @@ opts = [ "" -- Ubuntu/Debian | |||
36 | ] | 36 | ] |
37 | 37 | ||
38 | -- compile a simple program with symbols from GSL and LAPACK with the given libs | 38 | -- compile a simple program with symbols from GSL and LAPACK with the given libs |
39 | testprog libs fmks = "echo \"#include <gsl/gsl_math.h>\nint main(){zgesvd_(); gsl_sf_gamma();}\"" | 39 | testprog libs fmks = "echo \"#include <gsl/gsl_sf_gamma.h>\nint main(){zgesvd_(); gsl_sf_gamma(5);}\"" |
40 | ++" > /tmp/dummy.c; gcc /tmp/dummy.c -o /tmp/dummy " | 40 | ++" > /tmp/dummy.c; gcc /tmp/dummy.c -o /tmp/dummy " |
41 | ++ f1 libs ++ " " ++ f2 fmks ++ " > /dev/null 2> /dev/null" | 41 | ++ f1 libs ++ " " ++ f2 fmks ++ " > /dev/null 2> /dev/null" |
42 | 42 | ||
@@ -46,7 +46,7 @@ f2 = unwords . map ("-framework "++) . words | |||
46 | check libs fmks = (ExitSuccess ==) `fmap` system (testprog libs fmks) | 46 | check libs fmks = (ExitSuccess ==) `fmap` system (testprog libs fmks) |
47 | 47 | ||
48 | -- simple test for GSL | 48 | -- simple test for GSL |
49 | testGSL = "echo \"#include <gsl/gsl_math.h>\nint main(){gsl_sf_gamma();}\"" | 49 | testGSL = "echo \"#include <gsl/gsl_sf_gamma.h>\nint main(){gsl_sf_gamma(5);}\"" |
50 | ++" > /tmp/dummy.c; gcc /tmp/dummy.c -o /tmp/dummy -lgsl -lgslcblas" | 50 | ++" > /tmp/dummy.c; gcc /tmp/dummy.c -o /tmp/dummy -lgsl -lgslcblas" |
51 | ++ " > /dev/null 2> /dev/null" | 51 | ++ " > /dev/null 2> /dev/null" |
52 | 52 | ||
diff --git a/examples/root.hs b/examples/root.hs index 9a674fd..69db243 100644 --- a/examples/root.hs +++ b/examples/root.hs | |||
@@ -1,27 +1,17 @@ | |||
1 | -- root finding examples | 1 | -- root finding examples |
2 | import Numeric.GSL | 2 | import Numeric.GSL |
3 | import Numeric.LinearAlgebra | 3 | import Numeric.LinearAlgebra |
4 | import Graphics.Plot | ||
5 | import Text.Printf(printf) | 4 | import Text.Printf(printf) |
6 | 5 | ||
7 | rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ] | 6 | rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ] |
8 | 7 | ||
9 | disp = putStrLn . format " " (printf "%.3f") | ||
10 | |||
11 | -- Numerical estimation of the gradient | ||
12 | gradient f v = [partialDerivative k f v | k <- [0 .. length v -1]] | ||
13 | |||
14 | partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where | ||
15 | g x = f (concat [a,x:b]) | ||
16 | (a,_:b) = splitAt n v | ||
17 | |||
18 | test method = do | 8 | test method = do |
19 | print method | 9 | print method |
20 | let (s,p) = root method 1E-7 30 (rosenbrock 1 10) [-10,-5] | 10 | let (s,p) = root method 1E-7 30 (rosenbrock 1 10) [-10,-5] |
21 | print s -- solution | 11 | print s -- solution |
22 | disp p -- evolution of the algorithm | 12 | disp p -- evolution of the algorithm |
23 | -- let [x,y] = tail (toColumns p) | 13 | |
24 | -- mplot [x,y] -- path from the starting point to the solution | 14 | disp = putStrLn . format " " (printf "%.3f") |
25 | 15 | ||
26 | main = do | 16 | main = do |
27 | test Hybrids | 17 | test Hybrids |
diff --git a/lib/Numeric/GSL/Root.hs b/lib/Numeric/GSL/Root.hs index ad1b72c..d674fad 100644 --- a/lib/Numeric/GSL/Root.hs +++ b/lib/Numeric/GSL/Root.hs | |||
@@ -76,7 +76,7 @@ root method epsabs maxit fun xinit = rootGen (fi (fromEnum method)) fun xinit ep | |||
76 | rootGen m f xi epsabs maxit = unsafePerformIO $ do | 76 | rootGen m f xi epsabs maxit = unsafePerformIO $ do |
77 | let xiv = fromList xi | 77 | let xiv = fromList xi |
78 | n = dim xiv | 78 | n = dim xiv |
79 | fp <- mkVecVecfun (aux_vTov (fromList.f.toList)) | 79 | fp <- mkVecVecfun (aux_vTov (fromList . checkdim n f . toList)) |
80 | rawpath <- withVector xiv $ \xiv' -> | 80 | rawpath <- withVector xiv $ \xiv' -> |
81 | createMIO maxit (2*n+1) | 81 | createMIO maxit (2*n+1) |
82 | (c_root m fp epsabs (fi maxit) // xiv') | 82 | (c_root m fp epsabs (fi maxit) // xiv') |
@@ -115,3 +115,9 @@ createMIO r c fun msg = do | |||
115 | res <- createMatrix RowMajor r c | 115 | res <- createMatrix RowMajor r c |
116 | app1 fun mat res msg | 116 | app1 fun mat res msg |
117 | return res | 117 | return res |
118 | |||
119 | checkdim n f x | ||
120 | | length y /= n = error $ "Error: "++ show n | ||
121 | ++ " results expected in the function supplied to root" | ||
122 | | otherwise = y | ||
123 | where y = f x | ||