コード例 #1
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_with_transpose():
    shape_x = (8, 6)
    block_shape_x = (2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.3, format='bcoo').T
    x_d = x.todense()

    shape_y = (8, 4, 6)
    block_shape_y = (2, 2, 2)
    y = sparse.brandom(shape_y, block_shape_y, 0.3, format='bcoo').transpose(
        (2, 0, 1))
    y_d = y.todense()

    c = bcalc.einsum("ij,ijk->k", x, y, DEBUG=False)
    elemC = np.einsum("ij,ijk->k", x_d, y_d)
    assert_eq(elemC, c)

    shape_x = (8, 4, 9)
    block_shape_x = (1, 2, 3)
    x = sparse.brandom(shape_x, block_shape_x, 0.2, format='bcoo').transpose(
        (1, 0, 2))
    x_d = x.todense()

    shape_y = (9, 4, 6)
    block_shape_y = (3, 2, 2)
    y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo').T
    y_d = y.todense()

    c = bcalc.einsum("jik,nmk->minj", x, y, DEBUG=False)
    elemC = np.einsum("jik,nmk->minj", x_d, y_d)
    assert_eq(elemC, c)

    c = bcalc.einsum("jik,nmk->ijmn", x, y, DEBUG=False)
    elemC = np.einsum("jik,nmk->ijmn", x_d, y_d)
    assert_eq(elemC, c)
コード例 #2
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_as_transpose():
    # swap axes
    shape_y = (8, 4, 6)
    block_shape_y = (2, 2, 2)
    y = sparse.brandom(shape_y, block_shape_y, 0.3, format='bcoo').transpose(
        (2, 0, 1))
    y_d = y.todense()

    c = bcalc.einsum("ijk->kji", y)
    elemC = np.einsum("ijk->kji", y_d)
    assert_eq(elemC, c)

    c = bcalc.einsum("ijk->ijk", y)
    elemC = np.einsum("ijk->ijk", y_d)
    assert_eq(elemC, c)
コード例 #3
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_normal_einsum(shape_x, block_shape_x, shape_y, block_shape_y, descr):
    x = sparse.brandom(shape_x, block_shape_x, 0.3, format='bcoo')
    x_d = x.todense()

    y = sparse.brandom(shape_y, block_shape_y, 0.3, format='bcoo')
    y_d = y.todense()

    c = bcalc.einsum(descr, x, y, DEBUG=False)
    elemC = np.einsum(descr, x_d, y_d)
    assert_eq(elemC, c)
コード例 #4
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_inner_contraction():
    shape_x = (8, 6)
    block_shape_x = (2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.3, format='bcoo')
    x_d = x.todense()

    shape_y = (8, 6, 6)
    block_shape_y = (2, 2, 2)
    y = sparse.brandom(shape_y, block_shape_y, 0.3, format='bcoo')
    y_d = y.todense()

    elemC = np.einsum("in,ijj->n", x_d, y_d)
    with pytest.raises(NotImplementedError):
        c = bcalc.einsum("in,ijj->n", x, y, DEBUG=False)
        assert_eq(elemC, c)

    elemC = np.einsum("ijj->i", y_d)
    with pytest.raises(NotImplementedError):
        c = bcalc.einsum("ijj->i", y, DEBUG=False)
        assert_eq(elemC, c)
コード例 #5
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_mix_types():
    shape_x = (5, 1, 4, 2, 3)
    block_shape_x = (1, 1, 2, 2, 3)
    x = sparse.brandom(shape_x, block_shape_x, 0.5)
    x.data = x.data + 1j
    x_d = x.todense()

    shape_y = (5, 1, 11)
    block_shape_y = (1, 1, 1)
    y = sparse.brandom(shape_y, block_shape_y, 0.5)
    y.data = y.data.astype(np.float32)
    y_d = y.todense()

    c = bcalc.einsum('ijklm,ijn->lnmk', x, y)
    elemC = np.einsum('ijklm,ijn->lnmk', x_d, y_d)
    assert_eq(elemC, c)

    c = bcalc.einsum('ijklm,ijn->lnmk', x_d, y)
    elemC = np.einsum('ijklm,ijn->lnmk', x_d, y_d)
    assert_eq(elemC, c)
コード例 #6
0
def test_einsum_shape_error():
    # test for shape error while outer_shape is ok
    shape_x = (4, 2, 2)
    block_shape_x = (2, 2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.5, format='bcoo')
    x_d = x.todense()

    shape_y = (1, 1)
    block_shape_y = (1, 1)
    y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo')
    y_d = y.todense()

    shape_z = (2, 1)
    block_shape_z = (2, 1)
    z = sparse.brandom(shape_z, block_shape_z, 0.5, format='bcoo')
    z_d = z.todense()

    #FIXME:
    with pytest.raises(RuntimeError):
        c = bcalc.einsum('kxy,yz,zx->k', x, y, z)

    # test for block shape error
    shape_x = (4, 2, 2)
    block_shape_x = (2, 2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.5, format='bcoo')
    x_d = x.todense()

    shape_y = (2, 2)
    block_shape_y = (1, 1)
    y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo')
    y_d = y.todense()

    shape_z = (2, 2)
    block_shape_z = (2, 1)
    z = sparse.brandom(shape_z, block_shape_z, 0.5, format='bcoo')
    z_d = z.todense()

    #FIXME:
    with pytest.raises(ValueError):
        c = bcalc.einsum('kxy,yz,zx->k', x, y, z)
