summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL/Minimization.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-06-08 09:45:14 +0000
committerAlberto Ruiz <aruiz@um.es>2009-06-08 09:45:14 +0000
commitd9efdd9334da1a63f739d6e2e68c4ff78f52e505 (patch)
tree4c4c4c798fd1e67ec4565a441e1357d5b75f37da /lib/Numeric/GSL/Minimization.hs
parent34de6154086224a0e9f774bd8a2ab804d78e8a10 (diff)
auxiliary functions moved to Numeric.GSL.Internal
Diffstat (limited to 'lib/Numeric/GSL/Minimization.hs')
-rw-r--r--lib/Numeric/GSL/Minimization.hs61
1 files changed, 16 insertions, 45 deletions
diff --git a/lib/Numeric/GSL/Minimization.hs b/lib/Numeric/GSL/Minimization.hs
index 048c717..930fe8a 100644
--- a/lib/Numeric/GSL/Minimization.hs
+++ b/lib/Numeric/GSL/Minimization.hs
@@ -63,6 +63,7 @@ import Data.Packed.Internal
63import Data.Packed.Matrix 63import Data.Packed.Matrix
64import Foreign 64import Foreign
65import Foreign.C.Types(CInt) 65import Foreign.C.Types(CInt)
66import Numeric.GSL.Internal
66 67
67------------------------------------------------------------------------ 68------------------------------------------------------------------------
68 69
@@ -79,7 +80,7 @@ minimizeVectorBFGS2 step tol eps maxit f g xi = minimizeD VectorBFGS2 eps maxit
79 80
80data MinimizeMethod = NMSimplex 81data MinimizeMethod = NMSimplex
81 | NMSimplex2 82 | NMSimplex2
82 deriving (Enum,Eq,Show) 83 deriving (Enum,Eq,Show,Bounded)
83 84
84-- | Minimization without derivatives. 85-- | Minimization without derivatives.
85minimize :: MinimizeMethod 86minimize :: MinimizeMethod
@@ -97,7 +98,7 @@ data MinimizeMethodD = ConjugateFR
97 | VectorBFGS 98 | VectorBFGS
98 | VectorBFGS2 99 | VectorBFGS2
99 | SteepestDescent 100 | SteepestDescent
100 deriving (Enum,Eq,Show) 101 deriving (Enum,Eq,Show,Bounded)
101 102
102-- | Minimization with derivatives. 103-- | Minimization with derivatives.
103minimizeD :: MinimizeMethodD 104minimizeD :: MinimizeMethodD
@@ -143,13 +144,13 @@ minimizeDGen method eps maxit istep tol f df xi = unsafePerformIO $ do
143 let xiv = fromList xi 144 let xiv = fromList xi
144 n = dim xiv 145 n = dim xiv
145 f' = f . toList 146 f' = f . toList
146 df' = (fromList . df . toList) 147 df' = (checkdim1 n .fromList . df . toList)
147 fp <- mkVecfun (iv f') 148 fp <- mkVecfun (iv f')
148 dfp <- mkVecVecfun (aux_vTov df') 149 dfp <- mkVecVecfun (aux_vTov df')
149 rawpath <- withVector xiv $ \xiv' -> 150 rawpath <- withVector xiv $ \xiv' ->
150 createMIO maxit (n+2) 151 createMIO maxit (n+2)
151 (c_minimizeWithDeriv method fp dfp istep tol eps (fi maxit) // xiv') 152 (c_minimizeD method fp dfp istep tol eps (fi maxit) // xiv')
152 "minimizeDerivV" 153 "minimizeD"
153 let it = round (rawpath @@> (maxit-1,0)) 154 let it = round (rawpath @@> (maxit-1,0))
154 path = takeRows it rawpath 155 path = takeRows it rawpath
155 sol = toList $ cdat $ dropColumns 2 $ dropRows (it-1) path 156 sol = toList $ cdat $ dropColumns 2 $ dropRows (it-1) path
@@ -158,45 +159,15 @@ minimizeDGen method eps maxit istep tol f df xi = unsafePerformIO $ do
158 return (sol,path) 159 return (sol,path)
159 160
160foreign import ccall "gsl-aux.h minimizeD" 161foreign import ccall "gsl-aux.h minimizeD"
161 c_minimizeWithDeriv :: CInt -> FunPtr (CInt -> Ptr Double -> Double) 162 c_minimizeD :: CInt
162 -> FunPtr (CInt -> Ptr Double -> Ptr Double -> IO ()) 163 -> FunPtr (CInt -> Ptr Double -> Double)
163 -> Double -> Double -> Double -> CInt 164 -> FunPtr TVV
164 -> TVM 165 -> Double -> Double -> Double -> CInt
166 -> TVM
165 167
166--------------------------------------------------------------------- 168---------------------------------------------------------------------
167iv :: (Vector Double -> Double) -> (CInt -> Ptr Double -> Double) 169
168iv f n p = f (createV (fromIntegral n) copy "iv") where 170checkdim1 n v
169 copy n' q = do 171 | dim v == n = v
170 copyArray q p (fromIntegral n') 172 | otherwise = error $ "Error: "++ show n
171 return 0 173 ++ " components expected in the result of the gradient supplied to minimizeD"
172
173-- | conversion of Haskell functions into function pointers that can be used in the C side
174foreign import ccall "wrapper"
175 mkVecfun :: (CInt -> Ptr Double -> Double)
176 -> IO( FunPtr (CInt -> Ptr Double -> Double))
177
178-- | another required conversion
179foreign import ccall "wrapper"
180 mkVecVecfun :: (CInt -> Ptr Double -> Ptr Double -> IO ())
181 -> IO (FunPtr (CInt -> Ptr Double -> Ptr Double->IO()))
182
183aux_vTov :: (Vector Double -> Vector Double) -> (CInt -> Ptr Double -> Ptr Double -> IO())
184aux_vTov f n p r = g where
185 V {fptr = pr} = f x
186 x = createV (fromIntegral n) copy "aux_vTov"
187 copy n' q = do
188 copyArray q p (fromIntegral n')
189 return 0
190 g = withForeignPtr pr $ \p' -> copyArray r p' (fromIntegral n)
191
192--------------------------------------------------------------------
193
194createV n fun msg = unsafePerformIO $ do
195 r <- createVector n
196 app1 fun vec r msg
197 return r
198
199createMIO r c fun msg = do
200 res <- createMatrix RowMajor r c
201 app1 fun mat res msg
202 return res