21.06.2014 Views

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.6.1 Sparse matrix storage formats<br />

Special sparse matrix storage formats store only non-zero entries:<br />

(➣ usually O(n + m) storage required for sparse n × m-matrix)<br />

• Compressed Row Storage (CRS)<br />

• Compressed Column Storage (CCS)<br />

• Block Compressed Row Storage (BCRS)<br />

• Compressed Diagonal Storage (CDS)<br />

• Jagged Diagonal Storage (JDS)<br />

• Skyline Storage (SKS)<br />

→ used by MATLAB<br />

Option:<br />

⎛<br />

⎞<br />

10 0 0 0 −2 0<br />

3 9 0 0 0 3<br />

A =<br />

0 7 8 7 0 0<br />

⎜ 3 0 8 7 5 0<br />

⎟<br />

⎝ 0 8 0 9 9 13⎠<br />

0 4 0 0 2 −1<br />

val 10 -2 3 9 3 7 8 7 3 . . . 9 13 4 2 -1<br />

col_ind 1 5 1 2 6 2 3 4 1 . . . 5 6 2 5 6<br />

row_ptr 1 3 6 9 13 17 20<br />

diagonal CRS format (matrix diagonal stored in separate array)<br />

2.6.2 Sparse matrices in MATLAB<br />

✸<br />

mandatory for large sparse matrices.<br />

Detailed discussion in [17]<br />

Example 2.6.2 (Compressed row-storage (CRS) format).<br />

Data for matrix A = (a ij ) ∈ K n,n kept in three arrays<br />

Ôº½½ ¾º<br />

Initialization: A = sparse(m,n); A = spalloc(m,n,nnz)<br />

A = sparse(i,j,s,m,n);<br />

A = spdiags(B,d,m,n); A = speye(n); A = spones(S);<br />

Ôº½¿ ¾º<br />

double * val size nnz(A) := #{(i,j) ∈ {1, ...,n} 2 , a ij ≠ 0}<br />

unsigned int * col_ind size nnz(A)<br />

unsigned int * row_ptr size n + 1 & row_ptr[n + 1] = nnz(A) + 1<br />

Example 2.6.3 (Accessing rows and columns of sparse matrices).<br />

0<br />

Pattern for matrix A for n = 16<br />

nnz(A) ˆ= (number of nonzeros) of A<br />

2<br />

10 0<br />

row access<br />

column access<br />

O(n)<br />

Access to matrix entry a ij ≠ 0, 1 ≤ i,j ≤ n:<br />

4<br />

10 −1<br />

val<br />

val[k] = a ij<br />

⇔<br />

{<br />

col_ind[k] = j ,<br />

row_ptr[i] ≤ k < row_ptr[i + 1] ,<br />

a ij<br />

1 ≤ k ≤ nnz(A) .<br />

6<br />

8<br />

10<br />

access time [s]<br />

10 −2<br />

10 −3<br />

10 −4<br />

col_ind<br />

j<br />

12<br />

14<br />

10 −5<br />

row_ptr<br />

beginning of i-th row<br />

16<br />

0 2 4 6 8<br />

nz = 32<br />

10 12 14 16<br />

Fig. 11<br />

10 1 size n of sparse quadratic matrix<br />

10 −6<br />

10 0 10 1 10 2 10 3 10 4 10 5 10 6 10 7<br />

Fig. 12<br />

i<br />

Ôº½¾ ¾º<br />

Code 2.6.4: timing access to rows/columns of a sparse matrix<br />

1 figure ; spy ( spdiags ( repmat([−1 2 5 ] ,16 ,1) ,[ −8 ,0 ,8] ,16 ,16) ) ;<br />

2 t i t l e ( ’ Pattern f o r m a trix { \ b f A} f o r n = 16 ’ , ’ f o n t s i z e ’ ,14) ;<br />

3 p r i n t −depsc2 ’ . . / PICTURES/ spdiagsmatspy . eps ’ ;<br />

Ôº½ ¾º<br />

5 t = [ ] ;<br />

4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!