# 13 Pre-Class Assignment: Projections¶

## 1. Orthogonal and Orthonormal¶

Definition: A set of vectors is said to be orthogonal if every pair of vectors in the set is orthogonal (the dot product is 0). The set is orthonormal if it is orthogonal and each vector is a unit vector (norm equals 1).

Result: An orthogonal set of nonzero vectors is linearly independent.

Definition: A basis that is an orthogonal set is called an orthogonal basis. A basis that is an orthonormal set is called an orthonormal basis.

Result: Let $$\{u_1,\dots,u_n\}$$ be an orthonormal basis for a vector space $$V$$. Then for any vector $$v$$ in $$V$$, we have $$$v=(v\cdot u_1)u_1+(v\cdot u_2)u_2 +\dots + (v\cdot u_n)u_n$$$

Definition: A square matrix is orthogonal is $$A^{-1}=A^\top$$.

Result: Let $$A$$ be a square matrix. The following three statements are equivalent.

(a) $$A$$ is orthogonal.

(b) The column vectors of $$A$$ form an orthonormal set.

(c) The row vectors of $$A$$ form an orthonormal set.

(d) $$A^{-1}$$ is orthogonal.

(e) $$A^\top$$ is orthogonal.

Result: If $$A$$ is an orthogonal matrix, then we have $$|A|=\pm 1$$.

Consider the following vectors $$u_1, u_2$$, and $$u_3$$ that form a basis for $$R^3$$.

$u_1 = (1,0,0)$
$u_2 = (0, \frac{1}{\sqrt(2)}, \frac{1}{\sqrt(2)})$
$u_3 = (0, \frac{1}{\sqrt(2)}, -\frac{1}{\sqrt(2)})$

DO THIS: Show that the vectors $$u_1$$, $$u_2$$, and $$u_3$$ are linearly independent (HINT: see the pre-class for 0219-Change_Basis):

QUESTION 1: How do you show that $$u_1$$, $$u_2$$, and $$u_3$$ are orthogonal?

QUESTION 2: How do you show that $$u_1$$, $$u_2$$, and $$u_3$$ are normal vectors?

DO THIS: Express the vector $$v = (7,5,-1)$$ as a linear combination of the $$u_1$$, $$u_2$$, and $$u_3$$ basis vectors:

# Put your answer here


## 2. Code Review¶

In the next in-class assignment, we are going to avoid some of the more advanced libraries ((i.e. no numpy or scipy or sympy) to try to get a better understanding about what is going on in the math. The following code implements some common linear algebra functions:

#Standard Python Libraries only
import math
import copy

def dot(u,v):
'''Calculate the dot product between vectors u and v'''
temp = 0;
for i in range(len(u)):
temp += u[i]*v[i]
return temp


DO THIS: Write a quick test to compare the output of the above dot function with the numpy dot function.

# Put your test code here

def multiply(m1,m2):
'''Calculate the matrix multiplication between m1 and m2 represented as list-of-list.'''
n = len(m1)
d = len(m2)
m = len(m2[0])

if len(m1[0]) != d:
print("ERROR - inner dimentions not equal")

#make zero matrix
result = [[0 for j in range(m)] for i in range(n)]
#    print(result)
for i in range(0,n):
for j in range(0,m):
for k in range(0,d):
#print(i,j,k)
#print('result', result[i][j])
#print('m1', m1[i][k])
#print('m2', m2[k][j])
result[i][j] = result[i][j] + m1[i][k] * m2[k][j]
return result


DO THIS: Write a quick test to compare the output of the above multiply function with the numpy multiply function.

# Put your test code here


QUESTION: What is the big-O complexity of the above multiply function?

QUESTION: Line 11 in the provided multiply code initializes a matrix of the size of the output matrix as a list of lists with zeros. What is the big-O complexity of line 11?

def norm(u):
'''Calculate the norm of vector u'''
nm = 0
for i in range(len(u)):
nm += u[i]*u[i]
return math.sqrt(nm)


DO THIS: Write a quick test to compare the outputs of the above norm function with the numpy norm function.

#Put your test code here

def transpose(A):
'''Calculate the transpose of matrix A represented as list of lists'''
n = len(A)
m = len(A[0])
AT = list()
for j in range(0,m):
temp = list()
for i in range(0,n):
temp.append(A[i][j])
AT.append(temp)
return AT


DO THIS: Write a quick test to compare the output of the above transpose function with the numpy transpose function.

# Put your test code here


QUESTION: What is the big-O complexity of the above transpose function?

QUESTION: Explain any differences in results between the provided functions and their numpy counterparts.

## 3. Gram-Schmidt¶

Watch this video for the indroduction of Gram-Schmidt, which we will implement in class.

from IPython.display import YouTubeVideo


## 4. Assignment wrap-up¶

Assignment-Specific QUESTION: How do you show that $$u_1$$, $$u_2$$, and $$u_3$$ are orthogonal?

QUESTION: Summarize what you did in this assignment.

QUESTION: What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

QUESTION: How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

QUESTION: What was the most challenging part of this assignment for you?

QUESTION: What was the least challenging part of this assignment for you?

QUESTION: What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?