Skip to main content

Matrices and Matrix Algebra

Matrices organize linear information. A matrix can be a table of data, a coefficient array for a system, or the rule for a linear transformation. Matrix algebra is designed so that these interpretations agree: matrix-vector multiplication applies a linear rule, and matrix-matrix multiplication composes such rules.

The main surprise is that matrix arithmetic resembles ordinary arithmetic only partly. Addition behaves as expected, but multiplication is order-sensitive and dimension-sensitive. This is not a defect. It records the fact that applying one linear process after another depends on which process happens first and whether the output size of the first matches the input size of the second.

Definitions

An m×nm\times n matrix A=[aij]A=[a_{ij}] has mm rows and nn columns. Two matrices are equal if they have the same size and the same corresponding entries.

Matrix addition and scalar multiplication are entrywise:

(A+B)ij=aij+bij,(cA)ij=caij.(A+B)_{ij}=a_{ij}+b_{ij}, \qquad (cA)_{ij}=ca_{ij}.

If AA is m×nm\times n and BB is n×pn\times p, then ABAB is m×pm\times p with entries

(AB)ij=ai1b1j+ai2b2j++ainbnj.(AB)_{ij}=a_{i1}b_{1j}+a_{i2}b_{2j}+\cdots+a_{in}b_{nj}.

Equivalently, the jjth column of ABAB is AA times the jjth column of BB. This column viewpoint is often the cleanest way to remember multiplication:

AB=A[b1b2bp]=[Ab1Ab2Abp].AB=A \begin{bmatrix} \mathbf{b}_1&\mathbf{b}_2&\cdots&\mathbf{b}_p \end{bmatrix} = \begin{bmatrix} A\mathbf{b}_1&A\mathbf{b}_2&\cdots&A\mathbf{b}_p \end{bmatrix}.

The transpose ATA^T is formed by interchanging rows and columns. A square matrix has a main diagonal. Important square matrices include the identity matrix II, diagonal matrices, triangular matrices, and symmetric matrices satisfying AT=AA^T=A.

Key results

Matrix addition is commutative and associative. Matrix multiplication is associative and distributes over addition:

A(BC)=(AB)C,A(B+C)=AB+AC.A(BC)=(AB)C, \qquad A(B+C)=AB+AC.

However, multiplication is usually not commutative: ABAB and BABA may differ or one product may not even be defined. This is one of the most important habits to build early. When matrices represent functions, ABAB means "first apply BB, then apply AA," so reversing the order usually changes the result.

Transpose rules are:

(A+B)T=AT+BT,(cA)T=cAT,(AB)T=BTAT.(A+B)^T=A^T+B^T, \qquad (cA)^T=cA^T, \qquad (AB)^T=B^TA^T.

The reversed order in (AB)T(AB)^T is forced by dimensions and by entries. The ijij entry of (AB)T(AB)^T is the jiji entry of ABAB, which is row jj of AA dotted with column ii of BB. That same scalar is the ijij entry of BTATB^TA^T.

For compatible matrices, multiplication by the identity leaves a matrix unchanged:

IA=A,AI=A.IA=A,\qquad AI=A.

Zero matrices absorb under multiplication when dimensions allow:

A0=0,0A=0.A0=0,\qquad 0A=0.

But the cancellation law can fail. From AB=ACAB=AC, one cannot conclude B=CB=C unless AA has an appropriate inverse.

Matrix multiplication can be understood in several equivalent ways, and each viewpoint is useful in a different setting. The entry formula is best for direct arithmetic. The column viewpoint says that the columns of ABAB are the images of the columns of BB under AA. The row viewpoint says that the rows of ABAB are linear combinations of the rows of BB, weighted by rows of AA. The transformation viewpoint says that ABAB represents composition.

Block matrices extend the same ideas. If the sizes match, a matrix can be partitioned into submatrices and multiplied by blocks. For example,