コード例 #7
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_broadcast():
    shape_x = (8, 6)
    block_shape_x = (2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.3, format='bcoo')
    x_d = x.todense()

    shape_y = (8, 6, 6)
    block_shape_y = (2, 2, 2)
    y = sparse.brandom(shape_y, block_shape_y, 0.3, format='bcoo')
    y_d = y.todense()

    elemC = np.einsum("ij,klm->ijklm", x_d, y_d)
    c = bcalc.einsum("ij,klm->ijklm", x, y, DEBUG=False)
    assert_eq(elemC, c)
コード例 #8
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_zeros():
    shape_x = (5, 1, 4, 2, 3)
    block_shape_x = (1, 1, 2, 2, 3)
    x = sparse.bcoo.zeros(shape_x, np.complex, block_shape_x)
    x_d = x.todense()

    shape_y = (5, 1, 11)
    block_shape_y = (1, 1, 1)
    y = sparse.bcoo.zeros(shape_y, np.float32, block_shape_y)
    y_d = y.todense()

    c = bcalc.einsum('ijklm,ijn->lnmk', x, y)
    elemC = np.einsum('ijklm,ijn->lnmk', x_d, y_d)
    assert_eq(elemC, c)
コード例 #9
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_misc():
    shape_x = (4, 2, 2)
    block_shape_x = (2, 2, 2)
    x = sparse.brandom(shape_x, block_shape_x, 0.5, format='bcoo')
    x_d = x.todense()

    shape_y = (2, 2)
    block_shape_y = (2, 1)
    y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo')
    y_d = y.todense()

    shape_z = (2, 2)
    block_shape_z = (1, 2)
    z = sparse.brandom(shape_z, block_shape_z, 0.5, format='bcoo')
    z_d = z.todense()

    c = bcalc.einsum('kxy,yz->kxz', x, y)
    elemC = np.einsum('kxy,yz->kxz', x_d, y_d)
    assert_eq(elemC, c)

    c = bcalc.einsum('kxy,yz,zx->k', x, y, z)
    elemC = np.einsum('kxy,yz,zx->k', x_d, y_d, z_d)
    assert_eq(elemC, c)
コード例 #10
0
ファイル: test_block_einsum.py プロジェクト: zhcui/sparse
def test_einsum_multi_tensor():
    shape_x = (12, 8, 9)
    block_shape_x = (2, 4, 3)
    x = sparse.brandom(shape_x, block_shape_x, 0.5, format='bcoo')
    x_d = x.todense()

    shape_y = (9, 5)
    block_shape_y = (3, 1)
    y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo')
    y_d = y.todense()

    shape_z = (5, 8)
    block_shape_z = (1, 4)
    z = sparse.brandom(shape_z, block_shape_z, 0.5, format='bcoo')
    z_d = z.todense()

    c = bcalc.einsum('kxy,yz,zx->k', x, y, z)
    elemC = np.einsum('kxy,yz,zx->k', x_d, y_d, z_d)
    assert_eq(elemC, c)
コード例 #11
0
def test_block_svd():

    #np.set_printoptions(3, linewidth = 1000, suppress = True)
    np.set_printoptions(2, linewidth=1000, suppress=False)
    x = sparse.brandom((16, 8), (2, 2), 0.3, format='bcoo')
    '''
    a = np.zeros((8, 4))
    a[0:2, 2:4] = np.arange(1, 5).reshape((2, 2))
    a[4:8, 0:2] = np.arange(1, 9).reshape((4, 2))
    x = BCOO.from_numpy(a, block_shape = (2, 2)) 
    '''
    y = x.todense()
    u, sigma, vt = bcore.block_svd(x)

    xnew = bcalc.einsum("ij,jk,kl->il", u, sigma, vt)

    assert np.allclose(x.todense(), xnew.todense())

    u_d = u.todense()
    vt_d = vt.todense()

    u_np, sigma_np, vt_np = np.linalg.svd(y, full_matrices=True)

    print "u sp"
    print u_d
    print "vt sp"
    print vt_d
    print sigma_np
    print np.sqrt(y.T.dot(y).dot(vt_np.T) / (vt_np.T))
    print "u np"
    print u_np
    print "vt np"
    print vt_np
    print "uT A v"
    print u_d.T.dot(y.dot(vt_d.T))
    print sigma.todense()
    print sigma.coords
コード例 #12
0
def dot1(shape_x, block_shape_x, shape_y, block_shape_y):
    x = sparse.brandom(shape_x, block_shape_x, .3, format='bcoo')
    y = sparse.brandom(shape_y, block_shape_y, .3, format='bcoo')
    c = bcalc.einsum("ij,pjk->pik", x, y, DEBUG=False)