Exemplo n.º 1
0
    def test_against_dense(self):
        A, B, C, D = (
            rand_tensor([3, 5, 5], 'aef'),
            rand_tensor([3, 5, 5], 'beg'),
            rand_tensor([3, 5, 5], 'cfh'),
            rand_tensor([3, 5, 5], 'dhg'),
        )

        tn = A & B & C & D
        tn_lo = tn.aslinearoperator(('a', 'b'), ('c', 'd'))
        tn_d = (tn ^ ...).fuse([('u', ['a', 'b']), ('l', ['c', 'd'])]).data

        u, s, v = svds(tn_lo, k=5, backend='scipy')
        ud, sd, vd = svds(tn_d, k=5, backend='scipy')

        assert_allclose(s, sd)
Exemplo n.º 2
0
 def test_svds_sparse_wvecs(self, mat_nherm_sparse, backend):
     u, v, a = mat_nherm_sparse
     uk, sk, vk = qu.svds(a, k=3, return_vecs=True, backend=backend)
     assert_allclose(sk, [4, 3, 2])
     for i, j in zip((0, 1, 2), (2, 3, 1)):
         o = abs(uk[:, [i]].H @ u[:, [j]])
         assert_allclose(o, 1.)
         o = abs(vk[[i], :] @ v[:, [j]])
         assert_allclose(o, 1.)
Exemplo n.º 3
0
 def test_svds_smalldense_wvecs(self, mat_nherm_dense, backend):
     u, v, a = mat_nherm_dense
     uk, sk, vk = svds(a, k=3, return_vecs=True, backend=backend)
     assert_allclose(sk, [4, 3, 2])
     for i, j in zip((0, 1, 2), (2, 3, 1)):
         o = abs(uk[:, i].H @ u[:, j])
         assert_allclose(o, 1.)
         o = abs(vk[i, :] @ v[:, j])
         assert_allclose(o, 1.)
Exemplo n.º 4
0
    def test_against_dense(self):
        A, B, C, D = (
            rand_tensor([3, 5, 5], 'aef'),
            rand_tensor([3, 5, 5], 'beg'),
            rand_tensor([3, 5, 5], 'cfh'),
            rand_tensor([3, 5, 5], 'dhg'),
        )

        tn = A & B & C & D
        tn_lo = tn.aslinearoperator(('a', 'b'), ('c', 'd'))
        tn_d = tn.to_dense(['a', 'b'], ['c', 'd'])

        u, s, v = qu.svds(tn_lo, k=5, backend='scipy')
        ud, sd, vd = qu.svds(tn_d, k=5, backend='scipy')

        assert_allclose(s, sd)

        # test matmat
        X = np.random.randn(9, 8) + 1.0j * np.random.randn(9, 8)
        assert_allclose(tn_lo.dot(X), tn_d.dot(X))
Exemplo n.º 5
0
    def test_rsvd_adaptive(self, dtype, shape, q, p):
        X = rand_rank(*shape, 10, dtype=dtype)
        U, s, V = qu.rsvd(X, 1e-5, q=q, p=p, k_start=10)

        k = s.size
        assert 10 <= k <= 20

        assert U.dtype == dtype
        assert V.dtype == dtype

        assert_allclose(U.conj().T @ U, np.eye(k), rtol=1e-6, atol=1e-6)
        assert_allclose(V @ V.conj().T, np.eye(k), rtol=1e-6, atol=1e-6)

        Ue, se, Ve = qu.svds(X, k)
        act_err = qu.norm(X - usv2dense(U, s, V), 'fro')

        assert act_err < 1e-4

        assert_allclose(s[:k // 2], se[:k // 2], rtol=0.1)
Exemplo n.º 6
0
    def test_rsvd(self, dtype, shape, sparse, q, p):
        X = rand_rect(*shape, dtype=dtype, sparse=sparse)

        k = 15
        U, s, V = qu.rsvd(X, k, q=q, p=p)

        assert U.shape == (shape[0], k)
        assert s.shape == (k, )
        assert V.shape == (k, shape[1])

        assert U.dtype == dtype
        assert V.dtype == dtype

        assert_allclose(U.conj().T @ U, np.eye(k), rtol=1e-5, atol=1e-5)
        assert_allclose(V @ V.conj().T, np.eye(k), rtol=1e-5, atol=1e-5)

        Ue, se, Ve = qu.svds(X, k)
        opt_err = qu.norm(X.A - usv2dense(Ue, se, Ve), 'fro')
        act_err = qu.norm(X.A - usv2dense(U, s, V), 'fro')

        assert act_err < 1.2 * opt_err

        assert_allclose(s[:k // 2], se[:k // 2], rtol=0.05)
Exemplo n.º 7
0
 def test_svds_sparse_nvecs(self, mat_nherm_sparse, backend):
     _, _, a = mat_nherm_sparse
     sk = qu.svds(a, k=3, return_vecs=False, backend=backend)
     assert_allclose(sk, [4, 3, 2])