Notebook 6 – Math 2121, Fall 2020
In today's notebook we'll first explore some more plots of linear transformations. Then we'll take a look at the frequency that a random linear transformation is one-to-one or onto.
Running this notebook (optional)
If you have Pluto up and running, you can access the notebook we are currently viewing by entering this link (right click -> Copy Link) in the Open from file menu in Pluto.
xxxxxxxxxx
md"# Notebook 6 -- Math 2121, Fall 2020
In today's notebook we'll first explore some more plots of linear transformations. Then we'll take a look at the frequency that a random linear transformation is one-to-one or onto.
##### Running *this* notebook (optional)
If you have Pluto up and running, you can access the notebook we are currently viewing by entering [this link](http://www.math.ust.hk/~emarberg/teaching/2020/Math2121/julia/06_Math2121_Fall2020.jl) (right click -> Copy Link) in the *Open from file* menu in Pluto."
Helper methods
xxxxxxxxxx
pprint (generic function with 1 method)
xxxxxxxxxx
input_plot (generic function with 2 methods)
xxxxxxxxxx
transformation_plot (generic function with 2 methods)
xxxxxxxxxx
Linear transformations from matrices
xxxxxxxxxx
input parameters
x1
= y1
=
x2
= y2
=
matrix parameters
θ
= k
=
xxxxxxxxxx
begin
r_slider = r Slider(0:0.1:2 * pi, default=0, show_value=false)
theta_slider = theta Slider(0:0.1:2 * pi, default=0, show_value=true)
x1_slider = x1 Slider(-2:0.1:2, default=1, show_value=false)
x2_slider = x2 Slider(-2:0.1:2, default=0, show_value=false)
y1_slider = y1 Slider(-2:0.1:2, default=0, show_value=false)
y2_slider = y2 Slider(-2:0.1:2, default=1, show_value=false)
k_slider = k Slider(-1:0.1:1, default=1, show_value=true)
md"""**input parameters**
`x1` = $(x1_slider) `y1` = $(y1_slider) rotate = $(r_slider)
`x2` = $(x2_slider) `y2` = $(y2_slider)
**matrix parameters**
`θ` = $(theta_slider) `k` = $(k_slider)
"""
end
x = [ 1.0] y = [ 0.0]
[ 0.0] [ 1.0]
xxxxxxxxxx
begin
rmat = [cos(r) -sin(r); sin(r) cos(r)]
x = rmat * [x1; x2]
y = rmat * [y1; y2]
pprint("x", "y", x, y)
end
2×2 Array{Float64,2}:
1.0 -0.0
0.0 1.0
xxxxxxxxxx
A = [cos(theta) -sin(theta); sin(theta) cos(theta)] # rotates CCW by theta
# A = [1 0; 0 -1] # flips input across horizontal axis
# A = [-1 0; 0 1] # flips input across the vertical axis
# A = [0 1; 1 0] # reflects across the line x_1 = x_2
# A = [k 0; 0 1] # scales the horizontal components by factor k
# A = [1 0; 0 k] # scales the vertical components by factor k
# A = [1 k; 0 1] # shearing/skewing operation in horizontal direction
# A = [1 0; k 1] # shearing/skewing operation in vertical direction
xxxxxxxxxx
begin
p1 = input_plot(x, y, "x and y")
p2 = transformation_plot(A, x, y, "Ax and Ay")
plot(p1, p2, layout=2)
end
Random matrices, onto and one-to-one linear transformations
We can generate a random linear transformation
If
In other words, a sufficiently random linear transformation is almost always one-to-one if this is possible (that is, if
If
To explore this phenomenon, we'll consider our usual model of random matrix which is not that random: namely,
x
md"##### Random matrices, onto and one-to-one linear transformations
We can generate a random linear transformation $T: \mathbb{R}^n \to \mathbb{R}^m$
by generating a random $m\times n$ matrix $A$ and setting $T(x) = Ax$.
If $n > m$ then $T$ is never one-to-one, and if $n < m$ then $T$ is never onto.
If $n \leq m$, however, then a random linear transformation $T$ is injective with a high probability.
If $n \geq m$, similarly, then a random linear transformation $T$ is surjective with a high probability.
In other words, a sufficiently random linear transformation is almost always one-to-one if this is possible (that is, if $n \leq m$) and almost always onto if this is possible (that is, if $n \geq m$).
If $n=m$ then a random linear transformation is almost always both one-to-one and onto (which will be our definition of an *invertible function* next lecture).
To explore this phenomenon, we'll consider our usual model of random matrix which is not *that* random: namely, $01$-matrices whose entries are independently $0$ or $1$ with some probability $p$. For such matrices, we can see some interesting behavior while also observing convergence to the ''almost always'' properties described above.
"
Helper methods
xxxxxxxxxx
md"##### Helper methods"
RREF (generic function with 2 methods)
xxxxxxxxxx
npivots (generic function with 2 methods)
xxxxxxxxxx
is_onto (generic function with 1 method)
xxxxxxxxxx
function is_onto(A)
(m, n) = size(A)
return npivots(A) == m
end
is_one_to_one (generic function with 1 method)
xxxxxxxxxx
function is_one_to_one(A)
(m, n) = size(A)
return npivots(A) == n
end
random_boolean_matrix (generic function with 1 method)
x
function random_boolean_matrix(m, n, zero_probability)
A = rand(m, n)
for i=1:m
for j=1:n
A[i, j] = Int(A[i, j] > zero_probability)
end
end
return A
end
accumulate_mean (generic function with 1 method)
xxxxxxxxxx
accumulate_std (generic function with 1 method)
xxxxxxxxxx
accumulate_plot (generic function with 1 method)
xxxxxxxxxx
xxxxxxxxxx
plotly()
Parameters
x
md"##### Parameters"
1000
x
trials = 1000
nrows
=
ncols
=
xxxxxxxxxx
begin
M_slider = M Slider(1:50, default=2, show_value=true)
N_slider = N Slider(1:50, default=2, show_value=true)
md"""
`nrows` = $(M_slider)
`ncols` = $(N_slider)
"""
end
To make the following plot, for various probabilities p in [0,1], we generate 1000 random 01-matrices of size 10 by 20. For each p, we compute the proportition of these matrices that correspond to onto linear transformations, as well as the standard deviation of this statistic.
Then we plot both as a function of p.
xxxxxxxxxx
md"To make the following plot, for various probabilities p in [0,1], we generate **$(trials)** random 01-matrices of size
**$(M) by $(N)**. For each p, we compute the proportition of these matrices that correspond to **onto** linear transformations, as well as the standard deviation of this statistic.
Then we plot both as a function of p."
xxxxxxxxxx
begin
onto_title = "Random 01-matrices, size $(M)-by-$(N): onto transformations"
accumulate_plot(is_onto, onto_title, trials, M, N)
end
Interesting properties of this graph: when
The standard deviation is always bimodal, with maximum value 0.5 attained at two values
What are these two values of
x
md"Interesting properties of this graph: when $p$ is not too small or too large, our model of a random matrix is ''sufficiently'' random, so the measured proportion is 1.0: almost every random matrix corresponds to an onto linear transformation.
The standard deviation is always bimodal, with maximum value 0.5 attained at two values $p_1$ and $p_2$ of the probability $p$, which are also where the mean and standard deviation graphs intersect.
What are these two values of $p$? They are not symmetric, that is, $p_2 \neq 1-p_1$."
Why does the mean have value
xxxxxxxxxx
md"Why does the mean have value $0$ when $p=0$ and when $p=1$?"
Here is a random 01-matrix for
x
md"Here is a random 01-matrix for $p=0$:"
10×20 Array{Float64,2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0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
xxxxxxxxxx
S = random_boolean_matrix(M, N, 1)
0
xxxxxxxxxx
npivots(S)
In both cases the number of pivots is much less than the number of rows or columns, so these matrices correspond to linear transformations that are neither onto nor one-to-one.
x
md"In both cases the number of pivots is much less than the number of rows or columns, so these matrices correspond to linear transformations that are neither onto nor one-to-one."
To make the following plot, for various probabilities p in [0,1], we generate 1000 random 01-matrices of size 20 by 10 (note: reversed from above). For each p, we compute the proportition of these matrices that correspond to one-to-one linear transformations, as well as the standard deviation of this statistic.
Then we plot both as a function of p.
x
md"To make the following plot, for various probabilities p in [0,1], we generate **$(trials)** random 01-matrices of size
**$(N) by $(M)** (note: reversed from above). For each p, we compute the proportition of these matrices that correspond to **one-to-one** linear transformations, as well as the standard deviation of this statistic.
Then we plot both as a function of p."
xxxxxxxxxx
begin
one_to_one_title = "Random 01-matrices, size $(N)-by-$(M): 1-to-1 transformations"
accumulate_plot(is_one_to_one, one_to_one_title, trials, N, M)
end
Why are two graphs nearly the same?
The transpose of a matrix
We'll see later in the class, that the number of pivot positions in
x
md"Why are two graphs nearly the same?
The **transpose** of a matrix $A$ is then matrix $A^T$ formed by interchanging the rows and columns:
$$\left[\begin{array}{cc} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{array}\right]^T
=\left[\begin{array}{ccc} 1 & 3 & 5 \\ 2 & 4 & 6 \end{array}\right].$$
We'll see later in the class, that the number of pivot positions in $A$ is the same as in $A^T$."
This means that the number of 10 by 20 matrices with pivots in every row (corresponding to onto linear transformations) is the same as the number of 20 by 10 matrices with pivots in every column (corresponding to one-to-one linear transformations).
x
md"This means that the number of $(M) by $(N) matrices with pivots in every row (corresponding to **onto** linear transformations) is the same as the number of $(N) by $(M) matrices with pivots in every column (corresponding to **one-to-one** linear transformations)."