ArrayRules

ArrayRules[SparseArray[]]

gives the rules {pos1val1,pos2val2,} specifying elements in a sparse array.

ArrayRules[list]

gives rules for SparseArray[list].

Details

  • The last element of ArrayRules[s] is always {_,_,}->def, where def is the default value for unspecified elements in the sparse array. »
  • ArrayRules[list,val] takes the default value to be val. »
  • ArrayRules[list] assumes a default value of 0. »

Examples

open allclose all

Basic Examples  (1)

Get the explicit elements in a SparseArray:

These rules are sufficient to efficiently construct an identical SparseArray:

Scope  (2)

The last element of ArrayRules[s] is always {_,_,}->def:

A SparseArray with a default value of 2:

You can override this by explicitly specifying what default you would like:

These will construct a SparseArray identical to SparseArray[m,Automatic,1]:

Positions of 1 in an explicit array with the default taken to be 0:

These will construct a SparseArray identical to SparseArray[a]:

Positions of 0 with 1 taken as default:

These will construct a SparseArray identical to SparseArray[a,Automatic,1]:

Applications  (4)

Get the number of explicit elements in a SparseArray:

Get the explicit elements of a sparse array satisfying a condition:

Note the more complicated pattern is needed since Cases has special behavior for Rule:

SparseArray objects with positive and negative values:

Get the upper and lower triangular parts of a sparse matrix:

Lower triangular part with 1s on the diagonal:

This just happens to be the LU decomposition of a tridiagonal matrix:

Make a plot showing the positions of the explicit elements of a SparseArray with tooltips:

MatrixPlot generally makes a visually better plot:

Properties & Relations  (2)

For a SparseArray s, SparseArray[ArrayRules[s],Dimensions[s]] is identical to s:

Specifying the dimensions is needed since they would be inferred from explicit elements:

For an explicit array ArrayRules can be written in terms of Position:

This will not work for SparseArray objects because pattern matching works on the FullForm:

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

Text

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

CMS

Wolfram Language. 2003. "ArrayRules." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ArrayRules.html.

APA

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

BibTeX

@misc{reference.wolfram_2024_arrayrules, author="Wolfram Research", title="{ArrayRules}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/ArrayRules.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_arrayrules, organization={Wolfram Research}, title={ArrayRules}, year={2003}, url={https://reference.wolfram.com/language/ref/ArrayRules.html}, note=[Accessed: 21-December-2024 ]}