Exemple #1
0
def param_canon(expr, real_args, imag_args, real2imag):
    if expr.is_real():
        return expr, None
    elif expr.is_imag():
        imag = CallbackParam(lambda: np.imag(expr.value), expr.shape)
        return None, imag
    else:
        real = CallbackParam(lambda: np.real(expr.value), expr.shape)
        imag = CallbackParam(lambda: np.imag(expr.value), expr.shape)
        return (real, imag)
Exemple #2
0
 def canonicalize(self):
     """Represent the atom as an affine objective and conic constraints.
     """
     # Constant atoms are treated as a leaf.
     if self.is_constant():
         # Parameterized expressions are evaluated later.
         if self.parameters():
             param = CallbackParam(lambda: self.value, self.shape)
             return param.canonical_form
         # Non-parameterized expressions are evaluated immediately.
         else:
             return Constant(self.value).canonical_form
     else:
         arg_objs = []
         constraints = []
         for arg in self.args:
             obj, constr = arg.canonical_form
             arg_objs.append(obj)
             constraints += constr
         # Special info required by the graph implementation.
         data = self.get_data()
         graph_obj, graph_constr = self.graph_implementation(arg_objs,
                                                             self.shape,
                                                             data)
         return graph_obj, constraints + graph_constr
 def canonicalize_expr(self, expr, args):
     if isinstance(expr, Expression) and not expr.variables():
         # Parameterized expressions are evaluated in a subsequent
         # reduction.
         if expr.parameters():
             param = CallbackParam(lambda: expr.value, expr.shape)
             return param, []
         # Non-parameterized expressions are evaluated immediately.
         else:
             return Constant(expr.value), []
     elif type(expr) in self.canon_methods:
         return self.canon_methods[type(expr)](expr, args)
     else:
         return expr.copy(args), []