Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit eb2466c

Browse files
committed
sparse matrix documentation in the manual.
1 parent a97849d commit eb2466c

File tree

2 files changed

+100
-86
lines changed

2 files changed

+100
-86
lines changed

doc/manual/arrays.rst

+100
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,103 @@ stride parameters.
334334
-1.00091806276211814 -0.65508286752651457
335335
0.0 0.70738744643074303
336336

337+
Sparse Matrices
338+
---------------
339+
340+
`Sparse matrices <http://en.wikipedia.org/wiki/Sparse_matrix>`_ are
341+
matrices that are primarily populated with zeros. Sparse matrices may
342+
be used when operations on the sparse representation of a matrix lead
343+
to considerable gains in either time or space when compared to
344+
performing the same operations on a dense matrix.
345+
346+
In julia, sparse matrices are stored in the `Compressed Sparse Column
347+
(CSC) format
348+
<http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29>`_. Julia
349+
sparse matrices have the type ``SparseMatrixCSC{Tv,Ti}``, where ``Tv``
350+
is the type of the nonzero values, and ``Ti`` is the integer type for
351+
storing column pointers and row indices``.
352+
353+
::
354+
355+
type SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
356+
m::Int # Number of rows
357+
n::Int # Number of columns
358+
colptr::Vector{Ti} # Column i is in colptr[i]:(colptr[i+1]-1)
359+
rowval::Vector{Ti} # Row values of nonzeros
360+
nzval::Vector{Tv} # Nonzero values
361+
end
362+
363+
The simplest way to create sparse matrices are using functions
364+
equivalent to the ``zeros`` and ``eye`` functions that Julia provides
365+
for working with dense matrices. To produce sparse matrices instead,
366+
you can use the same names with an ``sp`` prefix:
367+
368+
::
369+
370+
julia> spzeros(3,5)
371+
3x5 sparse matrix with 0 nonzeros:
372+
373+
julia> speye(3,5)
374+
3x5 sparse matrix with 3 nonzeros:
375+
[1, 1] = 1.0
376+
[2, 2] = 1.0
377+
[3, 3] = 1.0
378+
379+
The ``sparse`` function is often a handy way to construct sparse
380+
matrices. It takes as its input a vector ``I`` of row indices, a
381+
vector ``J`` of column indices, and a vector ``V`` of nonzero
382+
values. ``sparse(I,J,V)`` constructs a sparse matrix such that
383+
``S[I[k], J[k]] = V[k]``.
384+
385+
::
386+
387+
julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];
388+
389+
julia> sparse(I,J,V)
390+
5x18 sparse matrix with 4 nonzeros:
391+
[1 , 4] = 1
392+
[4 , 7] = 2
393+
[5 , 9] = 3
394+
[3 , 18] = -5
395+
396+
The inverse of the ``sparse`` function is ``findn``, which
397+
retrieves the inputs used to create the sparse matrix.
398+
399+
::
400+
401+
julia> findn(S)
402+
([1, 4, 5, 3],[4, 7, 9, 18])
403+
404+
julia> findn_nzs(S)
405+
([1, 4, 5, 3],[4, 7, 9, 18],[1, 2, 3, -5])
406+
407+
Another way to create sparse matrices is to convert a dense matrix
408+
into a sparse matrix using the ``sparse`` function:
409+
410+
::
411+
412+
julia> sparse(eye(5))
413+
5x5 sparse matrix with 5 nonzeros:
414+
[1, 1] = 1.0
415+
[2, 2] = 1.0
416+
[3, 3] = 1.0
417+
[4, 4] = 1.0
418+
[5, 5] = 1.0
419+
420+
You can go in the other direction using the ``dense`` or the ``full``
421+
function. The ``issparse`` function can be used to query if a matrix
422+
is sparse.
423+
424+
::
425+
426+
julia> issparse(speye(5))
427+
true
428+
429+
Arithmetic operations on sparse matrices also work as they do on dense
430+
matrices. Indexing of, assignment into, and concatenation of sparse
431+
matrices work in the same way as dense matrices. Indexing operations,
432+
especially assignment, are expensive, when carried out one element at
433+
a time. In many cases it may be better to convert the sparse matrix
434+
into ``(I,J,V)`` format using ``find_nzs``, manipulate the nonzeros or
435+
the structure in the dense vectors ``(I,J,V)``, and then reconstruct
436+
the sparse matrix.

doc/stdlib/sparse.rst

-86
This file was deleted.

0 commit comments

Comments
 (0)