def combine_xyz(vec, square=False):
 if (vec.ndim != 2):
  raise ValueError('Input must be 2D')
 if ((vec.shape[0] % 3) != 0):
  raise ValueError('Input must have 3N rows')
 (n, p) = vec.shape
 if np.iscomplexobj(vec):
  vec = np.abs(vec)
 comb = (vec[0::3] ** 2)
 comb += (vec[1::3] ** 2)
 comb += (vec[2::3] ** 2)
 if (not square):
  comb = np.sqrt(comb)
 return comb

def combine_xyz ( vec , square = False ) : if ( vec . ndim != 2 ) : raise ValueError ( 'Input must be 2D' ) if ( ( vec . shape [ 0 ] % 3 ) != 0 ) : raise ValueError ( 'Input must have 3N rows' ) ( n , p ) = vec . shape if np . iscomplexobj ( vec ) : vec = np . abs ( vec ) comb = ( vec [ 0 : : 3 ] ** 2 ) comb += ( vec [ 1 : : 3 ] ** 2 ) comb += ( vec [ 2 : : 3 ] ** 2 ) if ( not square ) : comb = np . sqrt ( comb ) return comb
compute the three cartesian components of a vector or matrix together .