{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../../data/'\n",
    "path_images = '../../images/'\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# More on Arrays\n",
    "It's often necessary to compute something that involves data from more than one array. If two arrays are of the same size, Python makes it easy to do calculations involving both arrays.\n",
    "\n",
    "For our first example, we return once more to the temperature data.  This time, we create arrays of average daily [high](http://berkeleyearth.lbl.gov/auto/Regional/TMAX/Text/global-land-TMAX-Trend.txt) and [low](http://berkeleyearth.lbl.gov/auto/Regional/TMIN/Text/global-land-TMIN-Trend.txt) temperatures for the decades surrounding 1850, 1900, 1950, and 2000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13.6  , 14.387, 14.585, 15.164])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baseline_high = 14.48\n",
    "highs = np.array([baseline_high - 0.880, \n",
    "                   baseline_high - 0.093,\n",
    "                   baseline_high + 0.105, \n",
    "                   baseline_high + 0.684])\n",
    "highs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.128, 2.371, 2.874, 3.728])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baseline_low = 3.00\n",
    "lows = np.array([baseline_low - 0.872, baseline_low - 0.629,\n",
    "                  baseline_low - 0.126, baseline_low + 0.728])\n",
    "lows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suppose we'd like to compute the average daily *range* of temperatures for each decade.  That is, we want to subtract the average daily high in the 1850s from the average daily low in the 1850s, and the same for each other decade.\n",
    "\n",
    "We could write this laboriously using `.item`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11.472, 12.016, 11.711, 11.436])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# note - though the '.item()' can be used with numpy but not with pandas\n",
    "# you can mix lines of pandas code with numpy code\n",
    "\n",
    "np.array(\n",
    "    [highs.item(0) - lows.item(0),\n",
    "    highs.item(1) - lows.item(1),\n",
    "    highs.item(2) - lows.item(2),\n",
    "    highs.item(3) - lows.item(3)]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As when we converted an array of temperatures from Celsius to Fahrenheit, Python provides a much cleaner way to write this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11.472, 12.016, 11.711, 11.436])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "highs - lows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Array Subtraction](../../images/array_subtraction.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What we've seen in these examples are special cases of a general feature of arrays."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Elementwise arithmetic on pairs of numerical arrays\n",
    "If an arithmetic operator acts on two arrays of the same size, then the operation is performed on each corresponding pair of elements in the two arrays. The final result is an array. \n",
    "\n",
    "For example, if `array1` and `array2` have the same number of elements, then the value of `array1 * array2` is an array. Its first element is the first element of `array1` times the first element of `array2`, its second element is the second element of `array1` times the second element of `array2`, and so on."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: Wallis' Formula for $\\pi$ \n",
    "The number $\\pi$ is important in many different areas of math. Centuries before computers were invented, mathematicians worked on finding simple ways to approximate the numerical value of $\\pi$. We have already seen Leibniz's formula for $\\pi$. About half a century before Leibniz, the English mathematician [John Wallis](https://en.wikipedia.org/wiki/John_Wallis) (1616-1703) also expressed $\\pi$ in terms of simple fractions, as an infinite product.\n",
    "\n",
    "$$\n",
    "\\pi = 2 \\cdot \\left( \\frac{2}{1}\\cdot\\frac{2}{3}\\cdot\\frac{4}{3}\\cdot\\frac{4}{5}\\cdot\\frac{6}{5}\\cdot\\frac{6}{7}\\dots \\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is a product of \"even/odd\" fractions. Let's use arrays to multiply a million of them, and see if the product is close to $\\pi$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember that multiplication can done in any order [[1]](#footnotes), so we can readjust our calculation to:\n",
    "\n",
    "$$\\pi \\approx 2 \\cdot \\left( \\frac{2}{1} \\cdot \\frac{4}{3} \\cdot \\frac{6}{5} \\cdots \\frac{1,000,000}{999999} \\right) \\cdot \\left( \\frac{2}{3} \\cdot \\frac{4}{5} \\cdot \\frac{6}{7} \\cdots \\frac{1,000,000}{1,000,001} \\right)$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We're now ready to do the calculation. We start by creating an array of even numbers 2, 4, 6, and so on upto 1,000,000. Then we create two lists of odd numbers: 1, 3, 5, 7, ... upto 999,999, and 3, 5, 7, ... upto 1,000,001."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "even = np.arange(2, 1000001, 2)\n",
    "one_below_even = even - 1\n",
    "one_above_even = even + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember that `np.prod` multiplies all the elements of an array together. Now we can calculate Wallis' product, to a good approximation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.1415910827951143"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * np.prod(even/one_below_even) * np.prod(even/one_above_even)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's $\\pi$ correct to five decimal places.  Wallis clearly came up with a great formula."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id='footnotes'></a>\n",
    "### Footnotes\n",
    "[1] As we saw in the example about Leibniz's formula, when we add  *infinitely* many fractions, the order can matter. The same is true with multiplying fractions, as we are doing here. But our approximation to $\\pi$ uses only a large finite number of fractions, so it's okay to multiply the terms in any convenient order."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
