Example #1
0
def test_almost_equal(vector_arrays, tolerances, norms):
    v1, v2 = vector_arrays
    rtol, atol = tolerances
    n, o = norms
    try:
        dv1 = v1.to_numpy()
        dv2 = v2.to_numpy()
    except NotImplementedError:
        dv1 = dv2 = None
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        try:
            r = almost_equal(v1[ind1], v2[ind2], norm=n, rtol=rtol, atol=atol)
        except NotImplementedError as e:
            if n == 'l1':
                pytest.xfail('l1_norm not implemented')
            else:
                raise e
        assert isinstance(r, np.ndarray)
        assert r.shape == (v1.len_ind(ind1), )
        if dv1 is not None:
            if dv2.shape[1] == 0:
                continue
            assert np.all(r == (
                np.linalg.norm(
                    indexed(dv1, ind1) -
                    indexed(dv2, ind2), ord=o, axis=1) <= atol +
                rtol * np.linalg.norm(indexed(dv2, ind2), ord=o, axis=1)))
Example #2
0
def test_pairwise_apply2(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    for U_ind, V_ind in valid_inds_of_same_length(U, V):
        M = op.pairwise_apply2(V[V_ind], U[U_ind], mu=mu)
        assert M.shape == (V.len_ind(V_ind), )
        M2 = V[V_ind].pairwise_inner(op.apply(U[U_ind], mu=mu))
        assert np.allclose(M, M2)
Example #3
0
def test_almost_equal_incompatible(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        for n in ['sup', 'l1', 'l2']:
            c1, c2 = v1.copy(), v2.copy()
            with pytest.raises(Exception):
                almost_equal(c1[ind1], c2[ind2], norm=n)
Example #4
0
def test_almost_equal(vector_arrays, tolerances, sup_norm):
    v1, v2 = vector_arrays
    rtol, atol = tolerances
    try:
        dv1 = v1.to_numpy()
        dv2 = v2.to_numpy()
    except NotImplementedError:
        dv1 = dv2 = None
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        r = almost_equal(v1[ind1],
                         v2[ind2],
                         sup_norm=sup_norm,
                         rtol=rtol,
                         atol=atol)
        assert isinstance(r, np.ndarray)
        assert r.shape == (v1.len_ind(ind1), )
        if dv1 is not None:
            if dv2.shape[1] == 0:
                continue
            assert np.all(
                r == (np.linalg.norm(indexed(dv1, ind1) - indexed(dv2, ind2),
                                     ord=np.inf if sup_norm else None,
                                     axis=1) <= atol +
                      rtol * np.linalg.norm(indexed(dv2, ind2),
                                            ord=np.inf if sup_norm else None,
                                            axis=1)))
Example #5
0
def test_almost_equal_incompatible(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        for sup_norm in (False, True):
            c1, c2 = v1.copy(), v2.copy()
            with pytest.raises(Exception):
                almost_equal(c1[ind1], c2[ind2], sup_norm=sup_norm)
Example #6
0
def test_pairwise_inner_incompatible(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in pyst.valid_inds_of_same_length(v1,
                                                     v2,
                                                     random_module=False):
        c1, c2 = v1.copy(), v2.copy()
        with pytest.raises(Exception):
            c1[ind1].pairwise_inner(c2[ind2])
Example #7
0
def test_axpy_wrong_coefficients(vector_arrays, random):
    v1, v2 = vector_arrays
    # TODO data input from hypothesis strategy
    for ind1, ind2 in pyst.valid_inds_of_same_length(v1, v2):
        for alpha in ([
                np.array([]),
                np.eye(v1.len_ind(ind1)),
                np.random.random(v1.len_ind(ind1) + 1)
        ] if v1.len_ind(ind1) > 0 else [np.random.random(1)]):
            with pytest.raises(Exception):
                v1[ind1].axpy(alpha, v2[ind2])
Example #8
0
def test_axpy(vector_arrays, random):
    v1, v2 = vector_arrays

    for ind1, ind2 in pyst.valid_inds_of_same_length(v1,
                                                     v2,
                                                     random_module=False):
        if v1.len_ind(ind1) != v1.len_ind_unique(ind1):
            with pytest.raises(Exception):
                c1, c2 = v1.copy(), v2.copy()
                c1[ind1].axpy(0., c2[ind2])
            continue

        ind1_complement = ind_complement(v1, ind1)
        c1, c2 = v1.copy(), v2.copy()
        c1[ind1].axpy(0., c2[ind2])
        assert len(c1) == len(v1)
        assert np.all(almost_equal(c1, v1))
        assert np.all(almost_equal(c2, v2))

        for a in (1., 1.4, np.random.random(v1.len_ind(ind1))):
            c1, c2 = v1.copy(), v2.copy()
            c1[ind1].axpy(a, c2[ind2])
            assert len(c1) == len(v1)
            assert np.all(
                almost_equal(c1[ind1_complement], v1[ind1_complement]))
            assert np.all(almost_equal(c2, v2))
            assert np.all(c1[ind1].sup_norm() <= v1[ind1].sup_norm() +
                          abs(a) * v2[ind2].sup_norm() * (1. + 1e-10))
            assert np.all(c1[ind1].norm() <=
                          (v1[ind1].norm() + abs(a) * v2[ind2].norm()) *
                          (1. + 1e-10))
            try:
                x = v1.to_numpy(True).astype(
                    complex)  # ensure that inplace addition works
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(v2.to_numpy(), ind2) * a
                else:
                    if isinstance(a, np.ndarray):
                        aa = a[:, np.newaxis]
                    else:
                        aa = a
                    x[ind1] += indexed(v2.to_numpy(), ind2) * aa
                assert np.allclose(c1.to_numpy(), x)
            except NotImplementedError:
                pass
            c1[ind1].axpy(-a, c2[ind2])
            assert len(c1) == len(v1)
            assert np.all(almost_equal(c1, v1))
Example #9
0
def test_almost_equal_product(operator_with_arrays_and_products):
    _, _, v1, _, product, _ = operator_with_arrays_and_products
    if len(v1) < 2:
        return
    v2 = v1.empty()
    v2.append(v1[:len(v1) // 2])
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5,
                                                                      1e-8)):

            r = almost_equal(v1[ind1], v2[ind2], product=product)
            assert isinstance(r, np.ndarray)
            assert r.shape == (v1.len_ind(ind1), )
            assert np.all(
                r == ((v1[ind1] - v2[ind2]).norm(product) <= atol + rtol *
                      (v2[ind2]).norm(product)))
Example #10
0
def test_pairwise_inner(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in pyst.valid_inds_of_same_length(v1, v2):
        r = v1[ind1].pairwise_inner(v2[ind2])
        assert isinstance(r, np.ndarray)
        assert r.shape == (v1.len_ind(ind1), )
        r2 = v2[ind2].pairwise_inner(v1[ind1])
        assert np.allclose, (r, r2)
        assert np.all(r <= v1[ind1].norm() * v2[ind2].norm() * (1. + 1e-10))
        try:
            assert np.allclose(
                r,
                np.sum(indexed(v1.to_numpy(), ind1).conj() *
                       indexed(v2.to_numpy(), ind2),
                       axis=1))
        except NotImplementedError:
            pass
Example #11
0
def test_axpy_incompatible(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in pyst.valid_inds_of_same_length(v1,
                                                     v2,
                                                     random_module=False):
        c1, c2 = v1.copy(), v2.copy()
        with pytest.raises(Exception):
            c1[ind1].axpy(0., c2[ind2])
        c1, c2 = v1.copy(), v2.copy()
        with pytest.raises(Exception):
            c1[ind1].axpy(1., c2[ind2])
        c1, c2 = v1.copy(), v2.copy()
        with pytest.raises(Exception):
            c1[ind1].axpy(-1., c2[ind2])
        c1, c2 = v1.copy(), v2.copy()
        with pytest.raises(Exception):
            c1[ind1].axpy(1.42, c2[ind2])