def huber(x, M): """ Huber penalty function with parameter M. Convex. :param x: Number or scalar object. :param M: Positive constant. :rtype: Number or :class:`cvxpy_tree`. """ # x must be scalar if ((not np.isscalar(x)) and type(x).__name__ not in SCALAR_OBJS): raise ValueError('Invalid first argument') # M must be a positive constant if ((not np.isscalar(M)) or M <= 0): raise ValueError('Invalid second argument') # Construct and return program v = var('v') w = var('w') a = param('a') p = prog(minimize(2 * v + square(w)), [less(abs(a), w + v), greater(v, 0), greater(w, 0), less(w, 1)], [a], None, 'huber') return square(M) * p((1.0 / M) * x)
def huber(x,M): """ Huber penalty function with parameter M. Convex. :param x: Number or scalar object. :param M: Positive constant. :rtype: Number or :class:`cvxpy_tree`. """ # x must be scalar if((not np.isscalar(x)) and type(x).__name__ not in SCALAR_OBJS): raise ValueError('Invalid first argument') # M must be a positive constant if((not np.isscalar(M)) or M <= 0): raise ValueError('Invalid second argument') # Construct and return program v = var('v') w = var('w') a = param('a') p = prog(minimize(2*v+square(w)), [less(abs(a),w+v), greater(v,0), greater(w,0), less(w,1)], [a],None,'huber') return square(M)*p((1.0/M)*x)
def norm1(x): """ :math:`l_1` norm. Convex. :param x: :class:`cvxpy_matrix` or array object. :rtype: Number or :class:`cvxpy_tree`. """ # x must be matrix or array object if(type(x) is not cvxpy_matrix and type(x).__name__ not in ARRAY_OBJS): raise ValueError('Invalid argument') # Construct and return program (m,n) = x.shape t = var('t') a = param('a',m,n) p = prog(minimize(t), [less(sum(abs(a)),t)], [a], None, 'norm1') return p(x)