module Numeric.LevMar
(
Model
, Jacobian
, LevMarable(levmar)
, LinearConstraints
, Options(..)
, defaultOpts
, Constraints(..)
, noConstraints
, Info(..)
, StopReason(..)
, CovarMatrix
, LevMarError(..)
) where
import Control.Monad.Instances
import Control.Exception ( Exception )
import Data.Typeable ( Typeable )
import Data.Bool ( otherwise )
import Data.Either ( Either(Left, Right) )
import Data.Function ( ($) )
import Data.List ( lookup, map, concat, concatMap, length )
import Data.Maybe ( Maybe(Nothing, Just)
, isJust, fromJust, fromMaybe
)
import Data.Ord ( (<) )
import Foreign.Marshal.Array ( allocaArray, peekArray, pokeArray, withArray )
import Foreign.Ptr ( Ptr, nullPtr, plusPtr )
import Foreign.Storable ( Storable )
import Foreign.C.Types ( CInt )
import Prelude ( Enum, Fractional, Real, RealFrac
, Integer, Float, Double
, fromIntegral, realToFrac, toEnum
, (), error, floor
)
import System.IO ( IO )
import System.IO.Unsafe ( unsafePerformIO )
import Text.Read ( Read )
import Text.Show ( Show )
#if __GLASGOW_HASKELL__ < 700
import Prelude ( fromInteger )
#endif
import Data.Bool.Unicode ( (∧), (∨) )
import Data.Eq.Unicode ( (≡), (≢) )
import Data.Function.Unicode ( (∘) )
import Prelude.Unicode ( (⋅) )
import Bindings.LevMar ( c'LM_INFO_SZ
, withModel
, withJacobian
, c'LM_ERROR
, c'LM_ERROR_LAPACK_ERROR
, c'LM_ERROR_FAILED_BOX_CHECK
, c'LM_ERROR_MEMORY_ALLOCATION_FAILURE
, c'LM_ERROR_CONSTRAINT_MATRIX_ROWS_GT_COLS
, c'LM_ERROR_CONSTRAINT_MATRIX_NOT_FULL_ROW_RANK
, c'LM_ERROR_TOO_FEW_MEASUREMENTS
, c'LM_ERROR_SINGULAR_MATRIX
, c'LM_ERROR_SUM_OF_SQUARES_NOT_FINITE
, c'LM_INIT_MU
, c'LM_STOP_THRESH
, c'LM_DIFF_DELTA
)
import qualified Bindings.LevMar ( Model, Jacobian )
import Bindings.LevMar.CurryFriendly ( LevMarDer
, LevMarDif
, LevMarBCDer
, LevMarBCDif
, LevMarLecDer
, LevMarLecDif
, LevMarBLecDer
, LevMarBLecDif
, dlevmar_der, slevmar_der
, dlevmar_dif, slevmar_dif
, dlevmar_bc_der, slevmar_bc_der
, dlevmar_bc_dif, slevmar_bc_dif
, dlevmar_lec_der, slevmar_lec_der
, dlevmar_lec_dif, slevmar_lec_dif
, dlevmar_blec_der, slevmar_blec_der
, dlevmar_blec_dif, slevmar_blec_dif
)
type Model r = [r] → [r]
type Jacobian r = [r] → [[r]]
class LevMarable r where
levmar ∷ Model r
→ Maybe (Jacobian r)
→ [r]
→ [r]
→ Integer
→ Options r
→ Constraints r
→ Either LevMarError ([r], Info r, CovarMatrix r)
instance LevMarable Float where
levmar = gen_levmar slevmar_der
slevmar_dif
slevmar_bc_der
slevmar_bc_dif
slevmar_lec_der
slevmar_lec_dif
slevmar_blec_der
slevmar_blec_dif
instance LevMarable Double where
levmar = gen_levmar dlevmar_der
dlevmar_dif
dlevmar_bc_der
dlevmar_bc_dif
dlevmar_lec_der
dlevmar_lec_dif
dlevmar_blec_der
dlevmar_blec_dif
gen_levmar ∷ ∀ cr r. (Storable cr, RealFrac cr, Real r, Fractional r)
⇒ LevMarDer cr
→ LevMarDif cr
→ LevMarBCDer cr
→ LevMarBCDif cr
→ LevMarLecDer cr
→ LevMarLecDif cr
→ LevMarBLecDer cr
→ LevMarBLecDif cr
→ Model r
→ Maybe (Jacobian r)
→ [r]
→ [r]
→ Integer
→ Options r
→ Constraints r
→ Either LevMarError ([r], Info r, CovarMatrix r)
gen_levmar f_der
f_dif
f_bc_der
f_bc_dif
f_lec_der
f_lec_dif
f_blec_der
f_blec_dif
model mJac ps ys itMax opts (Constraints mLowBs mUpBs mWeights mLinC)
= unsafePerformIO ∘
withArray (map realToFrac ps) $ \psPtr →
withArray (map realToFrac ys) $ \ysPtr →
withArray (map realToFrac $ optsToList opts) $ \optsPtr →
allocaArray c'LM_INFO_SZ $ \infoPtr →
allocaArray covarLen $ \covarPtr →
withModel (convertModel model) $ \modelPtr → do
let runDif ∷ LevMarDif cr → IO CInt
runDif f = f modelPtr
psPtr
ysPtr
(fromIntegral lenPs)
(fromIntegral lenYs)
(fromIntegral itMax)
optsPtr
infoPtr
nullPtr
covarPtr
nullPtr
r ← case mJac of
Just jac → withJacobian (convertJacobian jac) $ \jacobPtr →
let runDer ∷ LevMarDer cr → IO CInt
runDer f = runDif $ f jacobPtr
in if boxConstrained
then if linConstrained
then withBoxConstraints
(withLinConstraints $ withWeights runDer)
f_blec_der
else withBoxConstraints runDer f_bc_der
else if linConstrained
then withLinConstraints runDer f_lec_der
else runDer f_der
Nothing → if boxConstrained
then if linConstrained
then withBoxConstraints
(withLinConstraints $ withWeights runDif)
f_blec_dif
else withBoxConstraints runDif f_bc_dif
else if linConstrained
then withLinConstraints runDif f_lec_dif
else runDif f_dif
if r < 0
∧ r ≢ c'LM_ERROR_SINGULAR_MATRIX
∧ r ≢ c'LM_ERROR_SUM_OF_SQUARES_NOT_FINITE
then return $ Left $ convertLevMarError r
else
do result ← peekArray lenPs psPtr
info ← peekArray c'LM_INFO_SZ infoPtr
let convertCovarMatrix ptr
| ptr ≡ covarPtr `plusPtr` covarLen = return []
| otherwise = do row ← peekArray lenPs ptr
rows ← convertCovarMatrix $ ptr `plusPtr` lenPs
return $ map realToFrac row : rows
covar ← convertCovarMatrix covarPtr
return $ Right ( map realToFrac result
, listToInfo info
, covar
)
where
lenPs = length ps
lenYs = length ys
covarLen = lenPs⋅lenPs
(cMat, rhcVec) = fromJust mLinC
linConstrained = isJust mLinC
boxConstrained = isJust mLowBs ∨ isJust mUpBs
withBoxConstraints f g =
maybeWithArray mLowBs $ \lBsPtr →
maybeWithArray mUpBs $ \uBsPtr →
f $ g lBsPtr uBsPtr
withLinConstraints f g =
withArray (map realToFrac $ concat cMat) $ \cMatPtr →
withArray (map realToFrac rhcVec) $ \rhcVecPtr →
f ∘ g cMatPtr rhcVecPtr ∘ fromIntegral $ length cMat
withWeights f g = maybeWithArray mWeights $ f ∘ g
convertModel ∷ (Real r, Fractional r, Storable c, Real c, Fractional c)
⇒ Model r → Bindings.LevMar.Model c
convertModel model =
\parPtr hxPtr numPar _ _ →
peekArray (fromIntegral numPar) parPtr >>=
pokeArray hxPtr ∘ map realToFrac ∘ model ∘ map realToFrac
convertJacobian ∷ (Real r, Fractional r, Storable c, Real c, Fractional c)
⇒ Jacobian r → Bindings.LevMar.Jacobian c
convertJacobian jac =
\parPtr jPtr numPar _ _ →
peekArray (fromIntegral numPar) parPtr >>=
pokeArray jPtr ∘ concatMap (map realToFrac) ∘ jac ∘ map realToFrac
type LinearConstraints r = ([[r]], [r])
data Options r =
Opts { optScaleInitMu ∷ r
, optStopNormInfJacTe ∷ r
, optStopNorm2Dp ∷ r
, optStopNorm2E ∷ r
, optDelta ∷ r
} deriving (Read, Show)
defaultOpts ∷ Fractional r ⇒ Options r
defaultOpts = Opts { optScaleInitMu = c'LM_INIT_MU
, optStopNormInfJacTe = c'LM_STOP_THRESH
, optStopNorm2Dp = c'LM_STOP_THRESH
, optStopNorm2E = c'LM_STOP_THRESH
, optDelta = c'LM_DIFF_DELTA
}
optsToList ∷ Options r → [r]
optsToList (Opts mu eps1 eps2 eps3 delta) =
[mu, eps1, eps2, eps3, delta]
data Constraints r = Constraints
{ lowerBounds ∷ Maybe [r]
, upperBounds ∷ Maybe [r]
, weights ∷ Maybe [r]
, linearConstraints ∷ Maybe (LinearConstraints r)
}
noConstraints ∷ Constraints r
noConstraints = Constraints Nothing Nothing Nothing Nothing
maybeWithArray ∷ (Real α, Fractional r, Storable r)
⇒ Maybe [α] → (Ptr r → IO β) → IO β
maybeWithArray Nothing f = f nullPtr
maybeWithArray (Just xs) f = withArray (map realToFrac xs) f
data Info r = Info
{ infNorm2initE ∷ r
, infNorm2E ∷ r
, infNormInfJacTe ∷ r
, infNorm2Dp ∷ r
, infMuDivMax ∷ r
, infNumIter ∷ Integer
, infStopReason ∷ StopReason
, infNumFuncEvals ∷ Integer
, infNumJacobEvals ∷ Integer
, infNumLinSysSolved ∷ Integer
} deriving (Read, Show)
listToInfo ∷ (RealFrac cr, Fractional r) ⇒ [cr] → Info r
listToInfo [a,b,c,d,e,f,g,h,i,j] =
Info { infNorm2initE = realToFrac a
, infNorm2E = realToFrac b
, infNormInfJacTe = realToFrac c
, infNorm2Dp = realToFrac d
, infMuDivMax = realToFrac e
, infNumIter = floor f
, infStopReason = toEnum $ floor g 1
, infNumFuncEvals = floor h
, infNumJacobEvals = floor i
, infNumLinSysSolved = floor j
}
listToInfo _ = error "liftToInfo: wrong list length"
data StopReason
= SmallGradient
| SmallDp
| MaxIterations
| SingularMatrix
| SmallestError
| SmallNorm2E
| InvalidValues
deriving (Read, Show, Enum)
type CovarMatrix r = [[r]]
data LevMarError
= LevMarError
| LapackError
| FailedBoxCheck
| MemoryAllocationFailure
| ConstraintMatrixRowsGtCols
| ConstraintMatrixNotFullRowRank
| TooFewMeasurements
deriving (Show, Typeable)
instance Exception LevMarError
levmarCErrorToLevMarError ∷ [(CInt, LevMarError)]
levmarCErrorToLevMarError =
[ (c'LM_ERROR, LevMarError)
, (c'LM_ERROR_LAPACK_ERROR, LapackError)
, (c'LM_ERROR_FAILED_BOX_CHECK, FailedBoxCheck)
, (c'LM_ERROR_MEMORY_ALLOCATION_FAILURE, MemoryAllocationFailure)
, (c'LM_ERROR_CONSTRAINT_MATRIX_ROWS_GT_COLS, ConstraintMatrixRowsGtCols)
, (c'LM_ERROR_CONSTRAINT_MATRIX_NOT_FULL_ROW_RANK, ConstraintMatrixNotFullRowRank)
, (c'LM_ERROR_TOO_FEW_MEASUREMENTS, TooFewMeasurements)
]
convertLevMarError ∷ CInt → LevMarError
convertLevMarError err = fromMaybe (error "Unknown levmar error") $
lookup err levmarCErrorToLevMarError