Matrix Multiplication as Transformation and Interpretation of Low Rank Matrices
Matrix multiplication as transformation
When a vector is multiplied by a suitable matrix, the operation essentially transforms the original vector into a new vector. This can be illustrated by the following example. The dimension of the resultant vector may also change (though it does not in this example).
Necessary imports:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import seaborn as sns
from sympy import Eq,Matrix,MatMul
Creating a vector and a transformation matrix
= np.array([[2,1],[1,4]])
A = np.array([1,1])
x = A @ x
Ax =False) Eq(Matrix(Ax),MatMul(Matrix(A),Matrix(x)),evaluate
A function to plot arrows
def plot_arrow(ax,v,color,label):
= mpatches.FancyArrowPatch((0,0),(v[0],v[1]),mutation_scale=9,color=color,label=label)
arrow
ax.add_patch(arrow)=(1.6,1),borderaxespad=0) ax.legend(bbox_to_anchor
A function to compute the transformed vector
def plot_transform(A,x):
= A @ x
Ax = plt.subplots()
fig, ax "k",f"Original Vector x: {x}")
plot_arrow(ax,x,"g",f"Transformed vector Ax: {Ax}")
plot_arrow(ax,Ax,-5,5))
plt.xlim((-5,5))
plt.ylim((=0.1)
plt.grid(alpha"equal") ax.set_aspect(
Using the above functions
2.0,1.0],[1.0,4.0]]),np.array([1.0,1.0])) plot_transform(np.array([[
Using matrix transformation to rotate a vector
Defining the required function
def plot_rot(theta,v):
= np.cos(theta)
c = np.sin(theta)
s = np.array([[c,-s],[s,c]])
rot_mat = rot_mat @ v
w = plt.subplots()
fig, ax "k",f"Original vector: {v}")
plot_arrow(ax,v,"g",f"Vector on rotation: {w}")
plot_arrow(ax,w,-6,6))
plt.xlim((-6,6))
plt.ylim((=0.4)
plt.grid(alpha"equal") ax.set_aspect(
Using the function to rotate [3.0 5.0] by 60 degrees anti-clockwise
/3,np.array([3.0,5.0])) plot_rot(np.pi
Matrix multiplication as transformation
Rank of a matrix is the minimum number of linearly independent rows and columns or the number of non-zero eigenvalues in case of a square matrix.
Consider the low rank matrix:
Defining the function:
def plot_lr(v,slope):
= np.array([1.0, 2.0])
A1 = np.vstack((A1,slope*A1)) # The low rank matrix
A = np.arange(-6,6,0.01)
x = slope*x
y
plot_transform(A,v)=5,alpha=0.4,label=f"y = {slope}x, Column Space of A")
plt.plot(x,y,lw=(1,1),borderaxespad=0) plt.legend(bbox_to_anchor
Lets transform the vector [1.0 2.0] using the above transformation matrix:
1.0, 2.0]),4)
plot_lr(np.array([ plt.tight_layout()
Importance of rowspace, columnspace and nullspace in low rank matrix transformations
= np.array([[1.0,2.0],[4.0,8.0]])
A print("The transformation matrix involved is :")
Matrix(A)
print("The rowspace of this matrix is spanned by : ")
1.0,2.0])) Matrix(np.array([
print("The nullspace of a matrix is always perpendicular to the rowspace.")
print("The nullspace of matrix A is spaned by : ")
-2.0,1.0])) Matrix(np.array([
print("Any vector in the nullspace is converted to a zero matrix after transformation.")
2.0,-1.0]),4)
plot_lr(np.array([-0.5*x,lw=5,alpha=0.4,label=f"y = {-0.5}x, Nullspace of A",color="g")
plt.plot(x,= np.arange(-6,6,0.01)
x =(1,1), borderaxespad=0)
plt.legend(bbox_to_anchor plt.tight_layout()
Consider the vector v = [1.0, 1.0] and the same low rank transformation matrix as above.
The matrix obtained on transformation is:
The projection of v along the rowspace of A is:
Step 1: Finding the projection matrix:
= np.array([1.0, 2.0])
r = np.outer(r,r)
proj = proj / np.inner(r,r)
proj Matrix(proj)
The projection matrix is:
Step 2: Finding the projection of v along the rowspace of A
b = np.array([1.0,1.0])
v = proj @ b
Matrix(v)
The matrix obtained on transformation of this projection vector is:
= A @ v
Av =False) Eq(MatMul(Matrix(A),Matrix(v)),Matrix(Av),evaluate
We notice that this is the same as the matrix obtained before.
Learnings:
Any vector can be written as the vector sum of the projection on the rowspace and projection perpendicular to the rowspace (ie; in the nullspace). Only the component along the rowspace gets transformed to a non-zero matrix, the transformation of the component in the nullspace is always a zero matrix.
Analogy to PCA: As all vectors after transformation lie in the column space of A, this can be thought of as dimensionality reduction where there is a change in dimension from the initial vector space to the dimension of the column space of the transformation matrix