稀疏矩阵的行压缩存储(CRS)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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;
0
2
5
16
val col_ind
10 1
-2 5
3 1
9 2
3 6
7 2
8 3
7 4
… …
4 2
2 5
-1 6
the number of nonzero elements in a matrix
row_ptr
0
2
5源自文库
8
12
16
19
the number of rows +1
•The number of nonzero elements of row i = row_ptr[i+1] - row_ptr[ i ]
– Using CRS format to store a sparse matrix will save a lot of memory.
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.
//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;
• 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.
0
1 2 3
4
5 0 1 2 3 4 5
val //store the matrix in CRS format
x x
...
index 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)
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.
//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(); }
•The value of nonzero elements of row i: val[ row_ptr[ i ] ], ... , val[ row_ptr[ i+1 ] -1 ]
//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){... } }