\documentclass[notitlepage]{article}
\usepackage{../../Math545}
\usepackage{listings}
\usepackage{numprint}
\usepackage{enumitem}
\usepackage{bbm}

\usepackage{amsfonts,amsmath}

\setlength{\parindent}{0in}

\def\Var{\text{Var}}
\def\Cov{\text{Cov}}
\def\Corr{\text{Corr}}



\lstloadlanguages{R}

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\lstset{basicstyle=\ttfamily, numbers=none, literate={~} {$\sim$}{2}}

\def\ctitle{MATH 545: Introduction to Time Series Analysis}
\def\ttitle{Winter 2025}
\def\stitle{Recap on Complex Numbers and \texttt{R} functionality}

\def\imagi{\dot{\iota}}

\def\Re{\text{Re}}
\def\Im{\text{Im}}


\begin{document}

\maketitle
A complex number $z$ takes the form
\[
z = x + y \imagi 
\]
where $x$ and $y$ are real numbers and $\imagi = \sqrt{-1}$.

\begin{itemize}[itemsep=0.01 in]
  \item $x$ is the Real part of $z$, $\Re(z)$, $y$ is the Imaginary part of $z$, $\Im(z)$.
  \item The Modulus of $z$, $|z|$, is defined by
  \[
  |z| = \sqrt{x^2+y^2}
  \]
  \item The Argument of $z$, $\arg(z)$, is defined by
  \[
  \arg(z) = \arctan(y/x)
  \]
  with range $(-\pi,\pi]$.
  \item We have that
  \[
  z = |z| e^{\imagi \arg(z)}
  \]
\end{itemize}
  For example, consider $z = 2+ 3 \imagi$.
  <<c1>>=
  z<-2+3i
  z
  Re(z)    #Real part
  Im(z)    #Imaginary part
  Mod(z)   #Modulus
  Mod(z)^2 #Square of modulus
  Arg(z)   #Argument
  atan(3/2)
  Mod(z)*exp(1i*Arg(z))
  @
  
Complex numbers can be manipulated using standard algebra:  if $z_1 = x_1 + y_1 \imagi$ and $z_2 = x_2 + y_2 \imagi$
\begin{align*}
  z_1 + z_2 & = (x_1 + x_2) + (y_1 + y_2) \imagi \\[6pt]
  z_1 z_2 & = (x_1 x_2 - y_1 y_2) + (x_1 y_2 + x_2 y_1) \imagi = |z_1| |z_2| e^{\imagi (\arg(z_1) + \arg(z_2))}\\[6pt]
  z_1^{-1} & = \frac{x_1}{x_1^2 + y_1^2} - \frac{y_1}{x_1^2 + y_1^2} \imagi = |z_1|^{-1} e^{-\imagi \arg(z_1)}
\end{align*}

\textbf{Polynomials:} Consider the polynomial
\[
A(z) = a_0 + a_1 z + a_2 z^2 + \cdots + a_{p-1} z^{p-1} + a_p z^p 
\]
where $a_0,a_1,\ldots,a_p$ are real values.  Roots of the polynomial are values of $z$ for which $A(z) = 0$.  When discussing the roots, without loss of generality we may set $a_0=1$ or $a_p = 1$.  The usual approach takes $a_p = 1$, in which case we may write
\begin{equation}
A(z) = \prod_{j=1}^p (z - z_j) \label{eq:root1}
\end{equation}
and we term $z_1,z_2,\ldots,z_p$ the roots of the polynomial.  The roots may be 
\begin{itemize}
  \item entirely real, with no imaginary part
  \item complex, in which case the roots must appear in complex conjugate pairs, say
  \[
  z_j = x + y \imagi \qquad z_{j+1} = x - y \imagi = \overline{z}_j.
  \]
\end{itemize}
If instead we take $a_0 = 1$, then the factorization in \eqref{eq:root1} can also be expressed
\begin{equation}
A(z) = \prod_{j=1}^p (1 - r_j z) \label{eq:root2}
\end{equation}
and the values $r_1,\ldots,r_p$ that make $A(z) = 0$ are termed the \textit{reciprocal roots}, as $r_j = 1/z_j$.  In \texttt{R}, the function \texttt{polyroot} can be used to find the $z_j$ and the $r_j$.

\medskip
\textbf{EXAMPLE: }  Suppose $p=3$, and
\[
A(z) = (z-1)(z-2i)(z+2i) = (z-1)(z^2+4) = z^3-z^2+4z-4 = -4 + 4z -z^2 +z^3.
\]
so $(a_0,a_1,a_2,a_3) = (-4,4,-1,1)$.
<<c2>>=
avec<-c(-4,4,-1,1)
#polyroot takes the arguments in increasing order of z^j
polyroot(avec)
round(polyroot(avec),10)     #Roots
round(1/polyroot(avec),10)   #Reciprocal roots
@
The negligible real and imaginary parts arise due to the numerical method of solution.  Note that if $r_1 = 1$, $r_2 = -\imagi/2$ and $r_3 = \imagi/2$
\[
(1-z) (1+(\imagi/2)z) (1-(\imagi/2)z) = (1-z)(1+z^2/4) = (1 - z + z^2/4 - z^3/4) = -\frac{1}{4} (-4 + 4z - z^2 + z^3)
\]

\bigskip

Note, however that an alternative calculation method works to compute the reciprocal roots directly.  Consider, for $z \neq 0$, the polynomial
\[
A^{\ast}(z) = A(1/z) = -4 + \frac{4}{z} - \frac{1}{z^2} + \frac{1}{z^3} = \frac{1}{z^3} (-4 z^3 + 4 z^2 - z + 1).
\]
We have that
\[
A(1/z) = (1/z - z_1) (1/z - z_2) (1/z - z_3) = \frac{1}{z^3} (1-z_1 z)(1-z_2 z)(1-z_3 z)
\]
so the solutions to
\[
A^\ast(z) = A(1/z) = 0
\]
are precisely the reciprocal roots.  Therefore, if we reverse the order of the arguments before passing them into the function, we obtain the reciprocal roots directly from \texttt{polyroot}.
<<c3>>=
avec<-rev(c(-4,4,-1,1))
avec                         #Reverse order coefficients
polyroot(avec)
round(polyroot(avec),10)     #Reciprocal roots
round(1/polyroot(avec),10)   #Roots
@


\end{document}
