Safe Haskell | None |
---|---|
Language | Haskell2010 |
The Converge type class.
Documentation
If a type is an instance of Converge then it represents a stream of values which are increasingly accurate approximations of a desired value
converge :: a -> Maybe (Element a) Source
converge
is a function that returns the value the stream is converging
to. If given a stream which doens't converge to a single value then
converge
will not terminate.
If the stream is empty then it should return nothing.
>>>
let initialGuess = 1 :: Double
>>>
let improve x = (x + 121 / x) / 2
>>>
converge (iterate improve initialGuess)
Just 11.0
>>>
converge [] :: Maybe [Int]
Nothing
convergeErr :: Ord (Element a) => (Element a -> Element a) -> a -> Maybe (Element a) Source
convergeErr
is a function that returns the value the stream is
converging to. It also takes a function err which returns a value which
varies monotonically with the error of the value in the stream. This can
be used to ensure that when convergeErr
terminates when given a
non-converging stream or a stream which enters a cycle close to the
solution. See the documentation for the CReal instance for a caveat with
that implementation.
It's often the case that streams generated with approximation functions such as Newton's method will generate worse approximations for some number of steps until they find the "zone of convergence". For these cases it's necessary to drop some values of the stream before handing it to convergeErr.
For example trying to find the root of the following funciton f
with a
poor choice of starting point. Although this doesn't find the root, it
doesn't fail to terminate.
>>>
let f x = x ^ 3 - 2 * x + 2
>>>
let f' x = 3 * x ^ 2 - 2
>>>
let initialGuess = 0.1 :: Float
>>>
let improve x = x - f x / f' x
>>>
let err x = abs (f x)
>>>
convergeErr err (iterate improve initialGuess)
Just 1.0142132
Eq a => Converge [a] Source | Every list of equatable values is an instance of |
Converge [CReal n] Source | The overlapping instance for It's important to note when the error function reaches zero this function
behaves like Find where log x = π using Newton's method
|