GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — SparseArray: Sparse representation of a vector, matrix or array—Wolfram Documentation

SparseArray[{pos1v1,pos2v2,…}]

yields a sparse array with all elements zero except for values vi at positions posi.

SparseArray[list]

yields a sparse array version of list.

SparseArray[data,{d1,d2,…}]

yields a sparse array representing a d1×d2×… array.

SparseArray[data,dims,val]

yields a sparse array in which unspecified elements are taken to have value val.

Details
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Applications  
Properties & Relations  
Possible Issues  
Neat Examples  
See Also
Tech Notes
Related Guides
Related Workflows
Related Links
History
Cite this Page

SparseArray

SparseArray[{pos1v1,pos2v2,…}]

yields a sparse array with all elements zero except for values vi at positions posi.

SparseArray[list]

yields a sparse array version of list.

SparseArray[data,{d1,d2,…}]

yields a sparse array representing a d1×d2×… array.

SparseArray[data,dims,val]

yields a sparse array in which unspecified elements are taken to have value val.

Details

  • SparseArray is also known as sparse matrix.
  • Sparse arrays are typically used for efficient linear algebra where most of the entries are zero and for graph adjacency matrices.
  • A sparse array stores only the positions where there are nonzero values, but represents the full array:
  • The data can have the following forms:
  • rulesrules specifying positions and values.
    listconvert an array to SparseArray
    SymmetrizedArray[…]convert to SparseArray
    QuantityArray[…]convert to SparseArray of Quantity
    SparseArray[…]minimize the explicit nonzero elements
  • With s=SparseArray[rules,…] the following specifications can be used:
  • {{i1 1,…,i1 r}v1,{i2 1,…,i2 r}v2,…}s[[i1 1, …, i1 r]] has value v1, s[[i2 1, …, i2 r]] has value v2, etc.
    pattv{{i1 1,…,i1 r}v,{i2 1,…,i2 r}v,…} for all {ik 1,…,ik r} that matches the pattern patt
    patt:>vevaluate the value v for each matching position
    Band[…]valsspecify values for bands and subblocks
    {pos1,pos2,…}{v1,v2,…}equivalent to {pos1v1,pos2v2,…}
  • SparseArray conversions include:
  • Normal[SparseArray[…]]convert to the ordinary List array.
    SymmetrizedArray[SparseArray[…]]convert to a symmetrized array.
    ArrayRules[SparseArray[…]]give the list of rules {pos1->v1,pos2->v2,…}
  • A SparseArray object is a representation of an ordinary array, so many functions work like they would on the ordinary array. Examples include functions like Dimensions, Part, Plus and LinearSolve.
  • SparseArray[data,…] is always converted to an optimized standard form with structure SparseArray[Automatic,dims,val,…].
  • SparseArray is treated as a raw object by functions like AtomQ and for purposes of pattern matching.
  • By default, SparseArray takes unspecified elements val to be zero.
  • The elements in SparseArray need not be numeric, but cannot themselves be lists.
  • SparseArray[…][prop] gives the property prop of the SparseArray object. The following properties may be given:
  • "ImplicitValue"gives the value for elements that are not given explicitly
    "ExplicitLength"gives the number of explicit values
    "ExplicitValues"gives the list of explicit values
    "ExplicitPositions"gives the list of positions corresponding to the explicit values
    "ColumnIndices"gives the column indices from the compressed sparse row representation
    "RowPointers"gives the row pointer list from the compressed sparse row representation
    "BandWidth"gives the off-diagonal bandwidth for a sparse matrix
    "Density"gives the ratio of the number of explicit elements to the total number of elements

Examples

open all close all

Basic Examples  (1)

Construct a sparse matrix with values at only a few specified positions:

Wolfram Language code: s = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]

View it as a matrix:

Wolfram Language code: MatrixForm[s]

Convert it to an ordinary dense matrix:

Wolfram Language code: Normal[s]

Scope  (7)

Make a large sparse vector:

Wolfram Language code: SparseArray[Table[{2 ^ i} -> 1, {i, 10}]]

Make a large sparse matrix:

Wolfram Language code: SparseArray[Table[{2 ^ i, 3 ^ i + i} -> 1, {i, 10}]]

