Esempio n. 1
0
 def _help_product_norm_fast(self, A, B):
     # for profiling
     t = 2
     itmax = 5
     D = MatrixProductOperator(A, B)
     est, v, w, nmults, nresamples = _onenormest_core(D, D.T, t, itmax)
     return est
Esempio n. 2
0
 def _help_product_norm_fast(self, A, B):
     # for profiling
     t = 2
     itmax = 5
     D = MatrixProductOperator(A, B)
     est, v, w, nmults, nresamples = _onenormest_core(D, D.T, t, itmax)
     return est
Esempio n. 3
0
def resolvent_onenormest(t, T, Z, eps_recip):
    op = get_resolvent_operator(T, Z, eps_recip)
    #return scipy.sparse.linalg.onenormest(op, t=t)
    stuff = _onenormest_core(op, op.H, t=t, itmax=5)
    #stuff = _onenormest_core(op, op.H, t=t, itmax=10)
    est, v, w, nmults, nresamples = stuff
    #print(nmults, nresamples)
    #assert_equal(v.dtype, np.dtype(float))
    #assert_equal(w.dtype, np.dtype(complex))
    return est
Esempio n. 4
0
    def test_onenormest_table_6_t_1(self):
        #TODO this test seems to give estimates that match the table,
        #TODO even though no attempt has been made to deal with
        #TODO complex numbers in the one-norm estimation.
        # This will take multiple seconds if your computer is slow like mine.
        # It is stochastic, so the tolerance could be too strict.
        np.random.seed(1234)
        t = 1
        n = 100
        itmax = 5
        nsamples = 5000
        observed = []
        expected = []
        nmult_list = []
        nresample_list = []
        for i in range(nsamples):
            A_inv = np.random.rand(n, n) + 1j * np.random.rand(n, n)
            A = scipy.linalg.inv(A_inv)
            est, v, w, nmults, nresamples = _onenormest_core(A, A.T, t, itmax)
            observed.append(est)
            expected.append(scipy.linalg.norm(A, 1))
            nmult_list.append(nmults)
            nresample_list.append(nresamples)
        observed = np.array(observed, dtype=float)
        expected = np.array(expected, dtype=float)
        relative_errors = np.abs(observed - expected) / expected

        # check the mean underestimation ratio
        underestimation_ratio = observed / expected
        underestimation_ratio_mean = np.mean(underestimation_ratio)
        assert_(0.90 < underestimation_ratio_mean < 0.99)

        # check the required column resamples
        max_nresamples = np.max(nresample_list)
        assert_equal(max_nresamples, 0)

        # check the proportion of norms computed exactly correctly
        nexact = np.count_nonzero(relative_errors < 1e-14)
        proportion_exact = nexact / float(nsamples)
        assert_(0.7 < proportion_exact < 0.8)

        # check the average number of matrix*vector multiplications
        mean_nmult = np.mean(nmult_list)
        assert_(4 < mean_nmult < 5)
Esempio n. 5
0
    def test_onenormest_table_6_t_1(self):
        #TODO this test seems to give estimates that match the table,
        #TODO even though no attempt has been made to deal with
        #TODO complex numbers in the one-norm estimation.
        # This will take multiple seconds if your computer is slow like mine.
        # It is stochastic, so the tolerance could be too strict.
        np.random.seed(1234)
        t = 1
        n = 100
        itmax = 5
        nsamples = 5000
        observed = []
        expected = []
        nmult_list = []
        nresample_list = []
        for i in range(nsamples):
            A_inv = np.random.rand(n, n) + 1j * np.random.rand(n, n)
            A = scipy.linalg.inv(A_inv)
            est, v, w, nmults, nresamples = _onenormest_core(A, A.T, t, itmax)
            observed.append(est)
            expected.append(scipy.linalg.norm(A, 1))
            nmult_list.append(nmults)
            nresample_list.append(nresamples)
        observed = np.array(observed, dtype=float)
        expected = np.array(expected, dtype=float)
        relative_errors = np.abs(observed - expected) / expected

        # check the mean underestimation ratio
        underestimation_ratio = observed / expected
        underestimation_ratio_mean = np.mean(underestimation_ratio)
        assert_(0.90 < underestimation_ratio_mean < 0.99)

        # check the required column resamples
        max_nresamples = np.max(nresample_list)
        assert_equal(max_nresamples, 0)

        # check the proportion of norms computed exactly correctly
        nexact = np.count_nonzero(relative_errors < 1e-14)
        proportion_exact = nexact / float(nsamples)
        assert_(0.7 < proportion_exact < 0.8)

        # check the average number of matrix*vector multiplications
        mean_nmult = np.mean(nmult_list)
        assert_(4 < mean_nmult < 5)
