{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a4482bdb",
   "metadata": {},
   "source": [
    "$$\n",
    "\\newcommand{\\argmax}{arg\\,max}\n",
    "\\newcommand{\\argmin}{arg\\,min}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac0304d1",
   "metadata": {},
   "source": [
    "\n",
    "<a id='complex-and-trig'></a>\n",
    "\n",
    "<a id='index-0'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63c4a9e3",
   "metadata": {},
   "source": [
    "# Complex Numbers and Trigonometry"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff7eab55",
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
    "This lecture introduces some elementary mathematics and trigonometry.\n",
    "\n",
    "Useful and interesting in its own right, these concepts reap substantial rewards when studying dynamics generated\n",
    "by linear difference equations or linear differential equations.\n",
    "\n",
    "For example, these tools are keys to understanding outcomes attained by Paul\n",
    "Samuelson (1939) [[Samuelson, 1939](https://intro.quantecon.org/zreferences.html#id835)] in his classic paper on interactions\n",
    "between the investment accelerator and the Keynesian consumption function, our\n",
    "topic in the lecture [Samuelson Multiplier Accelerator](https://dynamics.quantecon.org/samuelson.html).\n",
    "\n",
    "In addition to providing foundations for Samuelson’s work and extensions of\n",
    "it, this lecture can be read as a stand-alone quick reminder of key results\n",
    "from elementary high school trigonometry.\n",
    "\n",
    "So let’s dive in."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d68eafa",
   "metadata": {},
   "source": [
    "### Complex Numbers\n",
    "\n",
    "A complex number has a **real part** $ x $ and a purely **imaginary part** $ y $.\n",
    "\n",
    "Here, $ i $ denotes the imaginary unit, satisfying $ i^2 = -1 $.\n",
    "\n",
    "The Euclidean, polar, and trigonometric forms of a complex number $ z $ are:\n",
    "\n",
    "$$\n",
    "z = x + iy = re^{i\\theta} = r(\\cos{\\theta} + i \\sin{\\theta})\n",
    "$$\n",
    "\n",
    "The second equality above is known as **Euler’s formula**\n",
    "\n",
    "- [Euler](https://en.wikipedia.org/wiki/Leonhard_Euler) contributed many other formulas too!  \n",
    "\n",
    "\n",
    "The complex conjugate $ \\bar z $ of $ z $ is defined as\n",
    "\n",
    "$$\n",
    "\\bar z = x - iy = r e^{-i \\theta} = r (\\cos{\\theta} - i \\sin{\\theta} )\n",
    "$$\n",
    "\n",
    "The value $ x $ is the **real** part of $ z $ and $ y $ is the\n",
    "**imaginary** part of $ z $.\n",
    "\n",
    "The symbol $ | z | $ = $ \\sqrt{\\bar{z}\\cdot z} = r $ represents the **modulus** of $ z $.\n",
    "\n",
    "The value $ r $ is the Euclidean distance of vector $ (x,y) $ from the\n",
    "origin:\n",
    "\n",
    "$$\n",
    "r = |z| = \\sqrt{x^2 + y^2}\n",
    "$$\n",
    "\n",
    "The value $ \\theta $ is the angle of $ (x,y) $ with respect to the real axis.\n",
    "\n",
    "Evidently, the tangent of $ \\theta $ is $ \\left(\\frac{y}{x}\\right) $.\n",
    "\n",
    "Therefore,\n",
    "\n",
    "$$\n",
    "\\theta = \\tan^{-1} \\Big( \\frac{y}{x} \\Big)\n",
    "$$\n",
    "\n",
    "Three elementary trigonometric functions are\n",
    "\n",
    "$$\n",
    "\\cos{\\theta} = \\frac{x}{r} = \\frac{e^{i\\theta} + e^{-i\\theta}}{2} , \\quad\n",
    "\\sin{\\theta} = \\frac{y}{r} = \\frac{e^{i\\theta} - e^{-i\\theta}}{2i} , \\quad\n",
    "\\tan{\\theta} = \\frac{y}{x}\n",
    "$$\n",
    "\n",
    "We’ll need the following imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d320078",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams[\"figure.figsize\"] = (11, 5)  #set default figure size\n",
    "import numpy as np\n",
    "from sympy import (Symbol, symbols, Eq, nsolve, sqrt, cos, sin, simplify,\n",
    "                  init_printing, integrate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51c5eb32",
   "metadata": {},
   "source": [
    "### An Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09b9e062",
   "metadata": {},
   "source": [
    "**Example 23.1**\n",
    "\n",
    "Consider the complex number $ z = 1 + \\sqrt{3} i $.\n",
    "\n",
    "For $ z = 1 + \\sqrt{3} i $, $ x = 1 $, $ y = \\sqrt{3} $.\n",
    "\n",
    "It follows that $ r = 2 $ and\n",
    "$ \\theta = \\tan^{-1}(\\sqrt{3}) = \\frac{\\pi}{3} = 60^o $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338ab707",
   "metadata": {},
   "source": [
    "Let’s use Python to plot the trigonometric form of the complex number\n",
    "$ z = 1 + \\sqrt{3} i $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79c53bec",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Abbreviate useful values and functions\n",
    "π = np.pi\n",
    "\n",
    "\n",
    "# Set parameters\n",
    "r = 2\n",
    "θ = π/3\n",
    "x = r * np.cos(θ)\n",
    "x_range = np.linspace(0, x, 1000)\n",
    "θ_range = np.linspace(0, θ, 1000)\n",
    "\n",
    "# Plot\n",
    "fig = plt.figure(figsize=(8, 8))\n",
    "ax = plt.subplot(111, projection='polar')\n",
    "\n",
    "ax.plot((0, θ), (0, r), marker='o', color='b')          # Plot r\n",
    "ax.plot(np.zeros(x_range.shape), x_range, color='b')       # Plot x\n",
    "ax.plot(θ_range, x / np.cos(θ_range), color='b')        # Plot y\n",
    "ax.plot(θ_range, np.full(θ_range.shape, 0.1), color='r')  # Plot θ\n",
    "\n",
    "ax.margins(0) # Let the plot starts at origin\n",
    "\n",
    "ax.set_title(\"Trigonometry of complex numbers\", va='bottom',\n",
    "    fontsize='x-large')\n",
    "\n",
    "ax.set_rmax(2)\n",
    "ax.set_rticks((0.5, 1, 1.5, 2))  # Less radial ticks\n",
    "ax.set_rlabel_position(-88.5)    # Get radial labels away from plotted line\n",
    "\n",
    "ax.text(θ, r+0.01 , r'$z = x + iy = 1 + \\sqrt{3}\\, i$')   # Label z\n",
    "ax.text(θ+0.2, 1 , '$r = 2$')                             # Label r\n",
    "ax.text(0-0.2, 0.5, '$x = 1$')                            # Label x\n",
    "ax.text(0.5, 1.2, r'$y = \\sqrt{3}$')                      # Label y\n",
    "ax.text(0.25, 0.15, r'$\\theta = 60^o$')                   # Label θ\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b169c96",
   "metadata": {},
   "source": [
    "## De Moivre’s Theorem\n",
    "\n",
    "de Moivre’s theorem states that:\n",
    "\n",
    "$$\n",
    "(r(\\cos{\\theta} + i \\sin{\\theta}))^n =\n",
    "r^n e^{in\\theta} =\n",
    "r^n(\\cos{n\\theta} + i \\sin{n\\theta})\n",
    "$$\n",
    "\n",
    "To prove de Moivre’s theorem, note that\n",
    "\n",
    "$$\n",
    "(r(\\cos{\\theta} + i \\sin{\\theta}))^n = \\big( re^{i\\theta} \\big)^n\n",
    "$$\n",
    "\n",
    "and compute."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdce2413",
   "metadata": {},
   "source": [
    "## Applications of de Moivre’s Theorem"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d295234",
   "metadata": {},
   "source": [
    "### Example 1\n",
    "\n",
    "We can use de Moivre’s theorem to show that\n",
    "$ r = \\sqrt{x^2 + y^2} $.\n",
    "\n",
    "We have\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "1 &= e^{i\\theta} e^{-i\\theta} \\\\\n",
    "&= (\\cos{\\theta} + i \\sin{\\theta})(\\cos{(\\text{-}\\theta)} + i \\sin{(\\text{-}\\theta)}) \\\\\n",
    "&= (\\cos{\\theta} + i \\sin{\\theta})(\\cos{\\theta} - i \\sin{\\theta}) \\\\\n",
    "&= \\cos^2{\\theta} + \\sin^2{\\theta} \\\\\n",
    "&= \\frac{x^2}{r^2} + \\frac{y^2}{r^2}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "and thus\n",
    "\n",
    "$$\n",
    "x^2 + y^2 = r^2\n",
    "$$\n",
    "\n",
    "We recognize this as a theorem of **Pythagoras**."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db7b0e25",
   "metadata": {},
   "source": [
    "### Example 2\n",
    "\n",
    "Let $ z = re^{i\\theta} $ and $ \\bar{z} = re^{-i\\theta} $ so that $ \\bar{z} $ is the **complex conjugate** of $ z $.\n",
    "\n",
    "$ (z, \\bar z) $ form a **complex conjugate pair** of complex numbers.\n",
    "\n",
    "Let $ a = pe^{i\\omega} $ and $ \\bar{a} = pe^{-i\\omega} $ be\n",
    "another complex conjugate pair.\n",
    "\n",
    "For each element of a sequence of integers $ n = 0, 1, 2, \\ldots, $.\n",
    "\n",
    "To do so, we can apply de Moivre’s formula.\n",
    "\n",
    "Thus,\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "x_n &= az^n + \\bar{a}\\bar{z}^n \\\\\n",
    "&= p e^{i\\omega} (re^{i\\theta})^n + p e^{-i\\omega} (re^{-i\\theta})^n \\\\\n",
    "&= pr^n e^{i (\\omega + n\\theta)} + pr^n e^{-i (\\omega + n\\theta)} \\\\\n",
    "&= pr^n [\\cos{(\\omega + n\\theta)} + i \\sin{(\\omega + n\\theta)} +\n",
    "         \\cos{(\\omega + n\\theta)} - i \\sin{(\\omega + n\\theta)}] \\\\\n",
    "&= 2 pr^n \\cos{(\\omega + n\\theta)}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "684fd19d",
   "metadata": {},
   "source": [
    "### Example 3\n",
    "\n",
    "This example provides  machinery that is at the heard of Samuelson’s analysis of his multiplier-accelerator model [[Samuelson, 1939](https://intro.quantecon.org/zreferences.html#id835)].\n",
    "\n",
    "Thus, consider a **second-order linear difference equation**\n",
    "\n",
    "$$\n",
    "x_{n+2} = c_1 x_{n+1} + c_2 x_n\n",
    "$$\n",
    "\n",
    "whose **characteristic polynomial** is\n",
    "\n",
    "$$\n",
    "z^2 - c_1 z - c_2 = 0\n",
    "$$\n",
    "\n",
    "or\n",
    "\n",
    "$$\n",
    "(z^2 - c_1 z - c_2 ) = (z - z_1)(z- z_2) = 0\n",
    "$$\n",
    "\n",
    "has roots $ z_1, z_1 $.\n",
    "\n",
    "A **solution**  is a sequence $ \\{x_n\\}_{n=0}^\\infty $ that satisfies\n",
    "the difference equation.\n",
    "\n",
    "Under the following circumstances, we can apply our example 2 formula to\n",
    "solve the difference equation\n",
    "\n",
    "- the roots $ z_1, z_2 $ of the characteristic polynomial of the\n",
    "  difference equation form a complex conjugate pair  \n",
    "- the values $ x_0, x_1 $ are given initial conditions  \n",
    "\n",
    "\n",
    "To solve the difference equation, recall from example 2 that\n",
    "\n",
    "$$\n",
    "x_n = 2 pr^n \\cos{(\\omega + n\\theta)}\n",
    "$$\n",
    "\n",
    "where $ \\omega, p $ are coefficients to be determined from\n",
    "information encoded in the initial conditions $ x_1, x_0 $.\n",
    "\n",
    "Since\n",
    "$ x_0 = 2 p \\cos{\\omega} $ and $ x_1 = 2 pr \\cos{(\\omega + \\theta)} $\n",
    "the ratio of $ x_1 $ to $ x_0 $ is\n",
    "\n",
    "$$\n",
    "\\frac{x_1}{x_0} = \\frac{r \\cos{(\\omega + \\theta)}}{\\cos{\\omega}}\n",
    "$$\n",
    "\n",
    "We can solve this equation for $ \\omega $ then solve for $ p $ using $ x_0 = 2 pr^0 \\cos{(\\omega + n\\theta)} $.\n",
    "\n",
    "With the `sympy` package in Python, we are able to solve and plot the\n",
    "dynamics of $ x_n $ given different values of $ n $.\n",
    "\n",
    "In this example, we set the initial values: - $ r = 0.9 $ -\n",
    "$ \\theta = \\frac{1}{4}\\pi $ - $ x_0 = 4 $ -\n",
    "$ x_1 = r \\cdot 2\\sqrt{2} = 1.8 \\sqrt{2} $.\n",
    "\n",
    "We first numerically solve for $ \\omega $ and $ p $ using\n",
    "`nsolve` in the `sympy` package based on the above initial\n",
    "condition:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8b32354",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Set parameters\n",
    "r = 0.9\n",
    "θ = π/4\n",
    "x0 = 4\n",
    "x1 = 2 * r * sqrt(2)\n",
    "\n",
    "# Define symbols to be calculated\n",
    "ω, p = symbols('ω p', real=True)\n",
    "\n",
    "# Solve for ω\n",
    "## Note: we choose the solution near 0\n",
    "eq1 = Eq(x1/x0 - r * cos(ω+θ) / cos(ω), 0)\n",
    "ω = nsolve(eq1, ω, 0)\n",
    "ω = float(ω)\n",
    "print(f'ω = {ω:1.3f}')\n",
    "\n",
    "# Solve for p\n",
    "eq2 = Eq(x0 - 2 * p * cos(ω), 0)\n",
    "p = nsolve(eq2, p, 0)\n",
    "p = float(p)\n",
    "print(f'p = {p:1.3f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c81e607",
   "metadata": {},
   "source": [
    "Using the code above, we compute that\n",
    "$ \\omega = 0 $ and $ p = 2 $.\n",
    "\n",
    "Then we plug in the values we solve for $ \\omega $ and $ p $\n",
    "and plot the dynamic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09c18d3b",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Define range of n\n",
    "max_n = 30\n",
    "n = np.arange(0, max_n+1, 0.01)\n",
    "\n",
    "# Define x_n\n",
    "x = lambda n: 2 * p * r**n * np.cos(ω + n * θ)\n",
    "\n",
    "# Plot\n",
    "fig, ax = plt.subplots(figsize=(12, 8))\n",
    "\n",
    "ax.plot(n, x(n))\n",
    "ax.set(xlim=(0, max_n), ylim=(-5, 5), xlabel='$n$', ylabel='$x_n$')\n",
    "\n",
    "# Set x-axis in the middle of the plot\n",
    "ax.spines['bottom'].set_position('center')\n",
    "ax.spines['right'].set_color('none')\n",
    "ax.spines['top'].set_color('none')\n",
    "ax.xaxis.set_ticks_position('bottom')\n",
    "ax.yaxis.set_ticks_position('left')\n",
    "\n",
    "ticklab = ax.xaxis.get_ticklabels()[0] # Set x-label position\n",
    "trans = ticklab.get_transform()\n",
    "ax.xaxis.set_label_coords(31, 0, transform=trans)\n",
    "\n",
    "ticklab = ax.yaxis.get_ticklabels()[0] # Set y-label position\n",
    "trans = ticklab.get_transform()\n",
    "ax.yaxis.set_label_coords(0, 5, transform=trans)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6afef5fa",
   "metadata": {},
   "source": [
    "### Trigonometric Identities\n",
    "\n",
    "We can obtain a complete suite of trigonometric identities by\n",
    "appropriately manipulating polar forms of complex numbers.\n",
    "\n",
    "We’ll get many of them by deducing implications of the equality\n",
    "\n",
    "$$\n",
    "e^{i(\\omega + \\theta)} = e^{i\\omega} e^{i\\theta}\n",
    "$$\n",
    "\n",
    "For example, we’ll calculate identities for\n",
    "\n",
    "$ \\cos{(\\omega + \\theta)} $ and $ \\sin{(\\omega + \\theta)} $.\n",
    "\n",
    "Using the sine and cosine formulas presented at the beginning of this\n",
    "lecture, we have:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\cos{(\\omega + \\theta)} = \\frac{e^{i(\\omega + \\theta)} + e^{-i(\\omega + \\theta)}}{2} \\\\\n",
    "\\sin{(\\omega + \\theta)} = \\frac{e^{i(\\omega + \\theta)} - e^{-i(\\omega + \\theta)}}{2i}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "We can also obtain the trigonometric identities as follows:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\cos{(\\omega + \\theta)} + i \\sin{(\\omega + \\theta)}\n",
    "&= e^{i(\\omega + \\theta)} \\\\\n",
    "&= e^{i\\omega} e^{i\\theta} \\\\\n",
    "&= (\\cos{\\omega} + i \\sin{\\omega})(\\cos{\\theta} + i \\sin{\\theta}) \\\\\n",
    "&= (\\cos{\\omega}\\cos{\\theta} - \\sin{\\omega}\\sin{\\theta}) +\n",
    "i (\\cos{\\omega}\\sin{\\theta} + \\sin{\\omega}\\cos{\\theta})\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "Since both real and imaginary parts of the above formula should be\n",
    "equal, we get:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\cos{(\\omega + \\theta)} = \\cos{\\omega}\\cos{\\theta} - \\sin{\\omega}\\sin{\\theta} \\\\\n",
    "\\sin{(\\omega + \\theta)} = \\cos{\\omega}\\sin{\\theta} + \\sin{\\omega}\\cos{\\theta}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "The equations above are also known as the **angle sum identities**. We\n",
    "can verify the equations using the `simplify` function in the\n",
    "`sympy` package:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f161b314",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Define symbols\n",
    "ω, θ = symbols('ω θ', real=True)\n",
    "\n",
    "# Verify\n",
    "print(\"cos(ω)cos(θ) - sin(ω)sin(θ) =\",\n",
    "    simplify(cos(ω)*cos(θ) - sin(ω) * sin(θ)))\n",
    "print(\"cos(ω)sin(θ) + sin(ω)cos(θ) =\",\n",
    "    simplify(cos(ω)*sin(θ) + sin(ω) * cos(θ)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2342681e",
   "metadata": {},
   "source": [
    "### Trigonometric Integrals\n",
    "\n",
    "We can also compute the trigonometric integrals using polar forms of\n",
    "complex numbers.\n",
    "\n",
    "For example, we want to solve the following integral:\n",
    "\n",
    "$$\n",
    "\\int_{-\\pi}^{\\pi} \\cos(\\omega) \\sin(\\omega) \\, d\\omega\n",
    "$$\n",
    "\n",
    "Using Euler’s formula, we have:\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\int \\cos(\\omega) \\sin(\\omega) \\, d\\omega\n",
    "&=\n",
    "\\int\n",
    "\\frac{(e^{i\\omega} + e^{-i\\omega})}{2}\n",
    "\\frac{(e^{i\\omega} - e^{-i\\omega})}{2i}\n",
    "\\, d\\omega  \\\\\n",
    "&=\n",
    "\\frac{1}{4i}\n",
    "\\int\n",
    "e^{2i\\omega} - e^{-2i\\omega}\n",
    "\\, d\\omega  \\\\\n",
    "&=\n",
    "\\frac{1}{4i}\n",
    "\\bigg( \\frac{-i}{2} e^{2i\\omega} - \\frac{i}{2} e^{-2i\\omega} + C_1 \\bigg) \\\\\n",
    "&=\n",
    "-\\frac{1}{8}\n",
    "\\bigg[ \\bigg(e^{i\\omega}\\bigg)^2 + \\bigg(e^{-i\\omega}\\bigg)^2 - 2 \\bigg] + C_2 \\\\\n",
    "&=\n",
    "-\\frac{1}{8}  (e^{i\\omega} - e^{-i\\omega})^2  + C_2 \\\\\n",
    "&=\n",
    "\\frac{1}{2} \\bigg( \\frac{e^{i\\omega} - e^{-i\\omega}}{2i} \\bigg)^2 + C_2 \\\\\n",
    "&= \\frac{1}{2} \\sin^2(\\omega) + C_2\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "and thus:\n",
    "\n",
    "$$\n",
    "\\int_{-\\pi}^{\\pi} \\cos(\\omega) \\sin(\\omega) \\, d\\omega =\n",
    "\\frac{1}{2}\\sin^2(\\pi) - \\frac{1}{2}\\sin^2(-\\pi) = 0\n",
    "$$\n",
    "\n",
    "We can verify the analytical as well as numerical results using\n",
    "`integrate` in the `sympy` package:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5935fa3f",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Set initial printing\n",
    "init_printing(use_latex=\"mathjax\")\n",
    "\n",
    "ω = Symbol('ω')\n",
    "print('The analytical solution for integral of cos(ω)sin(ω) is:')\n",
    "integrate(cos(ω) * sin(ω), ω)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "372ebbcd",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "print('The numerical solution for the integral of cos(ω)sin(ω) \\\n",
    "from -π to π is:')\n",
    "integrate(cos(ω) * sin(ω), (ω, -π, π))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e5aca39",
   "metadata": {},
   "source": [
    "### Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1044023a",
   "metadata": {},
   "source": [
    "### Exercise 23.1\n",
    "\n",
    "We invite the reader to verify analytically and with the `sympy` package the following two equalities:\n",
    "\n",
    "$$\n",
    "\\int_{-\\pi}^{\\pi} \\cos (\\omega)^2 \\, d\\omega = \\pi\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\int_{-\\pi}^{\\pi} \\sin (\\omega)^2 \\, d\\omega = \\pi\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad3f11f8",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "Let’s import symbolic $ \\pi $ from `sympy`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11540d7d",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Import symbolic π from sympy\n",
    "from sympy import pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6efa2959",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "print('The analytical solution for the integral of cos(ω)**2 \\\n",
    "from -π to π is:')\n",
    "\n",
    "integrate(cos(ω)**2, (ω, -pi, pi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f7eb4ef",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "print('The analytical solution for the integral of sin(ω)**2 \\\n",
    "from -π to π is:')\n",
    "\n",
    "integrate(sin(ω)**2, (ω, -pi, pi))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0456c7b",
   "metadata": {},
   "source": [
    "### Exercise 23.2\n",
    "\n",
    "**Double-angle identities via De Moivre’s theorem.**\n",
    "\n",
    "Apply de Moivre’s theorem with $ n = 2 $:\n",
    "\n",
    "$$\n",
    "(\\cos\\theta + i\\sin\\theta)^2 = \\cos 2\\theta + i\\sin 2\\theta\n",
    "$$\n",
    "\n",
    "Expand the left-hand side as a complex square and equate real and imaginary\n",
    "parts to deduce the two **double-angle identities**\n",
    "\n",
    "$$\n",
    "\\cos 2\\theta = \\cos^2\\theta - \\sin^2\\theta, \\qquad\n",
    "\\sin 2\\theta = 2\\sin\\theta\\cos\\theta.\n",
    "$$\n",
    "\n",
    "Then use the Pythagorean identity $ \\cos^2\\theta + \\sin^2\\theta = 1 $ to write\n",
    "two alternative forms of the cosine identity:\n",
    "\n",
    "$$\n",
    "\\cos 2\\theta = 2\\cos^2\\theta - 1 = 1 - 2\\sin^2\\theta.\n",
    "$$\n",
    "\n",
    "Verify all four identities using `simplify` from `sympy`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "956aed71",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "De Moivre with $ n = 2 $ gives\n",
    "$ (\\cos\\theta + i\\sin\\theta)^2 = \\cos 2\\theta + i\\sin 2\\theta $.\n",
    "Expanding the left side:\n",
    "\n",
    "$$\n",
    "\\cos^2\\theta - \\sin^2\\theta \\;+\\; i\\,(2\\sin\\theta\\cos\\theta)\n",
    "= \\cos 2\\theta + i\\sin 2\\theta.\n",
    "$$\n",
    "\n",
    "Matching real parts gives $ \\cos 2\\theta = \\cos^2\\theta - \\sin^2\\theta $.\n",
    "\n",
    "Matching imaginary parts gives $ \\sin 2\\theta = 2\\sin\\theta\\cos\\theta $.\n",
    "\n",
    "Substituting $ \\sin^2\\theta = 1 - \\cos^2\\theta $ into the cosine formula gives\n",
    "$ \\cos 2\\theta = 2\\cos^2\\theta - 1 $, and substituting $ \\cos^2\\theta = 1 - \\sin^2\\theta $\n",
    "gives $ \\cos 2\\theta = 1 - 2\\sin^2\\theta $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "614ffe13",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "from sympy import Symbol, cos, sin, simplify\n",
    "\n",
    "θ = Symbol('θ', real=True)\n",
    "\n",
    "print(\"cos(2θ) = cos(θ)**2 - sin(θ)**2:\",\n",
    "      simplify(cos(2*θ) - (cos(θ)**2 - sin(θ)**2)))\n",
    "\n",
    "print(\"sin(2θ) = 2sinθcosθ:\",\n",
    "      simplify(sin(2*θ) - 2*sin(θ)*cos(θ)))\n",
    "\n",
    "print(\"cos(2θ) = 2*cos(θ)**2 - 1:\",\n",
    "      simplify(cos(2*θ) - (2*cos(θ)**2 - 1)))\n",
    "\n",
    "print(\"cos(2θ) = 1 - 2*sin(θ)**2:\",\n",
    "      simplify(cos(2*θ) - (1 - 2*sin(θ)**2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e847307",
   "metadata": {},
   "source": [
    "Each `simplify` call returns 0, confirming all four identities."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1ba93b8",
   "metadata": {},
   "source": [
    "### Exercise 23.3\n",
    "\n",
    "**Product-to-sum formulas by “adding appropriate pairs”.**\n",
    "\n",
    "The angle-sum identities derived in the lecture are:\n",
    "\n",
    "\n",
    "<a id='equation-ct-cos-sum'></a>\n",
    "$$\n",
    "\\cos(\\theta + w) = \\cos\\theta\\cos w - \\sin\\theta\\sin w \\tag{23.1}\n",
    "$$\n",
    "\n",
    "\n",
    "<a id='equation-ct-cos-diff'></a>\n",
    "$$\n",
    "\\cos(\\theta - w) = \\cos\\theta\\cos w + \\sin\\theta\\sin w \\tag{23.2}\n",
    "$$\n",
    "\n",
    "\n",
    "<a id='equation-ct-sin-sum'></a>\n",
    "$$\n",
    "\\sin(\\theta + w) = \\sin\\theta\\cos w + \\cos\\theta\\sin w \\tag{23.3}\n",
    "$$\n",
    "\n",
    "\n",
    "<a id='equation-ct-sin-diff'></a>\n",
    "$$\n",
    "\\sin(\\theta - w) = \\sin\\theta\\cos w - \\cos\\theta\\sin w \\tag{23.4}\n",
    "$$\n",
    "\n",
    "By adding and subtracting appropriate pairs of equations [(23.1)](#equation-ct-cos-sum)–[(23.4)](#equation-ct-sin-diff), derive the three **product-to-sum formulas**:\n",
    "\n",
    "$$\n",
    "\\cos\\theta\\cos w = \\frac{\\cos(\\theta+w) + \\cos(\\theta-w)}{2}\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\sin\\theta\\sin w = \\frac{\\cos(\\theta-w) - \\cos(\\theta+w)}{2}\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\sin\\theta\\cos w = \\frac{\\sin(\\theta+w) + \\sin(\\theta-w)}{2}\n",
    "$$\n",
    "\n",
    "Verify all three with `simplify` from `sympy`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7526684c",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "Adding (i) and (ii) gives $ \\cos(\\theta+w) + \\cos(\\theta-w) = 2\\cos\\theta\\cos w $.\n",
    "\n",
    "Subtracting (i) from (ii) gives $ \\cos(\\theta-w) - \\cos(\\theta+w) = 2\\sin\\theta\\sin w $.\n",
    "\n",
    "Adding (iii) and (iv) gives $ \\sin(\\theta+w) + \\sin(\\theta-w) = 2\\sin\\theta\\cos w $.\n",
    "\n",
    "Dividing each result by 2 yields the three product-to-sum formulas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5174081",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "from sympy import symbols, cos, sin, simplify\n",
    "\n",
    "θ, w = symbols('θ w', real=True)\n",
    "\n",
    "print(\"cos(θ+w) + cos(θ-w) - 2cos(θ)cos(w) =\",\n",
    "      simplify(cos(θ+w) + cos(θ-w) - 2*cos(θ)*cos(w)))\n",
    "\n",
    "print(\"cos(θ-w) - cos(θ+w) - 2sin(θ)sin(w) =\",\n",
    "      simplify(cos(θ-w) - cos(θ+w) - 2*sin(θ)*sin(w)))\n",
    "\n",
    "print(\"sin(θ+w) + sin(θ-w) - 2sin(θ)cos(w) =\",\n",
    "      simplify(sin(θ+w) + sin(θ-w) - 2*sin(θ)*cos(w)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54055ac2",
   "metadata": {},
   "source": [
    "All three expressions simplify to 0."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c7292bf",
   "metadata": {},
   "source": [
    "### Exercise 23.4\n",
    "\n",
    "**Orthogonality of cosines.**\n",
    "\n",
    "Apply the product-to-sum formula from Exercise 23.3 with $ \\theta = m\\phi $\n",
    "and $ w = n\\phi $:\n",
    "\n",
    "$$\n",
    "\\cos(m\\phi)\\cos(n\\phi) = \\frac{\\cos((m-n)\\phi) + \\cos((m+n)\\phi)}{2}.\n",
    "$$\n",
    "\n",
    "Use this identity and the fact that $ \\int_{-\\pi}^{\\pi} \\cos(k\\phi)\\,d\\phi = 0 $\n",
    "for every non-zero integer $ k $ to prove that for positive integers $ m $ and $ n $\n",
    "\n",
    "$$\n",
    "\\int_{-\\pi}^{\\pi} \\cos(m\\phi)\\cos(n\\phi)\\,d\\phi =\n",
    "\\begin{cases} \\pi & \\text{if } m = n \\\\ 0 & \\text{if } m \\neq n. \\end{cases}\n",
    "$$\n",
    "\n",
    "Verify this **orthogonality table** numerically with `integrate` from `sympy`\n",
    "for $ m, n \\in \\{1, 2, 3\\} $."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a76b971",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "**Case $ m \\neq n $:** Both $ m - n $ and $ m + n $ are non-zero integers, so\n",
    "$ \\int_{-\\pi}^{\\pi} \\cos((m-n)\\phi)\\,d\\phi = \\int_{-\\pi}^{\\pi} \\cos((m+n)\\phi)\\,d\\phi = 0 $,\n",
    "giving a total of 0.\n",
    "\n",
    "**Case $ m = n $:** The formula becomes\n",
    "$ \\cos(m\\phi)^2 = \\tfrac{1}{2}[1 + \\cos(2m\\phi)] $.\n",
    "\n",
    "Since $ \\int_{-\\pi}^{\\pi} \\cos(2m\\phi)\\,d\\phi = 0 $ for non-zero integer $ m $,\n",
    "the integral equals $ \\tfrac{1}{2} \\cdot 2\\pi = \\pi $."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f072790a",
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "from sympy import Symbol, cos, integrate, pi\n",
    "\n",
    "ϕ = Symbol('ϕ', real=True)\n",
    "\n",
    "print(f\"{'m':>3}  {'n':>3}  {'integral':>10}\")\n",
    "print('-' * 22)\n",
    "for m in [1, 2, 3]:\n",
    "    for n in [1, 2, 3]:\n",
    "        val = integrate(cos(m*ϕ) * cos(n*ϕ), (ϕ, -pi, pi))\n",
    "        print(f\"{m:>3}  {n:>3}  {str(val):>10}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbfbb919",
   "metadata": {},
   "source": [
    "The table confirms the rule that diagonal entries, where $ m = n $, all equal $ \\pi $ and every off-diagonal entry equals $ 0 $.\n",
    "\n",
    "This orthogonality property is the foundation of Fourier series because sinusoidal\n",
    "functions at different frequencies do not “interfere” when we decompose a signal into its frequency components."
   ]
  }
 ],
 "metadata": {
  "date": 1795846195.5063496,
  "filename": "complex_and_trig.md",
  "kernelspec": {
   "display_name": "Python",
   "language": "python3",
   "name": "python3"
  },
  "title": "Complex Numbers and Trigonometry"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}