Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: fix sparse docs "matrix" and "array" mixup #18898

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 32 additions & 32 deletions scipy/sparse/__init__.py
Expand Up @@ -152,30 +152,30 @@
Usage information
=================

There are seven available sparse matrix types:

1. csc_matrix: Compressed Sparse Column format
2. csr_matrix: Compressed Sparse Row format
3. bsr_matrix: Block Sparse Row format
4. lil_matrix: List of Lists format
5. dok_matrix: Dictionary of Keys format
6. coo_matrix: COOrdinate format (aka IJV, triplet format)
7. dia_matrix: DIAgonal format

To construct a matrix efficiently, use either dok_matrix or lil_matrix.
The lil_matrix class supports basic slicing and fancy indexing with a
There are seven available sparse array types:

1. `csc_array`: Compressed Sparse Column format
2. `csr_array`: Compressed Sparse Row format
3. `bsr_array`: Block Sparse Row format
4. `lil_array`: List of Lists format
5. `dok_array`: Dictionary of Keys format
6. `coo_array`: COOrdinate format (aka IJV, triplet format)
7. `dia_array`: DIAgonal format

To construct an array efficiently, use either `dok_array` or `lil_array`.
The `lil_array` class supports basic slicing and fancy indexing with a
similar syntax to NumPy arrays. As illustrated below, the COO format
may also be used to efficiently construct matrices. Despite their
may also be used to efficiently construct arrays. Despite their
similarity to NumPy arrays, it is **strongly discouraged** to use NumPy
functions directly on these matrices because NumPy may not properly convert
functions directly on these arrays because NumPy may not properly convert
them for computations, leading to unexpected (and incorrect) results. If you
do want to apply a NumPy function to these matrices, first check if SciPy has
its own implementation for the given sparse matrix class, or **convert the
sparse matrix to a NumPy array** (e.g., using the `toarray()` method of the
do want to apply a NumPy function to these arrays, first check if SciPy has
its own implementation for the given sparse array class, or **convert the
sparse array to a NumPy array** (e.g., using the ``toarray`` method of the
class) first before applying the method.

To perform manipulations such as multiplication or inversion, first
convert the matrix to either CSC or CSR format. The lil_matrix format is
convert the array to either CSC or CSR format. The `lil_array` format is
row-based, so conversion to CSR is efficient, whereas conversion to CSC
is less so.

Expand All @@ -184,17 +184,17 @@

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:
To do a vector product between a sparse array and a vector simply use
the array ``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]])
>>> from scipy.sparse import csr_array
>>> A = csr_array([[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,
.. warning:: As of NumPy 1.7, ``np.dot`` is not aware of sparse arrays,
therefore using it will result on unexpected results or errors.
The corresponding dense array should be obtained first instead:

Expand All @@ -203,18 +203,18 @@

but then all the performance advantages would be lost.

The CSR format is specially suitable for fast matrix vector products.
The CSR format is especially suitable for fast matrix vector products.

Example 1
---------
Construct a 1000x1000 lil_matrix and add some values to it:
Construct a 1000x1000 `lil_array` and add some values to it:

>>> from scipy.sparse import lil_matrix
>>> from scipy.sparse import lil_array
>>> from scipy.sparse.linalg import spsolve
>>> from numpy.linalg import solve, norm
>>> from numpy.random import rand

>>> A = lil_matrix((1000, 1000))
>>> A = lil_array((1000, 1000))
>>> A[0, :100] = rand(100)
>>> A[1, 100:200] = A[0, :100]
>>> A.setdiag(rand(1000))
Expand All @@ -225,7 +225,7 @@
>>> b = rand(1000)
>>> x = spsolve(A, b)

Convert it to a dense matrix and solve, and check that the result
Convert it to a dense array and solve, and check that the result
is the same:

>>> x_ = solve(A.toarray(), b)
Expand All @@ -242,14 +242,14 @@
Example 2
---------

Construct a matrix in COO format:
Construct an array in COO format:

>>> from scipy import sparse
>>> from numpy import array
>>> I = array([0,3,1,0])
>>> J = array([0,3,1,2])
>>> V = array([4,5,7,9])
>>> A = sparse.coo_matrix((V,(I,J)),shape=(4,4))
>>> A = sparse.coo_array((V,(I,J)),shape=(4,4))

Notice that the indices do not need to be sorted.

Expand All @@ -258,15 +258,15 @@
>>> I = array([0,0,1,3,1,0,0])
>>> J = array([0,2,1,3,1,0,0])
>>> V = array([1,1,1,1,1,1,1])
>>> B = sparse.coo_matrix((V,(I,J)),shape=(4,4)).tocsr()
>>> B = sparse.coo_array((V,(I,J)),shape=(4,4)).tocsr()

This is useful for constructing finite-element stiffness and mass matrices.

Further details
---------------

CSR column indices are not necessarily sorted. Likewise for CSC row
indices. Use the .sorted_indices() and .sort_indices() methods when
indices. Use the ``.sorted_indices()`` and ``.sort_indices()`` methods when
sorted indices are required (e.g., when passing data to other libraries).

"""
Expand Down