\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\graphicspath{{images/}}

\title{Module 1}
\author{Sebastian Koranda}
\date{September 2025}

\begin{document}
\maketitle

\section*{Exercise 1}
\includegraphics[width=\textwidth]{fig1}
\pagebreak

\section*{Exercise 2}

\subsection*{1a)}

\begin{verbatim}
x_row = 0 : 2 : 6
\end{verbatim}
gives row vector of numbers 0 to 6 with 2 step increments
$$
\boldsymbol{x}_{row} =
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix}
$$

\begin{verbatim}
x_col = x_row.'
\end{verbatim}
gives transposed column vector
$$
\boldsymbol{x}_{col} =
\begin{bmatrix}
    0 \\ 2 \\ 4 \\ 6
\end{bmatrix}
$$

\begin{verbatim}
x1 = [1,2,3]
\end{verbatim}
gives row vector
$$
\boldsymbol{x}_1 =
\begin{bmatrix}
    1 & 2 & 3
\end{bmatrix}
$$

\begin{verbatim}
x2 = [1;2;3]
\end{verbatim}
gives column vector
$$
\boldsymbol{x}_2 =
\begin{bmatrix}
    1 \\ 2 \\ 3
\end{bmatrix}
$$

\begin{verbatim}
x3 = linspace(1,10,4)
\end{verbatim}
gives row vector of 4 evenly spaced numbers from 1 to 10
$$
\boldsymbol{x}_3 =
\begin{bmatrix}
    1 & 4 & 7 & 10
\end{bmatrix}
$$

\pagebreak
\subsection*{1b)}

\begin{verbatim}
a = x_row * x_col
\end{verbatim}
inner product
$$
a = \boldsymbol{x}_{row}\boldsymbol{x}_{col} =
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix}
\begin{bmatrix}
    0 \\ 2 \\ 4 \\ 6
\end{bmatrix}
= 56
$$

\begin{verbatim}
b = x_col * x_row
\end{verbatim}
inner product
$$
\boldsymbol{B} = \boldsymbol{x}_{col}\boldsymbol{x}_{row} =
\begin{bmatrix}
    0 \\ 2 \\ 4 \\ 6
\end{bmatrix}
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix} =
\begin{bmatrix}
    0 & 0 & 0 & 0 \\
    0 & 4 & 8 & 12 \\
    0 & 8 & 16 & 24 \\
    0 & 12 & 24 & 36
\end{bmatrix}
$$

\begin{verbatim}
c = x_row .* x_col
\end{verbatim}
\verb|x_row| is treated as a $4\times4$ matrix with 4 identical rows, 
and \verb|x_col| as a $4\times4$ matrix with 4 identical columns, 
then element-wise (Hadamard) product
$$
\boldsymbol{C} =
\begin{bmatrix}
    0 & 2 & 4 & 6 \\
    0 & 2 & 4 & 6 \\
    0 & 2 & 4 & 6 \\
    0 & 2 & 4 & 6
\end{bmatrix}
\odot
\begin{bmatrix}
    0 & 0 & 0 & 0 \\
    2 & 2 & 2 & 2 \\
    4 & 4 & 4 & 4 \\
    6 & 6 & 6 & 6
\end{bmatrix} =
\begin{bmatrix}
    0 & 0 & 0 & 0 \\
    0 & 4 & 8 & 12 \\
    0 & 8 & 16 & 24 \\
    0 & 12 & 24 & 36
\end{bmatrix}
$$

\begin{verbatim}
d = x_row .* x_col'
\end{verbatim}
element-wise (Hadamard) product
$$
\boldsymbol{d} = \boldsymbol{x}_{row} \odot \boldsymbol{x}_{col}^T =
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix}
\odot
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix} =
\begin{bmatrix}
    0 & 4 & 16 & 36
\end{bmatrix}
$$

\begin{verbatim}
e = x_row' .* x_col
\end{verbatim}
element-wise (Hadamard) product
$$
\boldsymbol{e} = \boldsymbol{x}_{row}^T \odot \boldsymbol{x}_{col} =
\begin{bmatrix}
    0 \\ 2 \\ 4 \\ 6
