def __mul__(self, other): """The product of two expressions. """ # Multiplying by a constant on the right is handled differently # from multiplying by a constant on the left. if self.is_constant(): # TODO HACK catch c.T*x where c is a NumPy 1D array. if self.size[0] == other.size[0] and \ self.size[1] != self.size[0] and \ isinstance(self, types.constant()) and self.is_1D_array: self = self.T return types.mul_expr()(self, other) elif other.is_constant(): # Having the constant on the left is more efficient. if self.is_scalar() or other.is_scalar(): return types.mul_expr()(other, self) else: return types.rmul_expr()(self, other) # When both expressions are not constant # Allow affine * affine but raise DCPError otherwise # Cannot multiply two non-constant expressions. elif self.is_affine() and other.is_affine(): warnings.warn("Forming a nonconvex expression (affine)*(affine).") return types.affine_prod_expr()(self, other) else: raise DCPError("Cannot multiply %s and %s." % (self.curvature, other.curvature))
def __mul__(self, other): """The product of two expressions. """ # Cannot multiply two non-constant expressions. if not self.is_constant() and \ not other.is_constant(): raise TypeError("Cannot multiply two non-constants.") # Multiplying by a constant on the right is handled differently # from multiplying by a constant on the left. elif not self.is_constant(): return types.rmul_expr()(self, other) else: return types.mul_expr()(self, other)
def __mul__(self, other): """The product of two expressions. """ # Cannot multiply two non-constant expressions. if not self.is_constant() and \ not other.is_constant(): raise DCPError("Cannot multiply two non-constants.") # Multiplying by a constant on the right is handled differently # from multiplying by a constant on the left. elif self.is_constant(): # TODO HACK catch c.T*x where c is a NumPy 1D array. if self.size[0] == other.size[0] and \ self.size[1] != self.size[0] and \ isinstance(self, types.constant()) and self.is_1D_array: self = self.T return types.mul_expr()(self, other) # Having the constant on the left is more efficient. elif self.is_scalar() or other.is_scalar(): return types.mul_expr()(other, self) else: return types.rmul_expr()(self, other)