Notebook 10 – Math 2121, Fall 2020
In this short demonstration we explore the physical meaning of the sign of a determinant.
Some terminology:
An invertible matrix with positive determinant is called orientation-preserving.
An invertible matrix with negative determinant is called orientation-reversing.
Whether an
However, it's not so easy to make a rigorous definition of these properties without referencing the determinant.
xxxxxxxxxx
using Images, FileIO, Plots, LinearAlgebra, Random
A black and white image is encoded by a set of vectors
A
extract_image (generic function with 1 method)
function extract_image(filename)
# this function converts a png file to a list of vectors as we just described
img = download(filename) |> load
gx, gy = [], []
(m,n) = size(img)
for x=1:m
for y=1:n
if img[x,y] != img[1,1]
append!(gx, x)
append!(gy, y)
end
end
end
# now resize list of coordinates into 2-by-N matrix
return vcat(transpose(reverse(gx)), transpose(gy))
# note: we reverse order of x-coordinates because increasing the row
# index goes down in a matrix, which is opposite of cartesian coordinates
end
2×67870 Array{Any,2}:
380 380 380 380 380 380 380 380 … 12 12 12 12 12 12 12 12
198 199 200 201 202 203 204 205 431 432 433 434 435 436 437 438
g = extract_image("www.math.ust.hk/~emarberg/teaching/2020/Math2121/julia/base.png")
Here is what this image looks like if we plot all of the points:
make_displayable (generic function with 1 method)
xxxxxxxxxx
function make_displayable(g)
# this function converts our image as list of vectors into something displayable
padding = 3
gx, gy = g[1,:], g[2,:]
minx, miny = Int(floor(minimum(gx))) - 1, Int(floor(minimum(gy))) - 1
maxx, maxy = Int(ceil(maximum(gx))), Int(ceil(maximum(gy)))
ans = zeros(Int64, maxx - minx + padding, maxy - miny + padding)
for i = 1:length(gx)
x = Int(ceil(gx[i])) - minx
y = Int(ceil(gy[i])) - miny
for p=0:padding
for q=0:padding
ans[x + p, y + q] = 1
end
end
end
return ans
end
Below are some functions to create random
We'll just need the
randmat (generic function with 1 method)
xxxxxxxxxx
function randmat(n)
return 2 * rand(Float64, n, n) .- 1.0
end
positive_determinant (generic function with 1 method)
xxxxxxxxxx
function positive_determinant(n)
while true
A = randmat(n)
if abs(det(A)) < 0.1
continue
end
A = A / abs(det(A))^1/n
if det(A) > 0
return A
end
end
end
negative_determinant (generic function with 1 method)
xxxxxxxxxx
function negative_determinant(n)
while true
A = randmat(n)
if abs(det(A)) < 0.1
continue
end
A = A / abs(det(A))^1/n
if det(A) < 0
return A
end
end
end
illustrate (generic function with 1 method)
xxxxxxxxxx
function illustrate(g, A, B)
# function to plot side-by-side our image g transformed by A and B
plot(
heatmap(
make_displayable(A * g),
legend=false,
color=:binary
),
heatmap(
make_displayable(B * g),
legend=false,
color=:binary
),
layout = (1, 2),
)
end
The plots below show our starting image transformed by random matrices
Roughly speaking, if the matrix
If the matrix
illustrate(g, positive_determinant(2), negative_determinant(2))