\end{bmatrix}
\odot
\begin{bmatrix}
    0 \\ 2 \\ 4 \\ 6
\end{bmatrix} =
\begin{bmatrix}
    0 \\ 4 \\ 16 \\ 36
\end{bmatrix}
$$

\pagebreak
\subsection*{1c)}

\begin{verbatim}
z_row = x_row + 2j*x_row
\end{verbatim}
\begin{multline}
\boldsymbol{z}_{row} = \boldsymbol{x}_{row} + 2i\boldsymbol{x}_{row} =
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix}
+ 2i
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix} \\
=
\begin{bmatrix}
    0 & 2 & 4 & 6
\end{bmatrix}
+
\begin{bmatrix}
    0 + 0i & 0 + 4i & 0 + 8i & 0 + 12i
\end{bmatrix} \\
=
\begin{bmatrix}
    0 + 0i & 2 + 4i & 4 + 8i & 6 + 12i
\end{bmatrix}
\end{multline}

\subsection*{1d)}

\begin{verbatim}
z_col1 = z_row'
\end{verbatim}
$$
\boldsymbol{z}_{col1}^T 
=
\begin{bmatrix}
    0 + 0i \\ 
    2 - 4i \\ 
    4 - 8i \\ 
    6 - 12i
\end{bmatrix}
$$

\begin{verbatim}
z_col2 = z_row.'
\end{verbatim}
$$
\boldsymbol{z}_{col2}^*
=
\begin{bmatrix}
    0 + 0i \\ 
    2 + 4i \\ 
    4 + 8i \\ 
    6 + 12i
\end{bmatrix}
$$

\subsection*{2a)}
\begin{verbatim}
A = nan(3,3);
A(1,:) = [1,1,1];
A(2,:) = [2,2,2];
A(3,:) = [3,3,3];
\end{verbatim}
$$
A =
\begin{bmatrix}
    1 & 1 & 1 \\
    2 & 2 & 2 \\
    3 & 3 & 3
\end{bmatrix}
$$
\\
\begin{verbatim}
B = [1 1 1 1 1; 2 2 2 2 2; 3 3 3 3 3; 4 4 4 4 4; 5 5 5 5 5]
\end{verbatim}
$$
\\
B =
\begin{bmatrix}
    1 & 1 & 1 & 1 & 1 \\
    2 & 2 & 2 & 2 & 2 \\
    3 & 3 & 3 & 3 & 3 \\
    4 & 4 & 4 & 4 & 4 \\
    5 & 5 & 5 & 5 & 5
\end{bmatrix}
$$
\\
\begin{verbatim}
C = [1 2 3
     4 5 6]
\end{verbatim}
$$
C =
\begin{bmatrix}
    1 & 2 & 3 \\
    4 & 5 & 6
\end{bmatrix}
$$

\subsection*{2b)}
\begin{verbatim}
B = repmat(A,[2 1 2]);
\end{verbatim}
Repeat $A$ 2 times in the first dimension, once in the second dimension, and 2 times in the third dimension:
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\def\xs{1} %shift in x direction
\def\ys{0.5} %shift in y direction
\foreach \x in {1,2}
{

\matrix [draw, % for the rectangle border
         fill=white, % so that it is not transparent
         ampersand replacement=\&] %see explanation
(mm\x)%give the matrix a name
at(-\x * \xs, -\x * \ys) %shift the matrix
{
    \node {1}; \& \node {1}; \& \node {1};\\
    \node {2}; \& \node {2}; \& \node {2};\\
    \node {3}; \& \node {3}; \& \node {3};\\
    \node {1}; \& \node {1}; \& \node {1};\\
    \node {2}; \& \node {2}; \& \node {2};\\
    \node {3}; \& \node {3}; \& \node {3};\\
};
}

\draw [dashed,gray](mm1.north west) -- (mm2.north west);
\draw [dashed,gray](mm1.north east) -- (mm2.north east);
\draw [dashed,gray](mm1.south east) -- (mm2.south east);
\end{tikzpicture}
\end{figure}

