NMath是一個適用於全部.NET語言,如C#、Visual Basic、F#和.NET的數學庫,它包含了.NET平臺上的面向對象數字計算的基礎類。咱們將以連載的形式向你們介紹NMath的實用教程。本節展現在NMatht數學庫中的矩陣類函數。函數
最新版NMath點擊下載spa
NMath矩陣類是在返回IEnumerator對象時,會提供的標準.NET GetEnumerator()方法。對象
例: 教程
代碼示例 - C#element
int rows = 13,cols = 3; var A = new DoubleMatrix(rows,cols,0,.25); IEnumerator elements = A.GetEnumerator(); var data = new double [rows * cols]; i = 0; while(elements.MoveNext()){ data [i ++] =(double)elements.Current; }
代碼示例 - VBget
Dim Rows As Integer = 13 Dim Cols As Integer = 3 Dim A As New DoubleMatrix(Rows, Cols, 0.0, 0.25) Dim Elements As IEnumerator = A.GetEnumerator() Dim Data(Rows * Cols) As Double Dim I As Integer = 0 While Elements.MoveNext() I += 1 Data(I) = CType(Elements.Current, Double) End While
注意:IEnumerator 上的Current屬性會返回集合中的當前對象,因此必須將其強制轉換爲適當的類型。NMath 還提供自定義strongly-typed示例:IFloatEnumerator, IDoubleEnumerator,IFloatComplexEnumerator和IDoubleComplexEnumerator,這種形式的出現能夠避免本身製做時出現的錯誤。 數學
例:ast
代碼示例 - C# 基礎
int rows = 13, cols = 3; var A = new DoubleMatrix( rows, cols, 0, .25 );IDoubleEnumerator elements = A.GetDoubleEnumerator(); var data = new double[rows*cols]; i = 0; while ( elements.MoveNext() ) { data[i++] = elements.Current; // No need to cast to double }
代碼示例 - VB下載
Dim Rows As Integer = 13 Dim Cols As Integer = 3 Dim A As New DoubleMatrix(Rows, Cols, 0.0, 0.25) Dim Elements As IDoubleEnumerator = A.GetDoubleEnumerator() Dim Data(Rows * Cols) As Double Dim I As Integer = 0 While Elements.MoveNext() I += 1 Data(I) = Elements.Current ' No need to cast to Double End While