def test_dot(): shape_x = (8, 6) block_shape_x = (2, 2) shape_y = (6, 4) block_shape_y = (2, 4) x = sparse.brandom(shape_x, block_shape_x, 0.6, format='bcoo') x_d = x.todense() y = sparse.brandom(shape_y, block_shape_y, 0.6, format='bcoo') y_d = y.todense() c = x.dot(y) elemC = x_d.dot(y_d) assert_eq(elemC, c) shape_x = (9, 4, 18) block_shape_x = (9, 2, 1) shape_y = (18, 8, 10, 12) block_shape_y = (1, 8, 1, 3) x = sparse.brandom(shape_x, block_shape_x, 0.6, format='bcoo') x_d = x.todense() y = sparse.brandom(shape_y, block_shape_y, 0.6, format='bcoo') y_d = y.todense() c = x.dot(y) elemC = x_d.dot(y_d) assert_eq(elemC, c)
def test_multiply(): x = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') x_d = x.todense() y = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') y_d = y.todense() z = x * y z_d = x_d * y_d assert_eq(z, z_d) data = np.arange(1, 4).repeat(4).reshape(3, 2, 2) coords = np.array([[1, 1, 0], [0, 1, 1]]) x = BCOO(coords, data=data, shape=(4, 4), block_shape=(2, 2)) x_d = x.todense() y = x * x y_d = x_d * x_d assert_eq(y, y_d) data_x = np.arange(1, 4).repeat(4).reshape(3, 2, 2) coords_x = np.array([[1, 1, 0], [0, 1, 1]]) x = BCOO(coords_x, data=data_x, shape=(4, 4), block_shape=(2, 2)) x_d = x.todense() data_y = np.array([[[-2.0, -2.5], [-2, -2]], [[-2, -2], [-2, -2]]]) coords_y = np.array([[0, 1], [0, 1]]) y = BCOO(coords_y, data=data_y, shape=(4, 4), block_shape=(2, 2)) y_d = y.todense() z = x * y z_d = x_d * y_d assert_eq(z, z_d)
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)
def test_add_multi_dim_array(): x = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') x_d = x.todense() y = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') y_d = y.todense() z = x + y z_d = x_d + y_d assert_eq(z, z_d)
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)
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)
def test_block_eigh(): def is_diagonal(A): return np.allclose(A - np.diag(np.diagonal(A)), 0.0) #np.set_printoptions(3, linewidth = 1000, suppress = True) #np.random.seed(0) x = sparse.brandom((16, 16), (4, 4), 0.2, format='bcoo') x += x.T y = x.todense() #eigval_sp, eigvec_sp = bcore.block_eigh(x)[1].todense() eigval_sp_bcoo, eigvec_sp_bcoo = bcore.block_eigh(x, block_sort=True) eigval_sp = eigval_sp_bcoo.todense() eigvec_sp = eigvec_sp_bcoo.todense() diagonalized_mat_sp = eigvec_sp.T.dot(x.todense().dot(eigvec_sp)) assert (is_diagonal(diagonalized_mat_sp)) eigval_np = np.linalg.eigh(x.todense())[0] assert (np.allclose(np.sort(np.diagonal(diagonalized_mat_sp)), eigval_np)) eigval_norm = np.array([np.linalg.norm(d) for d in eigval_sp_bcoo.data]) print eigval_norm eig = np.diag(diagonalized_mat_sp) print eig for i in range(0, len(eig), 4): print np.linalg.norm(eig[i:(i + 4)])
def test_subtraction(): x = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') x_d = x.todense() y = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') y_d = y.todense() z = x - y z_d = x_d - y_d assert_eq(z, z_d) data = np.arange(1, 4).repeat(4).reshape(3, 2, 2) coords = np.array([[1, 1, 0], [0, 1, 1]]) x = BCOO(coords, data=data, shape=(4, 4), block_shape=(2, 2)) x_d = x.todense() y = x - x y_d = x_d - x_d assert_eq(y, y_d)
def test_block_slicing(index, npindex): s = sparse.brandom((4, 9, 16), (2, 3, 4), density=0.5) x = s.todense() if callable(npindex): assert_eq(s.getblock(index), npindex(x)) else: assert_eq(s.getblock(index), x[npindex])
def test_random_shape_nnz(shape, block_shape, density): s = sparse.brandom(shape, block_shape, density, format='bdok') assert isinstance(s, BDOK) assert s.shape == shape expected_nnz = density * np.prod(shape) assert np.floor(expected_nnz) <= s.nnz <= np.ceil(expected_nnz)
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)
def test_asformat(): s = sparse.brandom((4, 6, 8), (2, 3, 4), 0.5, format='bdok') s2 = s.asformat('bcoo') #s2 = s.asformat('bdok') #s = sparse.brandom((4, 3), (2, 1), 0.5, format='bdok') #x = np.array([[1,-1,0,0],[1,-1,0,0],[2,2,3,3],[2,2,3,3]]) #s = BDOK(x, block_shape = (2, 2)) #s2 = s.asformat('bcoo') assert_eq(s, s2)
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)
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)
def test_scaling(): data_x = np.arange(3, 6).repeat(4).reshape(3, 2, 2).astype(np.double) coords_x = np.array([[1, 1, 0], [0, 1, 1]]) x = BCOO(coords_x, data=data_x, shape=(4, 4), block_shape=(2, 2)) x_d = x.todense() z = x * -3.1 z_d = x_d * -3.1 assert_eq(z, z_d) x = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') x_d = x.todense() scaling_factor = np.random.random() z = x * scaling_factor z_d = x_d * scaling_factor assert_eq(z, z_d) x = sparse.brandom((4, 2, 6), (2, 1, 2), 0.5, format='bcoo') x_d = x.todense() scaling_factor = 0 z = x * scaling_factor z_d = x_d * scaling_factor assert_eq(z, z_d)
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)
def test_setitem(): s = sparse.brandom((6, 6, 8), (2, 3, 4), 0.5, format='bdok') x = s.todense() x_b = s.to_bumpy() shape = (3, 2, 2) value = np.random.random((2, 3, 4)) idx = np.random.randint(np.prod(shape)) idx = np.unravel_index(idx, shape) s[idx] = value x_b[idx] = value assert_eq(x_b.todense(), s.todense())
def test_bcoo_related(): s = sparse.brandom((10, 10), (2, 2), 0.3, format='bdok') s2 = s.asformat('bcoo') #s2 = s.asformat('bdok') #s = sparse.brandom((4, 3), (2, 1), 0.5, format='bdok') #x = np.array([[1,-1,0,0],[1,-1,0,0],[2,2,3,3],[2,2,3,3]]) #s = BDOK(x, block_shape = (2, 2)) #s2 = s.asformat('bcoo') print s.data exit() assert_eq(s, s2)
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)
def test_getitem(): s = sparse.brandom((4, 6, 8), (2, 3, 4), 0.5, format='bdok') x = s.todense() x_b = s.to_bumpy() shape = (2, 2, 2) #x = np.array([[1,-1,0,0],[1,-1,0,0],[2,2,3,3],[2,2,3,3]]) #s = BDOK(x, (2, 2)) #x_b = s.to_bumpy() #shape = (2,2) for _ in range(s.nnz): idx = np.random.randint(np.prod(shape)) idx = np.unravel_index(idx, shape) assert np.allclose(s[idx], x_b[idx])
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
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)
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)
def test_convert_to_numpy(): s = sparse.brandom((4, 6, 8), (2, 3, 4), 0.5, format='bdok') x = s.todense() assert_eq(x, s)
def test_convert_from_coo(): s1 = sparse.brandom((6, 6, 12), (2, 2, 3), 0.5, format='bcoo') s2 = BDOK(s1) assert_eq(s1, s2)
def test_convert_to_coo(): s1 = sparse.brandom((4, 6, 8), (2, 3, 4), 0.5, format='bdok') s2 = sparse.BCOO(s1) assert_eq(s1, s2)
def test_transpose(axis): x = sparse.brandom((6, 2, 4), (2, 2, 2), density=0.3) y = x.todense() xx = x.transpose(axis) yy = y.transpose(axis) assert_eq(xx, yy)
def test_slicing(index): s = sparse.brandom((4, 9, 16), (2, 3, 4), density=0.5) x = s.todense() assert_eq(x[index], s[index])
# c *= beta # c += ab # return c if __name__ == '__main__': data_x = np.arange(1, 7).repeat(4).reshape((-1, 2, 2)) coords_x = np.array([[0, 0, 0, 2, 1, 2], [0, 1, 1, 0, 2, 2]]) shape_x = (8, 6) block_shape_x = (2, 2) x = BCOO(coords_x, data=data_x, shape=shape_x, block_shape=block_shape_x) x_d = x.todense() shape_y = (8, 4, 6) block_shape_y = (2, 2, 2) y = sparse.brandom(shape_y, block_shape_y, 0.5, format='bcoo') y_d = y.todense() c = einsum("ij,ikj->k", x, y, DEBUG=True) elemC = np.einsum("ij,ikj->k", x_d, y_d) # another test ''' shape_x = (8,4,9) block_shape_x = (1,2,3) x = sparse.brandom(shape_x, block_shape_x, 0.2, format='bcoo') 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')
def test_invalid_shape_error(): with pytest.raises(RuntimeError): sparse.brandom((3, 4), block_shape=(2, 3), format='bcoo')