{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Math module</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miscellaneous  functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import isfinite, isinf, isnan\n",
    "\n",
    "print(isfinite(4.57), isfinite(float('inf')), isfinite(float('Nan')))\n",
    "print(isinf(4.57), isinf(float('inf')), isinf(float('Nan')))\n",
    "print(isnan(4.57), isinf(float('inf')), isnan(float('Nan')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import ceil, floor, trunc, fabs\n",
    "\n",
    "print(ceil(-5.0), ceil(-4.5), ceil(4.5), ceil(5))\n",
    "print(floor(-5.0), floor(-4.5), floor(4.5), floor(5))\n",
    "print(trunc(-4.4), trunc(-4.6), trunc(4.4), trunc(4.6))\n",
    "print()\n",
    "\n",
    "print(fabs(-3.8), fabs(-2), fabs(3.8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import modf, fmod, frexp, ldexp, copysign\n",
    "\n",
    "print(modf(-6.4))\n",
    "print(modf(6.4))\n",
    "print()\n",
    "\n",
    "print(fmod(-6.4, -2.1))\n",
    "print(fmod(-6.4, 2.1))\n",
    "print(fmod(6.4, -2.1))\n",
    "print(fmod(6.4, 2.1))\n",
    "print()\n",
    "\n",
    "# Write nonzero x as +/-0.1b_2b_3b_4... * 2^e\n",
    "# with b_2, b_3, b_3... being 0 or 1 and not cofinitely many of them being 1.\n",
    "# Then returns +/-0.1b_2b_3b_4..., so a number m with 0.5 <= |m| < 1, and e.\n",
    "print(frexp(-600.4))\n",
    "print(frexp(-0.006004))\n",
    "print(frexp(-0.5))\n",
    "print(frexp(0))\n",
    "print(frexp(0.5))\n",
    "print(frexp(0.006004))\n",
    "print(frexp(600.4))\n",
    "print()\n",
    "\n",
    "print(ldexp(4.5, -2))\n",
    "print(ldexp(4.5, -1))\n",
    "print(ldexp(4.5, 0))\n",
    "print(ldexp(4.5, 1))\n",
    "print(ldexp(4.5, 2))\n",
    "print()\n",
    "\n",
    "print(copysign(-4.5, -3.14))\n",
    "print(copysign(-4.5, 0))\n",
    "print(copysign(-4.5, 3.14))\n",
    "print(copysign(4.5, -3.14))\n",
    "print(copysign(4.5, 0))\n",
    "print(copysign(4.5, 3.14))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import fsum, hypot, factorial\n",
    "\n",
    "print(fsum([-2.1, -1.4, 0, 1.4, 2.1]))\n",
    "print(fsum((-2.2, -1.3, 0, 1.4, 2.5)))\n",
    "print(fsum({-2.3, -1.2, 0, 1.5, 2.4}))\n",
    "print(fsum({1.5: 'A', 2: 'B', 3.5: 'C'}))\n",
    "print()\n",
    "\n",
    "print(hypot(0, -4))\n",
    "print(hypot(3, 4))\n",
    "print()\n",
    "\n",
    "print(factorial(0))\n",
    "print(factorial(1))\n",
    "print(factorial(2))\n",
    "print(factorial(3))\n",
    "print(factorial(4))\n",
    "print(factorial(1000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trigonometric functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from math import pi, degrees, radians\n",
    "\n",
    "print(degrees(- 10 * pi))\n",
    "print(degrees(- pi / 3))\n",
    "print(degrees(pi / 3))\n",
    "print(degrees(10 * pi))\n",
    "print()\n",
    "\n",
    "print(radians(-1800))\n",
    "print(radians(-60))\n",
    "print(radians(60))\n",
    "print(radians(1800))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import xlim, plot\n",
    "\n",
    "from math import pi, cos\n",
    "\n",
    "min_x = -2 * pi\n",
    "max_x = 2 * pi\n",
    "xlim(min_x, max_x)\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [cos(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import acos\n",
    "\n",
    "min_x = -1\n",
    "max_x = 1\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [acos(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import xlim, plot\n",
    "\n",
    "from math import pi, sin\n",
    "\n",
    "min_x = -2 * pi\n",
    "max_x = 2 * pi\n",
    "xlim(min_x, max_x)\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [sin(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import asin\n",
    "\n",
    "min_x = -1\n",
    "max_x = 1\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [asin(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import xlim, plot\n",
    "\n",
    "from math import pi, tan\n",
    "\n",
    "min_x = - pi / 2 + 0.01\n",
    "max_x = pi / 2 - 0.01\n",
    "xlim(min_x, max_x)\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [tan(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import atan\n",
    "\n",
    "min_x = - 20\n",
    "max_x = 20\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [atan(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib notebook\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "from matplotlib.pyplot import figure\n",
    "\n",
    "from math import atan2\n",
    "\n",
    "min_x = -5\n",
    "max_x = 5\n",
    "nb_of_points_x = 70\n",
    "min_y = -5\n",
    "max_y = 5\n",
    "nb_of_points_y = 70\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points_x * i for i in range(nb_of_points_x + 1)]\n",
    "xs.remove(0)\n",
    "xs = [x for x in xs for i in range(nb_of_points_y)]\n",
    "ys = [min_x + (max_y - min_y) / nb_of_points_y * i for i in range(nb_of_points_y + 1)]\n",
    "ys.remove(0)\n",
    "ys = ys * nb_of_points_x\n",
    "zs = [atan2(y, x) for (x, y) in zip(xs, ys)]\n",
    "# s is for the size of the points, whose default is 20\n",
    "figure().add_subplot(111, projection='3d').scatter(xs, ys, zs, s = [1] *len(xs))\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hyperbolic functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import cosh\n",
    "\n",
    "min_x = -6\n",
    "max_x = 6\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [cosh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import acosh\n",
    "\n",
    "min_x = 1\n",
    "max_x = 200\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [acosh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import sinh\n",
    "\n",
    "min_x = -6\n",
    "max_x = 6\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [sinh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import asinh\n",
    "\n",
    "min_x = 0\n",
    "max_x = 200\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [asinh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import tanh\n",
    "\n",
    "min_x = -3\n",
    "max_x = 3\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [tanh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot\n",
    "\n",
    "from math import atanh\n",
    "\n",
    "min_x = -0.99\n",
    "max_x = 0.99\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [atanh(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exponential, logarithmic and power functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import xlim, plot\n",
    "\n",
    "from math import exp, expm1\n",
    "\n",
    "min_x = -3\n",
    "max_x = 4\n",
    "xlim(min_x, max_x)\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [exp(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "ys = [expm1(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot, subplot, text, xticks, yticks\n",
    "\n",
    "from math import e, log, log1p, log10, log2\n",
    "\n",
    "min_x = 0\n",
    "max_x = 12\n",
    "nb_of_points = 200\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]\n",
    "xs.remove(0)\n",
    "subplot(411)\n",
    "xticks(())\n",
    "yticks((-3, -1.5, 0, 1.5, 3))\n",
    "ys = [log(x, 0.5) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(10, -1, 'Base $\\\\frac{1}{2}$')\n",
    "subplot(412)\n",
    "xticks(())\n",
    "yticks((-3.5, -2, -0.5, 1, 2.5))\n",
    "ys = [log2(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(10, -1, 'Base 2')\n",
    "subplot(413)\n",
    "xticks(())\n",
    "yticks((range(-3, 5)))\n",
    "ys = [log(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "ys = [log1p(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(10, -1, 'Base $e$')\n",
    "subplot(414)\n",
    "yticks((-1, -0.5, 0, 0.5, 1))\n",
    "ys = [log10(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(10, 0, 'Base 10')\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import xlim, plot\n",
    "\n",
    "from math import sqrt\n",
    "\n",
    "min_x = 0\n",
    "max_x = 10\n",
    "nb_of_points = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]\n",
    "ys = [sqrt(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot, subplot, text, xticks, yticks, subplots_adjust\n",
    "\n",
    "from math import e, pow\n",
    "\n",
    "min_x = 0\n",
    "max_x = 3\n",
    "nb_of_points = 200\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]\n",
    "xs.remove(0)\n",
    "subplots_adjust(wspace = 0.3)\n",
    "subplot(141)\n",
    "xticks(range(0, 4))\n",
    "yticks((0.2, 0.5, 0.8, 1.1, 1.4, 1.7))\n",
    "ys = [pow(x, 0.5) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(0.6, 1.55, '$x^{0.5}$')\n",
    "subplot(142)\n",
    "xticks(range(0, 4))\n",
    "yticks(range(0, 10, 2))\n",
    "ys = [pow(x, 2) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(0.6, 8, '$x^2$')\n",
    "subplot(143)\n",
    "xticks(range(0, 4))\n",
    "yticks(range(0, 20, 4))\n",
    "ys = [pow(x, e) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(0.6, 17.7, '$x^e$')\n",
    "subplot(144)\n",
    "xticks(range(0, 4))\n",
    "yticks(range(0, 30, 5))\n",
    "ys = [pow(x, 3) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(0.6, 24.2, '$x^3$')\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Probability functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot, text\n",
    "\n",
    "from math import erf, erfc\n",
    "\n",
    "min_x = -3\n",
    "max_x = 3\n",
    "nb_of_intervals = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]\n",
    "ys = [erf(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "ys = [erfc(x) for x in xs]\n",
    "plot(xs, ys)\n",
    "text(-1.2, 1.5, 'erfc', color = 'g')\n",
    "text(-1.2, -0.5, 'erf', color = 'b')\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot, axvline, axhline\n",
    "\n",
    "from math import gamma\n",
    "\n",
    "min_x = 0\n",
    "max_x = 4.9\n",
    "nb_of_points = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]\n",
    "xs.remove(0)\n",
    "ys = [gamma(x) for x in xs]\n",
    "plot(xs, ys, color = 'blue')\n",
    "\n",
    "# For negative values of x,\n",
    "# Have to awfully hack point coordinates\n",
    "# to get decents results\n",
    "xs = [i / 100 for i in range(10, 91)] + [0.95]\n",
    "x_1s = [-0.95] + [-1 + x for x in xs]\n",
    "y_1s = [gamma(x) for x in x_1s]\n",
    "plot(x_1s, y_1s, color = 'b')\n",
    "x_2s = [-1.974] + [-2 + x for x in xs]\n",
    "y_2s = [gamma(x) for x in x_2s]\n",
    "plot(x_2s, y_2s, color = 'b')\n",
    "x_3s = [-2.992] + [-3 + x for x in xs] + [-2.025]\n",
    "y_3s = [gamma(x) for x in x_3s]\n",
    "plot(x_3s, y_3s, color = 'b')\n",
    "axvline(-2, linestyle = '--', color = 'y')\n",
    "axvline(-1, linestyle = '--', color = 'y')\n",
    "axvline(0, color = 'black')\n",
    "axhline(0, color = 'black')\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib.pyplot import plot, axvline, axhline\n",
    "\n",
    "from math import lgamma\n",
    "\n",
    "min_x = 0\n",
    "max_x = 4.9\n",
    "nb_of_points = 100\n",
    "xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]\n",
    "xs.remove(0)\n",
    "ys = [lgamma(x) for x in xs]\n",
    "plot(xs, ys, color = 'blue')\n",
    "\n",
    "# For negative values of x,\n",
    "# Have to awfully hack point coordinates\n",
    "# to get decents results\n",
    "xs = [i / 100 for i in range(10, 91)] + [0.95]\n",
    "x_1s = [-0.95] + [-1 + x for x in xs]\n",
    "y_1s = [lgamma(x) for x in x_1s]\n",
    "plot(x_1s, y_1s, color = 'b')\n",
    "x_2s = [-1.974] + [-2 + x for x in xs]\n",
    "y_2s = [lgamma(x) for x in x_2s]\n",
    "plot(x_2s, y_2s, color = 'b')\n",
    "x_3s = [-2.992] + [-3 + x for x in xs] + [-2.025]\n",
    "y_3s = [lgamma(x) for x in x_3s]\n",
    "plot(x_3s, y_3s, color = 'b')\n",
    "axvline(-2, linestyle = '--', color = 'y')\n",
    "axvline(-1, linestyle = '--', color = 'y')\n",
    "axvline(0, color = 'black')\n",
    "axhline(0, color = 'black')\n",
    "print()"
   ]
  }
 ],
 "metadata": {
  "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.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

Resource created Wednesday 07 October 2015, 10:22:46 AM.

file: math_module.ipynb


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G