Matrices and Vectors Tutorial

Matrices and Vectors Tutorial

A live version of this calculation is available at EngineeringPaper.xyz.

Defining Matrices and Vectors

In addition scalar values, EngineeringPaper.xyz supports matrices and vectors. Note that in EngineeringPaper.xyz, vectors are not different than matrices and are represented by matrices with either a single row or column. A matrix can be created either using the “Matrices” tab on the virtual keyboard or by typing [m,n] and than hitting enter, where m is the number of rows and n is the number of columns. For example, type [2,3] and than hit Enter to create a matrix with 2 rows and 3 columns.

Any valid expression can be entered into a matrix, including numbers with units. See the following example:

$$ \begin{bmatrix}1\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack\ 3\left\lbrack m\right\rbrack & 4\left\lbrack m\right\rbrack\end{bmatrix}= \begin{bmatrix} 1\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack \ 3\left\lbrack m\right\rbrack & 4\left\lbrack m\right\rbrack \end{bmatrix} $$

Like scalar quantities, the units of the matrix output can be specified by placing the desired units after the equals sign of the query statement (note that this only works if all of the elements of the matrix have the same units).

$$ \begin{bmatrix}1\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack\ 3\left\lbrack m\right\rbrack & 4\left\lbrack m\right\rbrack\end{bmatrix}=\left\lbrack yards\right\rbrack =\begin{bmatrix} 1.09\left\lbrack yards\right\rbrack & 2.19\left\lbrack yards\right\rbrack \ 3.28\left\lbrack yards\right\rbrack & 4.37\left\lbrack yards\right\rbrack \end{bmatrix} $$

It can be tedious to define the units for every element of a large matrix. In these cases, a scalar multiplied times the matrix can be used to set the units:

$$ 1\left\lbrack m\right\rbrack\cdot\begin{bmatrix}1 & 2\ 3 & 4\end{bmatrix}= \begin{bmatrix} 1\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack \ 3\left\lbrack m\right\rbrack & 4\left\lbrack m\right\rbrack \end{bmatrix} $$

Matrix Multiplication

EngineeringPaper.xyz has two different multiplication symbols. The typical scalar multiplication symbol can be typed using the * key and the matrix multiplication symbol can be typed using the @ key (or by typing Ctrl-*). In the actual mathematical expression, the scalar multiplication symbol is represented by a dot and the matrix multiplication symbol is represented by an x. From the virtual keyboard, the * button represents scalar multiplication and the x button on the “Matrices” tab of the keyboard represents matrix multiplication. In all cases, except for the cross product case described below, it is safe to use either multiplication symbol for scalar or matrix multiplication. However, using the matrix multiplication symbol for all multiplication between two matrices may be helpful to make the intent more clear as in the following examples:

$$ 2\cdot\begin{bmatrix}1 & 0\ 0 & 1\end{bmatrix}\times\begin{bmatrix}a\ b\end{bmatrix}= \begin{bmatrix} 2 \cdot a \ 2 \cdot b \end{bmatrix} $$

Like other expressions, matrices can be assigned to variables and used in expressions as shown below:

$$ A=\begin{bmatrix}a1 & a2\ a3 & a4\end{bmatrix},:v=\begin{bmatrix}2\ 4\end{bmatrix} $$

$$ A\times v= \begin{bmatrix} 2 \cdot a1 + 4 \cdot a2 \ 2 \cdot a3 + 4 \cdot a4 \end{bmatrix} $$

Dot Product and Cross Product

The dot product can be computed for two vectors of the same size using the dot function as shown below:

$$ \mathrm{dot}\left(\begin{bmatrix}a1\ a2\ a3\ a4\end{bmatrix},\begin{bmatrix}b1\ b2\ b3\ b4\end{bmatrix}\right)= a1 \cdot b1 + a2 \cdot b2 + a3 \cdot b3 + a4 \cdot b4 $$

or using row vectors:

$$ \mathrm{dot}\left(\begin{bmatrix}a1 & a2 & a3 & a4\end{bmatrix},\begin{bmatrix}b1 & b2 & b3 & b4\end{bmatrix}\right)= a1 \cdot b1 + a2 \cdot b2 + a3 \cdot b3 + a4 \cdot b4 $$

The matrix multiplication symbol will automatically calculate a cross product if both vectors are either 3x1 or 1x3 (note that the shape of the result vector matches the shape of the input vectors):

