4 开源Math.NET基础数学类库使用 C#解析Matrix Marke数据格式

前言

  上一篇文章,我们介绍了使用C#读写Matlab的Mat数据格式的情况。mat格式的广泛应用使得很多人都了解,但同样还有一些数据格式也是在科学计算,数据分析,测试等方面的通用数据格式,那就是接下来我们要介绍的Matrix Market格式。我们同样是使用C#来操作该格式。

 

1.Matrix Market格式介绍

  Matrix Market是一个基于AscII的可读性很强的文件格式,目的是促进矩阵数据的交流。NIST的数据存储就有大量的数值线性代数相关的研究比较测试数据采用该格式。其他信息可以参考官网:http://math.nist.gov/MatrixMarket/

http://en.wikipedia.org/wiki/Matrix_Market_exchange_formats

The Matrix Market exchange formats are a set of human readable, ASCII-based file formats designed to facilitate the exchange of matrix data. The file formats were designed and adopted for the Matrix Market, a NIST repository for test data for use in comparative studies of algorithms for numerical linear algebra。

下面是一个Matrix Market矩阵的部分截图,可以直接的理解该格式,的确是非常人性化,也方便不同软件,系统间的数据交换。

 

2.C#读取Matrix Market文件

  本文还是使用Math.NET提供的程序,只不过对其结构和使用进行分析。C#读取的返回值的矩阵或者向量格式也都是Math.NET中的类型。C#读取Martix Market文件的主要类型是MatrixMarketReader,在MathNet.Numerics.Data.Text项目中,而其中的方法都是静态方法,分别为读取矩阵和读取向量,并支持从文件和流中分别读取数据。看看如下几个静态函数的原型,就可以知道怎么样了:  

 1 public static Matrix<T> ReadMatrix<T>(string filePath,Compression compression=Compression.Uncompressed) where T : struct, IEquatable<T>, IFormattable
 2 
 3 public static Vector<T> ReadVector<T>(string filePath,Compression compression=Compression.Uncompressed) where T : struct, IEquatable<T>, IFormattable
 4 
 5 public static Matrix<T> ReadMatrix<T>(Stream stream) where T :struct,IEquatable<T>,IFormattable
 6 
 7 public static Vector<T> ReadVector<T>(Stream stream) where T :struct,IEquatable<T>,IFormattable
 8 
 9 public static Matrix<T> ReadMatrix<T>(TextReader reader) where T :struct,IEquatable<T>,IFormattable
10 
11 public static Vector<T> ReadVector<T>(TextReader reader) where T :struct,IEquatable<T>,IFormattable

  上面要注意的是,该文件支持压缩,所以有一个Compression参数,默认是未压缩的。

 

3.C#保存数据为Matrix Market文件

  C#写入Matrix Market文件的方法和上面的读取类似,使用的是MatrixMarketWriter类的静态方法,支持写入矩阵和向量,方法原型如下:

 1 public static void WriteMatrix<T>(string filePath, Matrix<T> matrix, Compression compression = Compression.Uncompressed) where T : struct, IEquatable<T>, IFormattable
 2 
 3 public static void WriteVector<T>(string filePath, Vector<T> vector, Compression compression = Compression.Uncompressed) where T : struct, IEquatable<T>, IFormattable
 4 
 5 public static void WriteMatrix<T>(Stream stream, Matrix<T> matrix) where T : struct, IEquatable<T>, IFormattable
 6 
 7 public static void WriteVector<T>(Stream stream, Vector<T> vector) where T:struct,IEquatable<T>,IFormattable
 8 
 9 public static void WriteMatrix<T>(TextWriter writer,Matrix<T> matrix) where T :struct,IEquatable<T>, IFormattable
10 
11 public static void WriteVector<T>(TextWriter writer, Vector<T> vector) where T :struct,IEquatable<T>, IFormattable

  一般来说,写入文件比较常用一点,可以用于系统之间和样本数据的传递。总共就2个类,常用的也就4个方法,使用C#操作该数据格式就可以无忧了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值