TExcelTAD
Home › Linear Algebra & Matrix Theory › Definition and Equality

Definition and Equality

A matrix is a rectangular grid of numbers arranged in rows and columns. This lesson fixes the vocabulary (size, entries, square, identity, zero) and the one rule for when two matrices are equal, and shows how SymPy and NumPy store them.

Rows, columns and entries

An m×nm \times n matrix has mm rows and nn columns. We name matrices with capital letters and write the entry in row ii, column jj as aija_{ij} (row first, then column):

A=[a11a12a13a21a22a23]A is 2×3: 2 rows, 3 columnsB=[4−10257]b12=−1,b23=7,b21=2\begin{aligned} A &= \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix} && A \text{ is } 2 \times 3\text{: 2 rows, 3 columns} \\[1ex] B &= \begin{bmatrix} 4 & -1 & 0 \\ 2 & 5 & 7 \end{bmatrix} && b_{12} = -1, \quad b_{23} = 7, \quad b_{21} = 2 \end{aligned}

The numbers mm and nn are the size (or shape) of the matrix. The entries can come from any field: rational or real numbers here, complex numbers later, and integers modulo pp in Matrix Arithmetic Modulo p. A shorthand for "the matrix whose entries are aija_{ij}" is A=[aij]A = [a_{ij}].

Special shapes and special matrices

Name Shape / rule Example
Row vector 1×n1 \times n [30−2]\begin{bmatrix} 3 & 0 & -2 \end{bmatrix}
Column vector m×1m \times 1 the vectors of Points and Vectors
Square matrix n×nn \times n 2×22 \times 2, 3×33 \times 3, 4×44 \times 4 …
Main diagonal entries a11,a22,…,anna_{11}, a_{22}, \ldots, a_{nn}
Zero matrix 00 every entry is 0 any size
Identity matrix II (or InI_n) square, 1 on the diagonal, 0 elsewhere I3I_3 below
Diagonal matrix square, zero off the diagonal diag⁡(2,5,−1)\operatorname{diag}(2, 5, -1)
Upper triangular zero below the diagonal the echelon forms of Gaussian Elimination
I3=[100010001]diag⁡(2,5,−1)=[20005000−1]I_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \qquad \operatorname{diag}(2, 5, -1) = \begin{bmatrix} 2 & 0 & 0 \\ 0 & 5 & 0 \\ 0 & 0 & -1 \end{bmatrix}

You have already met matrices: the augmented matrix of a system of linear equations is exactly such a grid, and row reduction works on it entry by entry.

Equality

Two matrices AA and BB are equal, A=BA = B, when

  1. they have the same size m×nm \times n, and
  2. aij=bija_{ij} = b_{ij} for every ii and jj.

So a 2×32 \times 3 matrix is never equal to a 3×23 \times 2 matrix, even if they contain the same six numbers, and a 1×31 \times 3 row vector is not equal to a 3×13 \times 1 column vector. An equation between matrices is really m⋅nm \cdot n equations between numbers, which is why matrix equations can be solved by comparing entries:

[x3y+12z]=[5348]  ⟺  x=5, y+1=4, 2z=8  ⟺  x=5, y=3, z=4\begin{bmatrix} x & 3 \\ y + 1 & 2z \end{bmatrix} = \begin{bmatrix} 5 & 3 \\ 4 & 8 \end{bmatrix} \iff x = 5,\ y + 1 = 4,\ 2z = 8 \iff x = 5,\ y = 3,\ z = 4

Matrices in SymPy and NumPy

SymPy's Matrix keeps exact rational entries; NumPy's ndarray stores fast floating-point (or integer) numbers. Both index from 0, so the textbook entry a12a_{12} is A[0, 1] in code.

python · try it yourself
from sympy import Matrix, Rational, eye, zeros, diag

B = Matrix([[4, -1, 0],
            [2,  5, 7]])
print("shape:", B.shape)
print("b12 (row 1, col 2) =", B[0, 1])
print("b23 (row 2, col 3) =", B[1, 2])
print("row 2:", list(B.row(1)), " column 3:", list(B.col(2)))

