def quad_over_lin_canon(expr, args):
    affine_expr = args[0]
    y = args[1]
    if isinstance(affine_expr, Variable):
        return SymbolicQuadForm(affine_expr, eye(affine_expr.size), expr), []
    else:
        t = Variable(affine_expr.shape)
        return SymbolicQuadForm(t, eye(affine_expr.size)/y, expr), [affine_expr == t]
Exemple #2
0
def quad_form_canon(expr, args):
    affine_expr = expr.args[0]
    P = expr.args[1]
    if isinstance(affine_expr, Variable):
        return SymbolicQuadForm(affine_expr, P, expr), []
    else:
        t = Variable(affine_expr.shape)
        return SymbolicQuadForm(t, P, expr), [affine_expr == t]
Exemple #3
0
def quad_over_lin_canon(expr, args):
    affine_expr = args[0]
    y = args[1]
    t = Variable(affine_expr.shape)
    return SymbolicQuadForm(t,
                            eye(affine_expr.size) / y,
                            expr), [affine_expr == t]
Exemple #4
0
def quad_over_lin_canon(expr, args):
    affine_expr = args[0]
    y = args[1]
    # Simplify if y has no parameters.
    if len(y.parameters()) == 0:
        quad_mat = eye(affine_expr.size)/y.value
    else:
        # TODO this codepath produces an intermediate dense matrix.
        # but it should be sparse the whole time.
        quad_mat = eye(affine_expr.size)/y

    if isinstance(affine_expr, Variable):
        return SymbolicQuadForm(affine_expr, quad_mat, expr), []
    else:
        t = Variable(affine_expr.shape)
        return SymbolicQuadForm(t, quad_mat, expr), [affine_expr == t]
Exemple #5
0
def power_canon(expr, args):
    affine_expr = args[0]
    p = expr.p
    if expr.is_constant():
        return Constant(expr.value), []
    elif p == 0:
        return np.ones(affine_expr.shape), []
    elif p == 1:
        return affine_expr, []
    elif p == 2:
        if isinstance(affine_expr, Variable):
            return SymbolicQuadForm(affine_expr, np.eye(affine_expr.size), expr), []
        else:
            t = Variable(affine_expr.shape)
            return SymbolicQuadForm(t, np.eye(t.size), expr), [affine_expr == t]
    raise ValueError("non-constant quadratic forms can't be raised to a power "
                     "greater than 2.")
Exemple #6
0
def quad_form_canon(expr, args):
    affine_expr = expr.args[0]
    P = expr.args[1]
    t = Variable(affine_expr.shape)
    return SymbolicQuadForm(t, P, expr), [affine_expr == t]