TensorTau PathToAGI Linear Algebra Level 01

Level 01 · Linear Algebra · Chapter 009

Matrix-Vector Multiplication as Transformation

What does a matrix actually to a vector?

Where We Are

Chapter 008 organized many numbers into a matrix.

That gave us a compact way to store rows of related values.

But matrices are more than spreadsheets.

A matrix can act on a vector and produce a new vector.

That action is one of the deepest ideas in linear algebra and one of the most common operations in neural networks.

Today: we discover matrix–vector multiplication as many dot products and as a transformation of space.

Next: once one matrix transforms a vector, what happens when we apply one matrix after another?


1. The Problem: One Neuron Is Not Enough

In Chapter 007 we saw a single neuron-like calculation:

z=wx+b.z=\mathbf{w}\cdot\mathbf{x}+b.

Suppose

x=[23].\mathbf{x}=\begin{bmatrix}2\\3\end{bmatrix}.

One weight vector might detect “large house”:

w1=[21].\mathbf{w}_1=\begin{bmatrix}2\\1\end{bmatrix}.

Another might detect “high area relative to rooms”:

w2=[12].\mathbf{w}_2=\begin{bmatrix}-1\\2\end{bmatrix}.

We could compute separately:

z1=w1xz_1=\mathbf{w}_1\cdot\mathbf{x}

and

z2=w2x.z_2=\mathbf{w}_2\cdot\mathbf{x}.

But a real layer may have hundreds or thousands of neurons.

Writing each dot product separately is not a scalable mathematical language.

We need one object that stores all those weight vectors and one operation that computes all outputs together.


2. Stack the Weight Vectors

Put each weight vector as a row of a matrix:

W=[2112].W= \begin{bmatrix} 2 & 1\\ -1 & 2 \end{bmatrix}.

Input:

x=[23].\mathbf{x}=\begin{bmatrix}2\\3\end{bmatrix}.

Now calculate

Wx.W\mathbf{x}.

The first row dots with x\mathbf{x}:

2(2)+1(3)=7.2(2)+1(3)=7.

The second row dots with x\mathbf{x}:

1(2)+2(3)=4.-1(2)+2(3)=4.

So

Wx=[74]\boxed{ W\mathbf{x} = \begin{bmatrix}7\\4\end{bmatrix} }

One matrix–vector multiplication computed two dot products at once.


3. The Row View

Suppose

WRm×nW\in\mathbb{R}^{m\times n}

and

xRn.\mathbf{x}\in\mathbb{R}^{n}.

Each row of WW contains nn numbers, exactly matching the nn entries of x\mathbf{x}.

So every row can take a dot product with x\mathbf{x}.

That produces one number per row.

Therefore

WxRm.W\mathbf{x}\in\mathbb{R}^{m}.

Shape rule:

(m × n)  times  (n,)

             (m,)

The inner dimension nn must match.

The outer dimension mm becomes the output size.

Intuition — Each row is one question asked of the same input vector. The output contains one answer per question.


4. A Tempting Wrong Idea: Multiply Matching Slots Only

A beginner may try to multiply

[2112]\begin{bmatrix} 2 & 1\\ -1 & 2 \end{bmatrix}

with

[23]\begin{bmatrix}2\\3\end{bmatrix}

entry by entry.

But the shapes do not even match as two grids.

More importantly, element-wise multiplication would not produce the weighted sums we need.

A Tempting Wrong Idea

Matrix multiplication is not “multiply whatever numbers happen to line up visually.” It is a structured collection of dot products.

This distinction will prevent many future bugs.


5. The Column View: A Different Mental Model

There is another equally important way to understand the same multiplication.

Write the matrix by columns:

W=[c1c2].W= \begin{bmatrix} | & |\\ \mathbf{c}_1 & \mathbf{c}_2\\ | & | \end{bmatrix}.

If

x=[x1x2],\mathbf{x}=\begin{bmatrix}x_1\\x_2\end{bmatrix},

then

Wx=x1c1+x2c2\boxed{ W\mathbf{x}=x_1\mathbf{c}_1+x_2\mathbf{c}_2 }

For our matrix,

c1=[21],c2=[12].\mathbf{c}_1=\begin{bmatrix}2\\-1\end{bmatrix}, \qquad \mathbf{c}_2=\begin{bmatrix}1\\2\end{bmatrix}.

With

x=[23],\mathbf{x}=\begin{bmatrix}2\\3\end{bmatrix},

we get

Wx=2[21]+3[12].W\mathbf{x} =2\begin{bmatrix}2\\-1\end{bmatrix} +3\begin{bmatrix}1\\2\end{bmatrix}.

So

=[42]+[36]=[74].=\begin{bmatrix}4\\-2\end{bmatrix} +\begin{bmatrix}3\\6\end{bmatrix} =\begin{bmatrix}7\\4\end{bmatrix}.

Same answer.

Two interpretations:

  • row view: many dot products,
  • column view: weighted combination of basis directions.

