Math.NET Numerics | Math.NET Project | GitHub


NIST MatrixMarket Text Files

MatrixMarket is both a vast repository of test data and a text-based exchange file format provided by NIST. Being text-based makes it convenient to deal with and program against, and also works well with versioning tools like Git. But other than CSV it can also store sparse matrices in a compact way.

Math.NET Numerics provides basic support for MatrixMarket files with the MathNet.Numerics.Data.Text package, which is available on NuGet as separate package and not included in the basic distribution.

Reading a matrix from a MatrixMarket file

The MatrixMarketReader class provides static functions to read a matrix or a vector from a file or string. It can read from:

All these functions expect the data type of the matrix to be generated as generic type argument. Only Double, Single, Complex and Complex32 are supported.

Example:

using MathNet.Numerics.Data.Text;

Matrix<double> matrix = MatrixMarketReader.ReadMatrix<double>("fidap007.mtx");

Writing a matrix to a MatrixMarket file

The dual to the reader above is the MatrixMarketWriter class that can serialize a matrix or vector to a MatrixMarket text file, stream or TextWriter.

Example:

MatrixMarketWriter.WriteMatrix("matrix.mtx", m);

Alternatives

The data extension packages also offer other ways to serialize a matrix to a binary stream or file. Among others: