# Basic Operations¶

`COO`

objects can have a number of operators applied to them. They support
operations with scalars, `scipy.sparse.spmatrix`

objects, and other
`COO`

objects. For example, to get the sum of two `COO`

objects, you
would do the following:

```
z = x + y
```

Note that in-place operators are currently not supported. For example,

```
x += y
```

will not work.

## Auto-Densification¶

Operations that would result in dense matrices, such as binary
operations with `numpy.ndarray`

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 == y
x + 5
x == 0
x != 5
x / y
```

However, all of the following are valid operations.

```
x + 0
x != y
x + y
x == 5
5 * x
x / 7.3
x != 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.

## Broadcasting¶

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`

.

## Full List of Operators¶

Here, `x`

and `y`

can be `COO`

arrays,
`scipy.sparse.spmatrix`

objects or scalars, keeping in mind auto
densification rules. The following operators are supported:

Basic algebraic operations

`operator.add`

(`x + y`

)`operator.neg`

(`-x`

)`operator.sub`

(`x - y`

)`operator.mul`

(`x * y`

)`operator.truediv`

(`x / y`

)`operator.floordiv`

(`x // y`

)`operator.pow`

(`x ** y`

)

Comparison operators

`operator.eq`

(`x == y`

)`operator.ne`

(`x != y`

)`operator.gt`

(`x > y`

)`operator.ge`

(`x >= y`

)`operator.lt`

(`x < y`

)`operator.le`

(`x <= y`

)

Bitwise operators

`operator.and_`

(`x & y`

)`operator.or_`

(`x | y`

)`operator.xor`

(`x ^ y`

)

Bit-shifting operators

`operator.lshift`

(`x << y`

)`operator.rshift`

(`x >> y`

)