<s> """<STR_LIT>""" <EOL> from scipy . optimize import brentq <EOL> from openmdao . main . driver import Driver <EOL> from openmdao . main . interfaces import IHasParameters , IHasEqConstraints , ISolver , implements <EOL> from openmdao . main . hasparameters import HasParameters <EOL> from openmdao . main . hasconstraints import HasEqConstraints <EOL> from openmdao . util . decorators import add_delegate <EOL> from openmdao . lib . datatypes . api import Float , Int , Slot <EOL> @ add_delegate ( HasParameters , HasEqConstraints ) <EOL> class Brent ( Driver ) : <EOL> """<STR_LIT>""" <EOL> implements ( IHasParameters , IHasEqConstraints , ISolver ) <EOL> lower_bound = Float ( <NUM_LIT:0.> , iotype = "<STR_LIT>" , <EOL> desc = "<STR_LIT>" ) <EOL> upper_bound = Float ( <NUM_LIT> , iotype = "<STR_LIT>" , <EOL> desc = "<STR_LIT>" ) <EOL> xtol = Float ( <NUM_LIT:0.0> , iotype = "<STR_LIT>" , <EOL> desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> rtol = Float ( <NUM_LIT:0.0> , iotype = "<STR_LIT>" , <EOL> desc = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> maxiter = Int ( <NUM_LIT:100> , iotype = "<STR_LIT>" , <EOL> desc = '<STR_LIT>' ) <EOL> iprint = Int ( <NUM_LIT:0> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> f_resize_bracket = Slot ( object , <EOL> desc = '<STR_LIT>' ) <EOL> invalid_bracket_return = Float ( - <NUM_LIT:1> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( Brent , self ) . __init__ ( ) <EOL> self . xstar = None <EOL> def _eval ( self , x ) : <EOL> """<STR_LIT>""" <EOL> self . set_parameters ( [ x ] ) <EOL> self . run_iteration ( ) <EOL> return self . eval_eq_constraints ( self . parent ) [ <NUM_LIT:0> ] <EOL> def execute ( self ) : <EOL> bracket_invalid = self . _eval ( self . lower_bound ) * self . _eval ( self . upper_bound ) > <NUM_LIT:0> <EOL> if bracket_invalid and self . f_resize_bracket : <EOL> iteration = <NUM_LIT:1> <EOL> should_continue = True <EOL> bracket_invalid = True <EOL> while bracket_invalid and should_continue : <EOL> self . lower_bound , self . upper_bound , should_continue = self . f_resize_bracket ( self . lower_bound , self . upper_bound , iteration ) <EOL> bracket_invalid = self . _eval ( self . lower_bound ) * self . _eval ( self . upper_bound ) > <NUM_LIT:0> <EOL> iteration += <NUM_LIT:1> <EOL> if bracket_invalid : <EOL> if self . invalid_bracket_return >= <NUM_LIT:0.0> and self . invalid_bracket_return <= <NUM_LIT:1.0> : <EOL> xstar = self . lower_bound + self . invalid_bracket_return * ( self . upper_bound - self . lower_bound ) <EOL> brent_iterations = '<STR_LIT>' <EOL> else : <EOL> self . raise_exception ( '<STR_LIT>' % <EOL> ( self . lower_bound , self . upper_bound ) ) <EOL> else : <EOL> kwargs = { '<STR_LIT>' : self . maxiter , '<STR_LIT:a>' : self . lower_bound , <EOL> '<STR_LIT:b>' : self . upper_bound , '<STR_LIT>' : True } <EOL> if self . xtol > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = self . xtol <EOL> if self . rtol > <NUM_LIT:0> : <EOL> kwargs [ '<STR_LIT>' ] = self . rtol <EOL> xstar , r = brentq ( self . _eval , ** kwargs ) <EOL> brent_iterations = r . iterations <EOL> self . set_parameters ( [ xstar ] ) <EOL> self . run_iteration ( ) <EOL> if self . iprint == <NUM_LIT:1> : <EOL> print '<STR_LIT>' , brent_iterations <EOL> print '<STR_LIT>' , self . eval_eq_constraints ( ) <EOL> def check_config ( self , strict = False ) : <EOL> '''<STR_LIT>''' <EOL> super ( Brent , self ) . check_config ( strict = strict ) <EOL> nparam = len ( self . get_parameters ( ) ) <EOL> if nparam != <NUM_LIT:1> : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % nparam ) <EOL> ncnst = len ( self . get_eq_constraints ( ) ) <EOL> if ncnst != <NUM_LIT:1> : <EOL> self . raise_exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ncnst ) </s>
