Matrix Market Files

The Matrix Market format was designed specifically for the exchange of matrix data. It is the format used by the NIST's Matrix Market. It is a text-based format that can be used to store both dense and sparse real or complex matrices.

Reading and writing Matrix Market files is implemented by the MatrixMarketFile and MatrixMarketStream classes. Only vectors and matrices are supported.

Reading Matrix Market files

The MatrixMarketFile class contains static methods for reading vectors and matrices from a file in Matrix Market format.

The ReadVector method reads a vector from the file. It takes one type argument that is required: the element type of the vector to read. The only actual argument specifies the source of the data. This may be a string containing the path to the file, or a Stream that has been opened for reading. If a filename is given, it may be the path to a local file, or the uri of a resource on the Internet.

The ReadMatrix method reads a matrix from the file. It has the same arguments and overloads as the ReadVector. The element type must be supplied as a generic type argument. The actual arguments are the path to the file or resource or the stream to read from, and optionally whether the element type should match exactly.

C#
var vector1 = MatrixMarketFile.ReadVector<double>(@"c:\vector.mtx");
var matrix1 = MatrixMarketFile.ReadMatrix<double>(
    "http://www.example.com/german.mtx");

The ReadComplexVector and ReadComplexMatrix methods read a complex vector and matrix from the file, respectively. These methods are identical to their real counterparts, except that the number of columns in the file must be twice the number of columns in the final object. This is because the real and imaginary parts of the complex values are stored in separate columns. So, a file storing a complex vector should have two columns, while a file storing a matrix with 5 columns should have 10 columns total.

Writing Matrix Market files

The Write method is used to write a vector or matrix to a file using the Matrix Market format. The method has many overloads.

The first argument always specifies the destination in one of two ways. It can be a string that contains the path to the file. If the file exists, it is overwritten. If it doesn't exist, then it is created. Alternatively, the destination can be specified as a Stream.

The second argument always specifies the object(s) to be written. This can be a vector or matrix. Both real and complex vectors and matrices are supported.

The third argument is a TextOptions object that specifies how the data should be written. This argument is optional. If omitted, the standard Matrix Market format is used.

In the example code below, we write a vector to a file, and then a matrix to a stream.

C#
MatrixMarketFile.Write(@"c:\data.mtx", vector1);
using (var stream = File.OpenWrite(@"c:\output.mtx"))
{
    MatrixMarketFile.Write(stream, matrix1);
}

Using Matrix Market Data Streams

Matrix Market data streams are implemented by the MatrixMarketStream class. This class has no constructors. Instead, use one of the methods of the MatrixMarketFile class. Streams can be opened for both reading and writing.

Opening files for reading

The Open(String, TextOptions) method opens a file or stream for reading. This method has two overloads that take two arguments. The first is a string or a stream. If it is a string, it is the path to the file that should be opened, or the URI of a network or Internet resource. If it is a stream, then it specifies the data stream that the objects should be read from. The second argument is optional. It is a TextOptions object that specifies the options used to read the data in the file.

The methods for reading objects from streams are similar to those of the MatrixMarketFile class, but with fewer arguments.

Reading from streams

The ReadVector<T> method reads a vector from the file. It takes one type argument that is required: the element type of the vector to read. This method takes one argument which is optional: a boolean value that specifies whether the element type of the stored vector should match the specified element type exactly. The default is false, which means that the read operation will succeed as long as the stored element type can be cast to the requested element type.

The ReadMatrix method reads a matrix from the file. It has the same arguments and overloads as the ReadVector. The element type must be supplied as a generic type argument. The one actual arguments is optional. It specifies whether the element type should match exactly.

C#
using (var s2 = MatrixMarketFile.Open(@"c:\vector.mtx"))
{
    var vector1 = s2.ReadVector<double>();
}

Writing objects

The Write method is used to write a vector or matrix to a file in Matrix Market format. The method has many overloads.

The first argument always specifies the object(s) to be written. This can be a vector or a matrix. Both real and complex vectors and matrices are supported. The following code creates a new .rda file, and writes a matrix to it:

C#
using (var stream = MatrixMarketFile.Create(@"c:\data.mtx"))
{
    stream.Write(matrix1);
}