 FromCompressedSparseForm - Maple Help

LinearAlgebra

 FromCompressedSparseForm
 translate compressed sparse row and column forms to native Maple form Calling Sequence FromCompressedSparseForm(CB, R, X, opts) Parameters

 CB - integer vector of column bounds R - integer vector of row coordinates X - hardware datatype vector with values opts - optional equations controlling the details of the computation Options

 • form = row or form = column
 This option determines whether $\mathrm{CB}$, $R$, and $X$ are interpreted as the compressed sparse column form of $A$ or as its compressed sparse row form. The default is compressed sparse column form.
 • otherdimension = n
 This option determines the number of columns of $A$ in the case of compressed sparse column form, and the number of rows of $A$ in the case of compressed sparse row form. If the option is not specified, Maple uses the maximal entry in $R$.
 • scan = true or scan = false
 This option determines whether or not $X$ is scanned for zeroes. If scan = true, the default, then $X$ is scanned to test if it contains any entries that are equal to 0. If this is the case, the operation is aborted with an error message: such entries are not allowed. If you are certain that there are no entries equal to 0, you can save a little bit of processing time by disabling this scan by passing scan = false to the procedure. Use this option with extreme caution.
 • rbase = posint
 This option determines at what number Maple starts numbering the rows, for compressed sparse column form, or the columns, for compressed sparse row form. The default is 1, corresponding to the standard Maple convention. Other values, in particular 0, are mainly useful if the data come from external code.
 The inverse command, CompressedSparseForm, also has a cbbase option. This corresponds to the first entry of $\mathrm{CB}$ and cannot be set manually for FromCompressedSparseForm. Description

 • The FromCompressedSparseForm function constructs a sparse Matrix $A$ from either its compressed sparse row form or its compressed sparse column form, performing the opposite function to CompressedSparseForm.
 • The compressed sparse column form of an $n$ by $m$ Matrix $A$ with $k$ nonzero entries consists of three Vectors, $\mathrm{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. $\mathrm{CB}$ is an $n+1$-element Vector; ${\mathrm{CB}}_{i}$ is the index in both $X$ and $R$ where the $i$th column starts. We always have ${\mathrm{CB}}_{n+1}={\mathrm{CB}}_{1}+k$, so that the entries in column $i$ have indices ${\mathrm{CB}}_{i}$ up to, but not including, ${\mathrm{CB}}_{i+1}$.
 • 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, ${\mathrm{CB}}_{i}$ is the index in $X$ and $R$ where the $i$th row starts.
 • The code for FromCompressedSparseForm relies on being able to construct $A$ as a NAG-sparse Matrix; that is, the datatype of $X$ as returned by rtable_options needs to be one of these values:
 $\mathrm{sfloat},\mathrm{complex}\left(\mathrm{sfloat}\right),{\mathrm{integer}}_{1},{\mathrm{integer}}_{2},{\mathrm{integer}}_{4},{\mathrm{integer}}_{8},{\mathrm{float}}_{4},{\mathrm{float}}_{8},{\mathrm{complex}}_{8}$ Examples

 > $\mathrm{with}\left(\mathrm{LinearAlgebra}\right):$

Let us examine the compressed sparse row and column form of an arbitrary matrix.

 > $m≔\mathrm{Matrix}\left(5,6,\left\{\left(1,2\right)=-81,\left(2,3\right)=-55,\left(2,4\right)=-15,\left(3,1\right)=-46,\left(3,3\right)=-17,\left(3,4\right)=99,\left(3,5\right)=-61,\left(4,2\right)=18,\left(4,5\right)=-78,\left(5,6\right)=22\right\},\mathrm{datatype}=\mathrm{integer}\left[4\right]\right)$
 ${m}{≔}\left[\begin{array}{cccccc}{0}& {-81}& {0}& {0}& {0}& {0}\\ {0}& {0}& {-55}& {-15}& {0}& {0}\\ {-46}& {0}& {-17}& {99}& {-61}& {0}\\ {0}& {18}& {0}& {0}& {-78}& {0}\\ {0}& {0}& {0}& {0}& {0}& {22}\end{array}\right]$ (1)
 > $\mathrm{cb},r,x≔\mathrm{CompressedSparseForm}\left(m\right)$
 ${\mathrm{cb}}{,}{r}{,}{x}{≔}\left[\begin{array}{c}{1}\\ {2}\\ {4}\\ {6}\\ {8}\\ {10}\\ {11}\end{array}\right]{,}\left[\begin{array}{c}{3}\\ {1}\\ {4}\\ {2}\\ {3}\\ {2}\\ {3}\\ {3}\\ {4}\\ {5}\end{array}\right]{,}\left[\begin{array}{c}{-46}\\ {-81}\\ {18}\\ {-55}\\ {-17}\\ {-15}\\ {99}\\ {-61}\\ {-78}\\ {22}\end{array}\right]$ (2)
 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb},r,x\right)$
 $\left[\begin{array}{cccccc}{0}& {-81}& {0}& {0}& {0}& {0}\\ {0}& {0}& {-55}& {-15}& {0}& {0}\\ {-46}& {0}& {-17}& {99}& {-61}& {0}\\ {0}& {18}& {0}& {0}& {-78}& {0}\\ {0}& {0}& {0}& {0}& {0}& {22}\end{array}\right]$ (3)
 > $\mathrm{cb},r,x≔\mathrm{CompressedSparseForm}\left(m,'\mathrm{form}=\mathrm{row}'\right)$
 ${\mathrm{cb}}{,}{r}{,}{x}{≔}\left[\begin{array}{c}{1}\\ {2}\\ {4}\\ {8}\\ {10}\\ {11}\end{array}\right]{,}\left[\begin{array}{c}{2}\\ {3}\\ {4}\\ {1}\\ {3}\\ {4}\\ {5}\\ {2}\\ {5}\\ {6}\end{array}\right]{,}\left[\begin{array}{c}{-81}\\ {-55}\\ {-15}\\ {-46}\\ {-17}\\ {99}\\ {-61}\\ {18}\\ {-78}\\ {22}\end{array}\right]$ (4)
 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb},r,x,'\mathrm{form}=\mathrm{row}'\right)$
 $\left[\begin{array}{cccccc}{0}& {-81}& {0}& {0}& {0}& {0}\\ {0}& {0}& {-55}& {-15}& {0}& {0}\\ {-46}& {0}& {-17}& {99}& {-61}& {0}\\ {0}& {18}& {0}& {0}& {-78}& {0}\\ {0}& {0}& {0}& {0}& {0}& {22}\end{array}\right]$ (5)
 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb},r,x\right)$
 $\left[\begin{array}{ccccc}{0}& {0}& {-46}& {0}& {0}\\ {-81}& {0}& {0}& {18}& {0}\\ {0}& {-55}& {-17}& {0}& {0}\\ {0}& {-15}& {99}& {0}& {0}\\ {0}& {0}& {-61}& {-78}& {0}\\ {0}& {0}& {0}& {0}& {22}\end{array}\right]$ (6)
 > $\mathrm{cb},r,x≔\mathrm{CompressedSparseForm}\left(m,'\mathrm{cbbase}'=3,'\mathrm{rbase}'=-2\right)$
 ${\mathrm{cb}}{,}{r}{,}{x}{≔}\left[\begin{array}{c}{3}\\ {4}\\ {6}\\ {8}\\ {10}\\ {12}\\ {13}\end{array}\right]{,}\left[\begin{array}{c}{0}\\ {-2}\\ {1}\\ {-1}\\ {0}\\ {-1}\\ {0}\\ {0}\\ {1}\\ {2}\end{array}\right]{,}\left[\begin{array}{c}{-46}\\ {-81}\\ {18}\\ {-55}\\ {-17}\\ {-15}\\ {99}\\ {-61}\\ {-78}\\ {22}\end{array}\right]$ (7)
 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb},r,x,'\mathrm{rbase}'=-2\right)$
 $\left[\begin{array}{cccccc}{0}& {-81}& {0}& {0}& {0}& {0}\\ {0}& {0}& {-55}& {-15}& {0}& {0}\\ {-46}& {0}& {-17}& {99}& {-61}& {0}\\ {0}& {18}& {0}& {0}& {-78}& {0}\\ {0}& {0}& {0}& {0}& {0}& {22}\end{array}\right]$ (8)

