18.2 Value Operations on Matrices (.NET, C#, CSharp, VB, Visual Basic, F#)
The NMath structured sparse matrix classes have read-only properties for all shape parameters, and for the underlying data vector:
On square matrix types, the Rows and Cols properties simply return the order. On banded types, the Bandwidth property returns the total bandwidth. For general banded matrices, the total bandwidth is LowerBandwidth + UpperBandwidth + 1; for symmetric and Hermitian banded types, the total bandwidth is 2 * HalfBandwidth + 1.
For example, if A is a FloatHermitianBandMatrix instance:
Code Example – C# matrix
int order = A.Order; int cols = A.Cols; // cols == order int rows = A.Rows; // rows == order int halfband = A.HalfBandwidth int band = A.Bandwidth // band = 2 * halfband + 1 FloatComplexVector data = A.DataVector;
Code Example – VB matrix
Dim Order As Integer = A.Order Dim Cols As Integer = A.Cols ' cols == order Dim Rows As Integer = A.Rows ' rows == order Dim HalfBand As Integer = A.HalfBandwidth Dim band As Integer = A.Bandwidth ' band = 2 * halfband + 1 Dim Data As FloatComplexVector = A.DataVector
Accessing and Modifying Matrix Values
The matrix classes provide standard indexers for getting and setting element value at a specified row and column position in a matrix. Thus, A[i,j] always returns the element in the ith row and jth column of matrix A's view of the data.
NOTE—Indexing starts at 0.
Attempts to set zero elements outside the stored region to nonzero values raise a NonModifiableElementException. For instance:
Code Example – C# matrix
var A = new FloatComplexTriDiagMatrix( 8, 8 ); try { A[7,0] = new FloatComplex( 1, -1 ); } catch ( NonModifiableElementException ) { // Do something here }
Code Example – VB matrix
Dim A As New FloatComplexTriDiagMatrix(8, 8) Try A(7, 0) = New FloatComplex(1.0F, -1.0F) Catch NonModifiableElementException ' Do something here End Try
Symmetric matrices are in a different category than the other structured sparse matrix types, because unstored values are not constrained to be zero. Thus, even though only the upper triangular region is stored, you can "set" values in the lower triangular region. The corresponding value in the upper triangular region is changed. Thus:
Code Example – C# matrix
var A = new DoubleSymmetricMatrix( 12 ); Console.WriteLine( A[7,2] ); // "0" Console.WriteLine( A[2,7] ); // "0" A[7,2] = 1; Console.WriteLine( A[7,2] ); // "1" Console.WriteLine( A[2,7] ); // "1"
Code Example – VB matrix
Dim A As New DoubleSymmetricMatrix(12) Console.WriteLine(A(7, 2)) ' "0" Console.WriteLine(A(2, 7)) ' "0" A(7, 2) = 1 Console.WriteLine(A(7, 2)) ' "1" Console.WriteLine(A(2, 7)) ' "1"
The matrix classes provide Resize() methods for changing the size of a matrix after it has been created. Zeros are added or values are truncated as necessary. For instance:
Code Example – C# matrix
int order = 7; var data = new DoubleComplexVector( ( order * ( order + 1 )) / 2, new RandGenMTwist() ); var A = new DoubleHermitianMatrix( data ); DoubleHermitianMatrix B2 = (DoubleHermitianMatrix)B.Clone(); B2.Resize( B.Order + 2 );
Code Example – VB matrix
Dim Order As Integer = 7 Dim Data As New DoubleComplexVector((Order * (Order + 1)) / 2, New RandGenMTwist()) Dim A As New DoubleHermitianMatrix(Data) Dim B2 As DoubleHermitianMatrix = CType(B.Clone(), DoubleHermitianMatrix) B.Resize(B.Order + 2)