TExcelTAD
Home › Calculus Essentials › Matrices and Determinants

Matrices and Determinants

A matrix is a rectangular grid of numbers. Multiplying a vector by a matrix moves it to a new place, so a matrix can describe a rotation, a scaling, a shear or a combination of them. The determinant measures how much a matrix stretches area or volume, and whether it can be undone.

Matrices and matrix-vector products

An m×nm \times n matrix has mm rows and nn columns. To multiply a 2×22 \times 2 matrix by a vector, take the dot product of each row with the vector:

[abcd][xy]=[a⋅x+b⋅yc⋅x+d⋅y]\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} a \cdot x + b \cdot y \\ c \cdot x + d \cdot y \end{bmatrix}

There is a second way to read the same product, and it explains what a matrix does: the result is x⋅(first column)+y⋅(second column)x \cdot (\text{first column}) + y \cdot (\text{second column}). The columns are where the basis vectors land. The matrix sends i=⟨1,0⟩\mathbf{i} = \langle 1, 0 \rangle to its first column and j=⟨0,1⟩\mathbf{j} = \langle 0, 1 \rangle to its second, and every other vector follows along because the map is linear.

Transform (2D) Matrix Columns say
Scale by sxs_x, sys_y [sx00sy]\begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} i→⟨sx,0⟩\mathbf{i} \to \langle s_x, 0 \rangle, j→⟨0,sy⟩\mathbf{j} \to \langle 0, s_y \rangle
Rotate by θ\theta (counter-clockwise) [cos⁡θ−sin⁡θsin⁡θcos⁡θ]\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} i→⟨cos⁡θ,sin⁡θ⟩\mathbf{i} \to \langle \cos\theta, \sin\theta \rangle
Shear in xx by kk [1k01]\begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix} j→⟨k,1⟩\mathbf{j} \to \langle k, 1 \rangle
Reflect across the x-axis [100−1]\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} j→⟨0,−1⟩\mathbf{j} \to \langle 0, -1 \rangle

Composing transforms: matrix multiplication

Applying BB and then AA to a vector is the same as applying the single matrix ABAB. Entry (i,j)(i, j) of ABAB is the dot product of row ii of AA with column jj of BB. Matrix multiplication is associative but not commutative: in general AB≠BAAB \ne BA, just as "rotate then scale" differs from "scale then rotate".

python · try it yourself
import numpy as np

theta = np.radians(90)
R = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta),  np.cos(theta)]]).round(12)
S = np.array([[2.0, 0.0],
              [0.0, 0.5]])

p = np.array([1.0, 1.0])
print("rotate p        :", R @ p)
print("scale then rotate:", (R @ S) @ p)
print("rotate then scale:", (S @ R) @ p)
print("R @ S =\n", R @ S)
print("S @ R =\n", S @ R)
rotate p        : [-1.  1.]
scale then rotate: [-0.5  2. ]
rotate then scale: [-2.   0.5]
R @ S =
 [[ 0.  -0.5]
 [ 2.   0. ]]
S @ R =
 [[ 0.  -2. ]
 [ 0.5  0. ]]

In R @ S @ p the matrix nearest the vector acts first. Game engines build a model matrix from translation, rotation and scale (TRS) in exactly this way; see Matrices and TRS. Translation is not linear, so engines use 3×33 \times 3 (2D) or 4×44 \times 4 (3D) homogeneous matrices to include it.

The determinant

For a 2×22 \times 2 matrix:

det⁡[abcd]=a⋅d−b⋅c\det \begin{bmatrix} a & b \\ c & d \end{bmatrix} = a \cdot d - b \cdot c

For a 3×33 \times 3 matrix, expand along the first row (cofactor expansion):

det⁡[abcdefghi]=a(ei−fh)−b(di−fg)+c(dh−eg)\det \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} = a(ei - fh) - b(di - fg) + c(dh - eg)

The determinant has a clean geometric meaning:

