WOLFRAM

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)Summary of the most common use cases

Get the explicit elements in a SparseArray:

Out[1]=1
Out[2]=2

These rules are sufficient to efficiently construct an identical SparseArray:

Out[3]=3
Out[4]=4

Scope  (2)Survey of the scope of standard use cases

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

Out[1]=1

A SparseArray with a default value of 2:

Out[2]=2
Out[3]=3

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

Out[4]=4

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

Out[5]=5

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

Out[1]=1
Out[2]=2

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

Out[3]=3

Positions of 0 with 1 taken as default:

Out[4]=4

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

Out[5]=5

Applications  (4)Sample problems that can be solved with this function

Get the number of explicit elements in a SparseArray:

Out[1]=1
Out[2]=2

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

Out[1]=1

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

Out[2]=2

SparseArray objects with positive and negative values:

Out[3]=3
Out[4]=4

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

Out[1]=1
Out[2]=2
Out[3]=3

Lower triangular part with 1s on the diagonal:

Out[4]=4

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:

Out[1]=1
Out[2]=2

MatrixPlot generally makes a visually better plot:

Out[3]=3

Properties & Relations  (2)Properties of the function, and connections to other functions

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

Out[1]=1
Out[2]=2
Out[3]=3

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

Out[4]=4
Out[5]=5

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

Out[2]=2
Out[3]=3
Out[4]=4
Out[5]=5

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

Out[6]=6
Wolfram Research (2003), ArrayRules, Wolfram Language function, https://reference.wolfram.com/language/ref/ArrayRules.html.
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.

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.

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

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

BibTeX

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

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

BibLaTeX

@online{reference.wolfram_2025_arrayrules, organization={Wolfram Research}, title={ArrayRules}, year={2003}, url={https://reference.wolfram.com/language/ref/ArrayRules.html}, note=[Accessed: 29-March-2025 ]}

@online{reference.wolfram_2025_arrayrules, organization={Wolfram Research}, title={ArrayRules}, year={2003}, url={https://reference.wolfram.com/language/ref/ArrayRules.html}, note=[Accessed: 29-March-2025 ]}