示例#1
0
        def classifier2(Y, soft=False):

            M = Y.size[0]

            # K = Y*X' / sigma
            K = matrix(theta, (width, M))
            blas.gemm(X,
                      Y,
                      K,
                      transB='T',
                      alpha=1.0 / sigma,
                      beta=-1.0,
                      m=width)

            K = exp(K)
            K = div(K - K**-1, K + K**-1)

            # complete K
            lapack.potrs(L11, K)
            K = matrix([K, matrix(0., (N - width, M))], (N, M))
            chompack.trsm(Lc, K, trans='N')
            chompack.trsm(Lc, K, trans='T')

            x = matrix(b, (M, 1))
            blas.gemv(K, z, x, trans='T', beta=1.0)

            if soft: return x
            else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
示例#2
0
        def classifier2(Y, soft=False):

            M = Y.size[0]

            # K = Y*X' / sigma
            K = matrix(0.0, (width, M))
            blas.gemm(X, Y, K, transB='T', alpha=1.0 / sigma, m=width)

            # c[i] = ||Yi||^2 / sigma
            ones = matrix(1.0, (max(width, n, M), 1))
            c = Y**2 * ones[:n]
            blas.scal(1.0 / sigma, c)

            # Kij := Kij - 0.5 * (ci + aj)
            #      = || yi - xj ||^2 / (2*sigma)
            blas.ger(ones[:width], c, K, alpha=-0.5)
            blas.ger(a[:width], ones[:M], K, alpha=-0.5)
            # Kij = exp(Kij)
            K = exp(K)

            # complete K
            lapack.potrs(L11, K)
            K = matrix([K, matrix(0., (N - width, M))], (N, M))
            chompack.trsm(Lc, K, trans='N')
            chompack.trsm(Lc, K, trans='T')

            x = matrix(b, (M, 1))
            blas.gemv(K, z, x, trans='T', beta=1.0)

            if soft: return x
            else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
示例#3
0
        def classifier2(Y, soft=False):
            M = Y.size[0]
            W = matrix(0., (width, M))
            blas.gemm(X, Y, W, transB='T', alpha=1.0 / sigma, m=width)
            lapack.potrs(L11, W)
            W = matrix([W, matrix(0., (N - width, M))])
            chompack.trsm(Lc, W, trans='N')
            chompack.trsm(Lc, W, trans='T')

            x = matrix(b, (M, 1))
            blas.gemv(W, z, x, trans='T', beta=1.0)
            if soft: return x
            else: return matrix([2 * (xk > 0.0) - 1 for xk in x])
示例#4
0
        def g(x, y, z):

            # Solve
            #
            #     [ K    d3    ] [ ux_y ]
            #     [            ] [      ] =
            #     [ d3'  1'*d3 ] [ ux_b ]
            #
            #         [ bx_y ]   [ D  ]
            #         [      ] - [    ] * D3 * (D2 * bx_v + bx_z - bx_w).
            #         [ bx_b ]   [ d' ]

            x[:N] -= mul(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:]))
            x[N] -= blas.dot(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:]))

            # Solve dy1 := K^-1 * x[:N]
            blas.copy(x, dy1, n=N)
            chompack.trsm(L, dy1, trans='N')
            chompack.trsm(L, dy1, trans='T')

            # Find ux_y = dy1 - ux_b * dy2 s.t
            #
            #     d3' * ( dy1 - ux_b * dy2 + ux_b ) = x[N]
            #
            # i.e.  x[N] := ( x[N] - d3'* dy1 ) / ( d3'* ( 1 - dy2 ) ).

            x[N] = ( x[N] - blas.dot(d3, dy1) ) / \
                ( blas.asum(d3) - blas.dot(d3, dy2) )
            x[:N] = dy1 - x[N] * dy2

            # ux_v = D4 * ( bx_v -  D1^-1 (bz_z + D * (ux_y + ux_b))
            #     - D2^-1 * bz_w )

            x[-N:] = mul(
                d4, x[-N:] - div(z[:N] + mul(d, x[:N] + x[N]), d1) -
                div(z[N:], d2))

            # uz_z = - D1^-1 * ( bx_z - D * ( ux_y + ux_b ) - ux_v )
            # uz_w = - D2^-1 * ( bx_w - uz_w )
            z[:N] += base.mul(d, x[:N] + x[N]) + x[-N:]
            z[-N:] += x[-N:]
            blas.scal(-1.0, z)

            # Return W['di'] * uz
            blas.tbmv(W['di'], z, n=2 * N, k=0, ldA=1)
