Example: ComplexSparseMatrix

The matrix product of two complex sparse matrices is computed using a method from the ComplexSparseMatrix class. The one-norm of the result is also computed. The matrix and its norm are printed.

import com.imsl.math.*;

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


		ComplexSparseMatrix c = new ComplexSparseMatrix(6,3);
		c.set(0, 0, new Complex(5.0, 0.0));
		c.set(1, 2, new Complex(-3.0, -4.0));
		c.set(2, 0, new Complex(1.0, 2.0));
		c.set(2, 2, new Complex(7.0, 1.0));
		c.set(3, 0, new Complex(2.0, -7.0));
		c.set(4, 1, new Complex(-5.0, 2.0));
		c.set(4, 2, new Complex(2.0, 1.0));
		c.set(5, 2, new Complex(4.0, 0.0));
		
		// A = b * c
		ComplexSparseMatrix  A = ComplexSparseMatrix.multiply(b, c);

		// Get the one norm of matrix A
		System.out.println("The 1-norm of the matrix is "+ A.oneNorm());

		ComplexSparseMatrix.SparseArray sa = A.toSparseArray();

		System.out.println("row  column  value");
		for (int i = 0;  i < sa.numberOfRows;  i++)
		{
			for (int j = 0;  j < sa.index[i].length;  j++)
			{
				System.out.println(" "+ i + "     "+sa.index[i][j]+
					"     "+
					"("+Complex.real(sa.values[i][j]) +","+
					Complex.imag(sa.values[i][j])+")");
			}
		}
	}
}

Output

The 1-norm of the matrix is 253.9370051143438
row  column  value
 0     0     (50.0,-15.0)
 1     0     (-16.0,1.0)
 1     2     (-53.0,-29.0)
 2     0     (5.0,35.0)
 2     2     (100.0,50.0)
 3     0     (17.0,-68.0)
 3     1     (9.0,8.0)
 3     2     (0.0,-5.0)
 4     0     (34.0,49.0)
 4     1     (1.0,17.0)
 4     2     (-7.0,-5.0)
 5     0     (-5.0,20.0)
 5     2     (34.0,-15.0)
Link to Java source.