{
 "metadata":{},
 "nbformat":4,
 "nbformat_minor":4,
 "cells":[
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- jupyter-book 04_testdoc.ipynb -->\n",
    "# Exercises\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Problem 2: Flip a Coin\n",
    "<div id=\"demo:ex:1\"></div>\n",
    "<!-- keywords = random numbers; Monte Carlo simulation; ipynb -->\n",
    "\n",
    "<!-- Torture tests -->\n",
    "\n",
    "**a)**\n",
    "Make a program that simulates flipping a coin $N$ times.\n",
    "Print out \"tail\" or \"head\" for each flip and\n",
    "let the program count the number of heads.\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint 1.**\n",
    "Use `r = random.random()` and define head as `r <= 0.5`.\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint 2.**\n",
    "Draw an integer among $\\{1,2\\}$ with\n",
    "`r = random.randint(1,2)` and define head when `r` is 1.\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "\n",
    "<!-- --- begin answer of exercise --- -->\n",
    "**Answer.**\n",
    "If the `random.random()` function returns a number $<1/2$, let it be\n",
    "head, otherwise tail. Repeat this $N$ number of times.\n",
    "<!-- --- end answer of exercise --- -->\n",
    "\n",
    "<!-- --- begin solution of exercise --- -->\n",
    "**Solution.**"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":9,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "import sys, random\n",
    "N = int(sys.argv[1])\n",
    "heads = 0\n",
    "for i in range(N):\n",
    "    r = random.random()\n",
    "    if r <= 0.5:\n",
    "        heads += 1\n",
    "print('Flipping a coin %d times gave %d heads' % (N, heads))"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- --- end solution of exercise --- -->\n",
    "\n",
    "**b)**\n",
    "Vectorize the code in a) using boolean indexing.\n",
    "\n",
    "Vectorized code can be written in many ways.\n",
    "Sometimes the code is less intuitive, sometimes not.\n",
    "At least there is not much to find in the section [Section 1](02_testdoc.html#sec1).\n",
    "\n",
    "**c)**\n",
    "Vectorize the code in a) using `numpy.sum`.\n",
    "\n",
    "<!-- --- begin answer of exercise --- -->\n",
    "**Answer.**\n",
    "`np.sum(np.where(r <= 0.5, 1, 0))` or `np.sum(r <= 0.5)`.\n",
    "<!-- --- end answer of exercise --- -->\n",
    "\n",
    "In this latter subexercise, we have an\n",
    "example where the code is easy to read.\n",
    "\n",
    "### My remarks\n",
    "\n",
    "Remarks with such a subsubsection is treated as more text\n",
    "after the last subexercise. Test a list too:\n",
    "\n",
    "1. Mark 1.\n",
    "\n",
    "2. Mark 2.\n",
    "\n",
    "Filenames: `flip_coin.py`, `flip_coin.pdf`.\n",
    "\n",
    "<!-- Closing remarks for this Problem -->\n",
    "\n",
    "### Remarks\n",
    "\n",
    "These are the exercise remarks, appearing at the very end.\n",
    "\n",
    "<!-- solution files: mysol.txt, mysol_flip_coin.py, yet_another.file -->\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "## Not an exercise\n",
    "\n",
    "Should be possible to stick a normal section in the middle of many\n",
    "exercises.\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Exercise 3: Test of plain text exercise\n",
    "<div id=\"my:exer1\"></div>\n",
    "\n",
    "Very short exercise. What is the capital\n",
    "of Norway?\n",
    "Filename: `myexer1`.\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Project 4: Compute a Probability\n",
    "<div id=\"demo:ex:2\"></div>\n",
    "\n",
    "<!-- Minimalistic exercise -->\n",
    "\n",
    "What is the probability of getting a number between 0.5 and 0.6 when\n",
    "drawing uniformly distributed random numbers from the interval $[0,1)$?\n",
    "\n",
    "At the end we have a list because that caused problems in LaTeX\n",
    "in previous DocOnce versions:\n",
    "\n",
    "1. item1\n",
    "\n",
    "2. item2\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint.**\n",
    "To answer this question empirically, let a program\n",
    "draw $N$ such random numbers using Python's standard `random` module,\n",
    "count how many of them, $M$, that fall in the interval $(0.5,0.6)$, and\n",
    "compute the probability as $M/N$.\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Project 5: Explore Distributions of Random Circles\n",
    "<div id=\"proj:circle1\"></div>\n",
    "<!-- keywords = ipynb -->\n",
    "\n",
    "The formula for a circle is given by"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- Equation labels as ordinary links -->\n",
    "<div id=\"circle:x\"></div>\n",
    "\n",
    "$$\n",
    "\\begin{equation}\n",
    "x = x_0 + R\\cos 2\\pi t,\n",
    "\\label{circle:x} \\tag{11}\n",
    "\\end{equation}\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- Equation labels as ordinary links -->\n",
    "<div id=\"circle:y\"></div>\n",
    "\n",
    "$$\n",
    "\\begin{equation}  \n",
    "y = y_0 + R\\sin 2\\pi t,\n",
    "\\label{circle:y} \\tag{12}\n",
    "\\end{equation}\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "where $R$ is the radius of the circle, $(x_0,y_0)$ is the\n",
    "center point, and $t$ is a parameter in the unit interval $[0,1]$.\n",
    "For any $t$, $(x,y)$ computed from ([11](04_testdoc.html#circle:x))-([12](04_testdoc.html#circle:y))\n",
    "is a point on the circle.\n",
    "The formula can be used to generate `n` points on a circle:"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":10,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "import numpy as np\n",
    "\n",
    "def circle(R, x0, y0, n=501):\n",
    "    t = np.linspace(0, 1, n)\n",
    "    x = x0 + R*np.cos(2*np.pi*t)\n",
    "    y = y0 + R*np.sin(2*np.pi*t)\n",
    "    return x, y\n",
    "\n",
    "x, y = circle(2.0, 0, 0)"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- Often in an exercise we have some comments about the solution -->\n",
    "<!-- which we normally want to keep where they are. -->\n",
    "\n",
    "The goal of this project is to draw $N$ circles with random\n",
    "center and radius. Plot each circle using the `circle` function\n",
    "above.\n",
    "\n",
    "**a)**\n",
    "Let $R$ be normally distributed and $(x_0,y_0)$ uniformly distributed.\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint.**\n",
    "Use the `numpy.random` module to draw the\n",
    "$x_0$, $y_0$, and $R$ quantities.\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "\n",
    "<!-- --- begin answer of exercise --- -->\n",
    "**Answer.**\n",
    "Here goes the short answer to part a).\n",
    "<!-- --- end answer of exercise --- -->\n",
    "\n",
    "<!-- --- begin solution of exercise --- -->\n",
    "**Solution.**\n",
    "Here goes a full solution to part a).\n",
    "<!-- --- end solution of exercise --- -->\n",
    "\n",
    "**b)**\n",
    "Let $R$ be uniformly distributed and $(x_0,y_0)$ normally distributed.\n",
    "Filename: `norm`.\n",
    "\n",
    "**c)**\n",
    "Let $R$ and $(x_0,y_0)$ be normally distributed.\n",
    "\n",
    "Filename: `circles`.\n",
    "\n",
    "<!-- Closing remarks for this Project -->\n",
    "\n",
    "### Remarks\n",
    "\n",
    "At the very end of the exercise it may be appropriate to summarize\n",
    "and give some perspectives.\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Exercise 6: Determine some Distance\n",
    "<div id=\"exer:dist\"></div>\n",
    "\n",
    "Intro to this exercise. Questions are in subexercises below.\n",
    "\n",
    "<!-- --- begin solution of exercise --- -->\n",
    "**Solution.**\n",
    "Here goes a full solution of the whole exercise.\n",
    "With some math $a=b$ in this solution:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "$$\n",
    "\\hbox{math in solution: } a = b\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "And code `a=b` in this solution:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        a = b  # code in solution\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "End of solution is here.\n",
    "\n",
    "<!-- --- end solution of exercise --- -->\n",
    "\n",
    "**a)**\n",
    "Subexercises are numbered a), b), etc.\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint 1.**\n",
    "First hint to subexercise a).\n",
    "With math $a=b$ in hint:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "$$\n",
    "a=b.\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "And with code (in plain verbatim) returning $x+1$ in hint:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        def func(x):\n",
    "            return x + 1  # with code in hint\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- --- end hint in exercise --- -->\n",
    "\n",
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint 2.**\n",
    "Second hint to subexercise a).\n",
    "\n",
    "Test list in hint:\n",
    "\n",
    "1. item1\n",
    "\n",
    "2. item2\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "Filename: `subexer_a.pdf`.\n",
    "\n",
    "<!-- --- begin answer of exercise --- -->\n",
    "**Answer.**\n",
    "Short answer to subexercise a).\n",
    "With math in answer: $a=b$.\n",
    "<!-- --- end answer of exercise --- -->\n",
    "\n",
    "**b)**\n",
    "Here goes the text for subexercise b).\n",
    "\n",
    "Some math $\\cos^2 x + \\sin^2 x = 1$ written one a single line:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "$$\n",
    "\\cos^2 x + \\sin^2 x = 1 \\thinspace .\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- --- begin hint in exercise --- -->\n",
    "\n",
    "**Hint.**\n",
    "A hint for this subexercise.\n",
    "\n",
    "<!-- --- end hint in exercise --- -->\n",
    "Filename: `subexer_b.pdf`.\n",
    "\n",
    "<!-- --- begin solution of exercise --- -->\n",
    "**Solution.**\n",
    "Here goes the solution of this subexercise.\n",
    "<!-- --- end solution of exercise --- -->\n",
    "\n",
    "<!-- No meaning in this weired test example: -->\n",
    "The text here belongs to the main (intro) part of the exercise. Need\n",
    "closing remarks to have text after subexercises.\n",
    "\n",
    "Test list in exercise:\n",
    "\n",
    "1. item1\n",
    "\n",
    "2. item2\n",
    "<!-- Closing remarks for this Exercise -->\n",
    "\n",
    "### Remarks\n",
    "\n",
    "Some final closing remarks, e.g., summarizing the main findings\n",
    "and their implications in other problems can be made. These\n",
    "remarks will appear at the end of the typeset exercise.\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Some exercise without the \"Exercise:\" prefix\n",
    "\n",
    "<!-- Another minimalistic exercise -->\n",
    "\n",
    "Just some text. And some math saying that $e^0=1$ on a single line,\n",
    "to test that math block insertion is correct:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "$$\n",
    "\\exp{(0)} = 1\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "And a test that the code `lambda x: x+2` is correctly placed here:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        lambda x: x+2\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- Have some comments at the end of the exercise to see that -->\n",
    "<!-- the Filename: ... is written correctly. -->\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Exercise 8: Solution of differential equation\n",
    "<div id=\"sec:this:exer:de\"></div>\n",
    "\n",
    "\n",
    "\n",
    "Given"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "$$\n",
    "\\frac{dy}{dx} = -y(x),\\quad y(0)=1\n",
    "$$"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "What is the solution of this equation?\n",
    "\n",
    "**Choice A:**\n",
    " $y=e^{-y}$\n",
    "\n",
    "**Choice B:**\n",
    " $y=e^{y}$\n",
    "\n",
    "**Choice C:**"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "from math import exp\n",
    "def f(x):\n",
    "    return exp(x)\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "**Choice D:**\n",
    " The solution cannot be found because there is a derivative in the equation.\n",
    "\n",
    "**Choice E:**\n",
    " The equation is meaningless: an equation must be an equation\n",
    "for $x$ or $y$, not a function $y(x)$.\n",
    "\n",
    "\n",
    "\n",
    "<!-- --- end exercise --- -->\n",
    "\n",
    "<!-- --- begin exercise --- -->\n",
    "\n",
    "## Example 9: Just an example\n",
    "\n",
    "<!-- This example needs the --examples_as_exercises option, otherwise -->\n",
    "<!-- it is just typeset as it is written. -->\n",
    "\n",
    "**a)**\n",
    "What is the capital of Norway?\n",
    "\n",
    "**Answer.**\n",
    "Oslo.\n",
    "\n",
    "<!-- --- end exercise --- -->"
   ]
  }
 ]
}