Aleksey Khudyakov avatar Aleksey Khudyakov committed 9766d8c

Optimize bisection.

Also takes care of case when a>b in (a,b) interval

Comments (0)

Files changed (1)

Numeric/Tools/Equation.hs

   | flo * fhi > 0 = NotBracketed
   | flo == 0      = Root lo
   | fhi == 0      = Root hi
-  | otherwise     = worker 0 lo flo hi fhi
+  | flo < 0       = worker 0 lo hi
+  | otherwise     = worker 0 hi lo
   where
     flo = f lo
     fhi = f hi
-    worker i a fa b fb
-      | within 1 a b      = Root a
-      | (b - a) <= eps    = Root c
-      | fc == 0           = Root c
-      | i >= (100 :: Int) = SearchFailed
-      | fa * fc < 0       = worker (i+1) a fa c fc
-      | otherwise         = worker (i+1) c fc b fb
+    -- Worker function. Preconditions:
+    --   f a < 0
+    --   f b > 0
+    worker i a b
+      | within 1 a b       = Root a
+      | abs (b - a) <= eps = Root c
+      | fc == 0            = Root c
+      | i >= (100 :: Int)  = SearchFailed
+      | fc < 0             = worker (i+1) c b
+      | otherwise          = worker (i+1) a c
       where
         c  = 0.5 * (a + b)
         fc = f c
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.