LinearAlgebra
CompressedSparseForm
compute compressed sparse row and column forms
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
CompressedSparseForm(A, opts)
A
-
sparse Matrix with hardware data type
opts
optional equations controlling the details of the computation
form = row or form = column
This option determines whether the compressed sparse column form or the compressed sparse row form is returned. The default is compressed sparse column form.
output = CB or R or X or a list of these values
If output is set to CB, then only CB is returned. Similarly for R and X. If a list of these values is selected, then Maple returns the corresponding values in that order, as an expression sequence. The default value is [CB, R, X]: return the sequence of all three Vectors.
cbbase = posint
rbase = posint
These options determines at what number Maple starts numbering the rows and columns. The default is 1, corresponding to the standard Maple convention. Other values, in particular 0, are mainly useful if the result is passed to external code.
The setting of cbbase changes the values in CB: it changes the start of indexing into R and X. The setting of rbase changes the values in R: it changes the start of indexing into A.
length0 = true or length0 = false
Maple has no problems working with Vectors of length 0, but some external tools do. If length0 is set to false, and A has no nonzero entries, then Maple returns values pretending that the [1, 1] entry is stored and has value 0. This and the includediagonal and structuralsymmetry options below are the only cases in which X contains a zero value.
includediagonal = true or includediagonal = false
If the includediagonal option is set to true, then Maple includes all diagonal entries in the result, even if their value is zero. If it is set to false (the default), then Maple omits all zero entries, including if they occur on the diagonal. The former is useful when preparing input for some external tools.
structuralsymmetry = true or structuralsymmetry = false
If the structuralsymmetry option is set to true, then Maple ensures that, whenever a value Ai,j is included in the result, then Aj,i is also included, even if it is zero. If it is set to false (the default), then Maple does not do this. The former is useful when preparing input for some external tools.
The structuralsymmetry option can only be used if A is a square Matrix.
The CompressedSparseForm function computes either the compressed sparse row form or the compressed sparse column form of the input Matrix A, which needs to have one of the hardware data types. It thus performs the opposite function to FromCompressedSparseForm.
The compressed sparse column form of an n by m Matrix A with k nonzero entries consists of three Vectors, CB, R, and X. These are obtained by sorting the k nonzero entries of A first by column, and then within each column sorting the entries by row. Now X is the k-element Vector of these nonzero values in A in this order, and R is the k-element Vector of row indices at which these values occur. CB is an n+1-element Vector; CBi is the index in both X and R where the ith column starts. We always have CB1=1 and CBn+1=k+1, so that the entries in column i have indices CBi up to, but not including, CBi+1. (If the cbbase option below is used, then CB1=cbbase and CBn+1=k+cbbase and a similar statement holds.)
The compressed sparse row form is very similar, but with the roles of rows and columns interchanged. Specifically, we first sort A by row and then within each row by column; X is still the nonzero values occurring in A. However, R is the k-element Vector of column indices. Similarly, CBi is the index in X and R where the ith row starts.
The compressed sparse forms are most useful for sparse Matrices. If A does not have sparse storage (as determined by rtable_option⁡X,storage being either sparse or sparseupper or sparselower), then a sparse storage copy is made before determining the compressed sparse form.
If A has any indexing functions (as determined by rtable_indfns⁡A), then these are bypassed when determining the compressed sparse row or column form: since CompressedSparseForm is a low-level command, Maple returns the compressed sparse form of the stored entries only. For example, if a Matrix is defined with option shape=symmetric and storage=sparseupper, then only one of each pair of off-diagonal entries will be represented in the compressed sparse form. However, if A has indexing functions but no sparse storage, then the copy made to give it sparse storage, as explained above, typically gets rid of the indexing function and stores all nonzero entries.
The code for CompressedSparseForm relies on A being a NAG-sparse Matrix (potentially after copying it to give it sparse storage); that is, its datatype as returned by rtable_options needs to be one of these values:
sfloat,complex⁡sfloat,integer1,integer2,integer4,integer8,float4,float8,complex8
with⁡LinearAlgebra:
Let us examine the compressed sparse row and column form of an arbitrary matrix.
m≔Matrix⁡6,6,1,2=−81,2,3=−55,2,4=−15,3,1=−46,3,3=−17,3,4=99,3,5=−61,4,2=18,4,5=−78,5,6=22,datatype=integer4
m≔0−81000000−55−1500−460−1799−61001800−7800000022000000
CompressedSparseForm⁡m
124681011,3142323345,−46−8118−55−17−1599−61−7822
CompressedSparseForm⁡m,form=row
1248101111,2341345256,−81−55−15−46−1799−6118−7822
Setting cbbase=0 causes the entries of CB to be reduced by 1.
CompressedSparseForm⁡m,cbbase=0
01357910,3142323345,−46−8118−55−17−1599−61−7822
Setting rbase=0 causes the entries of R to be reduced by 1.
CompressedSparseForm⁡m,rbase=0
124681011,2031212234,−46−8118−55−17−1599−61−7822
Using the includediagonal option ensures that all 6 diagonal entries are included, even though most of them are 0.
CompressedSparseForm⁡m,includediagonal
m2≔Matrix⁡0,1,0,2,0,0,3,0,4,5,6,7,datatype=float
m2≔0.1.0.2.0.0.3.0.4.5.6.7.
cb,r,x≔CompressedSparseForm⁡m2
cb,r,x≔1468,2341434,2.3.5.1.6.4.7.
We can obtain the entries of r corresponding to the second column as follows.
column≔2
rcbcolumn..cbcolumn+1−1
14
Similarly for the entries of x.
xcbcolumn..cbcolumn+1−1
1.6.
We will omit the last row of m2 and illustrate the includediagonal and structuralsymmetry options. First, this is what the result looks like without these options.
m3≔m21..3
m3≔0.1.0.2.0.0.3.0.4.
CompressedSparseForm⁡m3
1345,2313,2.3.1.4.
Adding the structuralsymmetry option makes Maple include the entry for m31,3, even though it is 0, because m33,1 is nonzero.
CompressedSparseForm⁡m3,structuralsymmetry
1346,23113,2.3.1.0.4.
Adding the includediagonal option makes Maple include the entries for m31,1 and m32,2, even though they are 0.
CompressedSparseForm⁡m3,includediagonal
1467,123123,0.2.3.1.0.4.
Adding both of these options makes Maple include the entries for m31,1, m32,2, and m31,3.
CompressedSparseForm⁡m3,includediagonal,structuralsymmetry
1468,1231213,0.2.3.1.0.0.4.
CompressedSparseForm does not work with Matrices that are not of a hardware data type.
m4≔Matrix⁡3,3,i,j↦i−j
m4≔0−1−210−1210
CompressedSparseForm⁡m4
Error, (in LinearAlgebra:-CompressedSparseForm) cannot compute compressed sparse form of a Matrix with non-hardware datatype anything
To make this work, we need to recreate m4 with, for example, option datatype=integer4.
m4≔Matrix⁡m4,datatype=integer4
cb,r,x≔CompressedSparseForm⁡m4
cb,r,x≔1357,231312,12−11−2−1
Here is an example of the antisymmetric indexing function being ignored.
m5≔Matrix⁡6,datatype=integer4,shape=antisymmetric,storage=sparselower,i,j↦`if`⁡irem⁡i+j,2=1,i−j,0
m5≔0−10−30−510−10−30010−10−33010−1003010−1503010
CompressedSparseForm⁡m5
146891010,246354656,135131311
The LinearAlgebra[CompressedSparseForm] command was introduced in Maple 17.
For more information on Maple 17 changes, see Updates in Maple 17.
The LinearAlgebra[CompressedSparseForm] command was updated in Maple 2022.
The length0, includediagonal and structuralsymmetry options were introduced in Maple 2022.
For more information on Maple 2022 changes, see Updates in Maple 2022.
See Also
LinearAlgebra[FromCompressedSparseForm]
LinearAlgebra[FromSplitForm]
LinearAlgebra[SplitForm]
rtable_indfns
rtable_options
Download Help Document