Matrices
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A matrix is a grid of numbers, and it is also a machine that moves every point of space at once. Follow one example, A = [[2, −1], [1, 1]] times x = (2, 1): multiply it row by row, watch A move the plane, read its columns and its determinant, then chain it with a rotation and use it as a neural-network layer.
A grid of numbers
A matrix is a grid of numbers in rows and columns. A has 2 rows and 2 columns, so it is a 2 × 2 matrix. The entry in row i, column j is written aᵢⱼ: here a₁₁ = 2 and a₁₂ = −1.
>>> import numpy as np >>> A = np.array([[2, -1], [1, 1]]) >>> A.shape # (rows, columns) (2, 2) >>> A[0, 1] # row 1, column 2 (Python counts from 0) np.int64(-1)
اقرأ الدرس خطوة بخطوة
A grid of numbers
A matrix is a grid of numbers in rows and columns. A has 2 rows and 2 columns, so it is a 2 × 2 matrix. The entry in row i, column j is written aᵢⱼ: here a₁₁ = 2 and a₁₂ = −1.
>>> import numpy as np >>> A = np.array([[2, -1], [1, 1]]) >>> A.shape # (rows, columns) (2, 2) >>> A[0, 1] # row 1, column 2 (Python counts from 0) np.int64(-1)A vector is an arrow
A vector is a list of numbers, written as a column. x = (2, 1) is also an arrow from the origin: 2 steps right and 1 step up. In graphics it can be a corner of a shape.
>>> x = np.array([2, 1]) >>> x.shape (2,)Row 1 times x
Multiply A by x one row at a time. Pair row 1 of A with x, multiply matching entries and add: 2 × 2 + (−1) × 1 = 3. That sum is a dot product: the first entry of y = Ax.
>>> A[0] @ x # row 1 dotted with x np.int64(3) >>> 2*2 + (-1)*1 3Row 2 times x
Row 2 gives the second entry: 1 × 2 + 1 × 1 = 3, so y = Ax = (3, 3). Each row of A must be as long as x. An m × n matrix takes n numbers in and gives m out.
>>> A @ x # @ multiplies matrices array([3, 3])A matrix moves points
So a matrix is a function: vector in, vector out. On the plane, A moves the point (2, 1) to (3, 3). It moves every point of the plane at once, so it can stretch, turn, slant or flip a whole shape.
>>> A @ np.array([0, 0]) # the origin never moves array([0, 0])Columns are where axes land
Follow the arrows e₁ = (1, 0) and e₂ = (0, 1). A sends them to its columns, (2, 1) and (−1, 1), and drags the grid along. Grid lines stay straight and evenly spaced, and the origin stays put: that is what linear means.
>>> A[:, 0], A[:, 1] # the columns (array([2, 1]), array([-1, 1])) >>> A @ np.array([1, 0]) # e1 lands on column 1 array([2, 1])A mix of columns
Columns explain it too. Ax = 2 × column 1 + 1 × column 2 = (4, 2) + (−1, 1) = (3, 3): walk column 1 twice, then column 2 once. A matrix times a vector is always a weighted mix of its columns.
>>> 2*A[:, 0] + 1*A[:, 1] array([3, 3])Determinant: area scale
The unit square on e₁ and e₂ becomes a parallelogram. Its area is the determinant: ad − bc = 2 × 1 − (−1) × 1 = 3, so A triples every area. A determinant of 0 would squash the plane onto a line.
>>> np.linalg.det(A) # a float: computed with LU np.float64(2.9999999999999996) >>> 2*1 - (-1)*1 3Turning with a matrix
Graphics programs move shapes with matrices. R turns the plane a quarter turn anticlockwise: its columns, (0, 1) and (−1, 0), are where e₁ and e₂ land. Applying R after A carries (3, 3) on to (−3, 3).
>>> R = np.array([[0, -1], [1, 0]]) >>> R @ (A @ x) array([-3, 3])Matrix times matrix
A, then R, is one matrix: the product RA. Entry (i, j) is row i of R dotted with column j of A, so row 1, column 1 is 0 × 2 + (−1) × 1 = −1. Order matters: AR is a different matrix.
>>> R @ A array([[-1, -1], [ 2, -1]]) >>> (R @ A) @ x # same as R @ (A @ x) array([-3, 3]) >>> A @ R # the other order array([[-1, -2], [ 1, -1]])A neural-network layer
A neural-network layer keeps its weights in a matrix W. It computes W @ x + b, then a simple function such as ReLU, max(0, ·). With W = A and b = (1, −4), (4, −1) becomes (4, 0). Training adjusts W and b.
>>> b = np.array([1, -4]) >>> A @ x + b array([ 4, -1]) >>> np.maximum(A @ x + b, 0) # ReLU array([4, 0])
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is [[1, 2], [3, 4]] times the vector (5, 6)?
Row 1 dotted with (5, 6): 1 × 5 + 2 × 6 = 17. Row 2: 3 × 5 + 4 × 6 = 39. So the answer is (17, 39).
02A matrix sends e₁ = (1, 0) to (3, 1) and e₂ = (0, 1) to (0, 2). Which matrix is it?
The columns of a matrix are where e₁ and e₂ land, so column 1 is (3, 1) and column 2 is (0, 2): [[3, 0], [1, 2]].
03What is the determinant of [[4, 2], [1, 3]], and what does it tell you?
ad − bc = 4 × 3 − 2 × 1 = 10. The unit square becomes a parallelogram of area 10, so every area is scaled by 10.
04In graphics you want to apply S first and then T to every point p. Which single matrix does that?
T(S p) = (TS) p, so the matrix applied first goes on the right. Matrix products usually depend on order: in the lesson, RA ≠ AR.
05A network layer has W = [[1, 0], [2, −1]] and b = (0, −1) and uses ReLU. What does it output for x = (1, 3)?
W x = (1, 2 − 3) = (1, −1). Add b: (1, −2). ReLU replaces negatives with 0: (1, 0).
المسرد
- Matrix
- A rectangular grid of numbers. An m × n matrix has m rows and n columns; the entry in row i, column j is written aᵢⱼ.
- Vector
- An ordered list of numbers, written as a column. In 2D it is also an arrow from the origin to a point.
- Dot product
- Multiply matching entries of two equal-length vectors and add: (2, −1) · (2, 1) = 4 − 1 = 3.
- Linear transformation
- A function that keeps lines straight, keeps grid lines evenly spaced and leaves the origin fixed. Every matrix is one.
- Basis vectors e₁, e₂
- The arrows (1, 0) and (0, 1). A matrix's columns are where it sends them.
- Determinant
- For [[a, b], [c, d]] it is ad − bc: how much the matrix scales areas (negative if it also flips). 0 means it squashes space flat.
- Matrix product
- PQ: entry (i, j) is row i of P dotted with column j of Q. It means "apply Q, then P".
- ReLU
- The function max(0, z), applied to each entry; a common activation in neural networks.
الملاحظات والمصادر
- An m × n matrix has m rows and n columns. It can multiply a vector with n entries and returns a vector with m entries; two matrices P and Q can be multiplied as PQ only when P has as many columns as Q has rows.
- np.linalg.det works with floating-point numbers (it uses an LU factorisation), so it prints 2.9999999999999996 instead of 3. Round it, or use the exact formula ad − bc for a 2 × 2 matrix.
- RA means "apply A first, then R", because (RA)x = R(Ax). Matrix products are associative but not commutative: here RA = [[−1, −1], [2, −1]] while AR = [[−1, −2], [1, −1]].
- The general rotation by an angle t anticlockwise is [[cos t, −sin t], [sin t, cos t]]. For t = 90°, cos t = 0 and sin t = 1, which gives R. Its determinant is 1: turning keeps every area.
- A negative determinant means the matrix also flips the plane over, like a mirror. The size of the determinant is still the area scale.
- MIT OCW 18.06 Linear Algebra (Gilbert Strang), lecture videos and notes
- G. Strang, Introduction to Linear Algebra (book site)
- MIT OCW 6.837 Computer Graphics: transformations
- Goodfellow, Bengio and Courville, Deep Learning, ch. 6 Deep Feedforward Networks
- PEP 465: A dedicated infix operator for matrix multiplication (@)
- NumPy reference: numpy.matmul
- NumPy reference: numpy.linalg.det
اختبار يتاح بعد الخطوة الأخيرة.