This function allows you to apply any arbitrary broadcasting function to any number of arguments
where the arguments can be SparseArray objects or scipy.sparse.spmatrix objects.
For example, the following will add two arrays:

sparse.elemwise(np.add,x,y)

Warning

Previously, elemwise was a method of the COO class. Now,
it has been moved to the sparse module.

Operations that would result in dense matrices, such as binary
operations with Numpy arrays objects or certain operations with
scalars are not allowed and will raise a ValueError. For example,
all of the following will raise a ValueError. Here, x and
y are COO objects.

x==yx+5x==0x!=5x/y

However, all of the following are valid operations.

x+0x!=yx+yx==55*xx/7.3x!=0

If densification is needed, it must be explicit. In other words, you must call
COO.todense on the COO object. If both operands are COO,
both must be densified.

Warning

Previously, operations with Numpy arrays were sometimes supported. Now,
it is necessary to convert Numpy arrays to COO objects.

All binary operators support broadcasting.
This means that (under certain conditions) you can perform binary operations
on arrays with unequal shape. Namely, when the shape is missing a dimension,
or when a dimension is 1. For example, performing a binary operation
on two COO arrays with shapes (4,) and (5,1) yields
an object of shape (5,4). The same happens with arrays of shape
(1,4) and (5,1). However, (4,1) and (5,1)
will raise a ValueError.

COO arrays support a variety of element-wise operations. However, as
with operators, operations that map zero to a nonzero value are not supported.

To illustrate, the following are all possible, and will produce another
COO array:

np.abs(x)np.sin(x)np.sqrt(x)np.conj(x)np.expm1(x)np.log1p(x)

However, the following are all unsupported and will raise a ValueError:

np.exp(x)np.cos(x)np.log(x)

Notice that you can apply any unary or binary numpy.ufunc to COO
arrays, and numpy.ndarray objects and scalars and it will work so
long as the result is not dense. When applying to numpy.ndarray objects,
we check that operating on the array with zero would always produce a zero.

This method can take an arbitrary numpy.ufunc and performs a
reduction using that method. For example, the following will perform
a sum:

x.reduce(np.add,axis=1)

Note

This library currently performs reductions by grouping together all
coordinates along the supplied axes and reducing those. Then, if the
number in a group is deficient, it reduces an extra time with zero.
As a result, if reductions can change by adding multiple zeros to
it, this method won’t be accurate. However, it works in most cases.

COO arrays can be indexed just like regular
numpy.ndarray objects. They support integer, slice and boolean indexing.
However, currently, numpy advanced indexing is not properly supported. This
means that all of the following work like in Numpy, except that they will produce
COO arrays rather than numpy.ndarray objects, and will produce
scalars where expected. Assume that z.shape is (5,6,7)