def test_broadcast(): a = np.array([[1, 2, 0, 0], [0, 3, 0, 0], [4, 5, 6, 0], [8, 0, 9, 0]]) b = np.broadcast_to(a, (3, 4, 4)) x = BCOO.from_numpy(a, block_shape=(2, 2)) y = x.broadcast_to((3, 4, 4), (3, 2, 2)) assert_eq(b, y) a = np.random.random((6, 5, 1, 4, 1)) a[a > 0.3] = 0.0 b = np.broadcast_to(a, (4, 6, 5, 3, 4, 4)) x = BCOO.from_numpy(a, block_shape=(3, 5, 1, 2, 1)) y = x.broadcast_to((4, 6, 5, 3, 4, 4), (2, 3, 5, 3, 2, 1)) assert_eq(b, y)
def test_from_numpy(): #a = np.random.random((6,5,4,1)) #a = np.zeros((6,5,4,1)) a = np.array([[1, 2, 0, 0], [0, 3, 0, 0], [4, 5, 6, 0], [8, 0, 9, 0]]) #x = BCOO.from_numpy(a, block_shape = (2,5,2,1)) x = BCOO.from_numpy(a, block_shape=(2, 2)) assert_eq(a, x)
def test_to_coo(): a = np.random.random((6, 5, 4, 1)) a[a > 0.3] = 0.0 #a = np.zeros((6,5,4,1)) x = BCOO.from_numpy(a, block_shape=(2, 5, 2, 1)) from sparse import COO y = COO.from_numpy(a) z = x.to_coo() assert_eq(y, z)
def test_transpose(axes): #x = sparse.brandom((2, 3, 4), ,density=.25) #x = sparse.brandom((4, 6, 8), (2, 3, 4), 0.5, format='bcoo') #x = sparse.brandom((4, 4), (2, 2), 1.0, format='bcoo') a = np.array([[1, -1, 0, 0], [1, -1, 0, 0], [2, 3, 6, 7], [4, 5, 8, 9]]) x = BCOO.from_numpy(a, block_shape=(2, 2)) y = x.todense() xx = x.transpose(axes) yy = y.transpose(axes) assert_eq(xx, yy)
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_block_reshape(): a = np.array([[1, -1, 0, 0], [1, -1, 0, 0], [2, 3, 6, 7], [4, 5, 8, 9]]) x = BCOO.from_numpy(a, block_shape=(2, 2)) y = x.todense() outer_shape_new = (1, 4) block_shape_new = (2, 2) # unchanged z = x.block_reshape(outer_shape_new, block_shape_new) print("original matrix (2,2)") print(y) print("block reshaped matrix (1,4)") print(z.todense())
def test_sizeof(): import sys x = np.eye(100) y = BCOO.from_numpy(x, block_shape=(5, 5)) nb = sys.getsizeof(y) assert 400 < nb < x.nbytes / 10
def test_from_numpy(): #a = np.random.random((6,5,4,1)) a = np.zeros((6, 5, 4, 1)) x = BCOO.from_numpy(a, block_shape=(2, 5, 2, 1)) assert_eq(a, x)