Ejemplo n.º 1
0
    def __init__(self, matrix, AAt=False, ordering="AMD"):
        # Initiate a Cholesky factorization

        # Set sizes
        self.N = matrix.shape[0]

        if AAt:
            # Analyze matrix
            self._chol = cholmod.analyze_AAt(matrix,
                                             mode="auto",
                                             ordering_method="best",
                                             use_long=False)
            # Factor matrix
            self._chol = self._chol.cholesky_AAt(matrix)
        else:
            # Analyze matrix
            self._chol = cholmod.analyze(matrix,
                                         mode="auto",
                                         ordering_method="best",
                                         use_long=False)
            # Factor matrix
            self._chol = self._chol.cholesky(matrix)

        if np.any(self._chol.D() <= 0):
            raise SparseCholeskyAbstract.PositiveDefiniteException(
                "Matrix is not positive definite")
Ejemplo n.º 2
0
    def factorAAt(self):
        """ Peform sparse cholesky factorization """
        global sym_factor, sym_shape

        with torch.no_grad():
            self.chols = []
            start = time.time()
            As = self.to_csc()

            if sym_factor is None or As[0].shape != sym_shape:
                sym_factor = cholmod.analyze_AAt(As[0], ordering_method='best')
                sym_shape = As[0].shape

            for A in As:
                chol = sym_factor.cholesky_AAt(A)
                self.chols.append(chol)

        return self.chols
Ejemplo n.º 3
0
    def _get_state_update(self):
        W, J, r = self._graph.get_linearization()

        Jt = J.T.tocsc()
        J = J.tocsc()

        # Decompose W such that W = U * U.T
        if self._sym_decomp_W is None:
            self._sym_decomp_W = analyze(W, mode='auto')

        chol_decomp_W = self._sym_decomp_W.cholesky(W)
        U = chol_decomp_W.L()
        JtU = Jt.dot(U)

        # A = J.T * W * J
        #   = J.T * U * U.T * J
        if self._sym_decomp_JtWJ is None:
            self._sym_decomp_JtWJ = analyze_AAt(JtU, mode='auto')

        chol_decomp_JtWJ = self._sym_decomp_JtWJ.cholesky_AAt(JtU)

        b = Jt.dot(W.dot(r))
        x = chol_decomp_JtWJ.solve_A(b)
        return x
Ejemplo n.º 4
0
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert_allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert_allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert_allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert_allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert_allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert_allclose(f3(Xty), answer)

            print(problem, mode)
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert_allclose(np.prod(f.D()), np.linalg.det(XtX.todense()))
                assert_allclose((f.L() * f.L().T).todense(), pXtX)
                L, D = f.L_D()
                assert_allclose((L * D * L.T).todense(), pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert_allclose(f.solve_A(b), np.dot(XtX.todense().I, b))
                assert_allclose(f(b), np.dot(XtX.todense().I, b))
                assert_allclose(f.solve_LDLt(b),
                                np.dot((L * D * L.T).todense().I, b))
                assert_allclose(f.solve_LD(b), np.dot((L * D).todense().I, b))
                assert_allclose(f.solve_DLt(b), np.dot((D * L.T).todense().I,
                                                       b))
                assert_allclose(f.solve_L(b), np.dot(L.todense().I, b))
                assert_allclose(f.solve_Lt(b), np.dot(L.T.todense().I, b))
                assert_allclose(f.solve_D(b), np.dot(D.todense().I, b))

                assert_allclose(f.apply_P(b), b[f.P(), :])
                assert_allclose(f.apply_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
Ejemplo n.º 5
0
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert_allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert_allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert_allclose(cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert_allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert_allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert_allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert_allclose(f3(Xty), answer)

            print(problem, mode)
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert_allclose(np.prod(f.D()),
                                np.linalg.det(XtX.todense()))
                assert_allclose((f.L() * f.L().T).todense(),
                                pXtX)
                L, D = f.L_D()
                assert_allclose((L * D * L.T).todense(),
                                pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert_allclose(f.solve_A(b),
                                np.dot(XtX.todense().I, b))
                assert_allclose(f(b),
                                np.dot(XtX.todense().I, b))
                assert_allclose(f.solve_LDLt(b),
                                np.dot((L * D * L.T).todense().I, b))
                assert_allclose(f.solve_LD(b),
                                np.dot((L * D).todense().I, b))
                assert_allclose(f.solve_DLt(b),
                                np.dot((D * L.T).todense().I, b))
                assert_allclose(f.solve_L(b),
                                np.dot(L.todense().I, b))
                assert_allclose(f.solve_Lt(b),
                                np.dot(L.T.todense().I, b))
                assert_allclose(f.solve_D(b),
                                np.dot(D.todense().I, b))

                assert_allclose(f.apply_P(b), b[f.P(), :])
                assert_allclose(f.apply_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert_allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])