示例#1
0
def linear_map(A, x):
    if dim(x, 1) != 1:
        raise ExpressionError("applying linear map to non vector", x)
    if A.n != dim(x):
        raise ExpressionError("linear map has wrong size: %s" % A, x)

    return Expression(expression_type=expression_pb2.Expression.LINEAR_MAP,
                      size=Size(dim=[A.m, 1]),
                      func_curvature=AFFINE,
                      linear_map=A.proto,
                      data=A.data,
                      arg=[x])
示例#2
0
def diag_vec(x):
    if dim(x, 1) != 1:
        raise ExpressionError("diag_vec on non vector")

    n = dim(x, 0)
    return Expression(expression_type=expression_pb2.Expression.DIAG_VEC,
                      size=Size(dim=[n, n]),
                      func_curvature=AFFINE,
                      arg=[x])
示例#3
0
def reshape(arg, m, n):
    if dim(arg, 0) == m and dim(arg, 1) == n:
        return arg

    if m * n != dim(arg):
        raise ExpressionError("cant reshape to %d x %d" % (m, n), arg)

    # If we have two reshapes that "undo" each other, cancel them out
    if (arg.expression_type == expression_pb2.Expression.RESHAPE
            and dim(arg.arg[0], 0) == m and dim(arg.arg[0], 1) == n):
        return arg.arg[0]

    return Expression(expression_type=expression_pb2.Expression.RESHAPE,
                      arg=[arg],
                      size=Size(dim=[m, n]),
                      func_curvature=AFFINE,
                      sign=arg.sign)
示例#4
0
def soc_elemwise_constraint(t, *args):
    t = reshape(t, dim(t), 1)
    X = hstack(*(reshape(arg, dim(arg), 1) for arg in args))
    if dim(t) != dim(X, 0):
        raise ExpressionError("Second order cone, incompatible sizes", t, X)
    return indicator(Cone.SECOND_ORDER, t, X)
示例#5
0
def soc_constraint(t, x):
    if dim(t, 1) != 1 or dim(t, 0) != dim(x, 0):
        raise ExpressionError("Second order cone, invalid dimensions", t)
    return indicator(Cone.SECOND_ORDER, t, x)
示例#6
0
def eq_constraint(x, y):
    if dims(x) != dims(y) and dim(x) != 1 and dim(y) != 1:
        raise ExpressionError("incompatible sizes", x, y)
    return indicator(Cone.ZERO, add(x, negate(y)))
示例#7
0
def only_arg(expr):
    if len(expr.arg) != 1:
        raise ExpressionError("wrong number of args", expr)
    return expr.arg[0]