Jump to content

University of Florida/Eml4500/f08.FEABBQ.Jayma/HW1

From Wikiversity

Jose's Contribution to HW#1

[edit | edit source]

This is me finishing what Grant started...

Section 19 - Sparse Matrix Computations

[edit | edit source]

Matlab typically handles matrices of which the contents are mostly nonzero. These matrices are considered "dense". A matrix with mostly zero entries is considered "sparse" and is worth defining for the possibility of ignoring the zero values and operating on the nonzero values only. The benefit of this is to reduce the memory footprint of the matrix and the computation time. Suppose a matrix is created by the following command,

    F=floor(10*rand(6))

and that the matrix is filled with mostly zeros by the following command,

    F=triu(tril(F,1),-1)

the output of which looks like

    F =
    9     4     0     0     0     0
    2     0     7     0     0     0
    0     8     1     0     0     0
    0     0     4     3     6     0
    0     0     0     8     2     4
    0     0     0     0     1     4

The nonzero values of the matrix can be easily found with the command, followed by the output,

    S=sparse(F)
    S =
    (1,1)        9
    (2,1)        2
    (1,2)        4
    (3,2)        8
    (2,3)        7
    (3,3)        1
    (4,3)        4
    (4,4)        3
    (5,4)        8
    (4,5)        6
    (5,5)        2
    (6,5)        1
    (5,6)        4
    (6,6)        4

The following commands will return the matrix to it's previous form and check it's storage mode, respectively

    F=full(S)
    issparse(F)

Sparse matrices can also be generated directly as opposed to calling the sparse function on a full ("dense") matrix. A sparse diagonal matrix can be generated with the spdiags command. The output is similar to calling the sparse.

    m=6;n=6;e=ones(n,1);d=-2*e;
    T=spdiags([e,d,e],[-1,0,1],m,n)
    T =
    (1,1)       -2
    (2,1)        1
    (1,2)        1
    (2,2)       -2
    (3,2)        1
    (2,3)        1
    (3,3)       -2
    (4,3)        1
    (3,4)        1
    (4,4)       -2
    (5,4)        1
    (4,5)        1
    (5,5)       -2
    (6,5)        1
    (5,6)        1
    (6,6)       -2

speye,sparse,spones,sprandn are the sparse analogs of eye,zeros,ones and randn, respectively. A more specific approach would be to define the nonzero values of the sparse matrix individually. For example (followed by output)

    i=[1 2 3 4 4 4];j=[1 2 3 1 2 3]; s=[5 6 7 8 9 10];
    full(S),S = sparse(i,j,s,4,3)
    ans =
    5     0     0
    0     6     0
    0     0     7
    8     9    10
    S =
    (1,1)        5
    (4,1)        8
    (2,2)        6
    (4,2)        9
    (3,3)        7
    (4,3)       10

The nonzero entities were stored in the row vector s. Another example of directly defining the nonzero values of a sparse matrix is

    n=6;e=floor(10*rand(n-1,1));E=sparse(2:n,1:n-1,e,n,n)
    E =
    (2,1)        8
    (3,2)        5
    (4,3)        2
    (5,4)        6
    (6,5)        8

The output of Matlab arithmetic and functions will be in one of the two storage modes (either Full(F) or Sparse(S)). The following defines the output results of the manipulation done to either full or sparse matrices

    Sparse: S+S, S*S, S.*S, S.*F, S^n, S.^n, S\S, inv(S), chol(S), lu(S), diag(S)
            max(S), sum(S)
    Full: S+F, S*F, S\F, F\S