ReadOnlyDoubleMatrixVec Method (IndexCollection) |
Namespace: Novacta.Analytics
Exception | Condition |
---|---|
ArgumentNullException | linearIndexes is null. |
ArgumentOutOfRangeException | linearIndexes contains an index which is greater than or equal to 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.
Let the collection of
indexes represented by linearIndexes.
This method, when called on a DoubleMatrix instance
representing matrix , returns a new
DoubleMatrix instance that
represents a column vector, say ,
such that:
In the following example, some matrix entries are vectorized.
using System; namespace Novacta.Analytics.CodeExamples { public class VecExample1 { public void Main() { // Create a matrix. var data = new double[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var matrix = DoubleMatrix.Dense(3, 3, data, StorageOrder.RowMajor); Console.WriteLine("Initial data matrix:"); Console.WriteLine(matrix); // Get the linear indexes of elements on // the main diagonal. var mainDiagonalIndexes = IndexCollection.Sequence(0, 4, 10); // Get the vectorization of the matrix main diagonal. var mainDiagonal = matrix.Vec(mainDiagonalIndexes); Console.WriteLine(); Console.WriteLine("Vectorized main diagonal:"); Console.WriteLine(mainDiagonal); // Entries can also be vectorized using a read-only wrapper of the matrix. ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly(); Console.WriteLine(); Console.WriteLine("Vectorized main diagonal of a read-only data matrix:"); Console.WriteLine(readOnlyMatrix.Vec(mainDiagonalIndexes)); } } } // Executing method Main() produces the following output: // // Initial data matrix: // 1 2 3 // 4 5 6 // 7 8 9 // // // // Vectorized main diagonal: // 1 // 5 // 9 // // // // Vectorized main diagonal of a read-only data matrix: // 1 // 5 // 9 //