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 norm1(x) elif p == "inf": return normInf(x) elif p == "nuc": return normNuc(x) elif p == "fro": return norm2(x) elif p == 2: if x.is_matrix(): return sigma_max(x) else: return norm2(x) else: raise Exception("Invalid value %s for p." % p)
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. 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)