In [1]:
import numpy as np
from scipy.sparse import csr_matrix, csc_matrix, coo_matrix, lil_matrix

In [2]:
l = [[0, 1, 2],
     [3, 0, 4],
     [0, 0, 0]]

In [3]:
csr = csr_matrix(l)

In [4]:
print((csr + csr).toarray())


[[0 2 4]
 [6 0 8]
 [0 0 0]]

In [5]:
print(type((csr + csr)))


<class 'scipy.sparse.csr.csr_matrix'>

In [6]:
print((csr - csr).toarray())


[[0 0 0]
 [0 0 0]
 [0 0 0]]

In [7]:
print((csr * csr).toarray())


[[3 0 4]
 [0 3 6]
 [0 0 0]]

In [8]:
print((csr.multiply(csr)).toarray())


[[ 0  1  4]
 [ 9  0 16]
 [ 0  0  0]]

In [9]:
print((csr.dot(csr)).toarray())


[[3 0 4]
 [0 3 6]
 [0 0 0]]

In [10]:
print(csr / csr)


[[nan  1.  1.]
 [ 1. nan  1.]
 [nan nan nan]]

In [11]:
print(type(csr / csr))


<class 'numpy.matrix'>

In [12]:
print(np.array(csr / csr))


[[nan  1.  1.]
 [ 1. nan  1.]
 [nan nan nan]]

In [13]:
print(type(np.array(csr / csr)))


<class 'numpy.ndarray'>

In [14]:
csr_full = csr_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

In [15]:
print(csr_full / csr_full)


[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

In [16]:
print(type(csr_full / csr_full))


<class 'numpy.matrix'>

In [17]:
# print(csr + 10)
# NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported

In [18]:
# print(csr - 10)
# NotImplementedError: subtracting a nonzero scalar from a sparse matrix is not supported

In [19]:
# print(csr_full + 10)
# NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported

In [20]:
# print(csr_full - 10)
# NotImplementedError: subtracting a nonzero scalar from a sparse matrix is not supported

In [21]:
print((csr * 10).toarray())


[[ 0 10 20]
 [30  0 40]
 [ 0  0  0]]

In [22]:
print((csr / 10).toarray())


[[0.  0.1 0.2]
 [0.3 0.  0.4]
 [0.  0.  0. ]]

In [23]:
print((csr ** 2).toarray())


[[3 0 4]
 [0 3 6]
 [0 0 0]]

In [24]:
print((csr ** 3).toarray())


[[ 0  3  6]
 [ 9  0 12]
 [ 0  0  0]]

In [25]:
print((csr * csr * csr).toarray())


[[ 0  3  6]
 [ 9  0 12]
 [ 0  0  0]]

In [26]:
# print((csr ** -1).toarray())
# alueError: exponent must be >= 0

In [27]:
# print((csr ** 0.5).toarray())
# ValueError: exponent must be an integer

In [28]:
csc = csc_matrix(l)
coo = coo_matrix(l)
lil = lil_matrix(l)

In [29]:
print(type(csc + csc))


<class 'scipy.sparse.csc.csc_matrix'>

In [30]:
print(type(csr + csc))


<class 'scipy.sparse.csr.csr_matrix'>

In [31]:
print(type(csc + csr))


<class 'scipy.sparse.csc.csc_matrix'>

In [32]:
print(type(coo + coo))


<class 'scipy.sparse.csr.csr_matrix'>

In [33]:
print(type(lil + lil))


<class 'scipy.sparse.csr.csr_matrix'>

In [34]:
a = np.array(l)
print(a)


[[0 1 2]
 [3 0 4]
 [0 0 0]]

In [35]:
print(type(a))


<class 'numpy.ndarray'>

In [36]:
print(a + csr)


[[0 2 4]
 [6 0 8]
 [0 0 0]]

In [37]:
print(type(a + csr))


<class 'numpy.matrix'>

In [38]:
print(type(csr - a))


<class 'numpy.matrix'>

In [39]:
# print(type(a / csr))
# TypeError: unsupported operand type(s) for /: 'numpy.ndarray' and 'csr_matrix'

In [40]:
print(csr / a)


[[nan  1.  1.]
 [ 1. nan  1.]
 [nan nan nan]]
/usr/local/lib/python3.7/site-packages/scipy/sparse/base.py:596: RuntimeWarning: invalid value encountered in true_divide
  return np.true_divide(self.todense(), other)

In [41]:
print(type(csr / a))


<class 'numpy.matrix'>
/usr/local/lib/python3.7/site-packages/scipy/sparse/base.py:596: RuntimeWarning: invalid value encountered in true_divide
  return np.true_divide(self.todense(), other)

In [42]:
print(csr * a)


[[3 0 4]
 [0 3 6]
 [0 0 0]]

In [43]:
print(type(csr * a))


<class 'numpy.ndarray'>

In [44]:
print(type(a * csr))


<class 'numpy.ndarray'>

In [45]:
print(type(csr.dot(a)))


<class 'numpy.ndarray'>

In [46]:
print(a.dot(csr))


[[<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]
 [<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]
 [<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]]

In [47]:
print(csr.multiply(a))


  (0, 1)	1
  (0, 2)	4
  (1, 0)	9
  (1, 2)	16

In [48]:
print(type(csr.multiply(a)))


<class 'scipy.sparse.coo.coo_matrix'>

In [49]:
print(csr.multiply(a).toarray())


[[ 0  1  4]
 [ 9  0 16]
 [ 0  0  0]]

In [50]:
print(np.multiply(a, csr))


[[<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]
 [<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]
 [<3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>
  <3x3 sparse matrix of type '<class 'numpy.int64'>'
	with 4 stored elements in Compressed Sparse Row format>]]