private void handleAllRoots ( ) { int totalFound = 0 ; double r = 1 ; int iter = 0 ; // increase the search region centered around 0 until at least one root has been found while ( iter ++ < maxBoundIterations && ( totalFound = sturm . countRealRoots ( - r , r ) ) <= 0 ) { r = 2 * r * r ; } if ( Double . isInfinite ( r ) ) throw new RuntimeException ( "r is infinite" ) ; if ( iter >= maxBoundIterations ) throw new RuntimeException ( "Too many iterations when searching center region" ) ; boundEachRoot ( - r , r , 0 , totalFound ) ; int target ; // search for roots below the center bound if ( totalFound < numRoots && ( target = sturm . countRealRoots ( Double . NEGATIVE_INFINITY , - r ) ) > 0 ) { double upper = - r ; double width = r ; iter = 0 ; while ( iter < maxBoundIterations && target > 0 ) { int N = sturm . countRealRoots ( upper - width , upper ) ; if ( N != 0 ) { boundEachRoot ( upper - width , upper , totalFound , N ) ; target -= N ; totalFound += N ; } upper -= width ; width = 2 * width * width ; } if ( iter >= maxBoundIterations ) throw new RuntimeException ( "Too many iterations when searching lower region" ) ; } if ( totalFound < numRoots && ( target = sturm . countRealRoots ( r , Double . POSITIVE_INFINITY ) ) > 0 ) { double lower = r ; double width = r ; iter = 0 ; while ( iter < maxBoundIterations && target > 0 ) { int N = sturm . countRealRoots ( lower , lower + width ) ; if ( N != 0 ) { boundEachRoot ( lower , lower + width , totalFound , N ) ; target -= N ; totalFound += N ; } lower += width ; width = 2 * width * width ; } if ( iter >= maxBoundIterations ) throw new RuntimeException ( "Too many iterations when searching upper region" ) ; } }
Search for all real roots . First find a region about 0 which contains at least one root . Then search above and below .