Matrix ====== The :code:`Matrix` class is used to store a mathematical matrix. Each instance of :code:`Matrix` represents an :math:`n \times m` matrix that can be written as: .. math:: A = \begin{bmatrix} a_{0,0} & \dots & a_{0,n-1} \\ \vdots & \ddots & \vdots \\ a_{m-1,0} & \dots & a_{m-1,n-1} \end{bmatrix} Creating a matrix ----------------- The :code:`Matrix` class provides four different constructors for creating a new :code:`Matrix`: 1. Create a new matrix and fill in the values: .. code-block:: cpp LouLib::Matrix A1(2, 3); A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; A[1][0] = 4; A[1][1] = 5; A[1][2] = 6; 2. Create a matrix from :code:`std::initializer_list>`: .. code-block:: cpp LouLib::Matrix A2({{1, 2, 3}, {4, 5, 6}}); 3. Create a matrix from :code:`std::vector>`: .. code-block:: cpp std::vector row1; row1.push_back(1); row1.push_back(2); row1.push_back(3); std::vector row2; row2.push_back(4); row2.push_back(5); row2.push_back(6); std::vector> matrixData; matrixData.push_back(row1); matrixData.push_back(row2); LouLib::Matrix A3(matrixData); 4. Create a copy of an already existing matrix: .. code-block:: cpp LouLib::Matrix A4(A1); All four of the examples above create the same matrix: .. math:: A_1 = A_2 = A_3 = A_4 = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} Accessing elements in a matrix ------------------------------ The elements of a matrix can be accessed using the :code:`[][]` operator, which returns a reference to the element at the given row and column: .. code-block:: cpp //Create new matrix LouLib::Math::Matrix A({{1,2},{3,4}}); //Get elements double firstElement = A[0][0]; double secondElement = A[0][1]; double thirdElement = A[1][0]; double fourthElement = A[1][1]; After running the example above, the value of :code:`firstElement` will be 1, the value of :code:`secondElement` will be 2, the value of :code:`thirdElement` will be 3, and the value of :code:`fourthElement` will be 4. The :code:`[][]` operator can also be used to edit the elements in a matrix: .. code-block:: cpp //Create new matrix LouLib::Math::Matrix A({{1, 2, 3},{4,5,6}}); //Edit element v[0][0] = 10; The example above will edit the elements of the matrix as follows: .. math:: A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \quad\enspace \boldsymbol{\Longrightarrow} \quad\enspace A = \begin{bmatrix} 10 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} The Matrix class also contains :code:`getRow` and :code:`getCol` methods that the return the row/column vector at the given index: .. code-block:: cpp //Create new matrix LouLib::Math::Matrix A({{1, 2, 3},{4,5,6}}); //Get first column LouLib::Math::Vector v1 = A.getCol(0); //Get first row LouLib::Math::Vector v2 = A.getRow(0); The example above will result in the following two vectors: .. math:: \mathbf{v}_1 = \begin{bmatrix} 1 \\ 4 \end{bmatrix} \quad\quad\quad \mathbf{v}_2 = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} Matrix methods -------------- The :code:`Matrix` class contains the following methods: * :code:`.rows()` - Returns the number of rows in the matrix * :code:`.cols()` - Returns the number of columns in the matrix * :code:`.transpose()` - Returns the transpose of the matrix * :code:`.norm()` - Returns the frobenius norm of the matrix: :math:`\sqrt{\sum\limits_{i=0}^{m-1} \sum\limits_{j=0}^{n-1} (a_{i,j})^2}` * :code:`.det()` - Returns the determinant of the matrix (only works for square matrices) * :code:`.inverse()` - Returns the inverse of the matrix (only works if the matrix is invertible) Matrix operators ---------------- The :code:`Matrix` class contains the following operators: * :code:`+` - Used to add two matrices * :code:`-` - Used to subtract two matrices * :code:`*` - Used to multipy two matrices, multiply a matrix by a scalar, or multiply a vector by a matrix