\documentclass[a4paper]{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{enumitem}

\graphicspath{ {./images/} }

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName}
\chead{\hmwkClass\ : \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Question \arabic{#1} (continued)}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Question \arabic{#1} (continued)}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Question \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Question \arabic{homeworkProblemCounter}}{}\nobreak{}

\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Question \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

\newcommand{\hmwkClass}{INF 1004 Mathematics 2}
\newcommand{\hmwkTitle}{Tutorial\ \#7}
\newcommand{\hmwkAuthorName}{\textbf{Woon Jun Wei}}
\newcommand{\hmwkStudentID}{\textbf{2200624}}

\title{
    \vspace{2in}
    \textmd{\textbf{\hmwkClass \\ \hmwkTitle}}\\
    \vspace{3in}
}

\author{\hmwkAuthorName \\ \hmwkStudentID}
\date{\today}

\renewcommand{\part}[1]{\textbf{\large Part \arabic{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% For derivatives
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)}

% For partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)}

% Integral dx
\newcommand{\dx}{\mathrm{d}x}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large My Solution\\}}
\newcommand{\answer}{\textbf{\large Sample Solutions\\}}

% Probability commands: Expectation, Variance, Covariance, Bias
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Bias}{\mathrm{Bias}}

\begin{document}

\maketitle
\pagebreak

\begin{homeworkProblem}
    Compute the euclidean vector norm for vectors
$$
\begin{bmatrix}
    1,0,2
\end{bmatrix}
,
\begin{bmatrix}
    3,4
\end{bmatrix}
,
\begin{bmatrix}
    -7,2,-4, \sqrt{12}
\end{bmatrix}
$$

\solution

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
Compute the corresponding unit length vector for these:
$$
\begin{bmatrix}
    3,4
\end{bmatrix}
,
\begin{bmatrix}
    -1,-2,3
\end{bmatrix}
,
\begin{bmatrix}
    -7,2,-4, \sqrt{12}
\end{bmatrix}
$$

\solution

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Compute the inner product between these vectors and their angle in degrees:
$$
\begin{bmatrix}
    3,-2,2
\end{bmatrix}
,
\begin{bmatrix}
1,2,2
\end{bmatrix}
$$

Compute the inner product between these vectors and their angle in degrees:
$$
\begin{bmatrix}
1,0,1
\end{bmatrix}
,
\begin{bmatrix}
    2,1,-2
\end{bmatrix}
,
\begin{bmatrix}
    \frac{1}{2\sqrt{2}}
    , -\frac{\sqrt{3}}{2}
    , \frac{1}{2\sqrt{2}}}
\end{bmatrix}
$$

\answer

    $$\frac{u}{\lVert u \rVert_2} \cdot \frac{v}{\lVert v \rVert_2} = \cos(\angle (u,v))$$

\part

$$
\begin{align*}
    \langle [3,-2,2], [1,2,2] \rangle &= 3 \cdot 1 + (-2) \cdot 2 + 2 \cdot 2 \\
                                      &= 3 + (-4) + 4 \\
                                      &= \boxed{3}
\end{align*}
$$

Angle:\\

Let $u$ be $\begin{bmatrix}3,-2,2\end{bmatrix}$ and $v$ be $\begin{bmatrix}1,2,2\end{bmatrix}$\\

$$
\begin{align*}
    \cos(\angle (u,v)) &= \frac{\langle u,v \rangle}{\lVert u \rVert_2 \cdot \lVert v \rVert_2} \\
                       &= \frac{\begin{bmatrix}3,-2,2\end{bmatrix} \cdot \begin{bmatrix}1,2,2\end{bmatrix}}{\lVert \begin{bmatrix}3,-2,2\end{bmatrix} \rVert_2 \cdot \lVert \begin{bmatrix}1,2,2\end{bmatrix} \rVert_2} \\
                       &= \frac{3}{\sqrt{17}\cdot\sqrt{9}} \\
                       &= \frac{3}{\sqrt{153}} \\
                       &= \frac{1}{\sqrt{17}}\\
           \angle (u,v) &= \cos^{-1}(\frac{3}{\sqrt{153}}) \\
                        &= \boxed{75.96^{\circ}} \text{ \textbf{OR}}\\
                        &= 360^{\circ} - 75.96^{\circ}\\
                        &= \boxed{284.04^{\circ}}
