Projections#
Let’s break down the concept of linear algebra projection, illustrate it with SageMath code examples, and explore visualizations to make it more intuitive.
Concept of Projection
Definitions: Projection
First recall that a subspace is a subset of a vector space that itself satisfies the properties of a vector space (e.g., closure under addition and scalar multiplication).
Projection: Given a vector v and a subspace W, the projection of v onto W is a vector p within W that is closest to v. This vector p is unique and is obtained by drawing a perpendicular line from the tip of v to the subspace W.
Why Projections?
Approximation: Projections help us find the “best” approximation of a vector within a smaller subspace. This is crucial in data compression, noise reduction, and solving equations.
Decomposition: Vectors can be decomposed into a part that lies within the subspace and a part that’s orthogonal (perpendicular) to it.
SageMath Code Examples
Let’s use SageMath to explore projections in a 2D plane.
from sage.modules.free_module_element import vector
from sage.modules.free_module import VectorSpace
from sage.plot.plot3d.shapes2 import line3d
# Define a subspace (a line)
u = vector([1, 2])
V = VectorSpace(QQ, 2) # Create a 2-dimensional vector space over rationals
subspace = V.submodule([u]) # The line spanned by vector u
# Vector to be projected
v = vector([3, 1])
# Calculate projection
proj_v = (v.dot_product(u) / u.dot_product(u)) * u
print("Projection of v onto subspace:", proj_v)
# Plotting
show(
point(v, color='blue', size=50) + # Original vector
point(proj_v, color='red', size=50) + # Projected vector
line3d([(0, 0, 0), (u[0], u[1], 0)], color='green') + # Subspace line
line3d([(v[0], v[1], 0), (proj_v[0], proj_v[1], 0)],
linestyle='dashed', color='purple'), # Projection line
aspect_ratio=1 # Make the axes have equal scales
)
Projection of v onto subspace: (1, 2)
(You can interactively rotate, zoom, and pan the above 3D plots using the mouse)
Explanation:
We define a vector
uthat spans our subspace (the line).We define a vector
vthat we want to project onto the line.The formula
(v.dot_product(u) / u.dot_product(u)) * ucalculates the projectionproj_v.