#NEWLINE# def clip( a , a_min , a_max ) : #NEWLINE# #INDENT# 'Clip (limit) the values in an array.\n\n    Given an interval, values outside the interval are clipped to the interval\n    edges. For example, if an interval of [0, 1] is specified, values smaller\n    than 0 become 0, and values larger than 1 become 1.' #NEWLINE# if ( not isinstance ( a , np . ndarray ) ) : #NEWLINE# #INDENT# max_mask = ( a > a_max ) #NEWLINE# max_tar = ( gp . ones ( a . shape ) * a_max ) #NEWLINE# min_mask = ( a < a_min ) #NEWLINE# min_tar = ( gp . ones ( max_tar . shape ) * a_min ) #NEWLINE# a_clipped = ( ( ( a * ( ( 1 - max_mask ) - min_mask ) ) + ( max_tar * max_mask ) ) + ( min_tar * min_mask ) ) #NEWLINE# return a_clipped #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return np . clip ( a , a_min , a_max )