def cartesian(arrays, out=None):
 arrays = [np.asarray(x) for x in arrays]
 shape = (len(x) for x in arrays)
 dtype = arrays[0].dtype
 ix = np.indices(shape)
 ix = ix.reshape(len(arrays), (-1)).T
 if (out is None):
  out = np.empty_like(ix, dtype=dtype)
 for (n, arr) in enumerate(arrays):
  out[:, n] = arrays[n][ix[:, n]]
 return out

def cartesian ( arrays , out = None ) : arrays = [ np . asarray ( x ) for x in arrays ] shape = ( len ( x ) for x in arrays ) dtype = arrays [ 0 ] . dtype ix = np . indices ( shape ) ix = ix . reshape ( len ( arrays ) , ( - 1 ) ) . T if ( out is None ) : out = np . empty_like ( ix , dtype = dtype ) for ( n , arr ) in enumerate ( arrays ) : out [ : , n ] = arrays [ n ] [ ix [ : , n ] ] return out
cartesian product of a list of arrays parameters: nodes: order: order in which the product is enumerated returns: out: each line corresponds to one point of the product space .