#NEWLINE# @ typed_macro #NEWLINE# def norm( x , ord = None ) : #NEWLINE# #INDENT# if ( ord is None ) : #NEWLINE# #INDENT# ord = 2 #NEWLINE# #UNINDENT# if isinstance ( x . type , ScalarT ) : #NEWLINE# #INDENT# return x #NEWLINE# #UNINDENT# assert isinstance ( x . type , ArrayT ) , ( "Argument to 'norm' must be array, got %s : %s" % ( x , x . type ) ) #NEWLINE# assert ( x . type . rank == 1 ) , 'Norm currently only supported for vectors, not for %s : %s' ( x , x . type ) #NEWLINE# if ( ord == 0 ) : #NEWLINE# #INDENT# return ( x != 0 ) . sum ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ord == 1 ) : #NEWLINE# #INDENT# return abs ( x ) . sum ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ord == 2 ) : #NEWLINE# #INDENT# return np . sqrt ( ( x ** 2 ) . sum ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( ( abs ( x ) ** ord ) . sum ( ) ** ( 1.0 / ord ) )