({{, (RefLink[SparseArray, paclet:ref/SparseArray])[{(Subscript[pos, 1])->(Subscript[val, 1]),(Subscript[pos, 2])->(Subscript[val, 2]),…}] 
yields a sparse array in which values (Subscript[val, i]) appear at positions (Subscript[pos, i]). }, {, (RefLink[SparseArray, paclet:ref/SparseArray])[{(Subscript[pos, 1]),(Subscript[pos, 2]),…}->{(Subscript[val, 1]),(Subscript[val, 2]),…}] 
yields the same sparse array. }, {, (RefLink[SparseArray, paclet:ref/SparseArray])[list] 
yields a sparse array version of list. }, {, (RefLink[SparseArray, paclet:ref/SparseArray])[data,{(Subscript[d, 1]),(Subscript[d, 2]),…}]
yields a sparse array representing a (Subscript[d, 1])×(Subscript[d, 2])×… array. }, {, (RefLink[SparseArray, paclet:ref/SparseArray])[data,dims,val]
yields a sparse array in which unspecified elements are taken to have value val. }})

OpenersNotesSection(Spacer1)Details

By default, (RefLink[SparseArray, paclet:ref/SparseArray]) takes unspecified elements to be 0.

(RefLink[SparseArray, paclet:ref/SparseArray])[data,…] is always converted to an optimized standard form with structure (RefLink[SparseArray, paclet:ref/SparseArray])[(RefLink[Automatic, paclet:ref/Automatic]),dims,val,…].

(RefLink[Normal, paclet:ref/Normal])[SparseArray[…]] gives the ordinary array corresponding to a sparse array object.

(RefLink[ArrayRules, paclet:ref/ArrayRules])[SparseArray[…]] gives the list of rules {(Subscript[pos, 1])->(Subscript[val, 1]),(Subscript[pos, 2])->(Subscript[val, 2]),…}.

The elements in (RefLink[SparseArray, paclet:ref/SparseArray]) need not be numeric.

The position specifications (Subscript[pos, i]) can contain patterns.

(RefLink[SparseArray, paclet:ref/SparseArray])[{{i_,i_}->1},{d,d}] gives a d×d identity matrix.

Rules of the form (RefLink[Band, paclet:ref/Band])[…]->vals specify values on bands in the sparse array.

With rules (Subscript[pos, i]):>(Subscript[val, i]) the (Subscript[val, i]) are evaluated separately for each set of indices that match (Subscript[pos, i]).

(RefLink[SparseArray, paclet:ref/SparseArray])[list] requires that list be a full array, with all parts at a particular level being lists of the same length.

The individual elements of a sparse array cannot themselves be lists.

(RefLink[SparseArray, paclet:ref/SparseArray])[rules] yields a sparse array with dimensions exactly large enough to include elements whose positions have been explicitly specified.

(RefLink[SparseArray, paclet:ref/SparseArray])[rules,(RefLink[Automatic, paclet:ref/Automatic]),val] takes unspecified elements to have value val.

List and matrix operations are typically set up to work as they do on (RefLink[Normal, paclet:ref/Normal])[SparseArray[…]].