print("I3 rows:", eye(3).tolist())
print("zero 2x3:", zeros(2, 3).tolist())
print("diag(2, 5, -1):", diag(2, 5, -1).tolist())

C = Matrix([[Rational(8, 2), -1, 0], [2, 5, 7]])   # 8/2 simplifies to 4
print("B == C:", B == C)
print("B == B.T:", B == B.T)                      # different shapes
shape: (2, 3)
b12 (row 1, col 2) = -1
b23 (row 2, col 3) = 7
row 2: [2, 5, 7]  column 3: [0, 7]
I3 rows: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
zero 2x3: [[0, 0, 0], [0, 0, 0]]
diag(2, 5, -1): [[2, 0, 0], [0, 5, 0], [0, 0, -1]]
B == C: True
B == B.T: False

In NumPy, == does not answer "are these matrices equal?". It compares entry by entry and returns a matrix of booleans. Use np.array_equal for exact equality and np.allclose when the entries are floats that went through rounding:

python · try it yourself
import numpy as np

A = np.array([[0.1 + 0.2, 1.0],
              [2.0,       3.0]])
B = np.array([[0.3, 1.0],
              [2.0, 3.0]])

print(A == B)                          # element-wise, not one answer
print("array_equal:", np.array_equal(A, B))
print("allclose:   ", np.allclose(A, B))
print("shapes differ:", np.array_equal(np.ones((2, 3)), np.ones((3, 2))))
[[False  True]
 [ True  True]]
array_equal: False
allclose:    True
shapes differ: False

Tip: if A == B: on NumPy arrays raises "The truth value of an array with more than one element is ambiguous". The error is NumPy telling you to choose array_equal, allclose, .all() or .any().

