Project 1: Sparse Matrix Class - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Project 1: Sparse Matrix Class

Description:

Discuss experiments on matrix multiplication. Note: ... of Sparse Matrix (cont.) Three Vector ... 1. Write the columnPointers vector for matrix M. ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 17
Provided by: csUi
Category:
Tags: class | matrix | project | sparse

less

Transcript and Presenter's Notes

Title: Project 1: Sparse Matrix Class


1
Project 1 Sparse Matrix Class
  • A matrix is sparse if it has only a small number
    of non-zeros.
  • t non-zeros mn entries
  • t is very small comparing to mn
  • Example - A 55 sparse matrix M with 11 non-zeros

2
Todays Outline
  • Understand Sparse Matrix representation
  • Go over methods in Sparse Matrix class
  • Algorithms used in set and removeRow methods
  • Discuss experiments on matrix multiplication
  • Note
  • In this project, we only deal with integer
    matrices.

3
Store Sparse Matrices
  • Sparse Matrices in real applications are usually
    huge. Much space can be saved if we only store
    non-zeros.
  • Space cost for all matrix entries O(mn)
  • Space cost for non-zeros entries O(t)
  • Problem If only non-zeros are stored, we also
    need to store their location in a matrix. So a
    good representation is required.

4
Representation of Sparse Matrix
  • Five protected variables are defined in Sparse
    Matrix class
  • Height number of rows
  • Width number of columns
  • 3 Vector Objects to store non-zeros entries and
    their locations.

5
Representation of Sparse Matrix (cont.)
  • Three Vector variables in class SparseMatrix
  • values
  • A Vector whose size of non-zero entries
  • Elements are stored by column.
  • Exercise
  • Write the values vector for matrix M.

6
Representation of Sparse Matrix (cont.)
  • columnPointers
  • A vector whose size n of columns
  • It contains the starting location for each column
    in vector values.
  • columnPointersi c means the first non-zero
    entry in column i has a location c in values.
  • If column i are all zeros, then columnPointersi
    -1.
  • Exercise
  • 1. Write the columnPointers vector for matrix M.
  • 2. How to decide the number of non-zeros in
    column i?

7
Representation of Sparse Matrix (cont.)
  • rowIndices
  • A vector whose size size of values of
    non-zero entries
  • It contains the row indices for those non-zero
    entries in values.
  • rowIndicesi r means the ith element of
    values is in row r of the sparse matrix.
  • Exercise
  • Write the rowIndices vector for matrix M.

8
Methods in Sparse Matrix Class
  • Sparse Matrix class support all the methods in
    Matrix class, plus 3 new methods.
  • More details about removeRow and removeCol methods

9
Methods in Sparse Matrix Class
  • Three New methods
  • public SparseMatrix ( Matrix x )
  • //constructs a SparseMatrix represeanting the
    same matrix as x.
  • public Matrix getRow (int r)
  • //returns the row in the same way as removeRow,
    without removing it from the matrix.
  • public Matrix getCol (int c)
  • //Return the row in the same way as removeCol,
    without removing it from the matrix.

10
Algorithm for set methods
  • The set method has the same function as in class
    Matrix. That is, set a new value at specified
    location.
  • Receive 3 parameters public void set (int r, int
    c, int value)
  • Algorithm
  • Step 1. If (value 0 Mrc0), return
  • If (value ! 0), go to Step 2
  • If (value 0 Mrc!0), go to step
    4.
  • Step 2. If (column c has no non-zeros entry ) or
    (Mrc 0), go to step 3. Otherwise go to
    Step 5.
  • Step 3. Find the location in values and add
    value. Update columnPointers and rowIndices
    accordingly.
  • Step 4. Remove Mrc from values and rowIndices
    , update columnPointers .
  • Step 5. Find the location l of Mrc in values.
    Set valuesl value.
  • Q1 At most how many values in columnPointers
    need to be updated in step 3?
  • Q2 Is it easy to find the location of Mrc?

11
Algorithm for removeRow methods
  • The removeRow method removes a row from matrix
    and returns a 2-row matrix.
  • Receive 1 parameter public void removeRow (int
    r)
  • To remove a row, we need to do the following
    work.
  • Decrease Height by 1.
  • Remove elements from vector values and rowIndices
    if the row has non-zeros.
  • Update the vector columnPointers
  • Construct the returning matrix object

12
Algorithm for removeRow methods (cont.)
  • Algorithm - public void removeRow (int r)
  • Step 1. Search rowIndices for next non-zero value
    in row r. If a non-zero in row r is found, say v
    valuesl , go to step 2. Otherwise go to step
    3
  • Step 2. Determine the column index c for v. Add v
    to row_val_vect, c to col_vect, l to indice_vect
    Go back to step 1
  • Step 3. Construct returning Matrix based on
    row_val_vect and col_vect
  • Step 4. Remove non-zeros in row r from values and
    rowIndices. Update columnPointers.

13
Tests and Experiments - Matrix Multiplication
  • Let C be the product of matrix A and B, in order
    to the multiplication, the dimensions of the
    matrics must satisfy that
  • (mn) (np) (mp)
  • A B C
  • Entries in C is defined as the following
  • C(i,j) A(i,0)B(0,j)A(i,1)B((1,j)A(i,n-1)
    B(n-1, j)
  • i.e. C(i,j) (the ith row of A ) (the jth
    column of B)
  • Example

14
Tests and Experiments - Matrix Multiplication
(cont.)
  • In our experiments, we only use square matrices.
  • A ,B, and their product C are all NN matrices.
  • Run the program for N1000, 2000, ..., 10,000.
  • In the construction of A and B, each entry is set
    to be non-zero with probability p. (You can use
    Random object in Java do this generate a number
    uniformly distributed on 0.0, 1.0).

15
Tests and Experiments - Timing
  • For both SparseMatrix and Matrix objects, record
    the time for
  • Matrix construction
  • Matrix multiplication
  • 4 groups of data are generated to do the
    plotting.
  • Matrix construction
  • SparseMatrix construction
  • Matrix mulitplicaion
  • SparseMatrix muliplicaion

16
Plotting tools
  • Matlab, Excel, Gnuplot,
  • x-axis value of N (matrix row/col number)
  • y-axis time
  • Explain your plots with several paragraphs
Write a Comment
User Comments (0)
About PowerShow.com