Make a large sparse depth-3 array:

Wolfram Language code: SparseArray[Table[{2 ^ i, 3 ^ i + i, i ^ 5} -> 1, {i, 10}]]

Construct a tridiagonal matrix using patterns for indices:

Wolfram Language code: s = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {5, 5}]
Wolfram Language code: MatrixForm[s]

Construct a 10,000×10,000 version:

Wolfram Language code: s = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {10 ^ 4, 10 ^ 4}]

Make a sparse diagonal matrix:

Wolfram Language code: d = RandomReal[1, {100}];
Wolfram Language code: m = SparseArray[{i_, i_} :> d[[i]], Length[d]{1, 1}]

This is equivalent to DiagonalMatrix:

Wolfram Language code: m == DiagonalMatrix[d]

Except that as a sparse matrix, it uses much less memory:

Wolfram Language code: {ByteCount[m], ByteCount[DiagonalMatrix[d]]}

Construct a block diagonal matrix using rules with Band:

Wolfram Language code: s = SparseArray[Band[{1, 1}] -> {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}]
Wolfram Language code: MatrixForm[s]

Convert an ordinary matrix into a sparse matrix:

Wolfram Language code: SparseArray[{{1, 0, 0}, {1, 2, 0}, {1, 2, 3}}]

Make a rank-4 sparse tensor with values at random positions:

Wolfram Language code: rules = Table[RandomInteger[{1, 2}, 4] -> i, {i, 10}]
Wolfram Language code: s = SparseArray[rules]
Wolfram Language code: Normal[%]

ArrayRules produces the minimal list of rules needed to specify the SparseArray:

Wolfram Language code: ArrayRules[s]

Many typical operations work with SparseArray objects as they would for equivalent lists:

Wolfram Language code: m1 = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {5, 5}]; m2 = SparseArray[{{i_, i_} -> 1}, {5, 5}];
Wolfram Language code: Map[MatrixForm, {m1, m2}]

Arithmetic works elementwise just as it does for lists:

Wolfram Language code: MatrixForm[m1 + 2 m2]

Matrix products are done with Dot:

Wolfram Language code: MatrixForm[m1.m1]

Many linear algebra functions are done efficiently with the sparse form:

Wolfram Language code: LinearSolve[N[m1], {0, 1, 2, 1, 0}]

Many other list commands work automatically:

Wolfram Language code: Map[f, m1, {2}]
Wolfram Language code: MatrixForm[%]

Generalizations & Extensions  (2)

The unspecified elements can have any value:

Wolfram Language code: s = SparseArray[{i_, i_} -> y, {3, 3}, x]
Wolfram Language code: MatrixForm[s]

Construct a sparse matrix with all machine-number values:

Wolfram Language code: ns = SparseArray[{{i_, i_} -> -2., {i_, j_} /; Abs[i - j] == 1 -> 1.}, {5, 5}, 0.]
Wolfram Language code: MatrixForm[ns]

Construct a sparse matrix with exact integer values:

Wolfram Language code: s = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {5, 5}]

N[s] is the same as ns:

Wolfram Language code: SameQ[ns, N[s]]

Applications  (4)

Create a list with a single nonzero element:

Wolfram Language code: Normal[SparseArray[10 -> 1, 19]]
Wolfram Language code: Normal[SparseArray[{3, 3} -> 1, 5]]

Plot a list of rules:

Wolfram Language code: ListLinePlot[SparseArray[{1 -> 2, 10 -> 7, 3 -> 2}]]

Represent a network with an adjacency matrix:

Wolfram Language code: s = SparseArray[{{i_, j_} /; Mod[2i + j, 7] == 1 -> 1}, {20, 20}]
Wolfram Language code: GraphPlot[s]

Solve a boundary-value problem using finite differences:

Wolfram Language code: n = 1000; h = 1. / n; xgrid = h Range[1, n]; f[x_] := 1 + 100 Exp[-(321(x - 1 / 2)) ^ 2]; g[x_] := Sin[Pi x];
Wolfram Language code: id = SparseArray[{i_, i_} -> 1., {n, n}, 0.]; d2 = SparseArray[{{i_, i_} -> -2., {n, n - 1} -> 2., {i_, j_} /; Abs[i - j] == 1 -> 1.}, {n, n}, 0.]
Wolfram Language code: u = LinearSolve[d2 / h ^ 2 + id f[xgrid], g[xgrid]];
Wolfram Language code: ListPlot[Transpose[{xgrid, u}]]

