コード例 #1
0
    def IC(self):  # Incomplete Cholesky decomposition
        matrix = loadMatrix('identity')
        pcwrap = preconditioner.ICPreconditioner()
        pc = pcwrap.create_preconditioner(matrix)
        saveMatrix(pc.unfactored(), 'matrix.out')
        self.assert_(
            fp_file_compare('matrix.out', os.path.join(datadir, 'identity'),
                            1.e-8))

        ## NOTE: The reference matrices were generated by this
        ## program, not SparseLib++, because the SparseLib++ IC
        ## preconditioner seems to be broken...

        matrix = loadMatrix('small_sym_sparse2.mat')
        pc = pcwrap.create_preconditioner(matrix)
        # Save the *upper* triangle, even though the lower one is more
        # natural for the ICPreconditioner class. The reference file
        # generated by SparseLib++ contains the upper triangle.
        saveMatrix(pc.upper(), 'matrix.out')
        self.assert_(
            fp_file_compare('matrix.out',
                            os.path.join(datadir, 'small_sym_upper2.out'),
                            1.e-8))

        matrix = loadMatrix('small_sym_sparse.mat')
        pc = pcwrap.create_preconditioner(matrix)
        # Save the *upper* triangle, even though the lower one is more
        # natural for the ICPreconditioner class. The reference file
        # generated by SparseLib++ contains the upper triangle.
        saveMatrix(pc.upper(), 'matrix.out')
        self.assert_(
            fp_file_compare('matrix.out',
                            os.path.join(datadir, 'small_sym_upper.out'),
                            1.e-8))

        # Big matrices from Matrix Market. 'bcsstk07' has been
        # removed. It can't be factored, apparently due to a pathology
        # of the matrix or the algorithm.  It passes the complete
        # Cholesky test.
        for mtxname in (
                's1rmq4m1',
                'bcsstk01',
        ):
            print >> sys.stderr, mtxname + '.mtx'
            matrix = loadMatrix(mtxname + '.mtx', fortran=True, symmetric=True)
            pc = pcwrap.create_preconditioner(matrix)
            self.assert_(pc.upper().unique_indices())
            saveMatrix(pc.upper(), 'matrix.out')
            self.assert_(
                fp_file_compare('matrix.out',
                                os.path.join(datadir, mtxname + '_upper.out'),
                                1.e-8))
        file_utils.remove('matrix.out')
コード例 #2
0
ファイル: matrix_test.py プロジェクト: pk-organics/OOF3D
    def Cholesky(self):
        # Cholesky decomposition of dense symmetric positive definite matrices.
        matrix = loadMatrix('identity-full')
        pcwrap = preconditioner.ICPreconditioner()
        pc = pcwrap.create_preconditioner(matrix)
        saveMatrix(pc.unfactored(), 'matrix.out')
        self.assert_(fp_file_compare('matrix.out',
                                     os.path.join(datadir, 'identity-full'),
                                     1.e-8))

        matrix = loadMatrix('small_sym_dense.mat')
        pc = pcwrap.create_preconditioner(matrix)
        saveMatrix(pc.unfactored(), 'matrix.out')
        self.assert_(fp_file_compare(
                'matrix.out', os.path.join(datadir, 'small_sym_dense.mat'),
                1.e-8))
        # Check preconditioner solutions
        for vecname in ('rhs0', 'rhs1'):
            vec = loadVector(vecname)
            result = pc.solve(vec)
            result.save('vector.out')
            self.assert_(fp_file_compare('vector.out',
                                         os.path.join(datadir,
                                                      vecname+'_ic.out'),
                                         1.e-8))
            resid = matrix*result - vec
            self.assertAlmostEqual(resid.norm(), 0.0, 8)

        matrix = loadMatrix('small_sym_dense2.mat')
        pc = pcwrap.create_preconditioner(matrix)
        saveMatrix(pc.unfactored(), 'matrix.out')
        self.assert_(fp_file_compare(
                'matrix.out', os.path.join(datadir, 'small_sym_dense2.mat'),
                1.e-8))
        for vecname in ('rhs2.1', 'rhs2.2', 'rhs2.3'):
            vec = loadVector(vecname)
            result = pc.solve(vec)
            result.save('vector.out')
            self.assert_(fp_file_compare('vector.out',
                                         os.path.join(datadir,
                                                      vecname+'_ic.out'),
                                         1.e-8))
            resid = matrix*result - vec
            self.assertAlmostEqual(resid.norm(), 0.0, 8)
            
        saveMatrix(pc.upper(), 'matrix.out')
        self.assert_(fp_file_compare(
                'matrix.out', os.path.join(datadir,
                                           'small_sym_dense2_lower.out'),
                1.e-8))

        # Hilbert matrix, which is notoriously badly conditioned.
        size = 12               # test fails for size > 12!
        matrix = sparsemat.SparseMat(size, size)
        for i in range(size):
            for j in range(size):
                matrix.insert(i, j, 1./(i+j+1.0))
        matrix.consolidate()
        norm = 1.0/matrix.norm()
        pc = pcwrap.create_preconditioner(matrix)
        resid = pc.unfactored() - matrix
        for i,j,x in resid:
            self.assertAlmostEqual(x*norm, 0.0, 8)

        # Large Matrix market matrices
        for mtxname in ('bcsstk01.mtx', 'bcsstk07.mtx'):
            print >> sys.stderr,  mtxname
            matrix = loadMatrix(mtxname, fortran=True, symmetric=True)
            fillMatrix(matrix)
            matrix.consolidate()
            pc = pcwrap.create_preconditioner(matrix)
            unf = pc.unfactored()
            resid = matrix - unf
            resid.consolidate()
            norm = 1./matrix.norm()
            for i,j,x in resid:
                self.assertAlmostEqual(x*norm, 0.0, 8)
        
        file_utils.remove('matrix.out')
        file_utils.remove('vector.out')