A.1.2 Creating sparse matrices

GNU Octave uses the compressed column format storage technique A.7 There are several modes to create a sparse matrix. Sparse matrices can be constructed from matrices or vectors.

The function $\mathbf{sparse(iv,jv,sv)}$ has constructed a sparse matrix $\mathbf{spA}$ (shown in computing diary A.1) from three vectors representing the row ($\mathbf{iv}$), column ($\mathbf{jv}$) and data ($\mathbf{sv}$) (see Eq. (A.12)). In this diary, the conversion of the sparse matrix $\mathbf{spA}$ to a full matrix $\mathbf{A}$ ( $\mathbf{full(spA)}$) is shown.

Computing diary A.1  
octave:1> iv = [1 2 3 3 4 4 5 5 6 6 7 7 8 8]
iv =

   1   2   3   3   4   4   5   5   6   6   7   7   8   8

octave:2> jv = [8 7 4 8 5 8 3 7 6 7 2 6 1 6]
jv =

   8   7   4   8   5   8   3   7   6   7   2   6   1   6

octave:3> sv = [6 -6 -8 -10 8 -2 8 -10 -8 -2 -1 1 1 4]
sv =

    6   -6   -8  -10    8   -2    8  -10   -8   -2   -1    1    1    4

octave:4> spA=sparse(iv, jv, sv)
spA =
Compressed Column Sparse (rows = 8, cols = 8, nnz = 14 [22%])
  (8, 1) ->  1
  (7, 2) -> -1
  (5, 3) ->  8                                                                                                                                                
  (3, 4) -> -8                                                                                                                                                
  (4, 5) ->  8                                                                                                                                                
  (6, 6) -> -8                                                                                                                                                
  (7, 6) ->  1                                                                                                                                                
  (8, 6) ->  4                                                                                                                                                
  (2, 7) -> -6                                                                                                                                                
  (5, 7) -> -10                                                                                                                                               
  (6, 7) -> -2                                                                                                                                                
  (1, 8) ->  6                                                                                                                                                
  (3, 8) -> -10                                                                                                                                               
  (4, 8) -> -2
octave:5> A=full(spA)
A =                                                                                                                                                                                                                                                                                                                       
    0    0    0    0    0    0    0    6                                                                                                                      
    0    0    0    0    0    0   -6    0                                                                                                                      
    0    0    0   -8    0    0    0  -10                                                                                                                      
    0    0    0    0    8    0    0   -2                                                                                                                      
    0    0    8    0    0    0  -10    0                                                                                                                      
    0    0    0    0    0   -8   -2    0                                                                                                                      
    0   -1    0    0    0    1    0    0                                                                                                                      
    1    0    0    0    0    4    0    0                                                                                                                                                                                                                                                                                   
octave:6>


The non-zero elements of the matrix $\mathbf{A}$ of Eq. (A.11) can be represented as the matrix $\mathbf{A1}$ with columns $\mathbf{iv}$, $\mathbf{jv}$ and $\mathbf{sv}$, see Eq. (A.12):

$\displaystyle \mathbf{A1} = \left[ \begin{array}{ccc}
1 & 8 & 6 \\
2 & 7 & -6 ...
...
7 & 2 & -1 \\
7 & 6 & 1 \\
8 & 1 & 1 \\
8 & 6 & 4
\end{array}\right] \quad$     (A.13)

The function $\mathbf{spconvert(A1)}$ (given in computing diary A.2) constructs a sparse matrix $\mathbf{spA}$ from the matrix $\mathbf{A1}$.

Computing diary A.2  
octave:1>  A1=[1   8    6;
    2   7   -6; 
    3   4   -8; 
    3   8  -10; 
    4   5    8; 
    4   8   -2; 
    5   3    8; 
    5   7  -10; 
    6   6   -8; 
    6   7   -2; 
    7   2   -1; 
    7   6    1; 
    8   1    1; 
    8   6    4] 
 
A1 =
    1    8    6
    2    7   -6
    3    4   -8
    3    8  -10
    4    5    8
    4    8   -2                                                                                                                                                                              
    5    3    8                                                                                                                                                                              
    5    7  -10                                                                                                                                                                              
    6    6   -8                                                                                                                                                                              
    6    7   -2                                                                                                                                                                              
    7    2   -1                                                                                                                                                                              
    7    6    1                                                                                                                                                                              
    8    1    1                                                                                                                                                                              
    8    6    4

octave:2> spA=spconvert(A1)                                                                                                                                                                          
spA =                                                                                                                                                                                                                                                                                                                                                                                    
Compressed Column Sparse (rows = 8, cols = 8, nnz = 14 [22%])    

  (8, 1) ->  1                                                                                                                                                                               
  (7, 2) -> -1                                                                                                                                                                               
  (5, 3) ->  8                                                                                                                                                                               
  (3, 4) -> -8                                                                                                                                                                               
  (4, 5) ->  8                                                                                                                                                                               
  (6, 6) -> -8                                                                                                                                                                               
  (7, 6) ->  1                                                                                                                                                                               
  (8, 6) ->  4                                                                                                                                                                               
  (2, 7) -> -6                                                                                                                                                                               
  (5, 7) -> -10                                                                                                                                                                              
  (6, 7) -> -2                                                                                                                                                                               
  (1, 8) ->  6                                                                                                                                                                               
  (3, 8) -> -10                                                                                                                                                                              
  (4, 8) -> -2

The function $\mathbf{sparse(A)}$ converts the full matrix $\mathbf{A}$ to a sparse matrix (see computing diary A.3).

Computing diary A.3  
octave:6> A
A =                                                                                                                                                                                                                                                                                                                       
    0    0    0    0    0    0    0    6                                                                                                                      
    0    0    0    0    0    0   -6    0                                                                                                                      
    0    0    0   -8    0    0    0  -10                                                                                                                      
    0    0    0    0    8    0    0   -2                                                                                                                      
    0    0    8    0    0    0  -10    0                                                                                                                      
    0    0    0    0    0   -8   -2    0                                                                                                                      
    0   -1    0    0    0    1    0    0                                                                                                                      
    1    0    0    0    0    4    0    0
octave:7> spA=sparse(A)
spA =
Compressed Column Sparse (rows = 8, cols = 8, nnz = 14 [22%])
  (8, 1) ->  1
  (7, 2) -> -1
  (5, 3) ->  8
  (3, 4) -> -8
  (4, 5) ->  8
  (6, 6) -> -8
  (7, 6) ->  1
  (8, 6) ->  4
  (2, 7) -> -6
  (5, 7) -> -10
  (6, 7) -> -2
  (1, 8) ->  6
  (3, 8) -> -10
  (4, 8) -> -2
octave:8>


andres
2014-09-09