def test_trinary_broadcasting(shapes, func): args = [sparse.random(s, density=0.5) for s in shapes] dense_args = [arg.todense() for arg in args] fs = sparse.elemwise(func, *args) assert isinstance(fs, COO) assert_eq(fs, func(*dense_args))
def test_trinary_broadcasting_pathological(shapes, func, value, fraction): args = [sparse.random(s, density=0.5, data_rvs=random_value_array(value, fraction)) for s in shapes] dense_args = [arg.todense() for arg in args] fs = sparse.elemwise(func, *args) assert isinstance(fs, COO) assert_eq(fs, func(*dense_args), equal_nan=True)
def test_sparsearray_elemwise(format): xs = sparse.random((3, 4), density=0.5, format=format) ys = sparse.random((3, 4), density=0.5, format=format) x = xs.todense() y = ys.todense() fs = sparse.elemwise(operator.add, xs, ys) assert isinstance(fs, COO) assert_eq(fs, x + y)
def test_elemwise_unsupported(): class A(): pass s1 = sparse.random((2, 3, 4), density=0.5) x2 = A() with pytest.raises(TypeError): s1 + x2 assert sparse.elemwise(operator.add, s1, x2) is NotImplemented
def test_elemwise_mixed_broadcast(): s1 = sparse.random((2, 3, 4), density=0.5) s2 = sparse.random(4, density=0.5) x3 = np.random.rand(3, 4) x1 = s1.todense() x2 = s2.todense() def func(x1, x2, x3): return x1 * x2 * x3 assert_eq(sparse.elemwise(func, s1, s2, x3), func(x1, x2, x3))
def test_elemwise_trinary(func, shape): xs = sparse.random(shape, density=0.5) ys = sparse.random(shape, density=0.5) zs = sparse.random(shape, density=0.5) x = xs.todense() y = ys.todense() z = zs.todense() fs = sparse.elemwise(func, xs, ys, zs) assert isinstance(fs, COO) assert_eq(fs, func(x, y, z))
def test_elemwise_noargs(): def func(): return np.float_(5.0) assert sparse.elemwise(func) == func()