本博客所有文章分類的總目錄:【總目錄】本博客博文總目錄-實時更新
開源Math.NET基礎數學類庫使用總目錄:【目錄】開源Math.NET基礎數學類庫使用總目錄
上一篇文章,我們介紹了使用C#讀寫Matlab的Mat數據格式的情況。mat格式的廣泛應用使得很多人都了解,但同樣還有一些數據格式也是在科學計算,數據分析,測試等方面的通用數據格式,那就是接下來我們要介紹的Matrix Market格式。我們同樣是使用C#來操作該格式。
如果本文資源或者顯示有問題,請參考本文原文地址:http://www.survivalescaperooms.com/asxinyu/p/4266758.html
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矩陣的部分截圖,可以直接的理解該格式,的確是非常人性化,也方便不同軟件,系統間的數據交換。
本文還是使用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>,IFormattable10 11 public static Vector<T> ReadVector<T>(TextReader reader) where T :struct,IEquatable<T>,IFormattable
上面要注意的是,該文件支持壓縮,所以有一個Compression參數,默認是未壓縮的。
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>, IFormattable10 11 public static void WriteVector<T>(TextWriter writer, Vector<T> vector) where T :struct,IEquatable<T>, IFormattable
一般來說,寫入文件比較常用一點,可以用于系統之間和樣本數據的傳遞。總共就2個類,常用的也就4個方法,使用C#操作該數據格式就可以無憂了。
源碼下載:參考官網網站。
如果本文資源或者顯示有問題,請參考本文原文地址:http://www.survivalescaperooms.com/asxinyu/p/4266758.html
本博客還有大量的.NET開源技術文章,您可能感興趣:
1.開源Math.NET基礎數學類庫使用系列文章:鏈接
2.開源C#彩票數據資料庫系列文章:鏈接
3.開源的.NET平臺ORM組件文章:鏈接
4.其他開源的.NET組件文章:鏈接
5..NET平臺機器學習組件-Infer.NET系列文章:鏈接
6.Matlab混合編程文章:鏈接
新聞熱點
疑難解答