Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2745 from Juanlu001/sparse-docs

DOC: sparse: Added basic sparse matrix vector product examples
  • Loading branch information...
commit 60fec09e2bd8a4b30fc2ac2e49bc4ea7dcb9b69b 2 parents 331b61e + c9996e8
@pv pv authored
Showing with 38 additions and 0 deletions.
  1. +2 −0  THANKS.txt
  2. +24 −0 scipy/sparse/__init__.py
  3. +12 −0 scipy/sparse/base.py
View
2  THANKS.txt
@@ -120,6 +120,8 @@ Alex Griffing for expm 2009, expm_multiply, expm_frechet,
implementations, plus bugfixes.
Nils Werner for signal windowing and wavfile-writing improvements.
Kenneth L. Ho for the wrapper around the Interpolative Decomposition code.
+Juan Luis Cano for refactorings in lti, sparse docs improvements and some
+ trivial fixes.
Institutions
View
24 scipy/sparse/__init__.py
@@ -106,6 +106,30 @@
All conversions among the CSR, CSC, and COO formats are efficient,
linear-time operations.
+Matrix vector product
+---------------------
+To do a vector product between a sparse matrix and a vector simply use
+the matrix `dot` method, as described in its docstring:
+
+>>> import numpy as np
+>>> from scipy.sparse import csr_matrix
+>>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
+>>> v = np.array([1, 0, -1])
+>>> A.dot(v)
+array([ 1, -3, -1], dtype=int64)
+
+.. warning:: As of NumPy 1.7, `np.dot` is not aware of sparse matrices,
+ therefore using it will result on unexpected results or errors.
+ The corresponding dense matrix should be obtained first instead:
+
+ >>> np.dot(A.todense(), v)
+ matrix([[ 1, -3, -1]], dtype=int64)
+
+ but then all the performance advantages would be lost.
+ Notice that it returned a matrix, because `todense` returns a matrix.
+
+The CSR format is specially suitable for fast matrix vector products.
+
Example 1
---------
Construct a 1000x1000 lil_matrix and add some values to it:
View
12 scipy/sparse/base.py
@@ -225,6 +225,18 @@ def multiply(self, other):
return self.tocsr().multiply(other)
def dot(self, other):
+ """Ordinary dot product
+
+ Examples
+ --------
+ >>> import numpy as np
+ >>> from scipy.sparse import csr_matrix
+ >>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
+ >>> v = np.array([1, 0, -1])
+ >>> A.dot(v)
+ array([ 1, -3, -1], dtype=int64)
+
+ """
return self * other
def __eq__(self, other):
Please sign in to comment.
Something went wrong with that request. Please try again.