//Compressed Row Storage format for a sparse square matrix public class CRS{ //the values of the nonzero elements of the matrix float[] val; //the column indices of the elements in the val vector int[] col_idx; //locations in the val vector that start a row //the size is decided by the size of the matrix int[] row_ptr; //the number of rows of the matrix int mSize=0; //constructor that takes a sparse matrix and convert it to a CRS object CRS( float[][] matrix){ int i;//row index int j;//column index int totalNonZeros;//the total number of nonzero in the matrix int index; //get the number of rows and columns mSize = matrix.length; //System.out.println("the size of the matrix is:"+mSize+"X"+mSize+"\n"); //find the number of nonzero entries in the matrix totalNonZeros = 0; for(i=0; i j) return 0; } // if we have reached the end of the non-zeroes without // find j, it must be the index of a trailing 0 return 0; } //print the matrix stored in CRS in a two dimensional array format public void printMatrix(){ int i, j, k, zeroIndex; System.out.println("print the matrix in CRS format as a "+mSize+"x"+mSize+" matrix"); //Scan the row_ptr array to find the beginning and end of row i for(i = 0; i < mSize; i++){ //the column index of the element to print zeroIndex = 0; // Print row i: index k goes from the beginning to the end of row i for(k = row_ptr[i]; k < row_ptr[i+1]; k++){ j = col_idx[k]; //print entries of zero values that exist between consecutive // non-zeroes while(zeroIndex < j){ System.out.print(0.0+", "); zeroIndex++; } // end of while-loop //print the nonzero value System.out.print(val[k]+", "); // Prepare zeroIndex for the next sequence of zeroes zeroIndex++; } // end of for-k loop //print the trailing zeroes in this row while(zeroIndex < mSize){ System.out.print(0.0+", "); zeroIndex++; } // Start the next line System.out.println(); } // end of for-i loop System.out.println(); } // end of printMatrix /*takes a vector x and returns the product of the matrix stored in the CRS object with x.*/ public float[] product(float[] x){ //create vector to save product float[] product = new float[mSize]; for (int i = 0; i < mSize; i++){ for( int j = row_ptr[i]; j < row_ptr[i+1]; j++){ product[i] += val[j] * x[col_idx[j]]; } } return product; }//end of product }