If the matrix has zero rows at the bottom, these are not reflected in the compressed sparse column form. (Similarly, zero columns at the right are not reflected in the compressed sparse row form.)

 > $\mathrm{m1}≔\mathrm{Matrix}\left(\left[\left[0,1,0\right],\left[2,0,0\right],\left[3,0,4\right]\right],'\mathrm{datatype}=\mathrm{float}'\right)$
 ${\mathrm{m1}}{≔}\left[\begin{array}{ccc}{0.}& {1.}& {0.}\\ {2.}& {0.}& {0.}\\ {3.}& {0.}& {4.}\end{array}\right]$ (9)
 > $\mathrm{m2}≔\mathrm{Matrix}\left(\left[\left[0,1,0\right],\left[2,0,0\right],\left[3,0,4\right],\left[0,0,0\right]\right],'\mathrm{datatype}=\mathrm{float}'\right)$
 ${\mathrm{m2}}{≔}\left[\begin{array}{ccc}{0.}& {1.}& {0.}\\ {2.}& {0.}& {0.}\\ {3.}& {0.}& {4.}\\ {0.}& {0.}& {0.}\end{array}\right]$ (10)
 > $\mathrm{cb1},\mathrm{r1},\mathrm{x1}≔\mathrm{CompressedSparseForm}\left(\mathrm{m1}\right)$
 ${\mathrm{cb1}}{,}{\mathrm{r1}}{,}{\mathrm{x1}}{≔}\left[\begin{array}{c}{1}\\ {3}\\ {4}\\ {5}\end{array}\right]{,}\left[\begin{array}{c}{2}\\ {3}\\ {1}\\ {3}\end{array}\right]{,}\left[\begin{array}{c}{2.}\\ {3.}\\ {1.}\\ {4.}\end{array}\right]$ (11)
 > $\mathrm{cb2},\mathrm{r2},\mathrm{x2}≔\mathrm{CompressedSparseForm}\left(\mathrm{m2}\right)$
 ${\mathrm{cb2}}{,}{\mathrm{r2}}{,}{\mathrm{x2}}{≔}\left[\begin{array}{c}{1}\\ {3}\\ {4}\\ {5}\end{array}\right]{,}\left[\begin{array}{c}{2}\\ {3}\\ {1}\\ {3}\end{array}\right]{,}\left[\begin{array}{c}{2.}\\ {3.}\\ {1.}\\ {4.}\end{array}\right]$ (12)

Therefore, to recover the original Matrix, you may need to use the otherdimension option.

 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb2},\mathrm{r2},\mathrm{x2}\right)$
 $\left[\begin{array}{ccc}{0.}& {1.}& {0.}\\ {2.}& {0.}& {0.}\\ {3.}& {0.}& {4.}\end{array}\right]$ (13)
 > $\mathrm{FromCompressedSparseForm}\left(\mathrm{cb2},\mathrm{r2},\mathrm{x2},'\mathrm{otherdimension}'=4\right)$
 $\left[\begin{array}{ccc}{0.}& {1.}& {0.}\\ {2.}& {0.}& {0.}\\ {3.}& {0.}& {4.}\\ {0.}& {0.}& {0.}\end{array}\right]$ (14) Compatibility

 • The LinearAlgebra[FromCompressedSparseForm] command was introduced in Maple 17.