det⁡A\det A What the transform does
∣det⁡A∣\lvert \det A \rvert factor by which it scales areas (2D) or volumes (3D)
det⁡A<0\det A < 0 flips orientation (a mirror image)
det⁡A=0\det A = 0 squashes space flat (onto a line or plane); AA has no inverse
det⁡(AB)=det⁡A⋅det⁡B\det(AB) = \det A \cdot \det B scale factors multiply when you compose

The 3×33 \times 3 determinant of the rows a\mathbf{a}, b\mathbf{b}, c\mathbf{c} equals the scalar triple product a⋅(b×c)\mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}), the signed volume of the box they span. That links this lesson to the cross product.

python · try it yourself
import numpy as np

A = np.array([[3.0, 1.0],
              [1.0, 2.0]])
square = np.array([[0, 1, 1, 0],
                   [0, 0, 1, 1]], dtype=float)   # unit square corners as columns
print("det A:", round(np.linalg.det(A), 6))
print("image of the unit square:\n", A @ square)

M = np.array([[2.0, 0.0, 1.0],
              [1.0, 3.0, 0.0],
              [0.0, 1.0, 4.0]])
print("det M          :", round(np.linalg.det(M), 6))
print("a . (b x c)    :", np.dot(M[0], np.cross(M[1], M[2])))
print("det of a mirror:", np.linalg.det(np.diag([1.0, -1.0])))
det A: 5.0
image of the unit square:
 [[0. 3. 4. 1.]
 [0. 1. 3. 2.]]
det M          : 25.0
a . (b x c)    : 25.0
det of a mirror: -1.0

The unit square (area 1) becomes the parallelogram with corners (0, 0), (3, 1), (4, 3), (1, 2), whose area is 5.

Inverses and linear systems

If det⁡A≠0\det A \ne 0, AA has an inverse A−1A^{-1} with A−1A=IA^{-1}A = I (the identity). For 2×22 \times 2:

[abcd]−1=1ad−bc⋅[d−b−ca]\begin{bmatrix} a & b \\ c & d \end{bmatrix}^{-1} = \frac{1}{ad - bc} \cdot \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

A system of linear equations is one matrix equation Ax=bA\mathbf{x} = \mathbf{b}, solved by x=A−1b\mathbf{x} = A^{-1}\mathbf{b}. In code, use np.linalg.solve, which is faster and more accurate than forming the inverse.

python · try it yourself
import numpy as np

# 2x + y = 7  and  x + 3y = 11
A = np.array([[2.0, 1.0],
              [1.0, 3.0]])
b = np.array([7.0, 11.0])

print("inverse:\n", np.linalg.inv(A))
print("solve  :", np.linalg.solve(A, b))
print("check  :", A @ np.linalg.solve(A, b))
inverse:
 [[ 0.6 -0.2]
 [-0.2  0.4]]
solve  : [2. 3.]
check  : [ 7. 11.]

In software

  • Graphics pipelines move every vertex through model, view and projection matrices; the camera "un-transform" uses the inverse.
  • Neural networks: a dense layer computes Wx+bW\mathbf{x} + \mathbf{b}, a matrix-vector product, millions of times per second on a GPU.
  • Calculus ahead: the Jacobian is the matrix of a function's best local linear approximation, and its determinant is the area scale factor when you change variables in a double integral.

NumPy's @ operator, np.linalg.det, inv and solve are covered in NumPy Linear Algebra. A full course on these ideas is planned under Linear Algebra.

Exercises

Exercise 1: Multiply by hand

Compute A⋅BA \cdot B and B⋅AB \cdot A for A=[1201]A = \begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix} and B=[3012]B = \begin{bmatrix} 3 & 0 \\ 1 & 2 \end{bmatrix}.

Answer

AB=[5412]AB = \begin{bmatrix} 5 & 4 \\ 1 & 2 \end{bmatrix} and BA=[3614]BA = \begin{bmatrix} 3 & 6 \\ 1 & 4 \end{bmatrix}, so AB≠BAAB \ne BA.

