Saturday, 6 September 2025

Math ABC: Orthogonal Vector Decomposition

Theory

To “decompose” a vector or matrix means to break up that matrix into multiple simpler pieces. Decompositions are used to reveal information that is “hidden” in a matrix, to make the matrix easier to work with, or for data compression. It is no understatement to write that much of linear algebra (in the abstract and in practice) involves matrix decompositions. Matrix decompositions are a big deal.

We can decompose the number 42 into the product of prime numbers 2, 3, and 7. This decomposition is called prime factorization and has many applications in numerical processing and cryptography.


Exercise 

Implement orthogonal vector decomposition. Start with two random-number vectors t and r. 

References

Cohen, Mike X. Practical Linear Algebra for Data Science (p. 56). O'Reilly Media. Kindle Edition. 


Code From Book

# import libraries

import numpy as np

import matplotlib.pyplot as plt



# NOTE: these lines define global figure properties used for publication.

import matplotlib_inline.backend_inline

matplotlib_inline.backend_inline.set_matplotlib_formats('svg') # display figures in vector format

plt.rcParams.update({'font.size':14}) # set global font size


# generate random R2 vectors (note: no orientation here! we don't need it for this exercise)

t = np.random.randn(2)

r = np.random.randn(2)


# the decomposition

t_para = r * (np.dot(t,r) / np.dot(r,r))

t_perp = t - t_para


# confirm that the two components sum to the target

print(t)

print( t_para+t_perp )


# confirm orthogonality (dot product must be zero!)

print( np.dot(t_para,t_perp) )

# Note about this result: Due to numerical precision errors, 

#   you might get a result of something like 10^-17, which can be interpretd as zero.




# draw them!

plt.figure(figsize=(6,6))


# draw main vectors

plt.plot([0,t[0]],[0,t[1]],color='k',linewidth=3,label=r'')

plt.plot([0,r[0]],[0,r[1]],color=[.7,.7,.7],linewidth=3,label=r'')


# draw decomposed vector components

plt.plot([0,t_para[0]],[0,t_para[1]],'k--',linewidth=3,label=r'')

plt.plot([0,t_perp[0]],[0,t_perp[1]],'k:',linewidth=3,label=r'')


plt.axis('equal')

plt.legend()

plt.savefig('Figure_02_08.png',dpi=300)

plt.show()

No comments:

Post a Comment