Matrices#

A matrix, like a vector, is also a collection of numbers. The difference is that a matrix is a table of numbers rather than a list. Many of the same rules we just outlined for vectors above apply equally well to matrices. In fact, you can think of vectors as matrices that happen to only have one column or one row.

The dimensions of a matrix tells its size: the number of rows and columns of the matrix, in that order.

Since matrix \(\mathbf{A}\) has two rows and three columns, we write its dimensions as \(2\times 3\), pronounced “two by three”. In contrast, matrix \(\mathbf{B}\) has three rows and two columns, so it is a \(3\times 2\) matrix.

\[\begin{split}\mathbf{A} = \begin{bmatrix} -2 & 5 & 6\\ 5 & 2 & 7 \end{bmatrix}\end{split}\]
\[\begin{split}\mathbf{B} = \begin{bmatrix} -8 & -4\\ 23 & 12\\ 18 & 10 \end{bmatrix}\end{split}\]

APSG offers matrix2 and matrix3 classes for square matrices commonly used in structural geology.

A = matrix2([[-2, 5], [5, 2]])
B = matrix2([[4, -1], [-1, 3]])

Matrix addition, substraction and multiplication#

First, let’s consider matrix addition and subtraction. This part is uncomplicated. You can add and subtract matrices the same way you add vectors – element by element:

\[\mathbf{A}+\mathbf{B}=\mathbf{C}\]
\[\begin{split}\begin{bmatrix} a_{11} & a_{12}\\a_{21} & a_{22}\end{bmatrix} + \begin{bmatrix} b_{11} & b_{12}\\b_{21} & b_{22}\end{bmatrix} = \begin{bmatrix} a_{11}+b_{11} & a_{12}+b_{12}\\a_{21}+b_{21} & a_{22}+b_{22}\end{bmatrix}\end{split}\]
A + B
Matrix2
[[2. 4.]
 [4. 5.]]

Matrix multiplication gets a bit more complicated, since multiple elements in the first matrix interact with multiple elements in the second to generate each element in the product matrix. This means that matrix multiplication can be a tedious task to carry out by hand, and can be time consuming on a computer for very large matrices.

\[\mathbf{A}\mathbf{B}=\mathbf{C}\]
\[\begin{split}\begin{bmatrix} a_{11} & a_{12}\\a_{21} & a_{22}\end{bmatrix} \begin{bmatrix} b_{11} & b_{12}\\b_{21} & b_{22}\end{bmatrix} = \begin{bmatrix} a_{11}b_{11}+a_{12}b_{21} & a_{11}b_{12}+a_{12}b_{22}\\a_{21}b_{21}+a_{12}b_{21} & a_{21}b_{22}+a_{12}b_{22}\end{bmatrix}\end{split}\]
A @ B
Matrix2
[[-13.  17.]
 [ 18.   1.]]