<s> from __future__ import division , print_function , absolute_import <EOL> from math import sqrt , exp , sin , cos <EOL> from numpy . testing import ( TestCase , assert_warns , assert_ , <EOL> run_module_suite , assert_allclose , <EOL> assert_equal ) <EOL> from numpy import finfo <EOL> from scipy . optimize import zeros as cc <EOL> from scipy . optimize import zeros <EOL> from scipy . optimize . _tstutils import functions , fstrings <EOL> class TestBasic ( TestCase ) : <EOL> def run_check ( self , method , name ) : <EOL> a = <NUM_LIT> <EOL> b = sqrt ( <NUM_LIT:3> ) <EOL> xtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> rtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> for function , fname in zip ( functions , fstrings ) : <EOL> zero , r = method ( function , a , b , xtol = xtol , rtol = rtol , <EOL> full_output = True ) <EOL> assert_ ( r . converged ) <EOL> assert_allclose ( zero , <NUM_LIT:1.0> , atol = xtol , rtol = rtol , <EOL> err_msg = '<STR_LIT>' % ( name , fname ) ) <EOL> def test_bisect ( self ) : <EOL> self . run_check ( cc . bisect , '<STR_LIT>' ) <EOL> def test_ridder ( self ) : <EOL> self . run_check ( cc . ridder , '<STR_LIT>' ) <EOL> def test_brentq ( self ) : <EOL> self . run_check ( cc . brentq , '<STR_LIT>' ) <EOL> def test_brenth ( self ) : <EOL> self . run_check ( cc . brenth , '<STR_LIT>' ) <EOL> def test_newton ( self ) : <EOL> f1 = lambda x : x ** <NUM_LIT:2> - <NUM_LIT:2> * x - <NUM_LIT:1> <EOL> f1_1 = lambda x : <NUM_LIT:2> * x - <NUM_LIT:2> <EOL> f1_2 = lambda x : <NUM_LIT> + <NUM_LIT:0> * x <EOL> f2 = lambda x : exp ( x ) - cos ( x ) <EOL> f2_1 = lambda x : exp ( x ) + sin ( x ) <EOL> f2_2 = lambda x : exp ( x ) + cos ( x ) <EOL> for f , f_1 , f_2 in [ ( f1 , f1_1 , f1_2 ) , ( f2 , f2_1 , f2_2 ) ] : <EOL> x = zeros . newton ( f , <NUM_LIT:3> , tol = <NUM_LIT> ) <EOL> assert_allclose ( f ( x ) , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> x = zeros . newton ( f , <NUM_LIT:3> , fprime = f_1 , tol = <NUM_LIT> ) <EOL> assert_allclose ( f ( x ) , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> x = zeros . newton ( f , <NUM_LIT:3> , fprime = f_1 , fprime2 = f_2 , tol = <NUM_LIT> ) <EOL> assert_allclose ( f ( x ) , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_deriv_zero_warning ( self ) : <EOL> func = lambda x : x ** <NUM_LIT:2> <EOL> dfunc = lambda x : <NUM_LIT:2> * x <EOL> assert_warns ( RuntimeWarning , cc . newton , func , <NUM_LIT:0.0> , dfunc ) <EOL> def test_gh_5555 ( ) : <EOL> root = <NUM_LIT:0.1> <EOL> def f ( x ) : <EOL> return x - root <EOL> methods = [ cc . bisect , cc . ridder ] <EOL> xtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> rtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> for method in methods : <EOL> res = method ( f , - <NUM_LIT> , <NUM_LIT> , xtol = xtol , rtol = rtol ) <EOL> assert_allclose ( root , res , atol = xtol , rtol = rtol , <EOL> err_msg = '<STR_LIT>' % method . __name__ ) <EOL> def test_gh_5557 ( ) : <EOL> def f ( x ) : <EOL> if x < <NUM_LIT:0.5> : <EOL> return - <NUM_LIT:0.1> <EOL> else : <EOL> return x - <NUM_LIT> <EOL> atol = <NUM_LIT> <EOL> rtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> methods = [ cc . brentq , cc . brenth ] <EOL> for method in methods : <EOL> res = method ( f , <NUM_LIT:0> , <NUM_LIT:1> , xtol = atol , rtol = rtol ) <EOL> assert_allclose ( <NUM_LIT> , res , atol = atol , rtol = rtol ) <EOL> class TestRootResults : <EOL> def test_repr ( self ) : <EOL> r = zeros . RootResults ( root = <NUM_LIT:1.0> , <EOL> iterations = <NUM_LIT> , <EOL> function_calls = <NUM_LIT> , <EOL> flag = <NUM_LIT:0> ) <EOL> expected_repr = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert_equal ( repr ( r ) , expected_repr ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_module_suite ( ) </s>
