45
45
-----------------------------------------------------------------------------
47
47
module Numeric.GSL.Root (
48
uniRoot, UniRootMethod(..),
49
uniRootJ, UniRootMethodJ(..),
48
50
root, RootMethod(..),
49
51
rootJ, RootMethodJ(..),
59
61
-------------------------------------------------------------------------
63
data UniRootMethod = Bisection
66
deriving (Enum, Eq, Show, Bounded)
68
uniRoot :: UniRootMethod
74
-> (Double, Matrix Double)
75
uniRoot method epsrel maxit fun xl xu = uniRootGen (fi (fromEnum method)) fun xl xu epsrel maxit
77
uniRootGen m f xl xu epsrel maxit = unsafePerformIO $ do
79
rawpath <- createMIO maxit 4
80
(c_root m fp epsrel (fi maxit) xl xu)
82
let it = round (rawpath @@> (maxit-1,0))
83
path = takeRows it rawpath
84
[sol] = toLists $ dropRows (it-1) path
86
return (sol !! 1, path)
88
foreign import ccall safe "root"
89
c_root:: CInt -> FunPtr (Double -> Double) -> Double -> CInt -> Double -> Double -> TM
91
-------------------------------------------------------------------------
92
data UniRootMethodJ = UNewton
95
deriving (Enum, Eq, Show, Bounded)
97
uniRootJ :: UniRootMethodJ
100
-> (Double -> Double)
101
-> (Double -> Double)
103
-> (Double, Matrix Double)
104
uniRootJ method epsrel maxit fun dfun x = uniRootJGen (fi (fromEnum method)) fun
107
uniRootJGen m f df x epsrel maxit = unsafePerformIO $ do
109
dfp <- mkDoublefun df
110
rawpath <- createMIO maxit 2
111
(c_rootj m fp dfp epsrel (fi maxit) x)
113
let it = round (rawpath @@> (maxit-1,0))
114
path = takeRows it rawpath
115
[sol] = toLists $ dropRows (it-1) path
117
return (sol !! 1, path)
119
foreign import ccall safe "rootj"
120
c_rootj :: CInt -> FunPtr (Double -> Double) -> FunPtr (Double -> Double)
121
-> Double -> CInt -> Double -> TM
123
-------------------------------------------------------------------------
61
125
data RootMethod = Hybrids
82
146
fp <- mkVecVecfun (aux_vTov (checkdim1 n . fromList . f . toList))
83
147
rawpath <- vec xiv $ \xiv' ->
84
148
createMIO maxit (2*n+1)
85
(c_root m fp epsabs (fi maxit) // xiv')
149
(c_multiroot m fp epsabs (fi maxit) // xiv')
87
151
let it = round (rawpath @@> (maxit-1,0))
88
152
path = takeRows it rawpath
89
153
[sol] = toLists $ dropRows (it-1) path
91
155
return (take n $ drop 1 sol, path)
94
foreign import ccall safe "root"
95
c_root:: CInt -> FunPtr TVV -> Double -> CInt -> TVM
158
foreign import ccall safe "multiroot"
159
c_multiroot:: CInt -> FunPtr TVV -> Double -> CInt -> TVM
97
161
-------------------------------------------------------------------------
120
184
jp <- mkVecMatfun (aux_vTom (checkdim2 n . fromLists . jac . toList))
121
185
rawpath <- vec xiv $ \xiv' ->
122
186
createMIO maxit (2*n+1)
123
(c_rootj m fp jp epsabs (fi maxit) // xiv')
187
(c_multirootj m fp jp epsabs (fi maxit) // xiv')
125
189
let it = round (rawpath @@> (maxit-1,0))
126
190
path = takeRows it rawpath
127
191
[sol] = toLists $ dropRows (it-1) path
129
193
freeHaskellFunPtr jp
130
194
return (take n $ drop 1 sol, path)
133
foreign import ccall safe "rootj"
134
c_rootj:: CInt -> FunPtr TVV -> FunPtr TVM -> Double -> CInt -> TVM
196
foreign import ccall safe "multirootj"
197
c_multirootj:: CInt -> FunPtr TVV -> FunPtr TVM -> Double -> CInt -> TVM
136
199
-------------------------------------------------------