Both matter.


6. A Matrix Moves the Basis Vectors

Take the standard basis vectors:

e1=[10],e2=[01].\mathbf{e}_1=\begin{bmatrix}1\\0\end{bmatrix}, \qquad \mathbf{e}_2=\begin{bmatrix}0\\1\end{bmatrix}.

Now multiply:

We1=[21]=c1W\mathbf{e}_1 = \begin{bmatrix}2\\-1\end{bmatrix} =\mathbf{c}_1

and

We2=[12]=c2.W\mathbf{e}_2 = \begin{bmatrix}1\\2\end{bmatrix} =\mathbf{c}_2.

The columns of the matrix tell us exactly where the basis vectors go.

That means the entire transformation is encoded by where it sends the basis.

This is a profound compression of information.


7. Geometry: Transform the Whole Grid

Imagine the usual coordinate grid.

Before transformation:

          e2 ↑
             |
             |
-------------+----→ e1

After applying WW:

  • e1e_1 moves to (2,1)(2,-1),
  • e2e_2 moves to (1,2)(1,2).

Every other vector is built from these basis vectors, so every other vector follows automatically.

If

x=2e1+3e2,\mathbf{x}=2\mathbf{e}_1+3\mathbf{e}_2,

then linearity gives

Wx=2We1+3We2.W\mathbf{x} =2W\mathbf{e}_1+3W\mathbf{e}_2.

This is why seeing the transformed basis tells us the transformed space.


8. Discover Linearity

A matrix transformation satisfies two key properties.

Additivity

W(u+v)=Wu+WvW(\mathbf{u}+\mathbf{v})=W\mathbf{u}+W\mathbf{v}

Scaling

W(cu)=cWuW(c\mathbf{u})=cW\mathbf{u}

Together:

W(au+bv)=aWu+bWv\boxed{ W(a\mathbf{u}+b\mathbf{v}) =aW\mathbf{u}+bW\mathbf{v} }

That property is why the operation is called linear.

A matrix does not arbitrarily bend space. It preserves linear combinations.


9. Example: Scaling

Take

S=[2003].S= \begin{bmatrix} 2 & 0\\ 0 & 3 \end{bmatrix}.

Then

S[xy]=[2x3y].S\begin{bmatrix}x\\y\end{bmatrix} = \begin{bmatrix}2x\\3y\end{bmatrix}.

So x-coordinates double and y-coordinates triple.

For

[11]\begin{bmatrix}1\\1\end{bmatrix}

we get

[23].\begin{bmatrix}2\\3\end{bmatrix}.

The matrix stretches space differently along different directions.


10. Example: Reflection

Take

R=[1001].R= \begin{bmatrix} -1 & 0\\ 0 & 1 \end{bmatrix}.

Then

R[xy]=[xy].R\begin{bmatrix}x\\y\end{bmatrix} = \begin{bmatrix}-x\\y\end{bmatrix}.

The x-coordinate flips sign while y stays unchanged.

That reflects the plane across the y-axis.


11. Example: Shear

Take

H=[1101].H= \begin{bmatrix} 1 & 1\\ 0 & 1 \end{bmatrix}.

Then

H[xy]=[x+yy].H\begin{bmatrix}x\\y\end{bmatrix} = \begin{bmatrix}x+y\\y\end{bmatrix}.

The vertical coordinate stays fixed, while x shifts according to y.

A square becomes a slanted parallelogram.

This is a shear.

Stretch and shear are not the same:

  • stretch changes size along a direction,
  • shear slides one layer relative to another.

12. Example: Rotation

A 2D rotation by angle θ\theta uses

R(θ)=[cosθsinθsinθcosθ].R(\theta)= \begin{bmatrix} \cos\theta & -\sin\theta\\ \sin\theta & \cos\theta \end{bmatrix}.

For 9090^\circ,

cos90=0,sin90=1.\cos90^\circ=0, \qquad \sin90^\circ=1.

So

R=[0110].R= \begin{bmatrix} 0 & -1\\ 1 & 0 \end{bmatrix}.

Apply it to

[10]:\begin{bmatrix}1\\0\end{bmatrix}: R[10]=[01].R\begin{bmatrix}1\\0\end{bmatrix} = \begin{bmatrix}0\\1\end{bmatrix}.

The right-pointing basis vector becomes the up-pointing basis vector.

That is exactly a 90° counter-clockwise rotation.


13. Shape Reasoning Before Arithmetic

Suppose

WR4×3W\in\mathbb{R}^{4\times3}

and

xR3.\mathbf{x}\in\mathbb{R}^{3}.

Then

WxR4.W\mathbf{x}\in\mathbb{R}^{4}.

We know the output shape before multiplying a single number.

But if

xR5,\mathbf{x}\in\mathbb{R}^{5},

then

(4×3)(5)(4\times3)(5)

is invalid.

Why?

Each matrix row expects 3 numbers for its dot product, but x provides 5.

