1 / 8

Lab 3

Lab 3. Why Compressed Row Storage. A sparse matrix has a lot of elements of value zero. Using a two dimensional array to store a sparse matrix wastes a lot of memory. Compressed Row Storage (CRS) format only stores the nonzero elements.

jalthea
Télécharger la présentation

Lab 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 3

  2. Why Compressed Row Storage • A sparse matrix has a lot of elements of value zero. • Using a two dimensional array to store a sparse matrix wastes a lot of memory. • Compressed Row Storage (CRS) format only stores the nonzero elements. • Using CRS format to store a sparse matrix will save a lot of memory.

  3. Compressed Row Storage • val array stores the values of the nonzero elements in a row-wise fashion. • col_ind array stores the corresponding column indices of the elements in the val array. • E.g. col_ind[5] stores the column index of val[5]. • row_ptr array stores the locations in the val array and col_ind array that start a row.

  4. 0 1 2 3 4 5 0 1 2 3 4 5 0 2 5 16 10 -2 3 9 3 7 8 7 … 4 2 -1 val 1 5 1 2 6 2 3 4 … 2 5 6 col_ind the number of nonzero elements in a matrix 0 2 5 8 12 16 19 row_ptr the number of rows +1 • The number of nonzero elements of row i = row_ptr[i+1] - row_ptr[ i ] • The value of nonzero elements of row i: val[ row_ptr[ i ] ], ... , val[ row_ptr[ i+1 ] -1 ]

  5. //Compressed Row Storage format //for a sparse square (mSize X mSize) matrix public class CRS{ //the values of the nonzero elements of the matrix float[] val; //the column indices of the elements in val array int[] col_idx; //locations in the val and col_idx array that start a row int[] row_ptr; //the size of the matrix: the number of rows int mSize=0; //constructor that takes a sparse matrix and convert it to a CRS object CRS( float[][] matrix){... } //print the matrix in CRS format. public void printCRS(){... } //test the program public static void main(String[] args){... } }

  6. CRS( float[][] matrix){ int i, j, index; //the total number of nonzero in the matrix int totalNonZeros; //get the number of rows and columns mSize = matrix.length; //get the total number of nonzero entries in the matrix totalNonZeros = 0; for( i=0; i<mSize; i++){ for( j=0; j<mSize; j++){ if(matrix[i][j] != 0) totalNonZeros++; } } //allocate memory for val and col_idx array val = new float[ totalNonZeros ]; col_idx = new int[ totalNonZeros ]; //allocate memory for row_ptr row_ptr = new int[ mSize+1]; row_ptr[ 0 ] = 0;

  7. val x x ... index //store the matrix in CRS format index = 0;// point to the next position to store the value for( i=0; i<mSize; i++ ){//each row for( j=0; j<mSize; j++ ){//each column if( matrix[i][j] != 0 ){ //add the value to val val[ index ] = matrix[ i ][ j ]; //record the column index in col_idx col_idx[ index ] = j; index++; } } //update row_ptr row_ptr[ i+1 ] = index; } }//end of CRS( float[][] matrix)

  8. //test the program public static void main(String[] args){ float[][] matrix = {{10, 0, 0, 0, -2, 0}, {3, 9, 0, 0, 0, 3}, {0, 7, 8, 7, 0, 0}, {3, 0, 8, 7, 5, 0}, {0, 8, 0, 9, 9, 13}, {0, 4, 0, 0, 2, -1}}; System.out.println("the original sparse matrix"); for(int i=0; i<6; i++){ for(int j=0; j<6; j++){ System.out.print(matrix[i][j]+", "); } System.out.println(); } System.out.println(); CRS crs = new CRS(matrix); crs.printMatrix(); crs.printCRS(); }

More Related