Skip to content
Browse files

stop iterating findZero when two consecutive guesses are equal

Compare this commit with 'e81d05947a05471cb5e3ae01b18adc8a63a7ceb9'
Due to lossy arithmetic an iterative approximation sequence might "converge",
but its limit is not an "exact" solution to the problem. "Converging" in lossy
arithmetic means the sequence becomes constant, so let us test for this.
  • Loading branch information...
1 parent 16b3354 commit 51bdf34e789e619874931a58ec8243493670baa5 @Borgvall committed Jan 14, 2012
Showing with 4 additions and 2 deletions.
  1. +2 −1 Numeric/AD/Halley.hs
  2. +2 −1 Numeric/AD/Newton.hs
View
3 Numeric/AD/Halley.hs
@@ -52,9 +52,10 @@ import Numeric.AD.Internal.Composition
findZero :: (Fractional a, Eq a) => UU a -> a -> [a]
findZero f = go
where
- go x = x : if y == 0 then [] else go (x - 2*y*y'/(2*y'*y'-y*y''))
+ go x = x : if y == 0 || x == xn then [] else go xn
where
(y:y':y'':_) = diffs0 f x
+ xn = x - 2*y*y'/(2*y'*y'-y*y'')
{-# INLINE findZero #-}
-- | The 'inverse' function inverts a scalar function using
View
3 Numeric/AD/Newton.hs
@@ -49,9 +49,10 @@ import Numeric.AD.Internal.Composition
findZero :: (Fractional a, Eq a) => UU a -> a -> [a]
findZero f = go
where
- go x = x : if y == 0 then [] else go (x - y/y')
+ go x = x : if y == 0 || x == xn then [] else go xn
where
(y,y') = diff' f x
+ xn = x - y/y'
{-# INLINE findZero #-}
-- | The 'inverseNewton' function inverts a scalar function using

0 comments on commit 51bdf34

Please sign in to comment.
Something went wrong with that request. Please try again.