def norm(x, p=2, axis=None): """Wrapper on the different norm atoms. Parameters ---------- x : Expression or numeric constant The value to take the norm of. p : int or str, optional The type of norm. Returns ------- Expression An Expression representing the norm. """ x = Expression.cast_to_const(x) if p == 1: return pnorm(x, 1, axis) elif p == "inf": return pnorm(x, 'inf', axis) elif p == "nuc": return normNuc(x) elif p == "fro": return pnorm(x, 2, axis) elif p == 2: if x.is_matrix(): return sigma_max(x) else: return pnorm(x, 2, axis) else: return pnorm(x, p, axis)
def norm(x, p=2, axis=None): """Wrapper on the different norm atoms. Parameters ---------- x : Expression or numeric constant The value to take the norm of. p : int or str, optional The type of norm. Returns ------- Expression An Expression representing the norm. """ x = Expression.cast_to_const(x) # Norms for scalars same as absolute value. if p == 1 or x.is_scalar(): return pnorm(x, 1, axis) elif p == "inf": return pnorm(x, 'inf', axis) elif p == "nuc": return normNuc(x) elif p == "fro": return pnorm(x, 2, axis) elif p == 2: if axis is None and x.is_matrix(): return sigma_max(x) else: return pnorm(x, 2, axis) else: return pnorm(x, p, axis)
def norm(x, p=2): """Wrapper on the different norm atoms. Parameters ---------- x : Expression or numeric constant The value to take the norm of. p : int or str, optional The type of norm. Returns ------- Expression An Expression representing the norm. """ x = Expression.cast_to_const(x) if p == 1: return pnorm(x, 1) elif p == "inf": return pnorm(x, 'inf') elif p == "nuc": return normNuc(x) elif p == "fro": return pnorm(x, 2) elif p == 2: if x.is_matrix(): return sigma_max(x) else: return pnorm(x, 2) else: return pnorm(x, p)
def DCCP_ini(self): dom_constr = self.objective.args[0].domain for arg in self.constraints: for dom in arg.args[0].domain: dom_constr.append(dom) for dom in arg.args[1].domain: dom_constr.append(dom) var_store = [] init_flag = [] for var in self.variables(): var_store.append(np.zeros((var._rows,var._cols))) init_flag.append(var.value is None) times = 3 for t in range(times): ini_cost = 0 var_ind = 0 for var in self.variables(): if init_flag[var_ind]: var.value = np.random.randn(var._rows,var._cols)*10 ini_cost += pnorm(var-var.value,2) var_ind += 1 ini_obj = Minimize(ini_cost) ini_prob = Problem(ini_obj,dom_constr) ini_prob.solve() var_ind = 0 for var in self.variables(): var_store[var_ind] = var_store[var_ind] + var.value/float(times) var_ind += 1 var_ind = 0 for var in self.variables(): var.value = var_store[var_ind] var_ind += 1
def norm(x, p=2, axis=None): """Wrapper on the different norm atoms. Parameters ---------- x : Expression or numeric constant The value to take the norm of. If `x` is 2D and `axis` is None, this function constructs a matrix norm. p : int or str, optional The type of norm. Valid options include any positive integer, 'fro' (for frobenius), 'nuc' (sum of singular values), np.inf or 'inf' (infinity norm). axis : The axis along which to apply the norm, if any. Returns ------- Expression An Expression representing the norm. """ x = Expression.cast_to_const(x) # matrix norms take precedence num_nontrivial_idxs = sum([d > 1 for d in x.shape]) if axis is None and x.ndim == 2: if p == 1: # matrix 1-norm return cvxpy.atoms.max(norm1(x, axis=0)) # Frobenius norm elif p == 'fro' or (p == 2 and num_nontrivial_idxs == 1): return pnorm(vec(x), 2) elif p == 2: # matrix 2-norm is largest singular value return sigma_max(x) elif p == 'nuc': # the nuclear norm (sum of singular values) return normNuc(x) elif p in [np.inf, "inf", "Inf"]: # the matrix infinity-norm return cvxpy.atoms.max(norm1(x, axis=1)) else: raise RuntimeError('Unsupported matrix norm.') else: if p == 1 or x.is_scalar(): return norm1(x, axis=axis) elif p in [np.inf, "inf", "Inf"]: return norm_inf(x, axis) else: return pnorm(x, p, axis)
def norm(x, p=2, axis=None): """Wrapper on the different norm atoms. Parameters ---------- x : Expression or numeric constant The value to take the norm of. p : int or str, optional The type of norm. Returns ------- Expression An Expression representing the norm. """ x = Expression.cast_to_const(x) # matrix norms take precedence if axis is None and x.ndim == 2: if p == 1: # matrix 1-norm return cvxpy.atoms.max(norm1(x, axis=0)) elif p == 2: # matrix 2-norm is largest singular value return sigma_max(x) elif p == 'nuc': # the nuclear norm (sum of singular values) return normNuc(x) elif p == 'fro': # Frobenius norm return pnorm(vec(x), 2) elif p in [np.inf, "inf", "Inf"]: # the matrix infinity-norm return cvxpy.atoms.max(norm1(x, axis=1)) else: raise RuntimeError('Unsupported matrix norm.') else: if p == 1 or x.is_scalar(): return norm1(x, axis=axis) elif p in [np.inf, "inf", "Inf"]: return norm_inf(x, axis) else: return pnorm(x, p, axis)
def harmonic_mean(x): """The harmonic mean of ``x``. Parameters ---------- x : Expression or numeric The expression whose harmonic mean is to be computed. Must have positive entries. Returns ------- Expression .. math:: \\frac{n}{\\left(\\sum_{i=1}^{n} x_i^{-1} \\right)}, where :math:`n` is the length of :math:`x`. """ x = Expression.cast_to_const(x) # TODO(akshayka): Behavior of the below is incorrect when x has negative # entries. Either fail fast or provide a correct expression with # unknown curvature. return x.size*pnorm(x, -1)
def normInf(x, axis=None): return pnorm(x, 'inf', axis)
def harmonic_mean(x): return np.prod(x.size)*pnorm(x, -1)
def harmonic_mean(x): x = Expression.cast_to_const(x) return np.prod(x.size)*pnorm(x, -1)
def norm1(x, axis=None): return pnorm(x, 1, axis)
def norm2(x): return pnorm(x, 2)
def norm2(x, axis=None): return pnorm(x, 2, axis)
def normInf(x): return pnorm(x, 'inf')
def norm1(x): return pnorm(x, 1)