\end{align*}
$$

\pagebreak

\part

$$
\begin{align*}
    \langle [1,0,1], [2,1,-2] \rangle &= 1 \cdot 2 + 0 \cdot 1 + 1 \cdot (-2) \\
                                      &= 2 + 0 + (-2) \\
                                      &= \boxed{0}
\end{align*}
$$

Angle:\\

Let $u$ be $\begin{bmatrix}1,0,1\end{bmatrix}$ and $v$ be $\begin{bmatrix}2,1,-2\end{bmatrix}$ and w be $\begin{bmatrix}\frac{1}{2\sqrt{2}}, -\frac{\sqrt{3}}{2}, \frac{1}{2\sqrt{2}}\end{bmatrix}$\\

$$
\begin{align*}
    \cos(\angle (u,v)) &= \frac{\langle u,v \rangle}{\lVert u \rVert_2 \cdot \lVert v \rVert_2} \\ &= \frac{\begin{bmatrix}1,0,1\end{bmatrix} \cdot \begin{bmatrix}2,1,-2\end{bmatrix}}{\lVert \begin{bmatrix}1,0,1\end{bmatrix} \rVert_2 \cdot \lVert \begin{bmatrix}2,1,-2\end{bmatrix} \rVert_2} \\
                       &= \frac{0}{\sqrt{2}\cdot\sqrt{6}} \\
                       &= 0 \\\\
           \angle (u,v) &= \cos^{-1}(0) \\
                        &= \boxed{90^{\circ}} \text{ \textbf{OR}}\\
                        &= 360^{\circ} - 90^{\circ}\\
                        &= \boxed{270^{\circ}}
\end{align*}
$$

\part

$$
\left \langle [1,0,1] \cdot \left[ \frac{1}{2\sqrt{2}}, -\frac{\sqrt{3}}{2}, \frac{1}{2\sqrt{2}} \right] \right \rangle = \frac{1}{\sqrt{2}}
$$\\

$$
\left\lVert \left [\frac{1}{2\sqrt{2}}, -\frac{\sqrt{3}}{2}, \frac{1}{2\sqrt{2}} \right] \right \rVert = \sqrt{\frac{1}{4\cdot 2} + \frac{3}{4} + \frac{1}{4 \cdot 2}} = 1
$$

$$
\begin{align}
\cos \angle(u,w) &= \frac{1}{\sqrt{2}} \cdot\frac{1}{\sqrt{2}}\cdot \frac{1}{1} \\
                 &= \frac{1}{2}\\
\angle(u,w) &= \cos^{-1}(\frac{1}{2}) \\
            &= 60^{\circ} \text{ \textbf{OR}}\\
\angle(u,w) &= 360^{\circ} - 60^{\circ} = 300^{\circ}
\end{align}
$$

\pagebreak

\part

$$
\begin{align}
    [2,1,-2] \cdot \left[\frac{1}{2\sqrt{2}},-\frac{\sqrt{3}}{2},\frac{1}{2\sqrt{2}}\right] &= \frac{1}{2\sqrt{2}} \cdot 2 + \frac{\sqrt{3}}{2} \cdot 1 + \frac{1}{2\sqrt{2}} \cdot (-2) \\
                                                                                            &= - \frac{\sqrt{3}}{2}\\\\
    \cos \angle(v,w) &= -\frac{\sqrt{3}}{2} \cdot \frac{1}{3} \cdot \frac{1}{1} \\
                     &= -\frac{1}{2\sqrt{3}}\\
        \angle(v,w) &= \cos^{-1}(-\frac{1}{2\sqrt{3}}) \\
                    &= 106.74^{\circ} \text{ \textbf{OR}}\\
        \angle(v,w) &= 360^{\circ} - 106.74^{\circ} = 253.26^{\circ}