示例#5
0
    def test_trsm(self):
        L = cp.cspmatrix(self.symb) + self.A
        cp.cholesky(L)

        B = cp.eye(self.symb.n)
        Bt = matrix(B)[self.symb.p, :]
        Lt = matrix(L.spmatrix(reordered=True, symmetric=False))
        cp.trsm(L, B)
        blas.trsm(Lt, Bt)
        diff = list((B - Bt[self.symb.ip, :])[:])
        self.assertAlmostEqualLists(diff, len(diff) * [0.0])

        B = cp.eye(self.symb.n)
        Bt = matrix(B)[self.symb.p, :]
        Lt = matrix(L.spmatrix(reordered=True, symmetric=False))
        cp.trsm(L, B, trans='T')
        blas.trsm(Lt, Bt, transA='T')
        diff = list(B - Bt[self.symb.ip, :])[:]
        self.assertAlmostEqualLists(diff, len(diff) * [0.0])
示例#6
0
    def test_trsm(self):
        L = cp.cspmatrix(self.symb) + self.A
        cp.cholesky(L)
        
        B = cp.eye(self.symb.n)
        Bt = matrix(B)[self.symb.p,:]
        Lt = matrix(L.spmatrix(reordered=True,symmetric=False))
        cp.trsm(L, B)
        blas.trsm(Lt,Bt)
        diff = list((B-Bt[self.symb.ip,:])[:])
        self.assertAlmostEqualLists(diff, len(diff)*[0.0])

        B = cp.eye(self.symb.n)
        Bt = matrix(B)[self.symb.p,:]
        Lt = matrix(L.spmatrix(reordered=True,symmetric=False))
        cp.trsm(L, B, trans = 'T')
        blas.trsm(Lt,Bt,transA='T')
        diff = list(B-Bt[self.symb.ip,:])[:]
        self.assertAlmostEqualLists(diff, len(diff)*[0.0])
示例#7
0
文件: base.py 项目: cvxopt/smcp
def completion(X):
    """
    Returns maximum-determinant positive definite completion of X
    if it exists, and otherwise an exception is raised.
    """
    from cvxopt.amd import order
    n = X.size[0]
    Xt = chompack.tril(X)
    p = order(Xt)
    # Xc,N = chompack.embed(Xt,p)
    # L = chompack.completion(Xc)
    symb = chompack.symbolic(Xt,p)
    L = chompack.cspmatrix(symb) + Xt
    chompack.completion(L)
    Xt = matrix(0.,(n,n))
    Xt[::n+1] = 1.
    # chompack.solve(L, Xt, mode=0)
    # chompack.solve(L, Xt, mode=1)
    chompack.trsm(L, Xt)
    chompack.trsm(L, Xt, trans = 'T')
    return Xt
示例#8
0
def completion(X):
    """
    Returns maximum-determinant positive definite completion of X
    if it exists, and otherwise an exception is raised.
    """
    from cvxopt.amd import order
    n = X.size[0]
    Xt = chompack.tril(X)
    p = order(Xt)
    # Xc,N = chompack.embed(Xt,p)
    # L = chompack.completion(Xc)
    symb = chompack.symbolic(Xt, p)
    L = chompack.cspmatrix(symb) + Xt
    chompack.completion(L)
    Xt = matrix(0., (n, n))
    Xt[::n + 1] = 1.
    # chompack.solve(L, Xt, mode=0)
    # chompack.solve(L, Xt, mode=1)
    chompack.trsm(L, Xt)
    chompack.trsm(L, Xt, trans='T')
    return Xt
示例#9
0
    def __init__(self, X, V, a=None, p=None):

        self._n = X.size[0]
        self._V = +V
        assert X.size[0] == X.size[1], 'X must be a square matrix'
        assert X.size[0] == V.size[
            0], 'V must have have the same number of rows as X'
        assert isinstance(V, matrix), 'V must be a dense matrix'

        if isinstance(X, cspmatrix) and X.is_factor is False:
            self._L0 = X.copy()
            cp.cholesky(self._L0)
            cp.trsm(self._L0, self._V)
        elif isinstance(X, cspmatrix) and X.is_factor is True:
            self._L0 = X
            cp.trsm(self._L0, self._V)
        elif isinstance(X, spmatrix):
            symb = symbolic(X, p=p)
            self._L0 = cspmatrix(symb) + X
            cp.cholesky(self._L0)
            cp.trsm(self._L0, self._V)
        elif isinstance(X, matrix):
            raise NotImplementedError
        else:
            raise TypeError

        if a is None: a = matrix(1.0, (self._n, 1))
        self._L = matrix(0.0, V.size)
        self._B = matrix(0.0, V.size)
        pfchol(a, self._V, self._L, self._B)
        return
示例#10
0
    def trsm(self, B, trans='N'):
        r"""
        Solves a triangular system of equations with multiple righthand
        sides. Computes

        .. math::

            B &:= L^{-1} B  \text{ if trans is 'N'}

           B &:= L^{-T} B  \text{ if trans is 'T'} 
        """

        if trans == 'N':
            cp.trsm(self._L0, B)
            pftrsm(self._V, self._L, self._B, B, trans='N')
        elif trans == 'T':
            pftrsm(self._V, self._L, self._B, B, trans='T')
            cp.trsm(self._L0, B, trans='T')
        elif type(trans) is str:
            raise ValueError("trans must be 'N' or 'T'")
        else:
            raise TypeError("trans must be 'N' or 'T'")
        return
