本博客所有文章分類的總目錄:【總目錄】本博客博文總目錄-實時更新
開源Math.NET基礎數學類庫使用總目錄:【目錄】開源Math.NET基礎數學類庫使用總目錄
前言
本人在09年使用該組件的時候,主要原因也是為了替代Matlab,進行相關數學計算,現在依然有很多人關注Matlab計算,特別是學生,而很多也在使用C#,所以這些人通常由於個人能力有限(無法精通某一個門語言來解決綜合問題),無法單純的通過C#或者Matlab來解決問題,就想通過混合編程來調用完成,其實本人也做過大量的Matlab.NET混合編程研究,而且也個人制作了一套視頻教程,編寫過很多文章,可以參考如下文章:
1.國內第一部Matlab和C#.Net混合編程入門級視頻教程【完全免費】
3.Matlab.NET混合編程技巧之——直接調用Matlab內置函數(附源碼)
4.Matlab.NET混合編程技巧之——找出Matlab內置函數
鑒於此,我也提醒過很多人,在.NET中可以使用Math.NET組件來替代Matlab的相關工作,可能效果不太好。今天就來介紹一個比較適用的功能,利用Math.NET提供的功能,使用C#來讀寫Matlab的mat數據格式,這個功能的使用場景也很廣泛,當然可能是研究偏多,大家思想可以放得更遠。
如果本文資源或者顯示有問題,請參考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4265972.html
1.Mat數據格式
用過一段matlab的人都知道,在matlab的工作空間中,可以將變量保存為mat數據格式,下次在程序中直接讀取和進行計算,非常方便。以前也沒有具體研究過這個格式,也趁這個寫博客的機會,一起來看看這個東西的作用和組成。雖然使用Math.NET提供的程序讀取和寫入Mat文件都很簡單,但簡單之余,了解一點其他知識也是不錯的。
Mat文件格式,實際上使用的是一種通用的數值數據存儲格式Hierarchical Data Format(HDF),該格式最先是由美國國家超級計算應用中心開發的,后來由HDF非盈利組織資助,進行不但完善和推廣。這個格式的使用是非常廣泛的(使用BSD許可證),例如一些大名鼎鼎的商業和非商業軟件LabVIEW,MATLAB,Scilab,Octave,Mathematica等都支持該格式,該格式目前主要有HDF4和HDF5。 Mat文件格式最新的7.3版是基於HDF5的。
有關HDF文件格式和Mat格式的資料如下:
wikipedia:http://en.wikipedia.org/wiki/Hierarchical_Data_Format
Matlab官方:http://cn.mathworks.com/help/matlab/import_export/mat-file-versions.html
HDF官方:http://www.hdfgroup.org/
Mat文件格式分為2個等級(目前我知道的) Level 4和 Level 5。Level 4 Mat文件格式支持只支持2維矩陣和字符串;而Level 5支持更多,如多維數組,字符串數組,Cell數組,稀疏矩陣,對象,結構等都支持。本文介紹的MathNet.Numerics.Data.Matlab是直接支持Level-5類型的,所有更強大。
2.Mat格式在Matlab中的使用
Matlab中mat數據的保存和讀取非常簡單,只需要使用Save和load命令即可。對Matlab熟悉的朋友可以隨便打開matlab敲幾個命令就可以了,由於電腦太慢,前段時間把Matlab卸載了,這里就只介紹mat格式讀取和保存的語法,實際的使用也是比較簡單的。
http://www.ilovematlab.cn/thread-78257-1-1.html
●save:將工作區中的所有變量保存在當前工作區中的文件中,文件名為 matlab.mat,MAT文件可以通過load函數再次導入工作區,MAT函數可以被不同的機器導入,甚至可以通過其他的程序調用。
●save('filename'):將工作區中的所有變量保存為文件,文件名由filename指定。如果filename中包含路徑,則將文件保存在相應目錄下,否則默認路徑為當前路徑。
●save('filename', 'var1', 'var2', ...):保存指定的變量在 filename 指定的文件中。
●save('filename', '-struct', 's'):保存結構體s中全部域作為單獨的變量。
●save('filename', '-struct', 's', 'f1', 'f2', ...):保存結構體s中的指定變量。
● save('-regexp', expr1, expr2, ...):通過正則表達式指定待保存的變量需滿足的條件。
● save('..., 'format'),指定保存文件的格式,格式可以為MAT文件、ASCII文件等。MATLAB中導入數據通常由函數load實現,該函數的用法如下:
●load:如果matlab.mat文件存在,導入matlab.mat中的所有變量,如果不存在,則返回error。
●load filename:將filename中的全部變量導入到工作區中。
●load filename X Y Z ...:將filename中的變量X、Y、Z等導入到工作區中,如果是MAT文件,在指定變量時可以使用通配符“*”。
●load filename -regexp expr1 expr2 ...:通過正則表達式指定需要導入的變量。
●load -ascii filename:無論輸入文件名是否包含有擴展名,將其以ASCII格式導入;如果指定的文件不是數字文本,則返回error。
●load -mat filename:無論輸入文件名是否包含有擴展名,將其以mat格式導入;如果指定的文件不是MAT文件,則返回error。
3.C#讀取Mat數據格式
Math.NET中有關Mat數據格式讀寫的組件是MathNet.Numerics.Data.Matlab,Mat數據格式的讀取主要用MatlabReader類,解析的功能函數就是下面這段代碼:
1 /// <summary>Extracts all matrix blocks in a format we support from a stream.</summary> 2 internal static List<MatlabMatrix> ParseFile(Stream stream) 3 { 4 var matrices = new List<MatlabMatrix>(); 5 6 using (var reader = new BinaryReader(stream)) 7 { 8 // skip header (116 bytes) 9 // skip subsystem data offset (8 bytes) 10 // skip version (2 bytes) 11 reader.BaseStream.Position = 126; 12 13 // endian indicator (2 bytes) 14 if (reader.ReadByte() != LittleEndianIndicator) 15 { 16 throw new NotSupportedException(Resources.BigEndianNotSupported); 17 } 18 19 // set position to first data element, right after full file header (128 bytes) 20 reader.BaseStream.Position = 128; 21 var length = stream.Length; 22 23 // for each data element add a MATLAB object to the file. 24 while (reader.BaseStream.Position < length) 25 { 26 // small format: size (2 bytes), type (2 bytes), data (4 bytes) 27 // long format: type (4 bytes), size (4 bytes), data (size, aligned to 8 bytes) 28 29 DataType type; 30 int size; 31 bool smallBlock; 32 ReadElementTag(reader, out type, out size, out smallBlock); 33 34 // read element data of the size provided in the element header 35 // uncompress if compressed 36 byte[] data; 37 if (type == DataType.Compressed) 38 { 39 data = UnpackCompressedBlock(reader.ReadBytes(size), out type); 40 } 41 else 42 { 43 data = new byte[size]; 44 reader.Read(data, 0, size); 45 SkipElementPadding(reader, size, smallBlock); 46 } 47 48 if (type == DataType.Matrix) 49 { 50 using (var matrixStream = new MemoryStream(data)) 51 using (var matrixReader = new BinaryReader(matrixStream)) 52 { 53 matrixReader.BaseStream.Seek(20, SeekOrigin.Current); 54 var matrixDim = matrixReader.ReadInt32()/8; 55 if (matrixDim > 2) 56 { 57 continue; 58 } 59 60 matrixReader.BaseStream.Seek(10, SeekOrigin.Current); 61 int matrixSize = matrixReader.ReadInt16(); 62 if (matrixSize == 0) 63 { 64 matrixSize = matrixReader.ReadInt32(); 65 } 66 67 var matrixName = Encoding.ASCII.GetString(matrixReader.ReadBytes(matrixSize)); 68 69 matrices.Add(new MatlabMatrix(matrixName, data)); 70 } 71 } 72 } 73 } 74 75 return matrices; 76 }
C#調用就更簡單了,上面那些實現只是一個幫助,大家以后可以了解解析其他類似的數據格式。看看調用的代碼:
1 using MathNet.Numerics.LinearAlgebra; 2 using MathNet.Numerics.Data.Matlab; 3 4 //從collection.mat文件中,讀取第一個double矩陣 5 Matrix<double> m = MatlabReader.Read<double>("collection.mat"); 6 7 //從collection.mat中讀取一個名稱為 vd 的特定矩陣 8 Matrix<double> m = MatlabReader.Read<double>("collection.mat", "vd"); 9 10 //直接選擇轉換為其他格式 11 Matrix<Complex> m = MatlabReader.Read<Complex>("collection.mat"); 12 13 //將一個文件的所有矩陣及其名稱存入字典中 14 Dictionary<string,Matrix<double>> ms = MatlabReader.ReadAll<double>("collection.mat"); 15 16 //讀取名為 Ad和vd 的矩陣到字典 17 var ms = MatlabReader.ReadAll<double>("collection.mat", "vd", "Ad");
這樣就可以直接在C#中進行相關計算了,也不用混合編程那么麻煩了。
4.C#保存Mat數據格式
Mat數據格式的寫入主要用MatlabWriter類,核心功能函數就是下面代碼:
1 /// <summary>Writes all matrix blocks to a stream.</summary> 2 internal static void FormatFile(Stream stream, IEnumerable<MatlabMatrix> matrices) 3 { 4 using (var buffer = new BufferedStream(stream)) 5 using (var writer = new BinaryWriter(buffer)) 6 { 7 // write header and subsystem data offset (116+8 bytes) 8 var header = Encoding.ASCII.GetBytes(HeaderText + DateTime.Now.ToString(Resources.MatlabDateHeaderFormat)); 9 writer.Write(header); 10 Pad(writer, 116 - header.Length + 8, 32); 11 12 // write version (2 bytes) 13 writer.Write((short)0x100); 14 15 // write little endian indicator (2 bytes) 16 writer.Write((byte)0x49); 17 writer.Write((byte)0x4D); 18 19 foreach (var matrix in matrices) 20 { 21 // write data type 22 writer.Write((int)DataType.Compressed); 23 24 // compress data 25 var compressedData = PackCompressedBlock(matrix.Data, DataType.Matrix); 26 27 // write compressed data to file 28 writer.Write(compressedData.Length); 29 writer.Write(compressedData); 30 } 31 32 writer.Flush(); 33 writer.Close(); 34 } 35 }
C#調用也很簡單,調用的代碼如下:
1 var matrices = new List<MatlabMatrix>(); 2 m.Add(MatlabWriter.Pack(myFirstMatrix, "m1"); 3 m.Add(MatlabWriter.Pack(mySecondMatrix, "m2"); 4 MatlabWrier.Store("file.mat", matrices); 5 6 //寫入單個的"myMatrix"矩陣,並命名為"m1". 7 MatlabWriter.Write("file.mat", myMatrix, "m1"); 8 9 //寫入多個矩陣,注意 矩陣列表 和 名稱列表 10 MatlabWriter.Write("file.mat", new[] { m1, m2 }, new[] { "m1", "m2" }); 11 12 //寫入字典矩陣,和讀取的原理類似 13 var dict = new Dictionary<string, Matrix<double>>(); 14 dict.Add("m1", m1); 15 dict.Add("m2", m2); 16 MatlabWriter.Write("file.mat", dict);
5.資源
接下來的文章將繼續介紹Math.NET的其他功能。
如果本文資源或者文章顯示有問題,請參考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4265972.html
