Exemplo n.º 1
0
 def test_vec_to_upper_tri(self) -> None:
     from cvxpy.atoms.affine.upper_tri import vec_to_upper_tri
     x = Variable(shape=(3, ))
     X = vec_to_upper_tri(x)
     x.value = np.array([1, 2, 3])
     actual = X.value
     expect = np.array([[1, 2], [0, 3]])
     assert np.allclose(actual, expect)
     y = Variable(shape=(1, ))
     y.value = np.array([4])
     Y = vec_to_upper_tri(y, strict=True)
     actual = Y.value
     expect = np.array([[0, 4], [0, 0]])
     assert np.allclose(actual, expect)
     A_expect = np.array([[0, 11, 12, 13], [0, 0, 16, 17], [0, 0, 0, 21],
                          [0, 0, 0, 0]])
     a = np.array([11, 12, 13, 16, 17, 21])
     A_actual = vec_to_upper_tri(a, strict=True).value
     assert np.allclose(A_actual, A_expect)
Exemplo n.º 2
0
def log_det_canon(expr, args):
    """Reduces the atom to an affine expression and list of constraints.

    Creates the equivalent problem::

       maximize    sum(log(D[i, i]))
       subject to: D diagonal
                   diag(D) = diag(Z)
                   Z is upper triangular.
                   [D Z; Z.T A] is positive semidefinite

    The problem computes the LDL factorization:

    .. math::

       A = (Z^TD^{-1})D(D^{-1}Z)

    This follows from the inequality:

    .. math::

       \\det(A) >= \\det(D) + \\det([D, Z; Z^T, A])/\\det(D)
               >= \\det(D)

    because (Z^TD^{-1})D(D^{-1}Z) is a feasible D, Z that achieves
    det(A) = det(D) and the objective maximizes det(D).

    Parameters
    ----------
    expr : log_det
    args : list
        The arguments for the expression

    Returns
    -------
    tuple
        (Variable for objective, list of constraints)
    """
    A = args[0]  # n by n matrix.
    n, _ = A.shape
    z = Variable(shape=(n * (n + 1) // 2, ))
    Z = vec_to_upper_tri(z, strict=False)
    d = diag_mat(Z)  # a vector
    D = diag_vec(d)  # a matrix
    X = bmat([[D, Z], [Z.T, A]])
    constraints = [PSD(X)]
    log_expr = log(d)
    obj, constr = log_canon(log_expr, log_expr.args)
    constraints += constr
    return sum(obj), constraints