Shapes are not bookkeeping. They encode whether the mathematical operation exists.


14. Neural-Network Connection

A dense layer computes

z=Wx+b.\mathbf{z}=W\mathbf{x}+\mathbf{b}.

Suppose

xR3\mathbf{x}\in\mathbb{R}^{3}

and we want 5 neurons.

Each neuron needs 3 weights.

So

WR5×3.W\in\mathbb{R}^{5\times3}.

Then

WxR5.W\mathbf{x}\in\mathbb{R}^{5}.

Bias must also have 5 entries:

bR5.\mathbf{b}\in\mathbb{R}^{5}.

This is the matrix form of five neurons working at once.


15. Code From Scratch

def matvec(W, x):
    out = []
    for row in W:
        total = 0.0
        for wi, xi in zip(row, x):
            total += wi * xi
        out.append(total)
    return out

W = [[2, 1], [-1, 2]]
x = [2, 3]

assert matvec(W, x) == [7, 4]

This explicit loop should be understood before using a library shortcut.

NumPy:

import numpy as np

W = np.array([[2., 1.], [-1., 2.]])
x = np.array([2., 3.])

y = W @ x
assert np.allclose(y, [7., 4.])

The @ operator means matrix multiplication.


16. Break It

Wrong input dimension

A (2×3)(2\times3) matrix cannot multiply a 4-vector.

Confusing rows and columns

If you store neuron weights as columns instead of rows, the expected multiplication changes.

Treating * as matrix multiplication

In NumPy,

W * x

means broadcasting / element-wise multiplication, not the same thing as

W @ x

Forgetting bias shape

If WxW\mathbf{x} has shape (5,), a bias intended to add one value per neuron should also align with (5,).


17. History Lens — Linear Maps Before Neural Networks

Linear transformations were studied long before computers because they capture structured changes: rotations, projections, changes of coordinates, systems of equations and physical transformations.

Machine learning inherited this language because a neural-network layer faces the same structural problem: take one vector space, transform it into another, and do so efficiently.

The modern notation is new compared with ancient geometry, but the underlying question is old:

How can we describe a transformation once and apply it everywhere consistently?

Matrices are the answer.


18. Distinctions That Matter

PairDifference
matrix as data table vs matrix as transformationstorage view vs action view
row view vs column viewmany dot products vs weighted combination of columns
element-wise multiply vs matrix multiplylocal pairwise products vs structured dot products
stretch vs shearscale along directions vs slide coordinates relative to each other
shape compatibility vs equal shapematrix multiplication needs matching inner dimensions, not identical shapes

19. What We Discovered

  1. Matrix–vector multiplication computes many dot products at once.
  2. Rows determine output coordinates.
  3. Columns show where basis vectors go.
  4. A matrix therefore encodes a transformation of space.
  5. Linear transformations preserve addition and scalar multiplication.
  6. Scaling, reflection, shear and rotation can all be expressed as matrices.
  7. Neural-network dense layers are matrix transformations plus bias.
  8. Shape reasoning can validate an operation before arithmetic begins.

20. One-Minute Explanation

A matrix multiplying a vector can be understood in two ways. Row by row, each matrix row takes a dot product with the input, producing one output number. Column by column, the input coordinates tell us how much of each matrix column to combine. Geometrically, the matrix tells us where the basis vectors move, which determines how the whole space transforms. This is why matrix multiplication powers dense neural-network layers: many neurons are simply many learned dot products computed together.


21. Mastery Check

  1. Compute [[2,1],[1,2]][2,3]T[[2,1],[-1,2]] [2,3]^T by hand.
  2. Explain the row interpretation.
  3. Explain the column interpretation.
  4. Why do matrix columns reveal transformed basis vectors?
  5. What does it mean for a transformation to be linear?
  6. What is the output shape of (7×3) @ (3,)?
  7. Why is (7×3) @ (4,) invalid?
  8. What geometric transformation does diag(2,3) perform?
  9. Why is W * x not generally the same as W @ x?
  10. How is a dense neural-network layer a matrix–vector multiplication?

🔭 Bridge to Chapter 010

A matrix can transform a vector.

But deep networks apply transformation after transformation:

xAxB(Ax).\mathbf{x} \rightarrow A\mathbf{x} \rightarrow B(A\mathbf{x}).

Writing nested transformations works, but we want to know whether the sequence itself can be represented by one matrix.

Can two transformations be composed into one transformation?

That question forces matrix–matrix multiplication.

Check your understanding

Chapter checkpoint

5 questions · untimed

Answer at your own pace. Review the explanation after submitting. Results are saved on this browser only.

1. The row view and the column view of Wx describe the same computation. What does the column view say?
2. What do the columns of a matrix tell you about the transformation it performs?
3. Which two properties make a matrix transformation linear?
4. A dense layer takes x ∈ ℝ³ and has 5 neurons. What shapes must W and b have?
5. Which distinction is stated correctly?