Example 2: SparseMatrix Using Matrix Market Format

The matrix market exchange format is an ASCII file format that represents sparse matrices in coordinate format. It consists of three sections: The header section is the first line in the file and contains general informations about the matrix, e.g. data type and symmetry properties. This line is followed by the comments section which consists of zero or more lines of comments. The remainder of the file is the data section. Its first line contains the row number, column number and number of nonzeros of the matrix. The following lines contain location and value of all nonzero entries of the matrix, usually one per line. A file in Matrix Market format is read and converted to a SparseMatrix . Matrix information and its one norm are printed.
using System;
using Imsl.Math;

public class SparseMatrixEx2
{
    public class MTXReader
    {
        public String TypeCode
        {
            get
            {
                return this.typecode;
            }
            
        }
        public SparseMatrix Matrix
        {
            get
            {
                return this.matrix;
            }
            
        }
        private String typecode;
        private SparseMatrix matrix;
        
        public virtual void  read(String filename)
        {
            System.IO.FileStream aFile = System.IO.File.OpenRead(filename);
            System.IO.StreamReader br = new System.IO.StreamReader(aFile);
             
            // read type code initial line
            String line = br.ReadLine();
            typecode = line.Replace("%%", "");

            // read comment lines if any
            bool comment = true;
            while (comment)
            {
                line = br.ReadLine();
                comment = line.StartsWith("%");
            }
            
            // line now contains the size information which needs to be parsed
            String[] str = line.Split(new Char[] {' '}); 
            int nRows = (Int32.Parse(str[0].Trim()));
            int nColumns = (Int32.Parse(str[1].Trim()));
            int nNonZeros = (Int32.Parse(str[2].Trim()));
            
            // now we're into the data section
            matrix = new SparseMatrix(nRows, nColumns);
            while (true)
            {
                line = br.ReadLine();
                if (line == null)
                    break;

                str = line.Split(new Char[] {' '});  
                int i = (Int32.Parse(str[0].Trim()));
                int j = (Int32.Parse(str[1].Trim()));

                double x=0.0;
                if (str[2].Trim() == "") 
                {
                    x = Convert.ToDouble(str[3].Trim());
                } 
                else 
                {
                    x = Convert.ToDouble(str[2].Trim());
                }
                matrix.Set(i - 1, j - 1, x);
            }
            br.Close();
        }
    }


    public static void  Main(String[] args)
    {
        MTXReader mr = new MTXReader();
        mr.read("bcsstk01.mtx");
        SparseMatrix A = mr.Matrix;
        
        // Print the matrix type
        Console.Out.WriteLine("The matrix type is " + mr.TypeCode);
        
        // Print the matrix information and its one norm
        Console.Out.WriteLine("The number of rows is " + A.NumberOfRows);
        int ncols = A.NumberOfColumns;
        Console.Out.WriteLine("The number of columns is " + ncols);
        long nnz = A.NumberOfNonZeros;
        Console.Out.WriteLine("The number of nonzero elements is " + nnz);
        Console.Out.WriteLine();
        Console.Out.WriteLine("The 1 norm of the matrix is " + A.OneNorm());
    }
}

Output

The matrix type is MatrixMarket matrix coordinate real symmetric
The number of rows is 48
The number of columns is 48
The number of nonzero elements is 224

The 1 norm of the matrix is 3009444444.44744

Link to C# source.