JensGrabner / mpmath-2

Automatically exported from code.google.com/p/mpmath
Other
3 stars 0 forks source link

bisect fails if root is at midpoint of initial range #233

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. findroot(x**2-1,(0,2),solver='bisect')

What is the expected output? What do you see instead?
mpf('1.0')

The following patch will fix the problem. Illinois and Ridder already detect 
"flat functions" and return in this situation so it's not a problem for them. 
An alternative to the patch below is just to make Bisection return in the same 
way as Illinois and Ridder.

--- a/sympy/mpmath/calculus/optimization.py
+++ b/sympy/mpmath/calculus/optimization.py
@@ -320,11 +320,14 @@ def __iter__(self):
         while True:
             m = self.ctx.ldexp(a + b, -1)
             fm = f(m)
-            if fm * fb < 0:
+            sign = fm * fb
+            if sign < 0:
                 a = m
-            else:
+            elif sign > 0:
                 b = m
                 fb = fm
+            else:
+                yield m, 0
             l /= 2
             yield (a + b)/2, abs(l)

Original issue reported on code.google.com by smi...@gmail.com on 24 Jan 2013 at 4:35

GoogleCodeExporter commented 9 years ago
Fixed in 
https://github.com/fredrik-johansson/mpmath/commit/316f8bf9c7c1a03b7357696f6bb63
9783704a7d5

Thanks!

Original comment by fredrik....@gmail.com on 25 Apr 2013 at 5:28