GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing reference.wolfram.com — SparseArray: Sparse representation of a vector, matrix or array—Wolfram Documentation SparseArray[{pos1v1,pos2v2,…}] 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
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
SparseArraySparseArray[{pos1v1,pos2v2,…}] 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
Examplesopen all close allBasic 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]Scope (7)Make a large sparse vector: SparseArray[Table[{2 ^ i} -> 1, {i, 10}]]Make a large sparse matrix: SparseArray[Table[{2 ^ i, 3 ^ i + i} -> 1, {i, 10}]]Make a large sparse depth-3 array: 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×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 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 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[%]ArrayRules produces the minimal list of rules needed to specify the SparseArray: ArrayRules[s]Many typical operations work with 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 elementwise just as it does for lists: MatrixForm[m1 + 2 m2]Matrix products are done with 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[%]Generalizations & Extensions (2)The unspecified elements can have any value: s = SparseArray[{i_, i_} -> y, {3, 3}, x]MatrixForm[s]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}]N[s] is the same as ns: SameQ[ns, N[s]]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 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}]]Properties & Relations (3)A SparseArray object is Equal to the corresponding ordinary list: s = SparseArray[{{1, 2} -> 3, {4, 5} -> 6}]s == Normal[s]They are not SameQ because the expression structure is different: s === Normal[s]For functions f that work with SparseArray objects, typically f[s]==f[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 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 SparseArray objects using CoefficientArrays: expr = ListCorrelate[{1, 2, 1}, Array[x, {100}], {2, 2}]; Short[expr]{v, m} = CoefficientArrays[expr, Array[x, {100}]]Convert from SparseArray to expressions using Dot: m.Array[x, {100}]//ShortPossible Issues (9)If a position is repeated in the rule list for SparseArray, the first instance is used: SparseArray[{{1, 1} -> 1, {1, 1} -> 2}]//NormalSparseArray[{{i_, i_} -> 1, {1, 1} -> 2}]//NormalSparseArray objects can represent data too large to represent in normal form: s = SparseArray[{{i_, i_} -> i}, {100000, 100000}]Using 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 - 2sMatrixForm[r]Use SparseArray to recompute the sparse structure: SparseArray[r]The internal structure of a SparseArray representation is not unique and SameQ detects this: s1 = SparseArray[{1, 1} -> 1, {2, 2}];
(s1 - s1) === SparseArray[{}, {2, 2}]Use SparseArray to recompute the sparse structure: SparseArray[s1 - s1] === SparseArray[{}, {2, 2}]Note that Equal works as expected: (s1 - s1) == SparseArray[{}, {2, 2}]The internal structure of SparseArray representation is not unique and setting parts can change that structure: s1 = SparseArray[{{3}, {5}} -> 1, {6}];
s2 = SparseArray[{{5}} -> 1, {6}];
s2[[3]] = 1;Test if the SparseArray instances are the same: s1 === s2Use SparseArray to recompute the sparse structure: s1 === SparseArray[s2]Note that Equal works as expected: s1 == s2Operations with side effects may give different values when iterating over SparseArray: s = SparseArray[{{1} -> 2, {4} -> 3}]Module[{i = 1}, Scan[i++&, s];i]Module[{i = 1}, Scan[i++&, Normal[s]];i]With Reap and Sow you can see what elements are accessed: Reap[Map[Sow, s]]Reap[Map[Sow, Normal[s]]]For a SparseArray object, Part gives parts of the represented list: s = SparseArray[{i_} -> i, {5}]Part[s, 1]The FullForm is a way of reconstructing the object from basic storage information: FullForm[s]A SparseArray object is treated as atomic for functions that do not work on the representation: s = SparseArray[{i_, i_} -> i, {5, 5}]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 ArrayRules to get the information without expanding: Cases[ArrayRules[s], (p_ -> i_ /; i > 0) -> i, 1]Even for a dense SparseArray there can be a division by 0: s = SparseArray[{{2, 1}, {1, 2}}]1 / sThe density of a SparseArray is 1: 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: 1 / Normal[s]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}]See AlsoArrayRules SparseArrayQ Normal Band CoefficientArrays ArrayPlot Array ConstantArray NumericArray Association Function Repository: SparseAssociation Related GuidesRelated Workflows
Related Workflows
▪
HistoryIntroduced 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).
TextWolfram Research (2003), SparseArray, Wolfram Language function, https://reference.wolfram.com/language/ref/SparseArray.html (updated 2021). CMSWolfram Language. 2003. "SparseArray." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/SparseArray.html. APAWolfram 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]} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. |