Esempio n. 6
0
 def test_onenormest_table_5_t_1(self):
     # "note that there is no randomness and hence only one estimate for t=1"
     t = 1
     n = 100
     itmax = 5
     alpha = 1 - 1e-6
     A = -scipy.linalg.inv(np.identity(n) + alpha * np.eye(n, k=1))
     first_col = np.array([1] + [0] * (n - 1))
     first_row = np.array([(-alpha)**i for i in range(n)])
     B = -scipy.linalg.toeplitz(first_col, first_row)
     assert_allclose(A, B)
     est, v, w, nmults, nresamples = _onenormest_core(B, B.T, t, itmax)
     exact_value = scipy.linalg.norm(B, 1)
     underest_ratio = est / exact_value
     assert_allclose(underest_ratio, 0.05, rtol=1e-4)
     assert_equal(nmults, 11)
     assert_equal(nresamples, 0)
     # check the non-underscored version of onenormest
     est_plain = scipy.sparse.linalg.onenormest(B, t=t, itmax=itmax)
     assert_allclose(est, est_plain)
Esempio n. 7
0
 def test_onenormest_table_5_t_1(self):
     # "note that there is no randomness and hence only one estimate for t=1"
     t = 1
     n = 100
     itmax = 5
     alpha = 1 - 1e-6
     A = -scipy.linalg.inv(np.identity(n) + alpha*np.eye(n, k=1))
     first_col = np.array([1] + [0]*(n-1))
     first_row = np.array([(-alpha)**i for i in range(n)])
     B = -scipy.linalg.toeplitz(first_col, first_row)
     assert_allclose(A, B)
     est, v, w, nmults, nresamples = _onenormest_core(B, B.T, t, itmax)
     exact_value = scipy.linalg.norm(B, 1)
     underest_ratio = est / exact_value
     assert_allclose(underest_ratio, 0.05, rtol=1e-4)
     assert_equal(nmults, 11)
     assert_equal(nresamples, 0)
     # check the non-underscored version of onenormest
     est_plain = scipy.sparse.linalg.onenormest(B, t=t, itmax=itmax)
     assert_allclose(est, est_plain)
Esempio n. 8
0
    def test_onenormest_table_3_t_2(self):
        # This will take multiple seconds if your computer is slow like mine.
        # It is stochastic, so the tolerance could be too strict.
        np.random.seed(1234)
        t = 2
        n = 100
        itmax = 5
        nsamples = 5000
        observed = []
        expected = []
        nmult_list = []
        nresample_list = []
        for i in range(nsamples):
            A = scipy.linalg.inv(np.random.randn(n, n))
            est, v, w, nmults, nresamples = _onenormest_core(A, A.T, t, itmax)
            observed.append(est)
            expected.append(scipy.linalg.norm(A, 1))
            nmult_list.append(nmults)
            nresample_list.append(nresamples)
        observed = np.array(observed, dtype=float)
        expected = np.array(expected, dtype=float)
        relative_errors = np.abs(observed - expected) / expected

        # check the mean underestimation ratio
        underestimation_ratio = observed / expected
        assert_(0.99 < np.mean(underestimation_ratio) < 1.0)

        # check the max and mean required column resamples
        assert_equal(np.max(nresample_list), 2)
        assert_(0.05 < np.mean(nresample_list) < 0.2)

        # check the proportion of norms computed exactly correctly
        nexact = np.count_nonzero(relative_errors < 1e-14)
        proportion_exact = nexact / float(nsamples)
        assert_(0.9 < proportion_exact < 0.95)

        # check the average number of matrix*vector multiplications
        assert_(3.5 < np.mean(nmult_list) < 4.5)
Esempio n. 9
0
    def test_onenormest_table_3_t_2(self):
        # This will take multiple seconds if your computer is slow like mine.
        # It is stochastic, so the tolerance could be too strict.
        np.random.seed(1234)
        t = 2
        n = 100
        itmax = 5
        nsamples = 5000
        observed = []
        expected = []
        nmult_list = []
        nresample_list = []
        for i in range(nsamples):
            A = scipy.linalg.inv(np.random.randn(n, n))
            est, v, w, nmults, nresamples = _onenormest_core(A, A.T, t, itmax)
            observed.append(est)
            expected.append(scipy.linalg.norm(A, 1))
            nmult_list.append(nmults)
            nresample_list.append(nresamples)
        observed = np.array(observed, dtype=float)
        expected = np.array(expected, dtype=float)
        relative_errors = np.abs(observed - expected) / expected

        # check the mean underestimation ratio
        underestimation_ratio = observed / expected
        assert_(0.99 < np.mean(underestimation_ratio) < 1.0)

        # check the max and mean required column resamples
        assert_equal(np.max(nresample_list), 2)
        assert_(0.05 < np.mean(nresample_list) < 0.2)

        # check the proportion of norms computed exactly correctly
        nexact = np.count_nonzero(relative_errors < 1e-14)
        proportion_exact = nexact / float(nsamples)
        assert_(0.9 < proportion_exact < 0.95)

        # check the average number of matrix*vector multiplications
        assert_(3.5 < np.mean(nmult_list) < 4.5)