34 lines
1.1 KiB
Haskell
34 lines
1.1 KiB
Haskell
{-# LANGUAGE NoMonomorphismRestriction #-}
|
|
module Petzval.Optimization where
|
|
|
|
import Control.Lens
|
|
import Control.Lens.Unsound (adjoin)
|
|
import Numeric.AD.Mode
|
|
import Numeric.AD.Mode.Reverse.Double
|
|
import Petzval.Optics
|
|
import Petzval.Types
|
|
|
|
-- | A set of modifiable parts of a lens system, expressed as a traversal.
|
|
-- The recommended way of generating a variable set is with the following construction:
|
|
-- @
|
|
-- vars = ix 1 . roc `adjoin` ix 2 . thickness
|
|
-- @
|
|
type VariableSet = forall mat a. Traversal' [Element mat a] a
|
|
type AdMode s = ReverseDouble s
|
|
|
|
extractVars :: VariableSet -> [Element mat a] -> [a]
|
|
extractVars vars system = system ^. partsOf vars
|
|
|
|
setVars :: VariableSet -> [Element mat a] -> [a] -> [Element mat a]
|
|
setVars vars system vals = system & partsOf vars .~ vals
|
|
|
|
gradientAt :: VariableSet -- ^ The set of independent variables
|
|
-> (forall a. Calcuable a => [Element mat a] -> a) -- ^ merit function
|
|
-> [Element mat Double] -- ^ The system
|
|
-> (Double, [Double]) -- ^ The gradient
|
|
gradientAt vars merit system = grad' (merit . setVars vars (system & each.liftFp %~ auto)) (extractVars vars system)
|
|
|
|
|
|
|
|
-- instances
|