示例#11
0
    def Fkkt(W):
        """
        Custom KKT solver for

            [  Qinv  0   0  -D    0  ] [ ux_y ]   [ bx_y ]
            [  0     0   0  -d'   0  ] [ ux_b ]   [ bx_b ]
            [  0     0   0  -I   -I  ] [ ux_v ] = [ bx_v ]
            [ -D    -d  -I  -D1   0  ] [ uz_z ]   [ bz_z ]
            [  0     0  -I   0   -D2 ] [ uz_w ]   [ bz_w ]

        with D1 = diag(d1), D2 = diag(d2), d1 = W['d'][:N]**2,
        d2 = W['d'][N:])**2.
        """

        d1, d2 = W['d'][:N]**2, W['d'][N:]**2
        d3, d4 = (d1 + d2)**-1, (d1**-1 + d2**-1)**-1

        # Factor the chordal matrix K = Qinv + (D_1+D_2)^-1.
        K.V = Qinv.V
        K[::N + 1] = K[::N + 1] + d3
        L = chompack.cspmatrix(symb) + K
        chompack.cholesky(L)

        # Solve (Qinv + (D1+D2)^-1) * dy2 = (D1 + D2)^{-1} * 1
        blas.copy(d3, dy2)
        chompack.trsm(L, dy2, trans='N')
        chompack.trsm(L, dy2, trans='T')

        def g(x, y, z):

            # Solve
            #
            #     [ K    d3    ] [ ux_y ]
            #     [            ] [      ] =
            #     [ d3'  1'*d3 ] [ ux_b ]
            #
            #         [ bx_y ]   [ D  ]
            #         [      ] - [    ] * D3 * (D2 * bx_v + bx_z - bx_w).
            #         [ bx_b ]   [ d' ]

            x[:N] -= mul(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:]))
            x[N] -= blas.dot(d, mul(d3, mul(d2, x[-N:]) + z[:N] - z[-N:]))

            # Solve dy1 := K^-1 * x[:N]
            blas.copy(x, dy1, n=N)
            chompack.trsm(L, dy1, trans='N')
            chompack.trsm(L, dy1, trans='T')

            # Find ux_y = dy1 - ux_b * dy2 s.t
            #
            #     d3' * ( dy1 - ux_b * dy2 + ux_b ) = x[N]
            #
            # i.e.  x[N] := ( x[N] - d3'* dy1 ) / ( d3'* ( 1 - dy2 ) ).

            x[N] = ( x[N] - blas.dot(d3, dy1) ) / \
                ( blas.asum(d3) - blas.dot(d3, dy2) )
            x[:N] = dy1 - x[N] * dy2

            # ux_v = D4 * ( bx_v -  D1^-1 (bz_z + D * (ux_y + ux_b))
            #     - D2^-1 * bz_w )

            x[-N:] = mul(
                d4, x[-N:] - div(z[:N] + mul(d, x[:N] + x[N]), d1) -
                div(z[N:], d2))

            # uz_z = - D1^-1 * ( bx_z - D * ( ux_y + ux_b ) - ux_v )
            # uz_w = - D2^-1 * ( bx_w - uz_w )
            z[:N] += base.mul(d, x[:N] + x[N]) + x[-N:]
            z[-N:] += x[-N:]
            blas.scal(-1.0, z)

            # Return W['di'] * uz
            blas.tbmv(W['di'], z, n=2 * N, k=0, ldA=1)

        return g
示例#12
0
print("Projected inverse/completion       :  err = %.3e" % (blas.nrm2(tmp)))

# Compute norm of error: L - Lc2
tmp = (L.spmatrix()-Lc2.spmatrix()).V
print("Projected inverse/completion (upd) :  err = %.3e" % (blas.nrm2(tmp)))

# Compute norm of error: At - U
tmp = (At-U).spmatrix().V
print("Hessian factors NN/TN/TI/NI        :  err = %.3e" % (blas.nrm2(tmp)))


# Test triangular matrix products and solve
p = L.symb.p

B = eye(n)
trsm(L, B)
print("trsm, trans = 'N'                  :  err = %.3e" % (blas.nrm2(L.spmatrix(reordered = True)*B[p,p] - eye(n))))

B = eye(n)
trsm(L, B, trans = 'T')
print("trsm, trans = 'T'                  :  err = %.3e" % (blas.nrm2(L.spmatrix(reordered = True).T*B[p,p] - eye(n))))

B = eye(n)
trmm(L, B)
print("trmm, trans = 'N'                  :  err = %.3e" % (blas.nrm2(L.spmatrix(reordered = True) - B[p,p])))

B = eye(n)
trmm(L, B, trans = 'T')
print("trmm, trans = 'T'                  :  err = %.3e" % (blas.nrm2(L.spmatrix(reordered = True).T - B[p,p])))

B = eye(n)