Esempio n. 1
0
def transform_sum(expr):
    x = only_arg(expr)
    m, n = dims(x)

    if not expr.has_axis:
        return expression.linear_map(linear_map.sum(m, n), transform_expr(x))

    if expr.axis == 0:
        return expression.linear_map(linear_map.sum_left(m, n), transform_expr(x))

    if expr.axis == 1:
        return expression.linear_map(linear_map.sum_right(m, n), transform_expr(x))

    raise TransformError("unknown axis attribute", expr)
Esempio n. 2
0
def transform_sum(expr):
    x = only_arg(expr)
    m, n = dims(x)

    if not expr.has_axis:
        return expression.linear_map(linear_map.sum(m, n), transform_expr(x))

    if expr.axis == 0:
        return expression.linear_map(linear_map.sum_left(m, n),
                                     transform_expr(x))

    if expr.axis == 1:
        return expression.linear_map(linear_map.sum_right(m, n),
                                     transform_expr(x))

    raise TransformError("unknown axis attribute", expr)
Esempio n. 3
0
def transform_index(expr):
    return expression.linear_map(
        linear_map.kronecker_product(
            linear_map.index(expr.key[1], dim(only_arg(expr), 1)), linear_map.index(expr.key[0], dim(only_arg(expr), 0))
        ),
        transform_expr(only_arg(expr)),
    )
Esempio n. 4
0
def transform_multiply(expr):
    if len(expr.arg) != 2:
        raise TransformError("wrong number of args", expr)

    m = dim(expr, 0)
    n = dim(expr, 1)
    if expr.arg[0].dcp_props.constant:
        A = multiply_constant(expr.arg[0], m)
        B = promote(transform_expr(expr.arg[1]), n * n)
        return expression.linear_map(linear_map.left_matrix_product(A, n), B)

    if expr.arg[1].dcp_props.constant:
        A = promote(transform_expr(expr.arg[0]), m * m)
        B = multiply_constant(expr.arg[1], n)
        return expression.linear_map(linear_map.right_matrix_product(B, m), A)

    raise TransformError("multiplying non constants", expr)
Esempio n. 5
0
def transform_multiply(expr):
    if len(expr.arg) != 2:
        raise TransformError("wrong number of args", expr)

    m = dim(expr, 0)
    n = dim(expr, 1)
    if expr.arg[0].dcp_props.constant:
        A = multiply_constant(expr.arg[0], m)
        B = promote(transform_expr(expr.arg[1]), n * n)
        return expression.linear_map(linear_map.left_matrix_product(A, n), B)

    if expr.arg[1].dcp_props.constant:
        A = promote(transform_expr(expr.arg[0]), m * m)
        B = multiply_constant(expr.arg[1], n)
        return expression.linear_map(linear_map.right_matrix_product(B, m), A)

    raise TransformError("multiplying non constants", expr)
Esempio n. 6
0
def transform_kron(expr):
    if len(expr.arg) != 2:
        raise TransformError("Wrong number of arguments", expr)

    if not expr.arg[0].dcp_props.constant:
        raise TransformError("First arg is not constant", expr)

    return expression.linear_map(
        linear_map.kronecker_product_single_arg(
            multiply_constant(expr.arg[0], 1), dim(expr.arg[1], 0),
            dim(expr.arg[1], 1)), transform_expr(expr.arg[1]))
Esempio n. 7
0
def transform_kron(expr):
    if len(expr.arg) != 2:
        raise TransformError("Wrong number of arguments", expr)

    if not expr.arg[0].dcp_props.constant:
        raise TransformError("First arg is not constant", expr)

    return expression.linear_map(
        linear_map.kronecker_product_single_arg(
            multiply_constant(expr.arg[0], 1), dim(expr.arg[1], 0), dim(expr.arg[1], 1)
        ),
        transform_expr(expr.arg[1]),
    )
Esempio n. 8
0
def transform_hstack(expr):
    m = dim(expr, 0)
    n = dim(expr, 1)
    offset = 0
    add_args = []
    for arg in expr.arg:
        ni = dim(arg, 1)
        add_args.append(
            expression.linear_map(
                linear_map.right_matrix_product(linear_map.index(slice(offset, offset + ni), n), m), transform_expr(arg)
            )
        )
        offset += ni
    return expression.add(*add_args)
Esempio n. 9
0
def transform_multiply_elementwise(expr):
    if len(expr.arg) != 2:
        raise TransformError("wrong number of args", expr)

    if expr.arg[0].dcp_props.constant:
        c_expr = expr.arg[0]
        x_expr = expr.arg[1]
    elif expr.arg[1].dcp_props.constant:
        c_expr = expr.arg[1]
        x_expr = expr.arg[0]
    else:
        raise TransformError("multiply non constants", expr)

    return expression.linear_map(multiply_elementwise_constant(c_expr), transform_expr(x_expr))
