ComplexMatrixItem Property (Int32) |
Namespace: Novacta.Analytics
Exception | Condition |
---|---|
ArgumentOutOfRangeException | linearIndex is less than zero. -or- linearIndex is equal to or greater than the Count of this instance. |
Let be a matrix, and consider its generic entry
where and
are the
number of rows and columns of , respectively.
A linear index completely
identifies an entry,
assuming that entries are linearly ordered following the
ColumnMajor
data order. This means that entry has linear
index equal to , and matrix entries can be enumerated as
follows:
where is the Count of the matrix.
In the following example, a matrix element is accessed using its linear index.
using System; using System.Numerics; namespace Novacta.Analytics.CodeExamples { public class ComplexLinearIndexerExample0 { public void Main() { // Create a matrix. var data = new Complex[8] { new Complex(1, -1), new Complex(5, -5), new Complex(2, -2), new Complex(6, -6), new Complex(3, -3), new Complex(7, -7), new Complex(4, -4), new Complex(8, -8) }; var matrix = ComplexMatrix.Dense(4, 2, data, StorageOrder.RowMajor); Console.WriteLine("Initial data matrix:"); Console.WriteLine(matrix); // Specify a linear index. int linearIndex = 3; Console.WriteLine(); Console.WriteLine("Linear index: {0}", linearIndex); // Set the corresponding entry. matrix[linearIndex] = new Complex(40, -40); Console.WriteLine(); Console.WriteLine("Updated data matrix:"); Console.WriteLine(matrix); // Entries can also be accessed using a read-only wrapper of the matrix. ReadOnlyComplexMatrix readOnlyMatrix = matrix.AsReadOnly(); Console.WriteLine(); Console.WriteLine("Updated matrix entry:"); Console.WriteLine(readOnlyMatrix[linearIndex]); } } } // Executing method Main() produces the following output: // // Initial data matrix: // ( 1, -1) ( 5, -5) // ( 2, -2) ( 6, -6) // ( 3, -3) ( 7, -7) // ( 4, -4) ( 8, -8) // // // // Linear index: 3 // // Updated data matrix: // ( 1, -1) ( 5, -5) // ( 2, -2) ( 6, -6) // ( 3, -3) ( 7, -7) // ( 40, -40) ( 8, -8) // // // // Updated matrix entry: // (40, -40)