In software

  • Images are matrices: a 1080×19201080 \times 1920 grayscale image is a matrix of brightness values, and a colour image is three of them (or one 1080×1920×31080 \times 1920 \times 3 array).
  • Tables of data: a dataset with 10 000 rows and 12 features is a 10 000×1210\,000 \times 12 matrix XX; machine-learning libraries expect exactly this shape (NumPy and pandas for ML).
  • Games and graphics use 3×33 \times 3 and 4×44 \times 4 matrices for transforms (Matrices and Multiplication).
  • Memory layout: RAM is one-dimensional, so an m×nm \times n matrix is stored either row after row (row-major, C and NumPy's default) or column after column (column-major, Fortran, MATLAB, GLSL). In row-major order entry (i,j)(i, j) lives at offset i⋅n+ji \cdot n + j. Mixing the two conventions silently transposes your matrix.
  • Testing: comparing float matrices with exact equality is a classic flaky test; tolerance-based checks (allclose) are the norm.

Exercises

Exercise 1: Read off entries

For M=[30−417229−5608]M = \begin{bmatrix} 3 & 0 & -4 & 1 \\ 7 & 2 & 2 & 9 \\ -5 & 6 & 0 & 8 \end{bmatrix} (given row by row), state the size of MM and the entries m24m_{24}, m31m_{31} and m13m_{13}.

Answer

MM is 3×43 \times 4; m24=9m_{24} = 9, m31=−5m_{31} = -5, m13=−4m_{13} = -4.

Solution
3 rows, 4 columns→3×4m24: row 2=[7,2,2,9], 4th entry→9m31: row 3=[−5,6,0,8], 1st entry→−5m13: row 1=[3,0,−4,1], 3rd entry→−4\begin{aligned} &\text{3 rows, 4 columns} \to 3 \times 4 \\ &m_{24}\text{: row 2} = [7, 2, 2, 9],\ \text{4th entry} \to 9 \\ &m_{31}\text{: row 3} = [-5, 6, 0, 8],\ \text{1st entry} \to -5 \\ &m_{13}\text{: row 1} = [3, 0, -4, 1],\ \text{3rd entry} \to -4 \end{aligned}
python · try it yourself
from sympy import Matrix
M = Matrix([[3, 0, -4, 1], [7, 2, 2, 9], [-5, 6, 0, 8]])
print(M.shape, M[1, 3], M[2, 0], M[0, 2])
(3, 4) 9 -5 -4

Exercise 2: Solve by equality

Find a,b,c,da, b, c, d so that [a+b2ca−bd−1]=[10−640]\begin{bmatrix} a + b & 2c \\ a - b & d - 1 \end{bmatrix} = \begin{bmatrix} 10 & -6 \\ 4 & 0 \end{bmatrix}.

Answer

a=7a = 7, b=3b = 3, c=−3c = -3, d=1d = 1.

Solution
a+b=10 and a−b=4→add: 2a=14→a=7, then b=32c=−6→c=−3d−1=0→d=1\begin{aligned} &a + b = 10 \text{ and } a - b = 4 && \to \text{add: } 2a = 14 \to a = 7, \text{ then } b = 3 \\ &2c = -6 && \to c = -3 \\ &d - 1 = 0 && \to d = 1 \end{aligned}

Exercise 3: Can they be equal?

Can a 2×32 \times 3 matrix ever equal a 3×23 \times 2 matrix? Can a 1×11 \times 1 matrix [5][5] be "equal" to the number 5?

Answer

No to the first (sizes differ). Strictly, [5][5] is a matrix and 5 is a number, but the two are usually identified, so in practice yes.

Solution

Equality needs the same size first, and 2×3≠3×22 \times 3 \ne 3 \times 2, so no entries need to be compared. A 1×11 \times 1 matrix has exactly one entry and behaves like that number under all operations, so most texts (and SymPy's Matrix([[5]])[0]) treat them as the same thing; NumPy keeps them distinct (np.array([[5]]).shape is (1, 1), a plain 5 has no shape).

Exercise 4: Build an identity in code

Write a function identity(n) that returns the n×nn \times n identity as a list of lists, without NumPy or SymPy, and check it against sympy.eye(4).

Answer

[[1 if i == j else 0 for j in range(n)] for i in range(n)], which equals eye(4).tolist().

Solution

The entry in row ii, column jj is 1 exactly when i=ji = j.

python · try it yourself
from sympy import eye

def identity(n):
    return [[1 if i == j else 0 for j in range(n)] for i in range(n)]

print(identity(4))
print(identity(4) == eye(4).tolist())
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
True

Exercise 5: Row-major offsets

A 4×64 \times 6 matrix is stored row-major in a flat array (0-based). At which offset is the textbook entry a35a_{35}? Which entry is at offset 13?

Answer

a35a_{35} is at offset 16; offset 13 holds a32a_{32}.

Solution
0-based indices: row i−1, col j−1, offset=(i−1)⋅n+(j−1) with n=6a35→2⋅6+4=16offset 13→13=2⋅6+1→0-based (2,1)→a32\begin{aligned} &\text{0-based indices: row } i - 1,\ \text{col } j - 1,\ \text{offset} = (i - 1) \cdot n + (j - 1) \text{ with } n = 6 \\ &a_{35} \to 2 \cdot 6 + 4 = 16 \\ &\text{offset } 13 \to 13 = 2 \cdot 6 + 1 \to \text{0-based } (2, 1) \to a_{32} \end{aligned}
python · try it yourself
import numpy as np
A = np.arange(24).reshape(4, 6)      # entry value = its flat offset
r, c = np.unravel_index(13, (4, 6))
print(int(A[2, 4]), (int(r), int(c)))
16 (2, 1)

Test yourself

In the matrix [4−10257]\begin{bmatrix} 4 & -1 & 0 \\ 2 & 5 & 7 \end{bmatrix}, what is a21a_{21}?

  1. -1
  2. 2
  3. 0
  4. 5

When are two matrices A and B equal?

  1. When they have the same size and every pair of corresponding entries is equal
  2. When they contain the same numbers in any order
  3. When they have the same number of entries
  4. When their diagonals agree

In NumPy, what does A == B return for two 3×33 \times 3 float arrays?

  1. A single True or False
  2. The number of equal entries
  3. An error always
  4. A 3×33 \times 3 array of booleans