Esempio n. 10
0
def transform_hstack(expr):
    m = dim(expr, 0)
    n = dim(expr, 1)
    offset = 0
    add_args = []
    for arg in expr.arg:
        ni = dim(arg, 1)
        add_args.append(
            expression.linear_map(
                linear_map.right_matrix_product(
                    linear_map.index(slice(offset, offset + ni), n), m),
                transform_expr(arg)))
        offset += ni
    return expression.add(*add_args)
Esempio n. 11
0
def transform_multiply_elementwise(expr):
    if len(expr.arg) != 2:
        raise TransformError("wrong number of args", expr)

    if expr.arg[0].dcp_props.constant:
        c_expr = expr.arg[0]
        x_expr = expr.arg[1]
    elif expr.arg[1].dcp_props.constant:
        c_expr = expr.arg[1]
        x_expr = expr.arg[0]
    else:
        raise TransformError("multiply non constants", expr)

    return expression.linear_map(multiply_elementwise_constant(c_expr),
                                 transform_expr(x_expr))
Esempio n. 12
0
def transform_vstack(expr):
    m = dim(expr, 0)
    n = dim(expr, 1)
    offset = 0
    add_args = []
    for arg in expr.arg:
        mi = dim(arg, 0)

        add_args.append(
            expression.linear_map(
                linear_map.left_matrix_product(
                    linear_map.transpose(
                        linear_map.index(slice(offset, offset + mi), m)), n),
                transform_expr(arg)))
        offset += mi
    return expression.add(*add_args)
Esempio n. 13
0
def transform_vstack(expr):
    m = dim(expr, 0)
    n = dim(expr, 1)
    offset = 0
    add_args = []
    for arg in expr.arg:
        mi = dim(arg, 0)

        add_args.append(
            expression.linear_map(
                linear_map.left_matrix_product(
                    linear_map.transpose(
                        linear_map.index(slice(offset, offset+mi), m)),
                    n),
                transform_expr(arg)))
        offset += mi
    return expression.add(*add_args)
Esempio n. 14
0
def transform_transpose(expr):
    x = only_arg(expr)
    return expression.linear_map(linear_map.transpose_matrix(*dims(x)), transform_expr(x))
Esempio n. 15
0
def transform_trace(expr):
    return expression.linear_map(linear_map.trace(dim(only_arg(expr), 0)), transform_expr(only_arg(expr)))
Esempio n. 16
0
def transform_upper_tri(expr):
    return expression.linear_map(linear_map.upper_tri(dim(expr, 0)), transform_expr(only_arg(expr)))
Esempio n. 17
0
def transform_diag_vec(expr):
    return expression.linear_map(linear_map.diag_vec(dim(expr, 0)), transform_expr(only_arg(expr)))
Esempio n. 18
0
def promote(expr, new_dim):
    if dim(expr) != 1 or dim(expr) == new_dim:
        return expr
    return expression.linear_map(linear_map.promote(new_dim), expr)
Esempio n. 19
0
def transform_diag_vec(expr):
    return expression.linear_map(linear_map.diag_vec(dim(expr, 0)),
                                 transform_expr(only_arg(expr)))
Esempio n. 20
0
def transform_upper_tri(expr):
    return expression.linear_map(linear_map.upper_tri(dim(expr, 0)),
                                 transform_expr(only_arg(expr)))
Esempio n. 21
0
def transform_trace(expr):
    return expression.linear_map(linear_map.trace(dim(only_arg(expr), 0)),
                                 transform_expr(only_arg(expr)))
Esempio n. 22
0
def transform_index(expr):
    return expression.linear_map(
        linear_map.kronecker_product(
            linear_map.index(expr.key[1], dim(only_arg(expr), 1)),
            linear_map.index(expr.key[0], dim(only_arg(expr), 0))),
        transform_expr(only_arg(expr)))
Esempio n. 23
0
def promote(expr, new_dim):
    if dim(expr) != 1 or dim(expr) == new_dim:
        return expr
    return expression.linear_map(linear_map.promote(new_dim), expr)
Esempio n. 24
0
def transform_negate(expr):
    return expression.linear_map(linear_map.negate(dim(expr)), transform_expr(only_arg(expr)))
Esempio n. 25
0
def transform_transpose(expr):
    x = only_arg(expr)
    return expression.linear_map(linear_map.transpose_matrix(*dims(x)),
                                 transform_expr(x))
Esempio n. 26
0
def transform_sum(expr):
    return expression.linear_map(
        linear_map.sum(dim(only_arg(expr))),
        transform_expr(only_arg(expr)))
Esempio n. 27
0
def transform_negate(expr):
    return expression.linear_map(linear_map.negate(dim(expr)),
                                 transform_expr(only_arg(expr)))