Properties & Relations  (3)

A SparseArray object is Equal to the corresponding ordinary list:

Wolfram Language code: s = SparseArray[{{1, 2} -> 3, {4, 5} -> 6}]
Wolfram Language code: s == Normal[s]

They are not SameQ because the expression structure is different:

Wolfram Language code: s === Normal[s]

For functions f that work with SparseArray objects, typically f[s]==f[Normal[s]]:

Wolfram Language code: s = SparseArray[RandomInteger[{1, 100}, {1000, 2}] -> RandomReal[1, 1000]]
Wolfram Language code: Transpose[s] == Transpose[Normal[s]]
Wolfram Language code: s.s == Normal[s].Normal[s]

This includes all functions with the attribute Listable:

Wolfram Language code: listable = Map[ToExpression, Cases[Names["System`*"], s_String /; MemberQ[Attributes[s], Listable]]];
Wolfram Language code: f = First[RandomChoice[listable, 1]]
Wolfram Language code: f[s] == f[Normal[s]]

Convert linear expressions to SparseArray objects using CoefficientArrays:

Wolfram Language code: expr = ListCorrelate[{1, 2, 1}, Array[x, {100}], {2, 2}]; Short[expr]
Wolfram Language code: {v, m} = CoefficientArrays[expr, Array[x, {100}]]

Convert from SparseArray to expressions using Dot:

Wolfram Language code: m.Array[x, {100}]//Short

Possible Issues  (9)

If a position is repeated in the rule list for SparseArray, the first instance is used:

Wolfram Language code: SparseArray[{{1, 1} -> 1, {1, 1} -> 2}]//Normal
Wolfram Language code: SparseArray[{{i_, i_} -> 1, {1, 1} -> 2}]//Normal

SparseArray objects can represent data too large to represent in normal form:

Wolfram Language code: s = SparseArray[{{i_, i_} -> i}, {100000, 100000}]

Using Normal will give a SystemException:

Wolfram Language code: Normal[s]

Sparse operations do not by default check for cancellation:

Wolfram Language code: s = SparseArray[{{i_, i_} -> 1, {i_, j_} /; Abs[i - j] == 1 -> 1}, {3, 3}]
Wolfram Language code: r = s.s - 2s
Wolfram Language code: MatrixForm[r]

Use SparseArray to recompute the sparse structure:

Wolfram Language code: SparseArray[r]

The internal structure of a SparseArray representation is not unique and SameQ detects this:

Wolfram Language code: s1 = SparseArray[{1, 1} -> 1, {2, 2}]; (s1 - s1) === SparseArray[{}, {2, 2}]

Use SparseArray to recompute the sparse structure:

Wolfram Language code: SparseArray[s1 - s1] === SparseArray[{}, {2, 2}]

Note that Equal works as expected:

Wolfram Language code: (s1 - s1) == SparseArray[{}, {2, 2}]

The internal structure of SparseArray representation is not unique and setting parts can change that structure:

Wolfram Language code: s1 = SparseArray[{{3}, {5}} -> 1, {6}]; s2 = SparseArray[{{5}} -> 1, {6}]; s2[[3]] = 1;

Test if the SparseArray instances are the same:

Wolfram Language code: s1 === s2

Use SparseArray to recompute the sparse structure:

Wolfram Language code: s1 === SparseArray[s2]

Note that Equal works as expected:

Wolfram Language code: s1 == s2

Operations with side effects may give different values when iterating over SparseArray:

Wolfram Language code: s = SparseArray[{{1} -> 2, {4} -> 3}]
Wolfram Language code: Module[{i = 1}, Scan[i++&, s];i]
Wolfram Language code: Module[{i = 1}, Scan[i++&, Normal[s]];i]

With Reap and Sow you can see what elements are accessed:

Wolfram Language code: Reap[Map[Sow, s]]
Wolfram Language code: Reap[Map[Sow, Normal[s]]]

