for (int i=0; i<SIZE; i++) {
double[] data=new double[DSIZE];
for (int j=0; j<DSIZE; j++) {
data[j]=Rand.nextDouble();
}
Index indy=Indexz.createRandomChoice(DSIZE, SIZE);
m.replaceRow(i,SparseIndexedVector.create(SIZE, indy, data));
}
printTime("Construct sparse matrix: ");
// System.out.println("First row sum = "+m.getRow(0).elementSum());
// Now we normalise each row to element sum = 1.0
// This demonstrates both the mutability of rows and the setRow functionality
startTimer();
for (int i=0; i<SIZE; i++) {
AVector row=m.getRow(i);
double sum=row.elementSum();
if (sum>0) {
row.divide(sum);
} else {
m.setRow(i, RepeatedElementVector.create(SIZE,1.0/SIZE));
}
}
printTime("Normalise all rows: ");
//System.out.println("First row sum = "+m.getRow(0).elementSum());
// We construct a dense matrix for later multiplication
startTimer();
AMatrix t=Matrixx.createRandomMatrix(SIZE, CSIZE);
printTime("Construct dense matrix: ");
System.out.println("Dense element sum = "+t.elementSum());
// Finally compute the innerProduct (matrix multiplication) of
// sparse matrix with dense matrix
startTimer();
AMatrix result=m.innerProduct(t);
printTime("Multiply with dense matrix: ");
System.out.println("Result element sum = "+result.elementSum());
// if this demo is working, the element sum should be roughly the same before and after transformation
// (modulo some small numerical errors)
// ----------------------------------------------------------------------
// Construct another (smaller) sparse matrix.
SparseRowMatrix M=SparseRowMatrix.create(SSIZE,SSIZE);
// First task is to construct the large sparse matrix
startTimer();
for (int i=0; i<SSIZE; i++) {
double[] data=new double[DSIZE];
for (int j=0; j<DSIZE; j++) {
data[j]=Rand.nextDouble();
}
Index indy=Indexz.createRandomChoice(DSIZE, SSIZE);
M.replaceRow(i,SparseIndexedVector.create(SSIZE, indy, data));
}
printTime("Construct small sparse matrix: ");