Solution
AB: row 1⋅cols: (1⋅3+2⋅1, 1⋅0+2⋅2)=(5,4)row 2⋅cols: (0⋅3+1⋅1, 0⋅0+1⋅2)=(1,2)BA: row 1⋅cols: (3⋅1+0⋅0, 3⋅2+0⋅1)=(3,6)row 2⋅cols: (1⋅1+2⋅0, 1⋅2+2⋅1)=(1,4)\begin{aligned} AB{:}\ & \text{row 1} \cdot \text{cols: } (1 \cdot 3 + 2 \cdot 1,\ 1 \cdot 0 + 2 \cdot 2) = (5, 4) \\ & \text{row 2} \cdot \text{cols: } (0 \cdot 3 + 1 \cdot 1,\ 0 \cdot 0 + 1 \cdot 2) = (1, 2) \\ BA{:}\ & \text{row 1} \cdot \text{cols: } (3 \cdot 1 + 0 \cdot 0,\ 3 \cdot 2 + 0 \cdot 1) = (3, 6) \\ & \text{row 2} \cdot \text{cols: } (1 \cdot 1 + 2 \cdot 0,\ 1 \cdot 2 + 2 \cdot 1) = (1, 4) \end{aligned}

Exercise 2: Area after a transform

The matrix [4123]\begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix} is applied to a triangle of area 1.5. What is the area of the image?

Answer
Solution

det⁡=4⋅3−1⋅2=10\det = 4 \cdot 3 - 1 \cdot 2 = 10, so every area is multiplied by ∣10∣\lvert 10 \rvert: 1.5⋅10=151.5 \cdot 10 = 15.

Exercise 3: A 3×33 \times 3 determinant

Find det⁡[1203−12041]\det \begin{bmatrix} 1 & 2 & 0 \\ 3 & -1 & 2 \\ 0 & 4 & 1 \end{bmatrix} by cofactor expansion along the first row, and say whether the matrix is invertible.

Answer

−15, so it is invertible (and it flips orientation).

Solution
det⁡=1⋅((−1)⋅1−2⋅4)−2⋅(3⋅1−2⋅0)+0⋅(…)=1⋅(−9)−2⋅3+0=−15\begin{aligned} \det &= 1 \cdot ((-1) \cdot 1 - 2 \cdot 4) - 2 \cdot (3 \cdot 1 - 2 \cdot 0) + 0 \cdot (\ldots) \\ &= 1 \cdot (-9) - 2 \cdot 3 + 0 \\ &= -15 \end{aligned}
python · try it yourself
import numpy as np
print(round(np.linalg.det(np.array([[1, 2, 0], [3, -1, 2], [0, 4, 1]])), 6))
-15.0

Exercise 4: Solve a system (code)

Solve x+y+z=6x + y + z = 6, 2y+5z=−42y + 5z = -4, 2x+5y−z=272x + 5y - z = 27 with NumPy and check the answer by substitution.

Answer

x=5x = 5, y=3y = 3, z=−2z = -2.

Solution
python · try it yourself
import numpy as np
A = np.array([[1.0, 1.0, 1.0],
              [0.0, 2.0, 5.0],
              [2.0, 5.0, -1.0]])
b = np.array([6.0, -4.0, 27.0])
x = np.linalg.solve(A, b)
print(np.round(x, 10), A @ x)
[ 5.  3. -2.] [ 6. -4. 27.]

Substitution: 5+3−2=65 + 3 - 2 = 6, 6−10=−46 - 10 = -4, 10+15+2=2710 + 15 + 2 = 27.

Test yourself

What is det⁡[2346]\det \begin{bmatrix} 2 & 3 \\ 4 & 6 \end{bmatrix}?

  1. 24
  2. 0
  3. −24
  4. 12

The first column of a 2×22 \times 2 matrix tells you…

  1. where the vector ⟨1,0⟩\langle 1, 0 \rangle ends up
  2. the determinant
  3. where ⟨0,1⟩\langle 0, 1 \rangle ends up
  4. the rotation angle

Which statement about matrix multiplication is true?

  1. AB=BAAB = BA always
  2. det⁡(AB)=det⁡A+det⁡B\det(AB) = \det A + \det B
  3. ABAB means: apply BB first, then AA
  4. Only square matrices can be multiplied