Functions with attribute (RefLink[Listable, paclet:ref/Listable]) are automatically threaded over the individual elements of the ordinary arrays represented by (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

(RefLink[Part, paclet:ref/Part]) extracts specified parts of the array represented by a (RefLink[SparseArray, paclet:ref/SparseArray]) object, rather than parts of the (RefLink[SparseArray, paclet:ref/SparseArray]) expression itself.

Functions like (RefLink[Map, paclet:ref/Map]) are automatically applied to components in a (RefLink[SparseArray, paclet:ref/SparseArray]) object.

(RefLink[SparseArray, paclet:ref/SparseArray]) is treated as a raw object by functions like (RefLink[AtomQ, paclet:ref/AtomQ]) and for purposes of pattern matching.

(RefLink[Dimensions, paclet:ref/Dimensions]) gives the dimensions of a sparse array.

The standard output format for a sparse array indicates the number of nondefault elements and the total dimensions.

OpenersPrimaryExamplesSection(Spacer1)Examples  (24)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

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

s=SparseArray[{{1,1}->1,{2,2}->2,{3,3}->3,{1,3}->4}]

View it as a matrix:

MatrixForm[s]

Convert it to an ordinary dense matrix:

Normal[s]

OpenersExampleSection1(Spacer1)Scope  (7)

Make a large sparse vector matrix and depth-3 array:

SparseArray[Table[{2^i}->1,{i,10}]]

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

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

Construct a tridiagonal matrix using patterns for indices:

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

MatrixForm[s]

Construct a 10,000 by 10,000 version:

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

Make a sparse diagonal matrix:

d=RandomReal[1,{100}];

m=SparseArray[{i_,i_}:>d[[i]],Length[d]{1,1}]

This is equivalent to (RefLink[DiagonalMatrix, paclet:ref/DiagonalMatrix]):

m==DiagonalMatrix[d]

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

{ByteCount[m],ByteCount[DiagonalMatrix[d]]}

Construct a block diagonal matrix using rules with (RefLink[Band, paclet:ref/Band]):

s=SparseArray[Band[{1,1}]->{{{1,2},{3,4}},{{5,6},{7,8}}}]

MatrixForm[s]

Convert an ordinary matrix into a sparse matrix:

SparseArray[{{1,0,0},{1,2,0},{1,2,3}}]

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

rules=Table[RandomInteger[{1,2},4]->i,{i,10}]

s=SparseArray[rules]

Normal[%]

(RefLink[ArrayRules, paclet:ref/ArrayRules]) produces the minimal list of rules needed to specify the (RefLink[SparseArray, paclet:ref/SparseArray]):

ArrayRules[s]

Many typical operations work with (RefLink[SparseArray, paclet:ref/SparseArray]) objects as they would for equivalent lists:

m1=SparseArray[{{i_,i_}->-2,{i_,j_}/;Abs[i-j]==1->1},{5,5}];
m2=SparseArray[{{i_,i_}->1},{5,5}];

Map[MatrixForm,{m1,m2}]

Arithmetic works element-wise just as it does for lists:

MatrixForm[m1+2 m2]

Matrix products are done with (RefLink[Dot, paclet:ref/Dot]):

MatrixForm[m1.m1]

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

LinearSolve[N[m1],{0,1,2,1,0}]

Many other list commands work automatically:

Map[f,m1,{2}]

MatrixForm[%]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (2)

The unspecified elements can have any value:

s=SparseArray[{i_,i_}->y,{3,3},x]

MatrixForm[s]

Symbolic values can be replaced with local definitions:

Block[{x=0,y=1},s]==IdentityMatrix[3]

Construct a sparse matrix with all machine-number values:

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

MatrixForm[ns]

Construct a sparse matrix with exact integer values:

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

(RefLink[N, paclet:ref/N])[s] is the same as ns:

SameQ[ns,N[s]]

OpenersExampleSection3(Spacer1)Applications  (4)

Create a list with a single nonzero element:

Normal[SparseArray[10->1,19]]

Normal[SparseArray[{3,3}->1,5]]

Plot a list of rules:

ListLinePlot[SparseArray[{1->2,10->7,3->2}]]

Represent a network with an adjacency matrix:

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

GraphPlot[s]

Solve a boundary-value problem (((∂^2)u)/(∂(x^2)))+f(x)u==g(x),u(0)==0, u'(1)==0 using finite differences:

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];

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.]

u=LinearSolve[d2/h^2+id f[xgrid],g[xgrid]];

ListPlot[Transpose[{xgrid,u}]]

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

A (RefLink[SparseArray, paclet:ref/SparseArray]) object is (RefLink[Equal, paclet:ref/Equal]) to the corresponding ordinary list:

s=SparseArray[{{1,2}->3,{4,5}->6}]

s==Normal[s]

They are not (RefLink[SameQ, paclet:ref/SameQ]) because the expression structure is different:

s===Normal[s]

For functions f that work with (RefLink[SparseArray, paclet:ref/SparseArray]) objects, typically f[s]==f[(RefLink[Normal, paclet:ref/Normal])[s]]:

s=SparseArray[RandomInteger[{1,100},{1000,2}]->RandomReal[1,1000]]

Transpose[s]==Transpose[Normal[s]]

s.s==Normal[s].Normal[s]

This includes all functions with the attribute (RefLink[Listable, paclet:ref/Listable]):

listable=Map[ToExpression,Cases[Names["System`*"],s_String/;MemberQ[Attributes[s],Listable]]];

f=First[RandomChoice[listable,1]]

f[s]==f[Normal[s]]

Convert linear expressions to (RefLink[SparseArray, paclet:ref/SparseArray]) objects using (RefLink[CoefficientArrays, paclet:ref/CoefficientArrays]):

expr=ListCorrelate[{1,2,1},Array[x,{100}],{2,2}]; Short[expr]

{v,m}=CoefficientArrays[expr,Array[x,{100}]]

Convert from (RefLink[SparseArray, paclet:ref/SparseArray]) to expressions using (RefLink[Dot, paclet:ref/Dot]):

m.Array[x,{100}]//Short

OpenersExampleSection5(Spacer1)Possible Issues  (6)

If a position is repeated in the rule list for (RefLink[SparseArray, paclet:ref/SparseArray]), the first instance is used:

SparseArray[{{1,1}->1,{1,1}->2}]//Normal

SparseArray[{{i_,i_}->1,{1,1}->2}]//Normal

(RefLink[SparseArray, paclet:ref/SparseArray]) objects can represent data too large to represent in normal form:

s=SparseArray[{{i_,i_}->i},{100000,100000}]