$$ \begin{bmatrix}a1\ a2\ a3\end{bmatrix}\times\begin{bmatrix}b1\ b2\ b3\end{bmatrix}= \begin{bmatrix} a2 \cdot b3 - a3 \cdot b2 \ - a1 \cdot b3 + a3 \cdot b1 \ a1 \cdot b2 - a2 \cdot b1 \end{bmatrix} $$

$$ \begin{bmatrix}a1 & a2 & a3\end{bmatrix}\times\begin{bmatrix}b1 & b2 & b3\end{bmatrix}= \begin{bmatrix} a2 \cdot b3 - a3 \cdot b2 & - a1 \cdot b3 + a3 \cdot b1 & a1 \cdot b2 - a2 \cdot b1 \end{bmatrix} $$

Transpose

The transpose of a matrix can be obtained using the ctrl-‘ (or command-‘ on mac) keyboard shortcut or using the virtual keyboard:

$$ \begin{bmatrix}a\ b\ c\end{bmatrix}^{\mathrm{T}}= \begin{bmatrix} a & b & c \end{bmatrix} $$

The transpose function can be used as well:

$$ \mathrm{transpose}\left(\begin{bmatrix}a\ b\ c\end{bmatrix}\right)= \begin{bmatrix} a & b & c \end{bmatrix} $$

Euclidean Norm of a Matrix

The magnitude of a matrix can be calculated using the norm function or using the matrix norm notation ||v||:

$$ \mathrm{norm}\left(\begin{bmatrix}a\ b\ c\end{bmatrix}\right)= \sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2} + \left|{c}\right|^{2}} $$

$$ \left\Vert\begin{bmatrix}a\ b\ c\end{bmatrix}\right\Vert= \sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2} + \left|{c}\right|^{2}} $$

Matrix Inverse and Determinant

The inverse or determinant of a matrix can be calculated using the inv and det functions. Additionally, the inverse can be obtained by using a exponent of -1:

$$ \mathrm{inv}\left(\begin{bmatrix}2\left\lbrack m\right\rbrack & 0\left\lbrack m\right\rbrack\ 0\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack\end{bmatrix}\right)= \begin{bmatrix} 0.5\left\lbrack \frac{1}{m}\right\rbrack & 0\left\lbrack \frac{1}{m}\right\rbrack \ 0\left\lbrack \frac{1}{m}\right\rbrack & 0.5\left\lbrack \frac{1}{m}\right\rbrack \end{bmatrix} $$

$$ \begin{bmatrix}2\left\lbrack m\right\rbrack & 0\left\lbrack m\right\rbrack\ 0\left\lbrack m\right\rbrack & 2\left\lbrack m\right\rbrack\end{bmatrix}^{-1}= \begin{bmatrix} 0.5\left\lbrack \frac{1}{m}\right\rbrack & 0\left\lbrack \frac{1}{m}\right\rbrack \ 0\left\lbrack \frac{1}{m}\right\rbrack & 0.5\left\lbrack \frac{1}{m}\right\rbrack \end{bmatrix} $$

$$ \mathrm{det}\left(\begin{bmatrix}a & b\ c & d\end{bmatrix}\right)= a \cdot d - b \cdot c $$

Indexing a Matrix

A subscript with two numbers separated by a comma can be used to access the elements of a matrix. Use the underscore keyboard shortcut to create a subscript. Note that matrix indexing starts with 1. The first index represents the row and the second index represents the column.

$$ B=\begin{bmatrix}1 & 2\ 3 & 4\ 5 & 6\end{bmatrix} $$

$$ B_{1,1}= 1 $$

$$ B_{3,2}= 6 $$

The indices may be any valid mathematical expression as long as it evaluates to an integer value:

$$ m=1,:n=1 $$

$$ B_{m+2,2\cdot n}= 6 $$

Note that since vectors are themselves matrices, two subscripts are required to index a vector:

$$ \begin{bmatrix}a\ b\ c\end{bmatrix}_{3,1}= c $$

Matrix Slicing

Ranges of rows and/or columns can be extracted from a matrix or vector using the slicing notation. The slicing notation is the same as the slicing notation that is used in MATLAB (note that the MATLAB slicing notation differs slightly from the Python slicing notation). A notation of $a:b$ can be used for an index to extract rows or columns $a$ through $b$. Similar to MATLAB, the keyword $end$ can be used to represent that last row or column where $1:end$ represents the range of all rows or columns. For example, the following can be used to extract all of the rows and the 2nd through 3rd columns of a matrix:

