Copyright | (c) 2011 Bryan O'Sullivan 2018 Alexey Khudyakov |
---|---|
License | BSD3 |
Maintainer | bos@serpentine.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Numeric.RootFinding
Description
Haskell functions for finding the roots of real functions of real arguments. These algorithms are iterative so we provide both function returning root (or failure to find root) and list of iterations.
Synopsis
- data Root a
- = NotBracketed
- | SearchFailed
- | Root !a
- fromRoot :: a -> Root a -> a
- data Tolerance
- withinTolerance :: Tolerance -> Double -> Double -> Bool
- class IterationStep a where
- findRoot :: IterationStep a => Int -> Tolerance -> [a] -> Root Double
- data RiddersParam = RiddersParam {
- riddersMaxIter :: !Int
- riddersTol :: !Tolerance
- ridders :: RiddersParam -> (Double, Double) -> (Double -> Double) -> Root Double
- riddersIterations :: (Double, Double) -> (Double -> Double) -> [RiddersStep]
- data RiddersStep
- data NewtonParam = NewtonParam {
- newtonMaxIter :: !Int
- newtonTol :: !Tolerance
- newtonRaphson :: NewtonParam -> (Double, Double, Double) -> (Double -> (Double, Double)) -> Root Double
- newtonRaphsonIterations :: (Double, Double, Double) -> (Double -> (Double, Double)) -> [NewtonStep]
- data NewtonStep
Data types
The result of searching for a root of a mathematical function.
Constructors
NotBracketed | The function does not have opposite signs when evaluated at the lower and upper bounds of the search. |
SearchFailed | The search failed to converge to within the given error tolerance after the given number of iterations. |
Root !a | A root was successfully found. |
Instances
Foldable Root # | |||||
Defined in Numeric.RootFinding Methods fold :: Monoid m => Root m -> m # foldMap :: Monoid m => (a -> m) -> Root a -> m # foldMap' :: Monoid m => (a -> m) -> Root a -> m # foldr :: (a -> b -> b) -> b -> Root a -> b # foldr' :: (a -> b -> b) -> b -> Root a -> b # foldl :: (b -> a -> b) -> b -> Root a -> b # foldl' :: (b -> a -> b) -> b -> Root a -> b # foldr1 :: (a -> a -> a) -> Root a -> a # foldl1 :: (a -> a -> a) -> Root a -> a # elem :: Eq a => a -> Root a -> Bool # maximum :: Ord a => Root a -> a # | |||||
Traversable Root # | |||||
Alternative Root # | |||||
Applicative Root # | |||||
Functor Root # | |||||
Monad Root # | |||||
MonadPlus Root # | |||||
Data a => Data (Root a) # | |||||
Defined in Numeric.RootFinding Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Root a -> c (Root a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Root a) # toConstr :: Root a -> Constr # dataTypeOf :: Root a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Root a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Root a)) # gmapT :: (forall b. Data b => b -> b) -> Root a -> Root a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Root a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Root a -> r # gmapQ :: (forall d. Data d => d -> u) -> Root a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Root a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Root a -> m (Root a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Root a -> m (Root a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Root a -> m (Root a) # | |||||
Generic (Root a) # | |||||
Defined in Numeric.RootFinding Associated Types
| |||||
Read a => Read (Root a) # | |||||
Show a => Show (Root a) # | |||||
NFData a => NFData (Root a) # | |||||
Defined in Numeric.RootFinding | |||||
Eq a => Eq (Root a) # | |||||
type Rep (Root a) # | |||||
Defined in Numeric.RootFinding type Rep (Root a) = D1 ('MetaData "Root" "Numeric.RootFinding" "math-functions-0.3.4.4-1lJnetaQ3dcFGUbfOsKxI6" 'False) (C1 ('MetaCons "NotBracketed" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SearchFailed" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Root" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))) |
Arguments
:: a | Default value. |
-> Root a | Result of search for a root. |
-> a |
Returns either the result of a search for a root, or the default value if the search failed.
Error tolerance for finding root. It describes when root finding algorithm should stop trying to improve approximation.
Constructors
RelTol !Double | Relative error tolerance. Given |
AbsTol !Double | Absolute error tolerance. Given |
Instances
Data Tolerance # | |||||
Defined in Numeric.RootFinding Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Tolerance -> c Tolerance # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Tolerance # toConstr :: Tolerance -> Constr # dataTypeOf :: Tolerance -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Tolerance) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Tolerance) # gmapT :: (forall b. Data b => b -> b) -> Tolerance -> Tolerance # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tolerance -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tolerance -> r # gmapQ :: (forall d. Data d => d -> u) -> Tolerance -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Tolerance -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Tolerance -> m Tolerance # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Tolerance -> m Tolerance # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Tolerance -> m Tolerance # | |||||
Generic Tolerance # | |||||
Defined in Numeric.RootFinding Associated Types
| |||||
Read Tolerance # | |||||
Show Tolerance # | |||||
Eq Tolerance # | |||||
type Rep Tolerance # | |||||
Defined in Numeric.RootFinding type Rep Tolerance = D1 ('MetaData "Tolerance" "Numeric.RootFinding" "math-functions-0.3.4.4-1lJnetaQ3dcFGUbfOsKxI6" 'False) (C1 ('MetaCons "RelTol" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :+: C1 ('MetaCons "AbsTol" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double))) |
withinTolerance :: Tolerance -> Double -> Double -> Bool #
Check that two values are approximately equal. In addition to specification values are considered equal if they're within 1ulp of precision. No further improvement could be done anyway.
class IterationStep a where #
Type class for checking whether iteration converged already.
Methods
matchRoot :: Tolerance -> a -> Maybe (Root Double) #
Return Just root
is current iteration converged within
required error tolerance. Returns Nothing
otherwise.
Instances
IterationStep NewtonStep # | |
Defined in Numeric.RootFinding | |
IterationStep RiddersStep # | |
Defined in Numeric.RootFinding |
Arguments
:: IterationStep a | |
=> Int | Maximum |
-> Tolerance | Error tolerance |
-> [a] | |
-> Root Double |
Find root in lazy list of iterations.
Ridders algorithm
data RiddersParam #
Parameters for ridders
root finding
Constructors
RiddersParam | |
Fields
|
Instances
Data RiddersParam # | |||||
Defined in Numeric.RootFinding Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> RiddersParam -> c RiddersParam # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c RiddersParam # toConstr :: RiddersParam -> Constr # dataTypeOf :: RiddersParam -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c RiddersParam) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c RiddersParam) # gmapT :: (forall b. Data b => b -> b) -> RiddersParam -> RiddersParam # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> RiddersParam -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> RiddersParam -> r # gmapQ :: (forall d. Data d => d -> u) -> RiddersParam -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> RiddersParam -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> RiddersParam -> m RiddersParam # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> RiddersParam -> m RiddersParam # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> RiddersParam -> m RiddersParam # | |||||
Generic RiddersParam # | |||||
Defined in Numeric.RootFinding Associated Types
| |||||
Read RiddersParam # | |||||
Defined in Numeric.RootFinding Methods readsPrec :: Int -> ReadS RiddersParam # readList :: ReadS [RiddersParam] # | |||||
Show RiddersParam # | |||||
Defined in Numeric.RootFinding Methods showsPrec :: Int -> RiddersParam -> ShowS # show :: RiddersParam -> String # showList :: [RiddersParam] -> ShowS # | |||||
Default RiddersParam # | |||||
Defined in Numeric.RootFinding Methods def :: RiddersParam # | |||||
Eq RiddersParam # | |||||
Defined in Numeric.RootFinding | |||||
type Rep RiddersParam # | |||||
Defined in Numeric.RootFinding type Rep RiddersParam = D1 ('MetaData "RiddersParam" "Numeric.RootFinding" "math-functions-0.3.4.4-1lJnetaQ3dcFGUbfOsKxI6" 'False) (C1 ('MetaCons "RiddersParam" 'PrefixI 'True) (S1 ('MetaSel ('Just "riddersMaxIter") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "riddersTol") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Tolerance))) |
Arguments
:: RiddersParam | Parameters for algorithms. |
-> (Double, Double) | Bracket for root |
-> (Double -> Double) | Function to find roots |
-> Root Double |
Use the method of Ridders[Ridders1979] to compute a root of a function. It doesn't require derivative and provide quadratic convergence (number of significant digits grows quadratically with number of iterations).
The function must have opposite signs when evaluated at the lower and upper bounds of the search (i.e. the root must be bracketed). If there's more that one root in the bracket iteration will converge to some root in the bracket.
riddersIterations :: (Double, Double) -> (Double -> Double) -> [RiddersStep] #
List of iterations for Ridders methods. See ridders
for
documentation of parameters
data RiddersStep #
Single Ridders step. It's a bracket of root
Constructors
RiddersStep !Double !Double | Ridders step. Parameters are bracket for the root |
RiddersBisect !Double !Double | Bisection step. It's fallback which is taken when Ridders update takes us out of bracket |
RiddersRoot !Double | Root found |
RiddersNoBracket | Root is not bracketed |
Instances
Newton-Raphson algorithm
data NewtonParam #
Parameters for ridders
root finding
Constructors
NewtonParam | |
Fields
|
Instances
Data NewtonParam # | |||||
Defined in Numeric.RootFinding Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> NewtonParam -> c NewtonParam # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c NewtonParam # toConstr :: NewtonParam -> Constr # dataTypeOf :: NewtonParam -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c NewtonParam) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c NewtonParam) # gmapT :: (forall b. Data b => b -> b) -> NewtonParam -> NewtonParam # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> NewtonParam -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> NewtonParam -> r # gmapQ :: (forall d. Data d => d -> u) -> NewtonParam -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> NewtonParam -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> NewtonParam -> m NewtonParam # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> NewtonParam -> m NewtonParam # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> NewtonParam -> m NewtonParam # | |||||
Generic NewtonParam # | |||||
Defined in Numeric.RootFinding Associated Types
| |||||
Read NewtonParam # | |||||
Defined in Numeric.RootFinding Methods readsPrec :: Int -> ReadS NewtonParam # readList :: ReadS [NewtonParam] # readPrec :: ReadPrec NewtonParam # readListPrec :: ReadPrec [NewtonParam] # | |||||
Show NewtonParam # | |||||
Defined in Numeric.RootFinding Methods showsPrec :: Int -> NewtonParam -> ShowS # show :: NewtonParam -> String # showList :: [NewtonParam] -> ShowS # | |||||
Default NewtonParam # | |||||
Defined in Numeric.RootFinding Methods def :: NewtonParam # | |||||
Eq NewtonParam # | |||||
Defined in Numeric.RootFinding | |||||
type Rep NewtonParam # | |||||
Defined in Numeric.RootFinding type Rep NewtonParam = D1 ('MetaData "NewtonParam" "Numeric.RootFinding" "math-functions-0.3.4.4-1lJnetaQ3dcFGUbfOsKxI6" 'False) (C1 ('MetaCons "NewtonParam" 'PrefixI 'True) (S1 ('MetaSel ('Just "newtonMaxIter") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Just "newtonTol") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Tolerance))) |
Arguments
:: NewtonParam | Parameters for algorithm. |
-> (Double, Double, Double) | Triple of |
-> (Double -> (Double, Double)) | Function to find root of. It returns pair of function value and its first derivative |
-> Root Double |
Solve equation using Newton-Raphson iterations.
This method require both initial guess and bounds for root. If Newton step takes us out of bounds on root function reverts to bisection.
newtonRaphsonIterations :: (Double, Double, Double) -> (Double -> (Double, Double)) -> [NewtonStep] #
List of iteration for Newton-Raphson algorithm. See documentation
for newtonRaphson
for meaning of parameters.
data NewtonStep #
Steps for Newton iterations
Constructors
NewtonStep !Double !Double | Normal Newton-Raphson update. Parameters are: old guess, new guess |
NewtonBisection !Double !Double | Bisection fallback when Newton-Raphson iteration doesn't work. Parameters are bracket on root |
NewtonRoot !Double | Root is found |
NewtonNoBracket | Root is not bracketed |
Instances
Data NewtonStep # | |||||
Defined in Numeric.RootFinding Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> NewtonStep -> c NewtonStep # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c NewtonStep # toConstr :: NewtonStep -> Constr # dataTypeOf :: NewtonStep -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c NewtonStep) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c NewtonStep) # gmapT :: (forall b. Data b => b -> b) -> NewtonStep -> NewtonStep # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> NewtonStep -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> NewtonStep -> r # gmapQ :: (forall d. Data d => d -> u) -> NewtonStep -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> NewtonStep -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> NewtonStep -> m NewtonStep # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> NewtonStep -> m NewtonStep # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> NewtonStep -> m NewtonStep # | |||||
Generic NewtonStep # | |||||
Defined in Numeric.RootFinding Associated Types
| |||||
Read NewtonStep # | |||||
Defined in Numeric.RootFinding Methods readsPrec :: Int -> ReadS NewtonStep # readList :: ReadS [NewtonStep] # readPrec :: ReadPrec NewtonStep # readListPrec :: ReadPrec [NewtonStep] # | |||||
Show NewtonStep # | |||||
Defined in Numeric.RootFinding Methods showsPrec :: Int -> NewtonStep -> ShowS # show :: NewtonStep -> String # showList :: [NewtonStep] -> ShowS # | |||||
NFData NewtonStep # | |||||
Defined in Numeric.RootFinding Methods rnf :: NewtonStep -> () # | |||||
Eq NewtonStep # | |||||
Defined in Numeric.RootFinding | |||||
IterationStep NewtonStep # | |||||
Defined in Numeric.RootFinding | |||||
type Rep NewtonStep # | |||||
Defined in Numeric.RootFinding type Rep NewtonStep = D1 ('MetaData "NewtonStep" "Numeric.RootFinding" "math-functions-0.3.4.4-1lJnetaQ3dcFGUbfOsKxI6" 'False) ((C1 ('MetaCons "NewtonStep" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :+: C1 ('MetaCons "NewtonBisection" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double))) :+: (C1 ('MetaCons "NewtonRoot" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :+: C1 ('MetaCons "NewtonNoBracket" 'PrefixI 'False) (U1 :: Type -> Type))) |
References
- Ridders, C.F.J. (1979) A new algorithm for computing a single root of a real continuous function. IEEE Transactions on Circuits and Systems 26:979–980.
- Press W.H.; Teukolsky S.A.; Vetterling W.T.; Flannery B.P. (2007). "Section 9.2.1. Ridders' Method". /Numerical Recipes: The Art of Scientific Computing (3rd ed.)./ New York: Cambridge University Press. ISBN 978-0-521-88068-8.