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.

using System;
using 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
        Console.Out.WriteLine("The 1-norm of the matrix is " 
            + A.OneNorm());
        
        ComplexSparseMatrix.SparseArray sa = A.ToSparseArray();
        
        Console.Out.WriteLine("row  column  value");
        for (int i = 0; i < sa.NumberOfRows; i++)
        {
            for (int j = 0; j < sa.Index[i].Length; j++)
            {
                Console.Out.WriteLine(" " + 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.937005114344
row  column  value
 0     0     (50,-15)
 1     0     (-16,1)
 1     2     (-53,-29)
 2     0     (5,35)
 2     2     (100,50)
 3     0     (17,-68)
 3     1     (9,8)
 3     2     (0,-5)
 4     0     (34,49)
 4     1     (1,17)
 4     2     (-7,-5)
 5     0     (-5,20)
 5     2     (34,-15)

Link to C# source.