<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import sys <EOL> import numpy <EOL> from numpy . core import numeric <EOL> import uncertainties <EOL> from uncertainties import umath <EOL> from uncertainties import __author__ <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> to_nominal_values = numpy . vectorize ( <EOL> uncertainties . nominal_value , <EOL> otypes = [ float ] , <EOL> doc = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> to_std_devs = numpy . vectorize ( <EOL> uncertainties . std_dev , <EOL> otypes = [ float ] , <EOL> doc = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def unumpy_to_numpy_matrix ( arr ) : <EOL> """<STR_LIT>""" <EOL> return arr . view ( numpy . matrix ) if isinstance ( arr , matrix ) else arr <EOL> def nominal_values ( arr ) : <EOL> """<STR_LIT>""" <EOL> return unumpy_to_numpy_matrix ( to_nominal_values ( arr ) ) <EOL> def std_devs ( arr ) : <EOL> """<STR_LIT>""" <EOL> return unumpy_to_numpy_matrix ( to_std_devs ( arr ) ) <EOL> def derivative ( u , var ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( u , uncertainties . AffineScalarFunc ) : <EOL> try : <EOL> return u . derivatives [ var ] <EOL> except KeyError : <EOL> return <NUM_LIT:0.> <EOL> else : <EOL> return <NUM_LIT:0.> <EOL> def wrap_array_func ( func ) : <EOL> """<STR_LIT>""" <EOL> @ uncertainties . set_doc ( """<STR_LIT>""" % ( func . __name__ , func . __doc__ ) ) <EOL> def wrapped_func ( arr , * args ) : <EOL> arr_nominal_value = nominal_values ( arr ) <EOL> func_nominal_value = func ( arr_nominal_value , * args ) <EOL> variables = set ( ) <EOL> for element in arr . flat : <EOL> if isinstance ( element , uncertainties . AffineScalarFunc ) : <EOL> variables |= set ( element . derivatives . iterkeys ( ) ) <EOL> if not variables : <EOL> return func_nominal_value <EOL> derivatives = numpy . vectorize ( lambda _ : { } ) ( func_nominal_value ) <EOL> for var in variables : <EOL> shift_var = max ( var . _std_dev / <NUM_LIT> , <NUM_LIT> * abs ( var . _nominal_value ) ) <EOL> if not shift_var : <EOL> shift_var = <NUM_LIT> <EOL> shift_arr = array_derivative ( arr , var ) * shift_var <EOL> shifted_arr_values = arr_nominal_value + shift_arr <EOL> func_shifted = func ( shifted_arr_values , * args ) <EOL> numerical_deriv = ( func_shifted - func_nominal_value ) / shift_var <EOL> for ( derivative_dict , derivative_value ) in ( <EOL> zip ( derivatives . flat , numerical_deriv . flat ) ) : <EOL> if derivative_value : <EOL> derivative_dict [ var ] = derivative_value <EOL> return numpy . vectorize ( uncertainties . AffineScalarFunc ) ( <EOL> func_nominal_value , derivatives ) <EOL> wrapped_func . __name__ = func . __name__ <EOL> return wrapped_func <EOL> _uarray = numpy . vectorize ( lambda v , s : uncertainties . Variable ( v , s ) , <EOL> otypes = [ object ] ) <EOL> def uarray ( ( values , std_devs ) ) : <EOL> """<STR_LIT>""" <EOL> return _uarray ( values , std_devs ) <EOL> def array_derivative ( array_like , var ) : <EOL> """<STR_LIT>""" <EOL> return numpy . vectorize ( lambda u : derivative ( u , var ) , <EOL> otypes = [ float ] ) ( array_like ) <EOL> def func_with_deriv_to_uncert_func ( func_with_derivatives ) : <EOL> """<STR_LIT>""" <EOL> def wrapped_func ( array_like , * args ) : <EOL> """<STR_LIT>""" <EOL> array_version = numpy . asarray ( array_like ) <EOL> variables = set ( ) <EOL> for element in array_version . flat : <EOL> if isinstance ( element , uncertainties . AffineScalarFunc ) : <EOL> variables |= set ( element . derivatives . iterkeys ( ) ) <EOL> array_nominal = nominal_values ( array_version ) <EOL> func_and_derivs = func_with_derivatives ( <EOL> array_nominal , <EOL> type ( array_like ) , <EOL> ( array_derivative ( array_version , var ) for var in variables ) , <EOL> * args ) <EOL> func_nominal_value = func_and_derivs . next ( ) <EOL> if not variables : <EOL> return func_nominal_value <EOL> derivatives = numpy . array ( <EOL> [ { } for _ in xrange ( func_nominal_value . size ) ] , dtype = object ) <EOL> derivatives . resize ( func_nominal_value . shape ) <EOL> for ( var , deriv_wrt_var ) in zip ( variables , func_and_derivs ) : <EOL> for ( derivative_dict , derivative_value ) in zip ( <EOL> derivatives . flat , deriv_wrt_var . flat ) : <EOL> if derivative_value : <EOL> derivative_dict [ var ] = derivative_value <EOL> result = numpy . vectorize ( uncertainties . AffineScalarFunc ) ( <EOL> func_nominal_value , derivatives ) <EOL> if isinstance ( result , numpy . matrix ) : <EOL> result = result . view ( matrix ) <EOL> return result <EOL> return wrapped_func <EOL> def inv_with_derivatives ( arr , input_type , derivatives ) : <EOL> """<STR_LIT>""" <EOL> inverse = numpy . linalg . inv ( arr ) <EOL> if issubclass ( input_type , numpy . matrix ) : <EOL> inverse = inverse . view ( numpy . matrix ) <EOL> yield inverse <EOL> inverse_mat = numpy . asmatrix ( inverse ) <EOL> for derivative in derivatives : <EOL> derivative_mat = numpy . asmatrix ( derivative ) <EOL> yield - inverse_mat * derivative_mat * inverse_mat <EOL> _inv = func_with_deriv_to_uncert_func ( inv_with_derivatives ) <EOL> _inv . __doc__ = """<STR_LIT>""" % numpy . linalg . inv . __doc__ <EOL> def pinv_with_derivatives ( arr , input_type , derivatives , rcond ) : <EOL> """<STR_LIT>""" <EOL> inverse = numpy . linalg . pinv ( arr , rcond ) <EOL> if issubclass ( input_type , numpy . matrix ) : <EOL> inverse = inverse . view ( numpy . matrix ) <EOL> yield inverse <EOL> inverse_mat = numpy . asmatrix ( inverse ) <EOL> PA = arr * inverse_mat <EOL> AP = inverse_mat * arr <EOL> factor21 = inverse_mat * inverse_mat . H <EOL> factor22 = numpy . eye ( arr . shape [ <NUM_LIT:0> ] ) - PA <EOL> factor31 = numpy . eye ( arr . shape [ <NUM_LIT:1> ] ) - AP <EOL> factor32 = inverse_mat . H * inverse_mat <EOL> for derivative in derivatives : <EOL> derivative_mat = numpy . asmatrix ( derivative ) <EOL> term1 = - inverse_mat * derivative_mat * inverse_mat <EOL> derivative_mat_H = derivative_mat . H <EOL> term2 = factor21 * derivative_mat_H * factor22 <EOL> term3 = factor31 * derivative_mat_H * factor32 <EOL> yield term1 + term2 + term3 <EOL> try : <EOL> _pinv_default = numpy . linalg . pinv . __defaults__ [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> _pinv_default = <NUM_LIT> <EOL> _pinv_with_uncert = func_with_deriv_to_uncert_func ( pinv_with_derivatives ) <EOL> @ uncertainties . set_doc ( """<STR_LIT>""" % numpy . linalg . pinv . __doc__ ) <EOL> def _pinv ( array_like , rcond = _pinv_default ) : <EOL> return _pinv_with_uncert ( array_like , rcond ) <EOL> class matrix ( numpy . matrix ) : <EOL> """<STR_LIT>""" <EOL> def __rmul__ ( self , other ) : <EOL> if numeric . isscalar ( other ) : <EOL> return numeric . dot ( self , other ) <EOL> else : <EOL> return numeric . dot ( other , self ) <EOL> def getI ( self ) : <EOL> "<STR_LIT>" <EOL> M , N = self . shape <EOL> if M == N : <EOL> func = _inv <EOL> else : <EOL> func = _pinv <EOL> return func ( self ) <EOL> I = property ( getI , numpy . matrix . I . fset , numpy . matrix . I . fdel , <EOL> numpy . matrix . I . __doc__ ) <EOL> @ property <EOL> def nominal_values ( self ) : <EOL> """<STR_LIT>""" <EOL> return nominal_values ( self ) <EOL> std_devs = std_devs <EOL> def umatrix ( * args ) : <EOL> """<STR_LIT>""" <EOL> return uarray ( * args ) . view ( matrix ) <EOL> def define_vectorized_funcs ( ) : <EOL> """<STR_LIT>""" <EOL> this_module = sys . modules [ __name__ ] <EOL> func_name_translations = dict ( <EOL> ( f_name , '<STR_LIT>' + f_name [ <NUM_LIT:1> : ] ) <EOL> for f_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> new_func_names = [ func_name_translations . get ( function_name , function_name ) <EOL> for function_name in umath . many_scalars_to_scalar_funcs ] <EOL> for ( function_name , unumpy_name ) in zip ( <EOL> umath . many_scalars_to_scalar_funcs , new_func_names ) : <EOL> func = getattr ( umath , function_name ) <EOL> setattr ( <EOL> this_module , unumpy_name , <EOL> numpy . vectorize ( func , <EOL> otypes = [ object ] , <EOL> doc = """<STR_LIT>""" % ( function_name , func . __doc__ ) ) ) <EOL> __all__ . append ( unumpy_name ) <EOL> define_vectorized_funcs ( ) </s>
