Ejemplo n.º 1
0
    def test_gmres(self):
        # Ensure repeatability
        random.seed(0)
        
        #  For these small matrices, Householder and MGS GMRES should give the same result, 
        #  and for symmetric (but possibly indefinite) matrices CR and GMRES should give same result
        for maxiter in [1,2,3]:
            for case, symm_case in zip(self.cases, self.symm_cases):
                A = case['A'] 
                b = case['b']
                x0 = case['x0']
                A_symm = symm_case['A'] 
                b_symm = symm_case['b']
                x0_symm = symm_case['x0']
                
                # Test agreement between Householder and GMRES
                (x, flag) = gmres_householder(A,b,x0=x0,maxiter=min(A.shape[0],maxiter))
                (x2, flag2) = gmres_mgs(A,b,x0=x0,maxiter=min(A.shape[0],maxiter))
                assert_array_almost_equal(x/norm(x), x2/norm(x2), 
                        err_msg='Householder GMRES and MGS GMRES gave different results for small matrix')
                assert_equal(flag, flag2, 
                        err_msg='Householder GMRES and MGS GMRES returned different convergence flags for small matrix')

                # Test agreement between GMRES and CR
                if A_symm.shape[0] > 1:
                    residuals2 = []
                    (x2, flag2) = gmres_mgs(A_symm, b_symm, x0=x0_symm, maxiter=min(A.shape[0],maxiter),residuals=residuals2)
                    residuals3 = []
                    (x3, flag2) = cr(A_symm, b_symm, x0=x0_symm, maxiter=min(A.shape[0],maxiter),residuals=residuals3)
                    residuals2 = array(residuals2)
                    residuals3 = array(residuals3)
                    assert_array_almost_equal(residuals3/norm(residuals3), residuals2/norm(residuals2), 
                            err_msg='CR and GMRES yield different residual vectors')
                    assert_array_almost_equal(x2/norm(x2), x3/norm(x3), err_msg='CR and GMRES yield different answers')
Ejemplo n.º 2
0
    def test_gmres(self):
        # Ensure repeatability
        random.seed(0)

        #  For these small matrices, Householder and MGS GMRES should give the
        #  same result, and for symmetric (but possibly indefinite) matrices CR
        #  and GMRES should give same result
        for maxiter in [1, 2, 3]:
            for case, symm_case in zip(self.cases, self.symm_cases):
                A = case['A']
                b = case['b']
                x0 = case['x0']
                A_symm = symm_case['A']
                b_symm = symm_case['b']
                x0_symm = symm_case['x0']

                # Test agreement between Householder and GMRES
                (x, flag) = gmres_householder(A, b, x0=x0,
                                              maxiter=min(A.shape[0], maxiter))
                (x2, flag2) = gmres_mgs(A, b, x0=x0, maxiter=min(A.shape[0],
                                        maxiter))
                err_msg = ('Householder GMRES and MGS GMRES gave '
                           'different results for small matrix')
                assert_array_almost_equal(x/norm(x), x2/norm(x2),
                                          err_msg=err_msg)

                err_msg = ('Householder GMRES and MGS GMRES returned '
                           'different convergence flags for small matrix')
                assert_equal(flag, flag2, err_msg=err_msg)

                # Test agreement between GMRES and CR
                if A_symm.shape[0] > 1:
                    residuals2 = []
                    (x2, flag2) = gmres_mgs(A_symm, b_symm, x0=x0_symm,
                                            maxiter=min(A.shape[0], maxiter),
                                            residuals=residuals2)
                    residuals3 = []
                    (x3, flag2) = cr(A_symm, b_symm, x0=x0_symm,
                                     maxiter=min(A.shape[0], maxiter),
                                     residuals=residuals3)
                    residuals2 = array(residuals2)
                    residuals3 = array(residuals3)

                    err_msg = 'CR and GMRES yield different residual vectors'
                    assert_array_almost_equal(residuals3/norm(residuals3),
                                              residuals2/norm(residuals2),
                                              err_msg=err_msg)

                    err_msg = 'CR and GMRES yield different answers'
                    assert_array_almost_equal(x2/norm(x2), x3/norm(x3),
                                              err_msg=err_msg)
Ejemplo n.º 3
0
    def test_gmres(self):
        for case in self.cases:
            A = case['A']
            b = case['b']
            x0 = case['x0']
            tol = case['tol']

            mgsres = []
            _ = gmres_mgs(A,
                          b,
                          x0,
                          residuals=mgsres,
                          tol=tol,
                          restrt=3,
                          maxiter=2)

            hhres = []
            _ = gmres_householder(A,
                                  b,
                                  x0,
                                  residuals=hhres,
                                  tol=tol,
                                  restrt=3,
                                  maxiter=2)

            scipyres = []
            normb = np.linalg.norm(b)

            def cb(x):
                scipyres.append(x * normb)

            _ = sla.isolve.iterative.gmres(A,
                                           b,
                                           x0,
                                           callback=cb,
                                           callback_type='pr_norm',
                                           tol=tol,
                                           atol=0,
                                           restrt=3,
                                           maxiter=2)

            assert_array_almost_equal(mgsres[1:], scipyres)
            assert_array_almost_equal(hhres[1:], scipyres)