[ABCD][xy]=[Ax+ByCx+Dy].\begin{bmatrix} A&B\\ C&D \end{bmatrix} \begin{bmatrix} \mathbf{x}\\ \mathbf{y} \end{bmatrix} = \begin{bmatrix} A\mathbf{x}+B\mathbf{y}\\ C\mathbf{x}+D\mathbf{y} \end{bmatrix}.

This notation is common in systems, least squares, numerical linear algebra, and theoretical proofs because it keeps related groups of variables together.

Special matrix classes carry extra structure. Diagonal matrices scale coordinate axes independently. Triangular matrices are easy to solve by substitution. Symmetric matrices satisfy AT=AA^T=A and have real eigenvalues with orthogonal eigenvectors. Orthogonal matrices satisfy QTQ=IQ^TQ=I and preserve dot products. Recognizing these classes often tells you which theorem or algorithm is appropriate before doing any arithmetic.

The trace of a square matrix is the sum of its diagonal entries:

tr(A)=a11+a22++ann.\operatorname{tr}(A)=a_{11}+a_{22}+\cdots+a_{nn}.

It satisfies tr(A+B)=tr(A)+tr(B)\operatorname{tr}(A+B)=\operatorname{tr}(A)+\operatorname{tr}(B) and tr(AB)=tr(BA)\operatorname{tr}(AB)=\operatorname{tr}(BA) when both products are defined as square matrices. Trace later connects to eigenvalues, quadratic forms, and matrix inner products.

Dimension discipline is a major part of matrix algebra. Before performing any calculation, identify the shape of each matrix and the shape of the result. This habit prevents many errors and also clarifies meaning: an m×nm\times n matrix maps coordinate vectors in Rn\mathbb{R}^n to coordinate vectors in Rm\mathbb{R}^m.

Visual

OperationConditionResult sizeKey warning
A+BA+Bsame sizesame as AA and BBentrywise only
cAcAscalar ccsame as AAscales every entry
ABABcolumns of AA = rows of BBrows of AA by columns of BBorder matters
ATA^Tany matrixcolumns of AA by rows of AAreverses products
AxA\mathbf{x}x\mathbf{x} has one entry per column of AAone entry per row of AAlinear combination of columns

Worked example 1: Compute a product and interpret columns

Problem: let

A=[120134],B=[210352].A= \begin{bmatrix} 1&2&0\\ -1&3&4 \end{bmatrix}, \qquad B= \begin{bmatrix} 2&1\\ 0&-3\\ 5&2 \end{bmatrix}.

Compute ABAB.

Step 1: check dimensions. AA is 2×32\times 3 and BB is 3×23\times 2, so ABAB is defined and has size 2×22\times 2.

Step 2: compute entries by row-column dot products.

(AB)11=12+20+05=2,(AB)12=11+2(3)+02=5,(AB)21=(1)2+30+45=18,(AB)22=(1)1+3(3)+42=2.\begin{aligned} (AB)_{11}&=1\cdot2+2\cdot0+0\cdot5=2,\\ (AB)_{12}&=1\cdot1+2(-3)+0\cdot2=-5,\\ (AB)_{21}&=(-1)2+3\cdot0+4\cdot5=18,\\ (AB)_{22}&=(-1)1+3(-3)+4\cdot2=-2. \end{aligned}

Thus

AB=[25182].AB= \begin{bmatrix} 2&-5\\ 18&-2 \end{bmatrix}.

Step 3: check with the column viewpoint. The first column of BB is [205]T\begin{bmatrix}2&0&5\end{bmatrix}^T, and

A[205]=[218],A\begin{bmatrix}2\\0\\5\end{bmatrix} = \begin{bmatrix}2\\18\end{bmatrix},

which matches the first column of ABAB.

Worked example 2: Show multiplication is not commutative

Problem: compare ABAB and BABA for

A=[1201],B=[3045].A= \begin{bmatrix} 1&2\\ 0&1 \end{bmatrix}, \qquad B= \begin{bmatrix} 3&0\\ 4&5 \end{bmatrix}.

Step 1: compute ABAB.

