def __mul__(self, other): """interpret other and call one of the following self._mul_scalar() self._mul_vector() self._mul_multivector() self._mul_sparse_matrix() """ M,N = self.shape if isscalarlike(other): # scalar value return self._mul_scalar(other) if issparse(other): if self.shape[1] != other.shape[0]: raise ValueError('dimension mismatch') return self._mul_sparse_matrix(other) try: other.shape except AttributeError: # If it's a list or whatever, treat it like a matrix other = np.asanyarray(other) other = np.asanyarray(other) if other.ndim == 1 or other.ndim == 2 and other.shape[1] == 1: # dense row or column vector if other.shape != (N,) and other.shape != (N,1): raise ValueError('dimension mismatch') result = self._mul_vector(np.ravel(other)) if isinstance(other, np.matrix): result = np.asmatrix(result) if other.ndim == 2 and other.shape[1] == 1: # If 'other' was an (nx1) column vector, reshape the result result = result.reshape(-1,1) return result elif other.ndim == 2: ## # dense 2D array or matrix ("multivector") if other.shape[0] != self.shape[1]: raise ValueError('dimension mismatch') result = self._mul_multivector(np.asarray(other)) if isinstance(other, np.matrix): result = np.asmatrix(result) return result else: raise ValueError('could not interpret dimensions')
def __rmul__(self, other): # other * self if isscalarlike(other): return self.__mul__(other) else: # Don't use asarray unless we have to try: tr = other.transpose() except AttributeError: tr = np.asarray(other).transpose() return (self.transpose() * tr).transpose()
def makeshapes(x, y, type=None, z=None, m=None): """ Make shapes by x and y coordinates. :param x: (*array_like*) X coordinates. :param y: (*array_like*) Y coordinates. :param type: (*string*) Shape type [point | line | polygon]. :param z: (*array_like*) Z coordinates. :param m: (*array_like*) M coordinates. :returns: Shapes """ shapes = [] if isinstance(x, (int, float)): shape = PointShape() shape.setPoint(PointD(x, y)) shapes.append(shape) else: x = np.asarray(x).array y = np.asarray(y).array if not z is None: if m is None: m = np.zeros(len(z)).array else: m = np.asarray(m).array z = np.asarray(z).array if type == 'point': if z is None: shapes = ShapeUtil.createPointShapes(x, y) else: shapes = ShapeUtil.createPointShapes(x, y, z, m) elif type == 'line': if z is None: shapes = ShapeUtil.createPolylineShapes(x, y) else: shapes = ShapeUtil.createPolylineShapes(x, y, z, m) elif type == 'polygon': if z is None: shapes = ShapeUtil.createPolygonShapes(x, y) else: shapes = ShapeUtil.createPolygonShape(x, y, z, m) return shapes
def to_native(A): return np.asarray(A,dtype=A.dtype.newbyteorder('native'))