The Core Idea of Linear Algebra

“Algebra” roughly translates to “relationships” and linear means “line-like,” thus “Linear Algebra” means “line-like relationships”.

Algebra

So why does algebra from school seem to be just about letters rather than numbers.

(x + y)² = x² + 2xy + y²
f(x, y) = (x + y)²

Linear

Linear relations are relation similar to what you can describe on a line. Linear relations are predictable. You double the input and you get double the output.

f(x) = x²
f(x) = 4x
f(x) = 2x + 4
f(x, y) = 2x + 4y
f(2x, 2y) = 2*2x + 2*4y
2(2x + 4y) = 2f(x, y)

The Matrix and Vector Connection

A problem with these functions is when they have a lot of arguments it becomes repetitive and a lot to write.

[2, 4] ⋅ [x, y] = 2x + 4y
f = [2, 4]
f ⋅ [x, y] = 2x + 4y
f(3, 4) = 2*3 + 4*4 = 22
f(2, 6) = 2*3 + 4*4 = 28
f(12, 1) = 2*12 + 4*1 = 28
f = [2 4]
X = [3 2 12; 4 6 1]
f * X = [2*3 + 4*4 2*3 + 4*4 2*12 + 4*1]
f * X = [22 28 28]

Benefits of a Matrix

This also provides a hint to why the function or operation we perform on the input should not be a vector but a matrix. What happens if you want to perform multiple operations on the same input?

costx(x) = 150x
costy(y) = 50y
costz(z) = 300z
taxx(x) = 1.2x
taxy(y) = 1.1y
taxz(z) = 1.3z
sum(x, y, z) = taxx(costx(x)) + taxy(costy(y)) + taxz(costz(z))
cost(x, y, z) = 150x + 50y + 300z
tax(x, y, z) = 1.2x + 1.1y + 1.3z
sum(x, y, z) = tax(cost(x, 0, 0), 0, 0) + 
tax(0, cost(0, y, 0), 0) +
tax(0, 0, cost(0, 0, z))
cost = [150 0 0; 0 50 0; 0 0 300]
cost * [x, y, z] = [150x, 50x, 300z]
tax = [1.2 0 0; 0 1.1 0; 0 0 1.3]
tax * cost * [x, y, z] = [1.2 * 150 * x, 1.1*50*y, 1.3*300*z]
tax * cost * [x, y, z] = [180x, 55y, 390z]
julia> cost = [150 0 0; 0 50 0; 0 0 300]
3×3 Array{Int64,2}:
150 0 0
0 50 0
0 0 300

julia> tax = [1.2 0 0; 0 1.1 0; 0 0 1.3]
3×3 Array{Float64,2}:
1.2 0.0 0.0
0.0 1.1 0.0
0.0 0.0 1.3
julia> amounts = [1 3;1 2; 1 1]
3×2 Array{Int64,2}:
1 3
1 2
1 1
julia> tax * cost * amounts
3×2 Array{Float64,2}:
180.0 540.0
55.0 110.0
390.0 390.0
julia> sum = [1 1 1]
1×3 Array{Int64,2}:
1 1 1

julia> sum * tax * cost * amounts
1×2 Array{Float64,2}:
625.0 1040.0
julia> 180 + 55 + 390
625

Examples of Useful Matrix Operations

Getting used to thinking of a matrix as a set of operations performed on some input that which may also be a matrix takes some time getting used to. But here are some examples to hammer home the point.

julia> inputs = [3, 2, 1]
3-element Array{Int64,1}:
3
2
1
julia> picky = [0 1 0]
1×3 Array{Int64,2}:
0 1 0

julia> picky * inputs
1-element Array{Int64,1}:
2
julia> swapper = [0 0 1; 0 1 0; 1 0 0]
3×3 Array{Int64,2}:
0 0 1
0 1 0
1 0 0

julia> swapper * inputs
3-element Array{Int64,1}:
1
2
3
julia> doubler = [2 0 0; 0 2 0; 0 0 2]
3×3 Array{Int64,2}:
2 0 0
0 2 0
0 0 2

julia> doubler * inputs
3-element Array{Int64,1}:
6
4
2

Representing Addition

Thus far we have only represented functions of the form:

f(x) = ax
f(x) = ax + b
f = [a b]
f * [x, 1] = ax + b
f = [a b c d]
f * [x, y, z, 1] = ax + by + cz + d

Affine Transformations

With this background it is perhaps easier to understand how affine transformations used in computer graphics to translate, rotate and scale objects work.

1  0  dx
0 1 dy
0 0 1
x' = f(x, y, 1)
y' = g(x, y, 1)

But why do we need the third row?

Notice how we need 3 columns to be able to offer an offset such as dx and dy. For that to work the input needs to have 3 rows where the last row is 1.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store