AB=[13+2410+2503+1400+15]=[111045].AB= \begin{bmatrix} 1\cdot3+2\cdot4 & 1\cdot0+2\cdot5\\ 0\cdot3+1\cdot4 & 0\cdot0+1\cdot5 \end{bmatrix} = \begin{bmatrix} 11&10\\ 4&5 \end{bmatrix}.

Step 2: compute BABA.

BA=[31+0032+0141+5042+51]=[36413].BA= \begin{bmatrix} 3\cdot1+0\cdot0 & 3\cdot2+0\cdot1\\ 4\cdot1+5\cdot0 & 4\cdot2+5\cdot1 \end{bmatrix} = \begin{bmatrix} 3&6\\ 4&13 \end{bmatrix}.

Step 3: compare. Since

[111045][36413],\begin{bmatrix} 11&10\\ 4&5 \end{bmatrix} \neq \begin{bmatrix} 3&6\\ 4&13 \end{bmatrix},

the products are not equal. Checked answer: both products exist, but ABBAAB\neq BA.

Code

import numpy as np

A = np.array([[1, 2, 0],
[-1, 3, 4]])
B = np.array([[2, 1],
[0, -3],
[5, 2]])

print(A @ B)
print((A @ B).T)
print(B.T @ A.T)
print(np.allclose((A @ B).T, B.T @ A.T))

The @ operator performs matrix multiplication in NumPy. The final line checks the transpose product rule (AB)T=BTAT(AB)^T=B^TA^T.

Common pitfalls

  • Multiplying corresponding entries and calling the result ABAB. Entrywise multiplication is a different operation.
  • Forgetting the dimension check before multiplying.
  • Reversing product order when translating composition. If TA(x)=AxT_A(\mathbf{x})=A\mathbf{x} and TB(x)=BxT_B(\mathbf{x})=B\mathbf{x}, then TATBT_A\circ T_B corresponds to ABAB.
  • Assuming AB=BAAB=BA because ordinary numbers commute.
  • Distributing transpose without reversing order.
  • Treating the identity matrix as one fixed size. The size of II depends on context.

A practical way to avoid multiplication mistakes is to annotate shapes before multiplying. If AA is 2×32\times3 and BB is 3×43\times4, then ABAB is 2×42\times4 because the inner dimensions match and disappear, while the outer dimensions remain. If the inner dimensions do not match, the product is not defined. This simple check should happen before any entry arithmetic begins.

When a product represents composition, read it from right to left as an action on vectors. The expression ABxAB\mathbf{x} means first compute BxB\mathbf{x}, then apply AA. This convention explains both the order of matrix multiplication and the transpose rule. It also prevents a common modeling error: writing transformations in the order they are described verbally rather than in the order they act on the vector.

Special matrices should trigger special expectations. A diagonal matrix scales coordinates independently. A triangular matrix leads naturally to substitution. A symmetric matrix is connected to quadratic forms and orthogonal eigenvectors. An orthogonal matrix preserves dot products and lengths. Identifying these structures early can reduce computation and suggest the correct theorem.

For block matrices, always check that each block operation is dimensionally valid. Blocks behave like entries only when the block sizes match the intended algebra. Used correctly, block notation makes large systems easier to read. Used carelessly, it hides dimension errors that would have been obvious in entry notation.

Matrix powers are defined only for square matrices. If A2A^2 appears, then AA must be square because the output of the first application must be a valid input for the second. Powers describe repeated application of the same linear transformation, which is why they occur in Markov chains, recurrence relations, and dynamical systems.

The identity and zero matrices play different algebraic roles. The identity matrix leaves vectors and compatible matrices unchanged, so it behaves like the number 11 under multiplication. The zero matrix sends every vector to zero, so it destroys information. Unlike ordinary arithmetic, it is possible for nonzero matrices AA and BB to have AB=0AB=0 if the range of BB lies in the null space of AA.

Transpose connects row and column viewpoints. The columns of AA become the rows of ATA^T, and symmetric matrices are exactly those where these viewpoints match across the diagonal. This simple operation becomes important in dot products, normal equations, orthogonal matrices, and quadratic forms.

Connections