Example 1: SparseMatrix

The matrix product of two matrices is computed using a method from the SparseMatrix class. The one norm of the result is also computed. The matrix and its norm are printed.
import com.imsl.math.*;

public class  SparseMatrixEx1 {
    public static void main(String args[]) {
        SparseMatrix b = new SparseMatrix(6, 6);
        b.set(0, 0, 10.0);
        b.set(1, 1, 10.0);
        b.set(1, 2, -3.0);
        b.set(1, 3, -1.0);
        b.set(2, 2, 15.0);  
        b.set(3, 0, -2.0);
        b.set(3, 3, 10.0);
        b.set(3, 4, -1.0); 
        b.set(4, 0, -1.0);
        b.set(4, 3, -5.0);
        b.set(4, 4, 1.0);
        b.set(4, 5, -3.0);
        b.set(5, 0, -1.0);
        b.set(5, 1, -2.0);
        b.set(5, 5, 6.0);        

        SparseMatrix c = new SparseMatrix(6, 3);
        c.set(0, 0, 5.0);
        c.set(1, 2, -3.0);
        c.set(2, 0, 1.0);
        c.set(2, 2, 7.0);
        c.set(3, 0, 2.0);
        c.set(4, 1, -5.0);
        c.set(4, 2, 2.0);
        c.set(5, 2, 4.0);
        
        SparseMatrix  A = SparseMatrix.multiply(b, c);
        
        // Get the one norm of matrix A
        System.out.println("The 1-norm of the matrix is "+ A.oneNorm());
        SparseMatrix.SparseArray sa = A.toSparseArray();
        
        // Print the matrix and its one norm
        System.out.println("row  column  value");
        for (int i = 0;  i < sa.numberOfRows;  i++) {
            for (int j = 0;  j < sa.index[i].length;  j++) {
                int jj = sa.index[i][j];
                System.out.println(" "+ i + "     "+jj+"     "+sa.values[i][j]);
            }
        }
    }
}

Output

The 1-norm of the matrix is 198.0
row  column  value
 0     0     50.0
 1     0     -5.0
 1     2     -51.0
 2     0     15.0
 2     2     105.0
 3     0     10.0
 3     1     5.0
 3     2     -2.0
 4     0     -15.0
 4     1     -5.0
 4     2     -10.0
 5     0     -5.0
 5     2     30.0
Link to Java source.