$$ C=\begin{bmatrix}1 & 2 & 3 & 4 & 5\ 6 & 7 & 8 & 9 & 10\ 11 & 12 & 13 & 14 & 15\end{bmatrix} $$

$$ C_{1:end,2:3}= \begin{bmatrix} 2 & 3 \ 7 & 8 \ 12 & 13 \end{bmatrix} $$

Similar to MATLAB and Python, a single colon may be used as a shortcut for all rows or columns. Using this shortcut, the following extracts the 2nd column and all of the rows

$$ C_{:,2}= \begin{bmatrix} 2 \ 7 \ 12 \end{bmatrix} $$

Rows can be extracted similarly. The following extracts the 3rd row:

$$ C_{3,1:end}= \begin{bmatrix} 11 & 12 & 13 & 14 & 15 \end{bmatrix} $$

A slicing notation of $a:s:b$ can be used to extract rows or columns $a$ through $b$ using a step of $s$. Note this is the same as MATLAB but differs from Python (Python places the step as the last value instead of the middle value). The following extracts every other value in the first row:

$$ C_{1,1:2:end}= \begin{bmatrix} 1 & 3 & 5 \end{bmatrix} $$

A stride of $-1$ can be used reverse a row or column. A reversed version of the first row can be obtained using:

$$ C_{1,end:-1:1}= \begin{bmatrix} 5 & 4 & 3 & 2 & 1 \end{bmatrix} $$

Assembling Matrices Using Submatrices

Larger vectors and matrices may be assembled from smaller vectors and matrices provided that their dimensions are compatible. This is best illustrated by the following example:

$$ D=\begin{bmatrix}1 & 2\ 3 & 4\end{bmatrix},:E=\begin{bmatrix}5 & 6\ 7 & 8\end{bmatrix},:F=\begin{bmatrix}9 & 10\ 11 & 12\end{bmatrix},G=\begin{bmatrix}13 & 14\ 15 & 16\end{bmatrix} $$

$$ \begin{bmatrix}D & E\ F & G\end{bmatrix}= \begin{bmatrix} 1 & 2 & 5 & 6 \ 3 & 4 & 7 & 8 \ 9 & 10 & 13 & 14 \ 11 & 12 & 15 & 16 \end{bmatrix} $$

Similarly, vectors can be combined as columns, side-by-side, or as rows:

$$ v_1=\begin{bmatrix}1\ 2\ 3\end{bmatrix},:v_2=\begin{bmatrix}4\ 5\ 6\end{bmatrix} $$

$$ \begin{bmatrix}v_1\ v_2\end{bmatrix}= \begin{bmatrix} 1 \ 2 \ 3 \ 4 \ 5 \ 6 \end{bmatrix} $$

$$ \begin{bmatrix}v_1 & v_2\end{bmatrix}= \begin{bmatrix} 1 & 4 \ 2 & 5 \ 3 & 6 \end{bmatrix} $$

$$ \begin{bmatrix}v_1^{\mathrm{T}} & v_2^{\mathrm{T}}\end{bmatrix}= \begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 \end{bmatrix} $$

Using Summation and Product Notation

Summation or product notation, along with the numrows and numcols functions, may be used to loop over matrices. For example, the following calculates the sum of the values in the second row of the matrix $C$:

$$ H=\begin{bmatrix}1 & 2 & 4\ 4 & 5 & 6\end{bmatrix} $$

$$ \sum_{j=1}^{\mathrm{numcols}\left(H\right)}\left(H_{2,j}\right)= 15 $$

Equation Solving with Matrix Equations

The symbolic and numerical equation solvers work with matrices as well. The following example shows how to solve for the Eigenvalues of a matrix:

$$ \text{System} = \begin{cases} \mathrm{det}\left(\begin{bmatrix}1 & 0 & 1\ 0 & 2 & 0\ 1 & 0 & 3\end{bmatrix}-\lambda\cdot\begin{bmatrix}1 & 0 & 0\ 0 & 1 & 0\ 0 & 0 & 1\end{bmatrix}\right)=0 \end{cases} $$

$$ \text{Solution} = \begin{cases} \lambda & = \quad 2 \end{cases} $$

The result value can then be used on other expressions as shown below:

$$ \lambda= 2 $$