Skip to content

Commit

Permalink
DOC: Add array representations to sparse array construction examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Sep 14, 2023
1 parent 44e434d commit e7a6163
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
55 changes: 42 additions & 13 deletions docs/construct.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ matrix:
... [0, 1, 2, 3, 4]]
>>> data = [10, 20, 30, 40, 50]
>>> s = sparse.COO(coords, data, shape=(5, 5))
>>> s.todense()
array([[10, 0, 0, 0, 0],
[ 0, 20, 0, 0, 0],
[ 0, 0, 30, 0, 0],
[ 0, 0, 0, 40, 0],
[ 0, 0, 0, 0, 50]])
>>> s
<COO: shape=(5, 5), dtype=int64, nnz=5, fill_value=0>
0 1 2 3 4
┌ ┐
010
120
230
340
450
└ ┘
In general :code:`coords` should be a :code:`(ndim, nnz)` shaped
array. Each row of :code:`coords` contains one dimension of the
Expand All @@ -47,6 +50,15 @@ identity matrix:
... [0, 1, 2, 3]]
>>> data = 1
>>> s = sparse.COO(coords, data, shape=(4, 4))
>>> s
<COO: shape=(4, 4), dtype=int64, nnz=4, fill_value=0>
0 1 2 3
┌ ┐
01
11
21
31
└ ┘
You can, and should, pass in :obj:`numpy.ndarray` objects for
:code:`coords` and :code:`data`.
Expand All @@ -61,9 +73,19 @@ explicitly. For example, if we did the following without the

.. code-block:: python
coords = [[0, 3, 2, 1], [4, 1, 2, 0]]
data = [1, 4, 2, 1]
s = COO(coords, data, shape=(5, 5))
>>> coords = [[0, 3, 2, 1], [4, 1, 2, 0]]
>>> data = [1, 4, 2, 1]
>>> s = COO(coords, data, shape=(5, 5))
>>> s
<COO: shape=(5, 5), dtype=int64, nnz=4, fill_value=0>
0 1 2 3 4
┌ ┐
01
11
22
34
4 │ │
└ ┘
:obj:`COO` arrays support arbitrary fill values. Fill values are the "default"
value, or value to not store. This can be given a value other than zero. For
Expand All @@ -73,9 +95,16 @@ with nonzero fill values.

.. code-block:: python
coords = [[0, 1], [1, 0]]
data = [0, 0]
s = COO(coords, data, fill_value=1)
>>> coords = [[0, 1], [1, 0]]
>>> data = [0, 0]
>>> s = COO(coords, data, fill_value=1)
>>> s
<COO: shape=(2, 2), dtype=int64, nnz=2, fill_value=1>
0 1
┌ ┐
00
10
└ ┘
From :std:doc:`Scipy sparse matrices <scipy:reference/generated/scipy.sparse.spmatrix>`
---------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion requirements/all.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
-r tox.txt
-r docs.txt
-r docs.txt
matrepr

0 comments on commit e7a6163

Please sign in to comment.