def mul_canon(expr, args): # Only allow param * var (not var * param). Associate right to left. # TODO: Only descend if both sides have parameters lhs = args[0] rhs = args[1] if not (lhs.parameters() and rhs.parameters()): return expr.copy(args), [] op_type = type(expr) if lhs.variables(): with scopes.dpp_scope(): assert rhs.is_affine() t = Variable(lhs.shape) return op_type(t, rhs), [t == lhs] elif rhs.variables(): with scopes.dpp_scope(): assert lhs.is_affine() t = Variable(rhs.shape) return op_type(lhs, t), [t == rhs] # Neither side has variables. One side must be affine in parameters. lhs_affine = False rhs_affine = False with scopes.dpp_scope(): lhs_affine = lhs.is_affine() rhs_affine = rhs.is_affine() assert lhs_affine or rhs_affine if lhs_affine: t = Variable(rhs.shape) return lhs * t, [t == rhs] else: t = Variable(lhs.shape) return t * rhs, [t == lhs]
def is_dgp(self, dpp: bool = False) -> bool: """The objective must be log-log concave. """ if dpp: with scopes.dpp_scope(): return self.args[0].is_log_log_concave() return self.args[0].is_log_log_concave()
def is_dcp(self, dpp: bool = False) -> bool: """The objective must be convex. """ if dpp: with scopes.dpp_scope(): return self.args[0].is_convex() return self.args[0].is_convex()
def is_dcp(self, dpp=False): """The objective must be concave. """ if dpp: with scopes.dpp_scope(): return self.args[0].is_concave() return self.args[0].is_concave()
def is_dpp(self, context='CP'): """The expression is a disciplined parameterized expression. context: cone program (CP) or quadratic program (QP) """ with scopes.dpp_scope(): return self.is_dcp()
def is_dgp(self, dpp: bool = False) -> bool: if dpp: with scopes.dpp_scope(): return (self.args[0].is_log_log_affine() and self.args[1].is_log_log_affine()) return (self.args[0].is_log_log_affine() and self.args[1].is_log_log_affine())
def is_dcp(self, dpp: bool = False) -> bool: if dpp: with scopes.dpp_scope(): args_ok = all(arg.is_affine() for arg in self.args) exps_ok = not isinstance(self.alpha, cvxtypes.parameter()) return args_ok and exps_ok return all(arg.is_affine() for arg in self.args)
def is_dcp(self, dpp: bool = False) -> bool: """An exponential constraint is DCP if each argument is affine. """ if dpp: with scopes.dpp_scope(): return all(arg.is_affine() for arg in self.args) return all(arg.is_affine() for arg in self.args)
def is_dgp(self, dpp=False): """The objective must be log-log convex. """ if dpp: with scopes.dpp_scope(): return self.args[0].is_log_log_convex() return self.args[0].is_log_log_convex()
def is_dcp(self, dpp=False): """An SOC constraint is DCP if each of its arguments is affine. """ if dpp: with scopes.dpp_scope(): return all(arg.is_affine() for arg in self.args) return all(arg.is_affine() for arg in self.args)
def is_dcp(self, dpp=False) -> bool: """A PSD constraint is DCP if the constrained expression is affine. """ if dpp: with scopes.dpp_scope(): return self.args[0].is_affine() return self.args[0].is_affine()
def is_dgp(self, dpp=False): if dpp: with scopes.dpp_scope(): return (self.args[0].is_log_log_convex() and self.args[1].is_log_log_concave()) return (self.args[0].is_log_log_convex() and self.args[1].is_log_log_concave())
def is_dpp(self, context='dcp') -> bool: with scopes.dpp_scope(): if context.lower() == 'dcp': return self.is_dcp(dpp=True) elif context.lower() == 'dgp': return self.is_dgp(dpp=True) else: raise ValueError("Unsupported context ", context)
def is_dcp(self, dpp: bool = False) -> bool: """A power cone constraint is DCP if each argument is affine. """ if dpp: with scopes.dpp_scope(): args_ok = self.args[0].is_affine() and self.args[1].is_affine() exps_ok = not isinstance(self.alpha, cvxtypes.parameter()) return args_ok and exps_ok return True
def is_dgp(self, dpp=False): """Checks whether the Expression is log-log DCP. Returns ------- bool True if the Expression is log-log DCP, False otherwise. """ if dpp: with scopes.dpp_scope(): return self.is_log_log_convex() or self.is_log_log_concave() return self.is_log_log_convex() or self.is_log_log_concave()
def is_dcp(self, dpp=False): """Checks whether the Expression is DCP. Parameters ---------- dpp : bool, optional If True, enforce the disciplined parametrized programming (DPP) ruleset; only relevant when the problem involves Parameters. Returns ------- bool True if the Expression is DCP, False otherwise. """ if dpp: with scopes.dpp_scope(): return self.is_convex() or self.is_concave() return self.is_convex() or self.is_concave()
def is_dcp(self, dpp=False) -> bool: """A non-negative constraint is DCP if its argument is concave.""" if dpp: with scopes.dpp_scope(): return self.args[0].is_concave() return self.args[0].is_concave()
def is_dcp(self, dpp: bool = False) -> bool: """A non-positive constraint is DCP if its argument is convex.""" if dpp: with scopes.dpp_scope(): return self.expr.is_convex() return self.expr.is_convex()
def is_dpp(self): """The objective must be concave. """ with scopes.dpp_scope(): return self.args[0].is_concave()
def is_dcp(self, dpp: bool = False) -> bool: """An equality constraint is DCP if its argument is affine.""" if dpp: with scopes.dpp_scope(): return self.expr.is_affine() return self.expr.is_affine()
def is_param_affine(expr) -> bool: """Returns true if expression is parameters-affine (and variable-free)""" with scopes.dpp_scope(): return not expr.variables() and expr.is_affine()
def is_dcp(self, dpp: bool = False) -> bool: """A zero constraint is DCP if its argument is affine.""" if dpp: with scopes.dpp_scope(): return self.args[0].is_affine() return self.args[0].is_affine()
def is_dpp(self): with scopes.dpp_scope(): return self.args[0].is_convex()
def is_dcp(self, dpp=False): """A non-positive constraint is DCP if its argument is convex.""" if dpp: with scopes.dpp_scope(): return self.args[0].is_convex() return self.args[0].is_convex()
def is_dpp(self): """The expression is a disciplined parameterized expression. """ with scopes.dpp_scope(): return self.is_dcp()