Esempio n. 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])
Esempio n. 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])
Esempio n. 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)
Esempio n. 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)
Esempio n. 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)
Esempio n. 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)))
Esempio n. 7
0
def only_arg(expr):
    if len(expr.arg) != 1:
        raise ExpressionError("wrong number of args", expr)
    return expr.arg[0]