开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
?開源Math.NET基礎數學類庫使用系列文章總目錄:?
? 1.開源.NET基礎數學計算組件Math.NET(一)綜合介紹??
? 2.開源.NET基礎數學計算組件Math.NET(二)矩陣向量計算?
? 3.開源.NET基礎數學計算組件Math.NET(三)C#解析Matlab的mat格式
? 4.開源.NET基礎數學類庫使用Math.NET(四)C#解析Matrix Marke數據格式
? 5.開源.NET基礎數學類庫使用Math.NET(五)C#解析Delimited Formats數據格式
? 6.開源.NET基礎數學類庫使用Math.NET(六)數值分析之線性方程直接求解
? 7.開源.NET基礎數學類庫使用Math.NET(七)常用的一些數學常數?
? 8.開源.NET基礎數學類庫使用Math.NET(八)C#進行數值積分
? 9.開源.NET基礎數學類庫使用Math.NET(九)相關數論函數使用
10.開源.NET基礎數學類庫使用Math.NET(十)C#進行數據統計
11.開源.NET基礎數學類庫使用Math.NET(十一)C#計算相關系數
12.開源.NET基礎數學類庫使用Math.NET(十二)隨機數擴展方法
13.開源.NET基礎數學類庫使用Math.NET(十三)C#實現其他隨機數生成器
14.開源.NET基礎數學類庫使用Math.NET(十四)安全的隨機數生成器擴展
后續繼續更新中。。如文章鏈接打開有誤,請關注博客,因為文章正在編輯修改中,所有已經列出的目錄都將在1個月之內發表。?
前言
上一篇文章,我們介紹了使用C#讀寫Matlab的Mat數據格式的情況。mat格式的廣泛應用使得很多人都了解,但同樣還有一些數據格式也是在科學計算,數據分析,測試等方面的通用數據格式,那就是接下來我們要介紹的Matrix Market格式。我們同樣是使用C#來操作該格式。
如果本文資源或者顯示有問題,請參考?本文原文地址:http://www.cnblogs.com/asxinyu/p/4266758.html
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#操作該數據格式就可以無憂了。
4.資源
源碼下載:參考官網網站。
如果本文資源或者顯示有問題,請參考?本文原文地址:http://www.cnblogs.com/asxinyu/p/4266758.html
本博客還有大量的.NET開源技術文章,您可能感興趣:?
1.開源Math.NET基礎數學類庫使用系列文章:鏈接
2.開源C#彩票數據資料庫系列文章:鏈接
3.開源的.NET平臺ORM組件文章:鏈接
4.其他開源的.NET組件文章:鏈接
5..NET平臺機器學習組件-Infer.NET系列文章:鏈接
6.Matlab混合編程文章:鏈接??
總結
以上是生活随笔為你收集整理的开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (三)java的数据类型
- 下一篇: C# 数据结构--排序[下]