Lecture Six.

We now present Berkeley's CS C151 Diagnostic Quiz.

We will solve it together in Google Colab for tomorrow.

This week's deliverable will have two parts:

Here's how you put together the first.

This is the answer to the first question:

1. What is the magnitude of $2 - \sqrt{2}i$?

Answer: By definition $|a + bi| = \sqrt{a^2+b^2}$. 

In our case $a = 2$ and $b=-\sqrt{2}$. 

So $|2-\sqrt{2}i| = \sqrt{2^2+(-\sqrt{2})^2} = \sqrt{4+2} = \sqrt{6}.$
The answer to the second question is:
Question 2. What is the complex conjugate of $e^{\frac{\pi i}{3}}$?

Answer: For every complex number $z = a + bi$ we have 

*   $z = a +ib = r e^{i\theta} = r (\cos{\theta} + i \sin{\theta})$
*   where $r = |z| = \sqrt{a^2 + b^2}$ and $\tan{\theta}=\frac{b}{a}$

So for $e^{\frac{\pi i}{3}}$ we have $r = 1$ and $\theta = \frac{\pi}{3}$ which means 
$z = \cos{\frac{\pi}{3}} + i \sin{\frac{\pi}{3}} = \frac{1}{2} + i \frac{\sqrt{3}}{2}$. 

The complex conjugate of a complex number $z = a + i b$ is $z^* = a - i b$. 

So the answer to our question is: $\frac{1}{2} - i \frac{\sqrt{3}}{2}$.
Here now is the answer to the third question:
Question 3. Compute $\begin{pmatrix} -1 & 1 \\ 3 & -1 \end{pmatrix} 
\cdot \begin{pmatrix} 0 & 1 \\ 2 & 0 \end{pmatrix} $

Answer: $\begin{pmatrix} -1 \cdot 0 + 1 \cdot 2 & -1 \cdot 1 + 1 \cdot 0 \\ 
3 \cdot 0 + (-1) \cdot 2 & 3 \cdot 1 + (-1) \cdot 0 \end{pmatrix} = 
\begin{pmatrix} 2 & -1 \\ -2 & 3\end{pmatrix}$
We can start answering the fourth question as follows:
Question 4. For each pair of matrices indicate if they commute. 

(Matrices $A$ and $B$ are said to commute if $AB = BA$). 

(a) $A = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \textrm{and} B = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} $

Second matrix is the identity so the answer here is: yes (here, $AB = BA = A$). 

(b) $\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \textrm{and} \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} $

Here $AB = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix} \ne \begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix} = BA$ so the answer is: no. 

We can multiply by hand, via Wolfram Alpha or using Python (numpy). 
Here's the numpy code that proves our last point:
import numpy as np

A = np.matrix([[0, 1], [1, 0]])
B = np.matrix([[1, 0], [0, -1]])

print(np.dot(A, B))
print(np.dot(B, A))
So we can now finish the answer to the fourth question as follows:
(c) $A = \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \textrm{and}~B = \begin {pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}$

Answer: I will let you prepare the answer for this one. 

(d) $A = \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} \textrm{and}~B = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}$

Answer: second matrix is the identity here. 

(e) $A = \begin{pmatrix} 2 & 0 \\ 0 & -1 \end{pmatrix} \textrm{and}~B= \begin{pmatrix} 3 & 0 \\ 0 & 2 \end{pmatrix}$

Answer: Two diagonal matrices always commute. 

Please let me know though what the value of $AB$ is and how you calculated it. 
To answer questions 5 and 6 we write:
Question 5: What is (i.e., determine) a unit vector in
$\mathbb{R}^2$ that makes a $\frac{\pi}{4}$ angle with both the $x$- and $y$-axes?

Answer: $r = 1$ and $\theta=\frac{\pi}{4}$ so $z = \cos{\frac{\pi}{4}}+i\sin{\frac{\pi}{4}} = \frac{\sqrt{2}}{2} + i \frac{\sqrt{2}}{2}$.
We can also write this as $\begin{pmatrix}
\frac{1}{\sqrt{2}}\\[0.5ex]
\frac{1}{\sqrt{2}}
\end{pmatrix}$.