For a SparseArray object, Part gives parts of the represented list:

Wolfram Language code: s = SparseArray[{i_} -> i, {5}]
Wolfram Language code: Part[s, 1]

The FullForm is a way of reconstructing the object from basic storage information:

Wolfram Language code: FullForm[s]

A SparseArray object is treated as atomic for functions that do not work on the representation:

Wolfram Language code: s = SparseArray[{i_, i_} -> i, {5, 5}]

Cases does not work on the represented matrix:

Wolfram Language code: Cases[s, i_ /; i > 0, Infinity]
Wolfram Language code: Cases[Normal[s], i_ /; i > 0, Infinity]

You can often use the result of ArrayRules to get the information without expanding:

Wolfram Language code: Cases[ArrayRules[s], (p_ -> i_ /; i > 0) -> i, 1]

Even for a dense SparseArray there can be a division by 0:

Wolfram Language code: s = SparseArray[{{2, 1}, {1, 2}}]
Wolfram Language code: 1 / s

The density of a SparseArray is 1:

Wolfram Language code: s["Density"]

Since a density 1. SparseArray is a very special case, making this work would be computationally excessively expansive. In this case, the use of Normal is justified as the matrix is dense:

Wolfram Language code: 1 / Normal[s]

Neat Examples  (1)

The Game of Life:

Wolfram Language code: SetAttributes[cellupdate, Listable]; cellupdate[1, 2] = cellupdate[_, 3] = 1; cellupdate[_, _] = 0;
Wolfram Language code: update[m_] := cellupdate[m, Sum[RotateRight[m, r], {r, {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}}]]
Wolfram Language code: Block[{n = 100, m = 10, h}, h = Floor[n / 2 - m / 2];s = SparseArray[Band[{h, h}] -> RandomInteger[1, {m, m}], {n, n}]]; Table[ArrayPlot[Do[s = update[s], {10}];s = SparseArray[s]], {8}]

See Also

ArrayRules  SparseArrayQ  Normal  Band  CoefficientArrays  ArrayPlot  Array  ConstantArray  NumericArray  Association

Function Repository: SparseAssociation

Tech Notes

    ▪
  • Constructing Lists
  • ▪
  • Sparse Arrays: Manipulating Lists
  • ▪
  • Sparse Arrays: Linear Algebra
  • ▪
  • Implementation notes: Numerical and Related Functions

Related Guides

    ▪
  • Sparse Arrays
  • ▪
  • Constructing Matrices
  • ▪
  • Matrices and Linear Algebra
  • ▪
  • Constructing Lists
  • ▪
  • Tensors
  • ▪
  • Graphs and Matrices
  • ▪
  • Operations on Vectors
  • ▪
  • Structure Matrices & Convolution Kernels
  • ▪
  • 3D Images
  • ▪
  • List Manipulation
  • ▪
  • Handling Arrays of Data
  • ▪
  • Systems Modeling
  • ▪
  • Linear and Nonlinear Filters

Related Workflows

    Related Workflows
    ▪
  • Create a Matrix

Related Links

  • An Elementary Introduction to the Wolfram Language : Arrays, or Lists of Lists

History

Introduced in 2003 (5.0) | Updated in 2007 (6.0) ▪ 2021 (13.0)

Wolfram Research (2003), SparseArray, Wolfram Language function, https://reference.wolfram.com/language/ref/SparseArray.html (updated 2021).

Text

Wolfram Research (2003), SparseArray, Wolfram Language function, https://reference.wolfram.com/language/ref/SparseArray.html (updated 2021).

CMS

Wolfram Language. 2003. "SparseArray." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/SparseArray.html.

APA

Wolfram Language. (2003). SparseArray. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SparseArray.html

BibTeX

@misc{reference.wolfram_2026_sparsearray, author="Wolfram Research", title="{SparseArray}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/SparseArray.html}", note=[Accessed: 12-November-2026]}

BibLaTeX

@online{reference.wolfram_2026_sparsearray, organization={Wolfram Research}, title={SparseArray}, year={2021}, url={https://reference.wolfram.com/language/ref/SparseArray.html}, note=[Accessed: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.