The SPRSIN function converts an array, or list of subscripts and values, into a row-index sparse storage mode, retaining only elements with an absolute magnitude greater than or equal to the specified threshold. The list form is much more efficient than the array form if the density of the matrix is low.
SPRSIN is based on the routine sprsin described in section 2.7 of Numerical Recipes in C: The Art of Scientific Computing (Second Edition), published by Cambridge University Press, and is used by permission.
Examples
Example1
Suppose we wish to convert the following array to sparse storage format:
A = [[ 5.0, -0.2, 0.1], $
[ 3.0, -2.0, 0.3], $
[ 4.0, -1.0, 0.0]]
sparse = SPRSIN(A, THRESH = 0.5)
The variable SPARSE now contains a representation of A in structure form. See the description of FULSTR for an example that restores such a structure to full storage mode.
Example2
This example demonstrates how to use the list form of the call to SPRSIN. The following line of code creates a sparse matrix, equivalent to a 100 by 100 identity matrix, i.e. all diagonal elements are set to 1, all other elements are zero:
I100 = SPRSIN(LINDGEN(100), LINDGEN(100), REPLICATE(1.0,100), 100)
Syntax
Result = SPRSIN( A [, /COLUMN] [, /DOUBLE] [, THRESHOLD=value] )
or
Result = SPRSIN(Columns, Rows, Values, N [, /DOUBLE] [, THRESHOLD=value])
Return Value
The result is a row-indexed sparse array contained in structure form. The structure consists of two linear sparse storage vectors: SA, a vector of array values, and IJA, a vector of subscripts to the SA vector. The length of these vectors is equal to 1 plus the number of diagonal elements of the array, plus the number of off-diagonal elements with an absolute magnitude greater that or equal to the threshold value. Diagonal elements of the array are always retained even if their absolute magnitude is less than the specified threshold.
Arguments
A
An n by n array of any type except string or complex.
Columns
A vector containing the column subscripts of the non-zero elements. Values must be in the range of 0 to (N-1).
Rows
A vector, of the same length as Column, containing the row subscripts of the non-zero elements. Values must be in the range of 0 to (N-1).
Values
A vector, of the same length as Column, containing the values of the non-zero elements.
N
The size of the resulting sparse matrix.
Keywords
COLUMN
Set this keyword if the input array A is in column-major format (composed of column vectors) rather than in row-major format (composed of row vectors). This keyword is not allowed in the list form of the call.
DOUBLE
Set this keyword to convert the sparse array to double-precision.
THRESHOLD
Use this keyword to set the criterion for deciding the absolute magnitude of the elements to be retained in sparse storage mode. For single-precision calculations, the default value is 1.0 x 10-7. For double-precision values, the default is 1.0 x 10-14.
Version History
See Also
FULSTR, LINBCG, SPRSAB, SPRSAX, SPRSTP, READ_SPR, WRITE_SPR