Skip to content

Matrix Views

Draken Wan edited this page Feb 16, 2024 · 4 revisions

matrixView class creates a view of the elements of the matrix class. I've only started working on it to be able to access a slice of a large matrix without having to make use of extra memory operations that would take place if I performed the generic slice operation of matrix class. Below is a simple usage example. You can also refer to the test_matview.cpp file in the root folder to see an example.

Usage

Below usage example shows a 4x4 double matrix being initialized. Then a 2x2 view is generated that has row range [0,2) and col range [0,2). It also shows the member function cvtToMatrix() which when invoked by a matView object will return a completely new matrix generated from that view. Any changes made to this newly generated matrix object will not be reflected in the matrix from which it was generated.

matrix<double> A = {{1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 8, 7, 6},
                    {5, 4, 3, 2}};
A.display("A:-");

matrixView<double> viewA(A, range(2), range(3));
viewA.display("viewA:-");
matrix<double> splitA = viewA.cvtToMatrix();
splitA.display("splitA");

gives


A:-
 1 2 3 4
 5 6 7 8
 9 8 7 6
 5 4 3 2

[(matrixView)] viewA:-
 1 2 3
 5 6 7

splitA
 1 2 3
 5 6 7

Any changes you make to the view of the matrix will reflect in the original matrix. I am trying to expand views to be able to interface with other linear data structures such as a C style array in such a way that matView class assumes it as a flattened matrix representation (for a row major representation) and performs operations on it similar to matrix.

Clone this wiki locally