Beispiel #1
0
def add_matrices(a, b):
    if isinstance(a, scisp.spmatrix) and isinstance(b, scisp.spmatrix):
        return a.tocsr() + b.tocsr()
    elif isinstance(a, sparse.COO) and isinstance(b, sparse.COO):
        return sparse.elemwise(np.add, a, b)
    else:
        logger.error('Adding two different matrix types is not supported')
Beispiel #2
0
def test_trinary_broadcasting(shapes, args):
    dense_args = [arg.todense() for arg in args]

    fs = sparse.elemwise(func, *args)
    assert isinstance(fs, COO)

    assert_eq(fs, func(*dense_args))
Beispiel #3
0
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))
Beispiel #4
0
def test_elemwise_trinary(func, shape, formats):
    xs, ys, zs = xyz

    x = xs.todense()
    y = ys.todense()
    z = zs.todense()

    fs = sparse.elemwise(func, xs, ys, zs)
    assert_eq(fs, func(x, y, z))
Beispiel #5
0
def test_elemwise_mixed_broadcast(s1, s2):
    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))
Beispiel #6
0
def test_elemwise_unsupported(s1):
    class A:
        pass

    x2 = A()

    with pytest.raises(TypeError):
        s1 + x2

    assert sparse.elemwise(operator.add, s1, x2) is NotImplemented
Beispiel #7
0
def test_elemwise_trinary(func, shape, formats):
    xs = sparse.random(shape, density=0.5, format=formats[0])
    ys = sparse.random(shape, density=0.5, format=formats[1])
    zs = sparse.random(shape, density=0.5, format=formats[2])

    x = xs.todense()
    y = ys.todense()
    z = zs.todense()

    fs = sparse.elemwise(func, xs, ys, zs)
    assert_eq(fs, func(x, y, z))
Beispiel #8
0
def test_elemwise_unsupported(format):
    class A:
        pass

    s1 = sparse.random((2, 3, 4), density=0.5, format=format)
    x2 = A()

    with pytest.raises(TypeError):
        s1 + x2

    assert sparse.elemwise(operator.add, s1, x2) is NotImplemented
Beispiel #9
0
def test_elemwise_mixed_broadcast(format):
    s1 = sparse.random((2, 3, 4), density=0.5, format=format)
    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))
Beispiel #10
0
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))
Beispiel #11
0
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)
    if format == "gcxs":
        assert isinstance(fs, GCXS)
    else:
        assert isinstance(fs, COO)

    assert_eq(fs, x + y)
def where(*args, **kwargs):
    return elemwise(np.where, *args, **kwargs)
Beispiel #13
0
def test_elemwise_noargs():
    def func():
        return np.float_(5.0)

    assert_eq(sparse.elemwise(func), func())