Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Mathematics and Algorithms > Matrices and Linear Algebra > Sparse Arrays >

ArrayRules

ArrayRules[SparseArray[...]]
gives the rules {pos1->val1, pos2->val2, ...} specifying elements in a sparse array.
ArrayRules[list]
gives rules for SparseArray[list].
  • The last element of ArrayRules[s] is always {_, _, ...}->def, where def is the default value for unspecified elements in the sparse array.  »
Get the explicit elements in a SparseArray:
These rules are sufficient to efficiently construct an identical SparseArray:
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]:
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 ones 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:
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:
New in 5
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team