Question 6:What is a real unit vector in
$\mathbb{R}^2$ orthogonal to $\begin{pmatrix}
\frac{1}{\sqrt{3}}\\[0.5ex]
\frac{\sqrt{2}}{\sqrt{3}}
\end{pmatrix}$?

Answer: we need to determine a vector $\begin{pmatrix}
a\\[0.5ex]
b
\end{pmatrix}$ such that \begin{cases} \frac{a}{\sqrt{3}}+ \frac{{b\sqrt{2}}}{\sqrt{3}} = 0 \\ a^2 + b^2 = 1\end{cases}

The first condition is the orthogonality the second that it's a unit vector. 

We can solve this by hand, in Wolfram Alpha or in Python. An answer is: $\begin{pmatrix}
\frac{\sqrt{2}}{\sqrt{3}}\\[0.5ex]
-\frac{1}{\sqrt{3}}
\end{pmatrix}$ 
Here's the code that solves the system in Python for question 6:
from sympy import *

a, b = symbols('a, b')
eq1 = Eq(a/sqrt(3)+b*sqrt(2)/sqrt(3), 0)
eq2 = Eq(a**2+b**2, 1)
sol = solve([eq1, eq2], [a, b])
print(sol)
You can tell there are two possible answers.

Questions 7 and 8 can be addressed as follows:

Question 7: What is the inner product 
between the vectors $\begin{pmatrix}
\frac{\sqrt{3}}{2}\\[0.5ex]
\frac{1}{2}
\end{pmatrix} \textrm{and} 
\begin{pmatrix}
\frac{1}{2}\\[0.5ex]
\frac{\sqrt{3}}{2}
\end{pmatrix}$ ?

The inner product is the sum of products of the corresponding elements: 

We calculate as follows from the given vectors $\frac{\sqrt{3}}{2} \cdot \frac{1}{2} + \frac{1}{2} \cdot \frac{\sqrt{3}}{2} = 2 \cdot \frac{\sqrt{3}}{4} = \frac{\sqrt{3}}{2}$

Question 8. And what is the angle between the two vectors in degrees? 

To calculate the cosine of the angle between two vectors in a 2D space:

* Find the dot product of the vectors.
* Divide the dot product by the magnitude of the first vector.
* Divide the resultant by the magnitude of the second vector

So here $\cos{\theta} = \frac{\frac{\sqrt{3}}{2}}{(\frac{3}{4} + \frac{1}{4}) (\frac{1}{4} + \frac{3}{4})} = \frac{\sqrt{3}}{2}$ so $\theta = 30^\circ$.
Let's do this in numpy just for the fun of it:
v1 = np.array([[np.sqrt(3)/2], [1/2]])
v2 = np.array([[1/2], [np.sqrt(3)/2]])
inner_result = np.vdot(v1, v2)
print(inner_result)
np.degrees(np.arccos(inner_result))
Here we used the fact that the vectors already had magnitude 1.

So we're now left with just the last two problems:

Problem 9: Determine the eigenvalues of the matrix $
\begin{pmatrix}
0&1\\
1&0
\end{pmatrix} $

Answer: $-1, 1$

Problem 10: Determine the eigenvector corresponding to the largest eigenvalue of the above matrix. 

What other eigenvectors does the matrix have?

Answer: $\begin{pmatrix}
\frac{1}{\sqrt{2}}\\[0.5ex]
\frac{1}{\sqrt{2}}
\end{pmatrix}$ and $\begin{pmatrix}
-\frac{1}{\sqrt{2}}\\[0.5ex]
\frac{1}{\sqrt{2}}
\end{pmatrix}$
Solve this by typing the following in Wolfram Alpha:
eigenvalues and eigenvectors of [[0, 1],[1, 0]]
Likewise we can solve with numpy in Python:
a = np.matrix([[0, 1], [1, 0]])
eva, eve = np.linalg.eig(a)
print(eva)
print(eve)
Why the difference?

Answer: the vectors need to be normalized.

More to come soon.