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_add_mismatch_block(): 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_tobsr(): data = np.arange(1, 7).repeat(4).reshape((-1, 2, 2)) coords = np.array([[0, 0, 0, 2, 1, 2], [0, 1, 1, 0, 2, 2]]) block_shape = (2, 2) shape = (8, 6) print coords print data #x = BCOO(coords, data=data, shape=shape, block_shape=block_shape, sorted = True, has_duplicates = False) x = BCOO(coords, data=data, shape=shape, block_shape=block_shape, sorted=True, has_duplicates=False) y = x.todense() #print y #print x.coords z = x.tobsr() #print z.data #print z.has_canonical_format #print z.has_sorted_indices x2 = BCOO(coords.copy(), data=data.copy(), shape=shape, block_shape=block_shape, sorted=False, has_duplicates=True) y2 = x2.todense() print y print y2 print x.coords print x2.coords #print y #print x.coords z2 = x2.tobsr() #print z2.data print z.has_canonical_format print z.has_sorted_indices print z2.has_canonical_format print z2.has_sorted_indices #print "z" print z.toarray() #print "z2" print z2.toarray() #assert_eq(z,z2) exit() assert_eq(z, y)
def test_add_simple(): 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_tobsr(): data = np.arange(1, 7).repeat(4).reshape((-1, 2, 2)) coords = np.array([[0, 0, 0, 2, 1, 2], [0, 1, 1, 0, 2, 2]]) block_shape = (2, 2) shape = (8, 6) x = BCOO(coords, data=data, shape=shape, block_shape=block_shape) y = x.todense() z = x.tobsr() assert_eq(z, y)
def test_add_zero(): # the result is zero 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() y = -x y_d = y.todense() z = x + y z_d = x_d + y_d assert_eq(z, z_d) # all add numbers are zero a = np.zeros((6, 5, 4, 1), dtype=np.complex) x = BCOO.from_numpy(a, block_shape=(2, 5, 2, 1)) x_d = x.todense() y = BCOO.from_numpy(a, block_shape=(2, 5, 2, 1)) y_d = y.todense() z = x + y z_d = x_d + y_d assert_eq(z, z_d)
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_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)
# else: # if beta == 0: # c[:] = 0 # else: # 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()