\subsection*{2c)}
\begin{verbatim}
sum1 = sum(B);
\end{verbatim}
Sum of elements of \verb|B| along the first array dimension whose size does not equal to 1:
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\def\xs{1} %shift in x direction
\def\ys{0.5} %shift in y direction
\foreach \x in {1,2}
{

\matrix [draw, % for the rectangle border
         fill=white, % so that it is not transparent
         ampersand replacement=\&] %see explanation
(mm\x)%give the matrix a name
at(-\x * \xs, -\x * \ys) %shift the matrix
{
    \node {12}; \& \node {12}; \& \node {12};\\
};
}

\draw [dashed,gray](mm1.north west) -- (mm2.north west);
\draw [dashed,gray](mm1.north east) -- (mm2.north east);
\draw [dashed,gray](mm1.south east) -- (mm2.south east);
\end{tikzpicture}
\end{figure}

\begin{verbatim}
sum2 = sum(B,2);
\end{verbatim}
Sum of elements of \verb|B| along dimension 2:
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\def\xs{1} %shift in x direction
\def\ys{0.5} %shift in y direction
\foreach \x in {1,2}
{

\matrix [draw, % for the rectangle border
         fill=white, % so that it is not transparent
         ampersand replacement=\&] %see explanation
(mm\x)%give the matrix a name
at(-\x * \xs, -\x * \ys) %shift the matrix
{
    \node {3};\\
    \node {6};\\
    \node {9};\\
    \node {3};\\
    \node {6};\\
    \node {9};\\
};
}

\draw [dashed,gray](mm1.north west) -- (mm2.north west);
\draw [dashed,gray](mm1.north east) -- (mm2.north east);
\draw [dashed,gray](mm1.south east) -- (mm2.south east);
\end{tikzpicture}
\end{figure}

\pagebreak
\begin{verbatim}
sum3 = sum(B,3);
\end{verbatim}
Sum of elements of \verb|B| along dimension 3:
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\def\xs{1} %shift in x direction
\def\ys{0.5} %shift in y direction
\foreach \x in {1,1}
{

\matrix [draw, % for the rectangle border
         fill=white, % so that it is not transparent
         ampersand replacement=\&] %see explanation
(mm\x)%give the matrix a name
at(-\x * \xs, -\x * \ys) %shift the matrix
{
    \node {2}; \& \node {2}; \& \node {2};\\
    \node {4}; \& \node {4}; \& \node {4};\\
    \node {6}; \& \node {6}; \& \node {6};\\
    \node {2}; \& \node {2}; \& \node {2};\\
    \node {4}; \& \node {4}; \& \node {4};\\
    \node {6}; \& \node {6}; \& \node {6};\\
};
}
\end{tikzpicture}
\end{figure}

\begin{verbatim}
sum4 = sum(B, 'all');
\end{verbatim}
Sum of elements of \verb|B| along all dimensions:
\begin{figure}[!h]
\centering
$72$
\end{figure}

\begin{verbatim}
sum5 = sum(sum(sum(B)));
\end{verbatim}
Iteratively sum elements of \verb|B| along the first dimension, 3 times. Effectively same as previous case:
\begin{figure}[!h]
\centering
$72$
\end{figure}

\subsection*{2c)}
\verb|sum1| has size $ 1 \times 3 \times 2 $. The first dimension of length 1 is a singleton dimension.
The \verb|squeeze|-function removes every singleton dimension, giving us a $ 3 \times 2 $ matrix.
\begin{verbatim}
sum1_squeezed = squeeze(sum1);
\end{verbatim}
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\def\xs{1} %shift in x direction
\def\ys{0.5} %shift in y direction
\foreach \x in {1,1}
{

\matrix [draw, % for the rectangle border
         fill=white, % so that it is not transparent
         ampersand replacement=\&] %see explanation
(mm\x)%give the matrix a name
at(-\x * \xs, -\x * \ys) %shift the matrix
{
    \node {12}; \& \node {12};\\
    \node {12}; \& \node {12};\\
    \node {12}; \& \node {12};\\
};
}
\end{tikzpicture}
\end{figure}

\end{document}