Using (RefLink[Normal, paclet:ref/Normal]) will give a SystemException:

Normal[s]

Sparse operations do not by default check for cancellation:

s=SparseArray[{{i_,i_}->1,{i_,j_}/;Abs[i-j]==1->1},{3,3}]

r=s.s-2s

MatrixForm[r]

Use (RefLink[SparseArray, paclet:ref/SparseArray]) to recompute the sparse structure:

SparseArray[r]

Operations with side effects may give different values when iterating over (RefLink[SparseArray, paclet:ref/SparseArray]):

s=SparseArray[{{1}->2,{4}->3}]

Module[{i=1},Scan[i++&,s];i]

Module[{i=1},Scan[i++&,Normal[s]];i]

With (RefLink[Reap, paclet:ref/Reap]) and (RefLink[Sow, paclet:ref/Sow]) you can see what elements are accessed:

Reap[Map[Sow,s]]

Reap[Map[Sow,Normal[s]]]

For a (RefLink[SparseArray, paclet:ref/SparseArray]) object, (RefLink[Part, paclet:ref/Part]) gives parts of the represented list:

s = SparseArray[{i_}->i,{5}]

Part[s, 1]

The (RefLink[FullForm, paclet:ref/FullForm]) is a way of reconstructing the object from basic storage information:

FullForm[s]

It should not be necessary, but if you want, you can get pieces of the full form with patterns:

HoldPattern[spart[SparseArray[stuff___], p_]] := {stuff}[[p]]

spart[s,1]

A (RefLink[SparseArray, paclet:ref/SparseArray]) object is treated as atomic for functions that do not work on the representation:

s = SparseArray[{i_, i_}->i,{5,5}]

(RefLink[Cases, paclet:ref/Cases]) does not work on the represented matrix:

Cases[s, i_/;i>0, Infinity]

Cases[Normal[s], i_/;i>0, Infinity]

You can often use the result of (RefLink[ArrayRules, paclet:ref/ArrayRules]) to get the information without expanding:

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

OpenersExampleSection6(Spacer1)Neat Examples  (1)

The Game of Life:

SetAttributes[cellupdate,Listable];
cellupdate[1,2]=cellupdate[_,3]=1;
cellupdate[_,_]=0;

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}}}]]

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}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ArrayRules, paclet:ref/ArrayRules])  ▪  (RefLink[Normal, paclet:ref/Normal])  ▪  (RefLink[Band, paclet:ref/Band])  ▪  (RefLink[CoefficientArrays, paclet:ref/CoefficientArrays])  ▪  (RefLink[ArrayPlot, paclet:ref/ArrayPlot])  ▪  (RefLink[Array, paclet:ref/Array])  ▪  (RefLink[ConstantArray, paclet:ref/ConstantArray])  ▪  (RefLink[NumericArray, paclet:ref/NumericArray])  ▪  (RefLink[Association, paclet:ref/Association])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Constructing Lists, paclet:tutorial/ConstructingLists])}, {▪(RefLinkPlain[Sparse Arrays: Manipulating Lists, paclet:tutorial/SparseArrays-ManipulatingLists])}, {▪(RefLinkPlain[Sparse Arrays: Linear Algebra, paclet:tutorial/SparseArrays-LinearAlgebra])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Constructing Matrices, paclet:guide/ConstructingMatrices])}, {▪(RefLinkPlain[Matrices and Linear Algebra, paclet:guide/MatricesAndLinearAlgebra])}, {▪(RefLinkPlain[Constructing Lists, paclet:guide/ConstructingLists])}, {▪(RefLinkPlain[Tensors, paclet:guide/Tensors])}, {▪(RefLinkPlain[Sparse Arrays, paclet:guide/SparseArrays])}, {▪(RefLinkPlain[Graphs and Matrices, paclet:guide/GraphsAndMatrices])}, {▪(RefLinkPlain[Operations on Vectors, paclet:guide/OperationsOnVectors])}, {▪(RefLinkPlain[Structure Matrices & Convolution Kernels, paclet:guide/StructureMatricesAndConvolutionKernels])}, {▪(RefLinkPlain[3D Images, paclet:guide/3DImages])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Handling Arrays of Data, paclet:guide/HandlingArraysOfData])}, {▪(RefLinkPlain[Systems Modeling, paclet:guide/SystemsModeling])}, {▪(RefLinkPlain[Linear and Nonlinear Filters, paclet:guide/LinearAndNonlinearFilters])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(Spacer1)▪(RefLinkPlain[(Spacer1)Implementation notes: Numerical and Related Functions, paclet:tutorial/SomeNotesOnInternalImplementation#7977])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/13-arrays-or-lists-of-lists.html])(WebLink[: Arrays, or Lists of Lists, https://www.wolfram.com/language/elementary-introduction/13-arrays-or-lists-of-lists.html])}})}})