\end{align}
$$

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    \begin{itemize}
        \item What is the projection of $\begin{bmatrix}5,2 \end{bmatrix}$ onto the subspace spanned by vector $\begin{bmatrix} 1,1 \end{bmatrix}$?
        \item What is the projection of $\begin{bmatrix}0,2,1 \end{bmatrix}$ onto the subspace spanned by vector $\begin{bmatrix} 1,-1,-1 \end{bmatrix}$?
        \item Project $\begin{bmatrix} 5,2 \end{bmatrix}$ onto the subspace spanned by vectors $\begin{bmatrix} 2,3 \end{bmatrix}$, $\begin{bmatrix}1,1 \end{bmatrix}$
        \item What is the projection of $\begin{bmatrix} 1,-1,1 \end{bmatrix}$ onto the subspace spanned by vector $\begin{bmatrix} 1,1,1 \end{bmatrix}$ onto the subspace spanned by vectors  $\begin{bmatrix} 0,0,-1 \end{bmatrix}, \begin{bmatrix} 2,0,1 \end{bmatrix}$? Hint: this one is more tricky. Reason: $\begin{bmatrix} 0,-1,-1 \end{bmatrix} \cdot \begin{bmatrix}2,0,1\end{bmatrix} \neq 0$
    \end{itemize}

    \solution

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Compute these matrix multiplications:
    $$
    \begin{bmatrix}
        2 & 1\\3&-2
    \end{bmatrix}
    \begin{bmatrix}
        -1 &0\\-4&-2
    \end{bmatrix}
    $$
    \\
    $$
    \begin{bmatrix}
        -3\\2\\1
    \end{bmatrix}
    \begin{bmatrix}
        2&4&-2
    \end{bmatrix}
    $$
    \\
    Question: Do you need more of them to practice? If so, you can do at home:\\
    $$
    \begin{bmatrix}
        1&2\\2&4
    \end{bmatrix}
    \begin{bmatrix}
        3&0&1\\0&1&2
    \end{bmatrix}
    $$
    \\
    $$
    \begin{bmatrix}
        0.5&2.5\\-3.5&1.5
    \end{bmatrix}
    \begin{bmatrix}
        6\\4
    \end{bmatrix}
    $$

    \solution

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}

    \begin{itemize}
        \item Project $\begin{bmatrix} 5,2 \end{bmatrix}$ onto the orthogonal space of vector $\begin{bmatrix} 2,-3 \end{bmatrix}$
        \item Project $\begin{bmatrix} 1,-1,3 \end{bmatrix}$ onto the orthogonal space of vector $\begin{bmatrix} -3,1,1\end{bmatrix}$
        \item Project $\begin{bmatrix} 1,-1,3,1 \end{bmatrix}$ onto the orthogonal space of vector $\begin{bmatrix} -2,2,0,0\end{bmatrix}$, $\begin{bmatrix} 0,0,\sqrt{2}, \sqrt{2}\end{bmatrix}$
    \end{itemize}

    \solution


\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Run Gram-Schmid-orthogonalization on the vectors
    $$\begin{bmatrix} 12,12,6 \end{bmatrix}, \begin{bmatrix} 2,-2,4 \end{bmatrix}, \begin{bmatrix} -2,-2,1 \end{bmatrix}$$

    \solution


\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
{\large \textbf{Understanding Distances coming from $\ell_p$-norms}}\\

    Coding: plot in python or similar the set of points $x \in \mathbb{R}^2$ usuch that $\lVert x \rVert_p = 1$ for
    \begin{itemize}
        \item $p = 0.2$
        \item $p = 0.5$
        \item $p = 1$
        \item $p = 1.5$
        \item $p = 2$
        \item $p = 4$
        \item $p = 8$
        \item $p = 16$
    \end{itemize}
    Hint: in 2 dimensions for $p=2$ the solution is given by
    $$x(t) = \left(\cos(t), \sin(t)\right)$$
    due to $\cos^2(t) + \sin^2(t) = 1$.

you can use the same idea with different powers. You can start by considering $(\cos^r(t), \sin^r(t))$. One thing to node: $\cos(t)^r + \sin(t)^r$ is not always defined for negative values and certain $r$.\\

For $p \neq 2$, you can consider this, thich deals with the signs:
$$x(t) = (sign(\cos(t))|\cos(t)|^r, sign(\sin(t))|\sin(t)|^r)$$
for the right choice of r. Find out which $r$ is suitable for a general $p > 0$ such that $\lVert x \rVert_p = 1$. Then plot it in python.\\

    \solution

\end{homeworkProblem}
\end{document}
