{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SymPy - Symbolic algebra in Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "J.R. Johansson (jrjohansson at gmail.com)\n",
    "\n",
    "The latest version of this [IPython notebook](http://ipython.org/notebook.html) lecture is available at [http://github.com/jrjohansson/scientific-python-lectures](http://github.com/jrjohansson/scientific-python-lectures).\n",
    "\n",
    "The other notebooks in this lecture series are indexed at [http://jrjohansson.github.io](http://jrjohansson.github.io)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two notable Computer Algebra Systems (CAS) for Python:\n",
    "\n",
    "* [SymPy](http://sympy.org/en/index.html) - A python module that can be used in any Python program, or in an IPython session, that provides powerful CAS features. \n",
    "* [Sage](http://www.sagemath.org/) - Sage is a full-featured and very powerful CAS environment that aims to provide an open source system that competes with Mathematica and Maple. Sage is not a regular Python module, but rather a CAS environment that uses Python as its programming language.\n",
    "\n",
    "Sage is in some aspects more powerful than SymPy, but both offer very comprehensive CAS functionality. The advantage of SymPy is that it is a regular Python module and integrates well with the IPython notebook. \n",
    "\n",
    "In this lecture we will therefore look at how to use SymPy with IPython notebooks. If you are interested in an open source CAS environment I also recommend to read more about Sage.\n",
    "\n",
    "To get started using SymPy in a Python program or notebook, import the module `sympy`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get nice-looking $\\LaTeX$ formatted output run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "init_printing()\n",
    "\n",
    "# or with older versions of sympy/ipython, load the IPython extension\n",
    "#%load_ext sympy.interactive.ipythonprinting\n",
    "# or\n",
    "#%load_ext sympyprinting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Symbolic variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In SymPy we need to create symbols for the variables we want to work with. We can create a new symbol using the `Symbol` class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = Symbol('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEgAAAAbBAMAAAAt2dQtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABYElEQVQoFY2SMUjEQBBFf+4kiSTGqGBhlUKwVcFO8IQUIihyYGWTTu1iIRaiXqWW1tpY2Qhq4VU2EQ6usrDWwtJG1FYCcXZn4xI8kptidubvY3aWGaDK2nNJFQLHH2pUQqbvfVdDl8ZPJQTYaR+QGfcBtYkxAnK9reaTXlsjN9YbkOoz+WvcAIslUAfwlk7nuZrmTnRIkRNjMMu+YIp3tRUh94xvxjUgoiKEZb7dpaN7MLEfc6ogKyOjb62zOgu4d7VNs1GAHt53pkZImWR1i9pLnNRWrXElr+X4h+L+mKFzqoSBaZnch+F2GK6IuI5XcXwKBxAEDKuOdOMfiMSFgug5it+EF5b/rgk5+ytWqXE7OYKVcKogN4XcItU4zfi2tYBRZvJKVoQLoWywTAWf9rrNgLMcqgd4Ecoqy6VjMSKG5LpwKPyjDimiAbPNFORi0snTsqX7K2AEOf7vlOtL6i9nYkLvuMXjdgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left(x + \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "       2\n",
       "(x + π) "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(pi + x)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# alternative way of defining symbols\n",
    "a, b, c = symbols(\"a, b, c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sympy.core.symbol.Symbol"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can add assumptions to symbols when we create them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = Symbol('x', real=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.is_imaginary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = Symbol('x', positive=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACoAAAAPBAMAAABgjEDtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMA782r3SJ2ZjIQmUS7VIlAnjihAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAv0lEQVQYGWNg/GQs72z0hQEV8Acw5BcwNKIKMsxiAIkyo4mGg0XZJqAKR4BFOUCi0Q2c3QFwWaAJ3Iq5j0LXH+A9n8DAuvwxWAooysC4dn4B0wEG/gSGLRO4JUEaQKJMDgwMPGBROQYGMaAgRNQAKsrxq7zcHC66ACrK/hckBARgExbA1H4DiyFEmQ8wxCcwODEwTIOpZQGqZRdguHiSob+AYSUDA/caeZkV3Of/XGBgeJc2RWQCp1XeBKghaBQAM0c287zNvm0AAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\mathrm{True}$$"
      ],
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Complex numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The imaginary unit is denoted `I` in SymPy. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACoAAAAQBAMAAACSDPCjAAAALVBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAOrOgAAAADnRSTlMAVO8Qq5l2zWaJ3SK7RPx7N2kAAAAJcEhZcwAADsQAAA7EAZUrDhsAAABySURBVBgZY2DABCwHGBgYldHFWRoYTEIeQ0UZE5Ck2SgRNQsDmoRhwoRLWES5BcLhopzl5VXq5eUODAzMDM/gokAGzGWMIEdhmMvuYIBF1E5gAhbRe9wNDKxOzz2A5gABzFyWDggfQsJEkcWATtoA5wMA/Fcc5MixWvAAAAAASUVORK5CYII=",
      "text/latex": [
       "$$1 + i$$"
      ],
      "text/plain": [
       "1 + ⅈ"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1+1*I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABgAAAAPBAMAAAAMihLoAAAAJFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADHJj5lAAAAC3RSTlMAEM3dMlTvq5l2ZtVdCTcAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAqSURBVAgdY2DAClgTEcLi7RsRHAZOMjlCxiCgwkC2ATA3cJRtqoKxwTQAC0AL2ft3JesAAAAASUVORK5CYII=",
      "text/latex": [
       "$$-1$$"
      ],
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEwAAAAbBAMAAAAkMnRXAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRImrEDIioekeAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABXElEQVQ4EY2SPUvEQBCG3z0xauKeQUEsg4i9gqVwYgqRIDZWWlhYaJcmWB1i40chXm2jlRaClYeVyJ2IpRHs/BHGykPQczYTwq0mXgayzLzzZIbZHaC71WfC7hAse7BSADPs8nsR7Ex8FsCA/q9CmOEXwupECYeOPCvZlCkt0zFC330ehgZlLnEFzJHTzMQO6VZfgPLCwSxX1Kh9juTDE2GWj4F2O4KhumuWYMAOYbLGuVENUYGGYZHzAf0R+JDbjYsbVnRshcVp6m/W8IpJfyMLm2BxExjrreAWd+FuFrbH4gkQDvkIMR/H16675bpe7KsR8JZiOFbxB8e/R0gwagpP0iV+Z2PnLNMIsmXhqC9ClRV90mQEen0zGjZbPZFwsrBVFlXRpiMfq8E4C+n1rp16DrDE6j+PpQCxzli8TOwm53NnRE/PNtWp/vFpkdjUWuZbWkQ4+RDiJaf8DywZUcb5dpcnAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\left(i x + 1\\right)^{2}$$"
      ],
      "text/plain": [
       "         2\n",
       "(ⅈ⋅x + 1) "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x * I + 1)**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rational numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are three different numerical types in SymPy: `Real`, `Rational`, `Integer`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "r1 = Rational(4,5)\n",
    "r2 = Rational(5,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAsAAAAqBAMAAACXcryGAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBUq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAkklEQVQYGWNgYGAQAmIgMAGTrClgqmIKmFoApjgKwNRWBjC1AEzxCICpXQxg6uzdu9+ugnVAtDN8AXOW/L8Bpski/oPAB6K0Tt0gdACoMP//L5DyWVcLQNQFEAGnbrQqgnjeDPUPQDQDiwGYYvrOwMA7gYHrHwMD2wQGpt8MDEwMDMwTGBjYFRjaQMYUrdVmYAAALnIpDsFeUO4AAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\frac{4}{5}$$"
      ],
      "text/plain": [
       "4/5"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABUAAAAqBAMAAACuFQ3dAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBUq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA5UlEQVQYGWNgYGAQAmIYMAEyjm0A81hTGLgv3YewK6YAheIh7AUINkcBgr2VAcFegGDzCCDYuxgQ7LN37367CjeTIQVhPsMXBHvJ/xsMuvlzG4AiNAb/4eADZTYVqTgyMGxasRBoCvcFhvUPGOYwbC5gYODdwMCWwJHAwDWBgYHNgIH3I8sFBp7fDAycv4Fs/gsMfH/A1jL/3q/AwPcVzH6/oH4BA+MvMNuWoV4BymZSYICrCWJgAOrlAenlUWAoZ3Fg4ACaCYrRcKBdTEC72K1W6Row2DBsEwDaBfSjAcOu0IsMDACGnlMXi4yUmQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\frac{41}{20}$$"
      ],
      "text/plain": [
       "41\n",
       "──\n",
       "20"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1+r2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABUAAAAqBAMAAACuFQ3dAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWaJMkS7It2ZnNffAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA/klEQVQYGWNggICZmQtADCEDBgbzDawFDAzMrv5A9lEGngMg8XwDBu6/IAYQANk8CmAWmM1fNS0azAOK21cwMAmAOCD2ZwZWZSib/wEDx28om6+AgeMHlM2igBDnAqpXgIozeDIYgcwJ0S9pYOAM9QIJ0xj8h4MPlNl0NeYUA0OZwVKg+5kdGOZvYND//xNoIosBA5MCQ6XbBSCb6QEDyy8GB7A97J8RbKAA32cGj7YosAzD/gkMhxnubwBznoBIngcgkjUATIKjIxVocAEDJyhsGQMY7jIVMLB+BrKXMjDksgINK2Bg4H05M+QBbwBDE9BidqAfHzBcnRLMwAAAq1hKQiigoh8AAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\frac{16}{25}$$"
      ],
      "text/plain": [
       "16\n",
       "──\n",
       "25"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1/r2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numerical evaluation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SymPy uses a library for arbitrary precision as numerical backend, and has predefined SymPy expressions for a number of mathematical constants, such as: `pi`, `e`, `oo` for infinity.\n",
    "\n",
    "To evaluate an expression numerically we can use the `evalf` function (or `N`). It takes an argument `n` which specifies the number of significant digits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgsAAAAPBAMAAACYf5HCAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIom7VJlmdt1E7xDNqzIhoty3AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGm0lEQVRIDe2X22tcVRTGv5lMJsmcmWQsVEQFh5G0ItaMVMEL2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NVp4f/OMJpIpF659rwCNCOje96Xyt86eVeZfqOb4SKyH9Qrq2VYG4uErRtoiwcMr5//oXWhd7due/PDYAAAAASUVORK5CYII=",
      "text/latex": [
       "$$3.1415926535897932384626433832795028841971693993751$$"
      ],
      "text/plain": [
       "3.1415926535897932384626433832795028841971693993751"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi.evalf(n=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y = (x + pi)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHUAAAAbBAMAAACw1N2lAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACJElEQVQ4EZ2UP2hTURTGv6RJ+sy7/aOigw6+is7GduqUFIKDU7A6CC1UqFS6+JYiIpKAi5Md6mClpW8SnHwiIYMgd7I4iEW6dChmcHOISKhLJT3n3HdfWnkUfQfyzrnnfL97T07eDZDO3OmZdCBRb7Gdmr2DzmhaeBEXgrQssKzTswvpUbdGbMajx//YmIgv8/N0Epd5/65xKD/Mh7C5rQ8Y4gEXw2IbmOLc33YGzi/JOWvsCiWAI3UdN6EqlPn+9IlG1m7JGji+ONQb2OPo8+w+u4clEw3UMAG0KLPR69GW3EFsln0dqK4kB5nNXKJzOXoj4k9WftYG4i1LHZiehXVzETspolwQMUvkNx+fe+TLus9+DCUh7Jhluz+WAiBfkxIwThMIswuFiqxj9vxdLQlmlR+xquvhBZCtSAm4R4PX7r4zKuuYhftSEsy6sGxP47aGMxexJFFSI021eu15tbplKquaPbPPLMuzvxJg+KdRQLYf8aOVPfckUG5wjlgVxuw8sV6fpZ6BTpufZJal5mLW2d39tu5LBztybnFOtDIrR9cxqGVt2RXgvkyAewZOlEz3Hfq+h2bVpF+8UcYpQeNzL8L5jS8VQwAjETsUKppzPjRivKIX78HmDe8oW7w1HaCwhfxO9ysNsfzHM1Fztg3k6COW/E5GxWQXv4pH74KSL5mMxFm+C8au2uBfvdxBI068+8ftM9DvLeMdJ0yomf8cKhwAL6SHm0ESaMoAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left(x + 3.1416\\right)^{2}$$"
      ],
      "text/plain": [
       "            2\n",
       "(x + 3.1416) "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N(y, 5) # same as evalf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we numerically evaluate algebraic expressions we often want to substitute a symbol with a numerical value. In SymPy we do that using the `subs` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFYAAAAbBAMAAAAUvmV2AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABlElEQVQ4EY2TO0jDUBSG/1QSLUkfKLqaQRA3Lbh1qBBQlEoXJx901EGog651sro5u7STiyAKdlQiuFsFcdRFcFNBFxXiOffePBpEcyDn8Z+vNyfJKZDQ2pNuQhJmPlNKyhr57FtitqV9JmWBvu/krFFLzrYJ1Wxyf1oqT+1UhdxAyE27T9thFWY3lB7hGJiiZM8Vjab3JWLcXQHZmd0in23dPUh27p7vxtaQQXmzhrTnvcLgdt0Vakf14qy1LxtDHP5hMSvZzQh7O7oixWCGuud5H8CClAsRtojhllTVvOZFo3+DhhyR6mqEBXqqXAbzPuO095HKHbrIDtipeQGdl+nMcdYcp8wNjGdc8i+cdrG0oaZaJv+dZTsGQ4qNzJAuQVfL5LO6nXOJPeQfAJFn0wGjJMTgPVy7uRYp6tl4fXhe6x1aBWP8Zcj8c7eQPqFyUYji+OVm2cY6MHi+JMWALSDD7LyUxTdWRBj8c6WiVWUUexlCKrvsUmh3pE10yb8WtJPSIrvuS/EYHKfZ8Va8Fv8hEn8A9AZYsVBvIRkAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left(1.5 + \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "         2\n",
       "(1.5 + π) "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.subs(x, 1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKAAAAAPBAMAAACRq9klAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12VGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC7klEQVQ4Ea2UTWhUVxiGnzuTOz+ZmWTMRqwLb2MbFREjNqCCJGRhoZvclQtpyRCo+JsMURwU0UvBhbqIUiKxRRxc6KILZ2PrQnAMlFJoyVBKoRu9bhRRoqLRmDG5vufcaTbdeuC+95z7fueZ7zs/g9O9dYD/Wu7Xtk3qt3ktWVnZCV//th661n4BTuVIA9Inj7birXPHH2zg/jEe8NVIpVJmN+l5KEzamGwUldVp743F8XgQ4pFquCVOB3RRWMK9zUbZ//pYh6vRW0jAYZ5HUVTiW7jM7p45xUDu2D3z+t0AJflFOquFEKeW8sleYKbBAkmPrbgjYz7W4ZdDRbgPq/kUUvADjA2Qj4EaqzlPBTSSmWK4lu7FrWarpF5zNnCbTGi+2oyPdSiZ0RHYQxm0GHf9/wHTOQGtoJIzr/x0mJszQC3NEtsNwAKtEwMn+jilj5m6sbjpL2c4Oz0ADw3QCu4NFbBkYmk3Rewp0/xyOlBPGcbO6LPPVGr0ONTHpB5htaitkpO+M49bF9AKzonPlWt0y4RNlGHlNd9thlzR0ACt08eaAE43NbZZ6qdLy0B920EaAa1oeDzgn3PvAvWu6yE95UY+GzTZAFtOskph380pcF+YGLr1tDJUb3PxbwO0omF2sqPEzKQOhqeRToS/AM8DC2w5JF7yiPw7LVzNhOQ9I/EuH1TuZ8oCukZwiiQXs0XaFmFaUSugv/GTgKEFWidVMwf5Igw14vPLX2ZeC/ijMvxm796xS38aqXfOCTgs0igFTzhV29/Yv5yhdbI1EnOm1lTIcEmxumb5ZaAHSoBsbyx6t79UHmxhEO7xvU5EcUhrKFtraJ2ENqFmMkwWGdJ8PqmMH4hLHq7pbnXIotMAJR0hD+qZW3TUnRuVniqzFN6TKrutXY4djydFzvs806E1wLtRtEBif3PU7LazbsTXVva/CWP5rnsb7Fq3npyuaZVM99oAxnsGoOfqz6F16Dqmo9U2oj8HVjUE/NjtA/9e98ZPxvCDAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$21.5443823618587$$"
      ],
      "text/plain": [
       "21.5443823618587"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N(y.subs(x, 1.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `subs` function can of course also be used to substitute Symbols and expressions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFIAAAAbBAMAAAAdVcUMAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQMqvFy5UvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABm0lEQVQ4EY2TO0gDQRCG/8vrEu9OToSUkkKwja/OQuEQESRBCBZpUlppLGwEIY1ip4gIPgorQUSwSRWEFNYmpZXGRgsFY2OhgXP25nY1IZGbYmfn/7/sIzsHBIryZDUQB8Punw5GxmzrIyB5pn0FI4F4KygZKwYlywRqKRr+iZBNZihLw2A3SsuN16X+RJNLXAEzUvmb72Coy94C1tzOFK+roLh/8DfgSIpGEQnXbSImjqFCkofAu9ze3GM7qSgxkWSm+ktinpF1Slp+6ZQrSVK1WtVdiiyQY2+M0oUdaXaSfd+o3K+NDJA+zN4yYO4jmu4kYw2rZNibQt5m7xiItuA9luE4sweO0/AMsWUY9B8Aj54AIhNp1OpcqXPqdD48oCBkn6Tda9fYFQqFIreg2ViE10Hn7NGNaiVMxO020spCt80WvIfyb0RNEimGVgz+nVwzWSk/Qy/gRMh59mhp8+VmaKOdzLjuJ8IpvAp5gb3ur8kej1qBs9d5yjD5uKoWE+oQjlE/90zUdRxdO1maIqultJQoe4f3dZD9A6SlVff4q7eXAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\left(a + 2 \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "         2\n",
       "(a + 2⋅π) "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.subs(x, a+pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also combine numerical evaluation of expressions with NumPy arrays:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x_vec = numpy.arange(0, 10, 0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10860 characters]",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1093af4d0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(x_vec, y_vec);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, this kind of numerical evaluation can be very slow, and there is a much more efficient way to do it: Use the function `lambdify` to \"compile\" a SymPy expression into a function that is much more efficient to evaluate numerically:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f = lambdify([x], (x + pi)**2, 'numpy')  # the first argument is a list of variables that\n",
    "                                         # f will be a function of: in this case only x -> f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y_vec = f(x_vec)  # now we can directly pass a numpy array and f(x) is efficiently evaluated"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The speedup when using \"lambdified\" functions instead of direct numerical evaluation can be significant, often several orders of magnitude. Even in this simple example we get a significant speed up:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 28.2 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "\n",
    "y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The slowest run took 8.86 times longer than the fastest. This could mean that an intermediate result is being cached \n",
      "100000 loops, best of 3: 2.93 µs per loop\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "\n",
    "y_vec = f(x_vec)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algebraic manipulations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One of the main uses of an CAS is to perform algebraic manipulations of expressions. For example, we might want to expand a product, factor an expression, or simplify an expression. The functions for doing these basic operations in SymPy are demonstrated in this section."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Expand and factor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first steps in an algebraic manipulation "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMEAAAAUBAMAAADGn0QzAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACtklEQVQ4EY2Vv2tTURTHv+/l10vyUmOlDrqkRZ2jf4B94sNBUIKlkxReQVERMQ6lg5XGRReHgi6KQybFyYCKQ4U+QSwihdZF3Lo4G6SoQzCec38k75QX6h1uzj3nez/f3PN+AU4NI8akyY+PqMOt6opTG6VQiAOjqqhogFNPU0zMnAS2dCUV4ay+amnEqbT98J4CfqBKWfNPhc7fxFobH3QuFTEB76dCuI3kTq+pVxtzPQreqsXRpAD39aoSo7iCspJLhFUst/BLIfLiH1oHFNjhk6LNpzkUI1T+wl/hmkRYhxdtf0chDor90iHbpqITCYU5Q7bHDjjLNYmwDuAugRALJFm/c+i2bo90yDWoWKDJX9x6rls23I88HXOWHSRiqPjYAQhxgvZ33Cv5gLWQDm5AqUwN+IxjzUsU0zBnoOglAY5wSiIGisOXY8ANcBUox+WeVyXpbgcvolS+BbzDWnyXYhpDhxu0uscpiRgqyk8IGIFmH9k6K8theOZRGG5yrK70WJeiYhuIcZqTeB2G18LwnIpVC39wmEAIBR7HIASVgX1NnmnILg0cgN+6njgDdx/WIYGwZ9gPTLfYgY5Iwm2eaUiHUkQp7hLG6L7Rw3bJa4AYzzgpEdahH7MDIegyefEyCrECSAc3oCRf6QeFLpYopmEdvgO3hlc6gbCKh8DNKtwAb+imaE1jnHfvPkOuQ6lyA6U/ma5To5iGcXCur85FwEVOSYRVTMGj1hKCzrmxuH6hxtKhQ+7bzhd6XLYpRef0vy4tTHGZhnHI9vv9CDjPKYmwitLsTFsh5CM/6BLvNA/rvIrtZLuk1k7EPxJhHZSAEfK15Vd1Qc9pb773SUHqm08oGHE8uUXE5u2dEa5CYd7eeyBSPx+KY9BOXVCTC4PeA+HUknuS8aRZmPssWdLxf31F/wED/Kt4r5jyqgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left(x + 1\\right) \\left(x + 2\\right) \\left(x + 3\\right)$$"
      ],
      "text/plain": [
       "(x + 1)⋅(x + 2)⋅(x + 3)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x+1)*(x+2)*(x+3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKsAAAAWBAMAAABNknGBAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsyme8Q6PJIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACJUlEQVQ4EZWSMWgUQRSG/7277K53u7klAcHmsomQLigStAlhG7E9LKyEXFaw8vA67SJ2dkEQPWNxgpVVUkt0K9sEDhEL5VKa6i4Y9IJwzu7M251xMxvd4s2b9733z9s3A/Av7HvCyy/tXT3LZ8sRt+Nuy3vZN4Pyprz/D99tWUNduh04P3TszLi+W7tZOT6zXJfQiHQEqI70rJiUnhZw2y+AxSg3hLuLEVW0yfmn1QzvZXnOSebH3oWmS/dv7auITeWARRLzNwGMZ/hC0UZU+cn9qs/XXdS2Bb2F2yqb7bNhJ0ZkxAsV1vbxjsJ2YMUNsE/QqexNOS/f7igMKMV3mBgOYkuya0EWc8I7YidobZDC6clEvGmqLJR9zwtnLl5f9FMNOrT+KvwEnM6kbo2F5ZshL6Yzxx+XmmzCLeuxvZmTnXuO6Z6GSbLn8dB/osga4w4OAdMzR9UgLzuCe6Bhkuw67ntvVNmJh28eDJyjG2dndLsvvne7l4H6I1jHGibJethKRLNC/AI22BRQ9/lx3IoRsblY7MmdyiRZ4IiKabZsJhsdFtyLiMSroOUB61bDZFnnNxWT7GrSbdW7hJJHLJV12GwHGibJvi4NMc+LSXaPzRZY633AjUyVusVVNHo6lr7bqZPasBL/MftIttwy2EuYXZj5LIBCza8rWuaujq8hNsaV+aUHSVkmi3Y/EiFpoUOlUOoWsbTbNFtxjEDZKpsiBgX+AfzFj7EIR0Y4AAAAAElFTkSuQmCC",
      "text/latex": [
       "$$x^{3} + 6 x^{2} + 11 x + 6$$"
      ],
      "text/plain": [
       " 3      2           \n",
       "x  + 6⋅x  + 11⋅x + 6"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+1)*(x+2)*(x+3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `expand` function takes a number of keywords arguments which we can tell the functions what kind of expansions we want to have performed. For example, to expand trigonometric expressions, use the `trig=True` keyword argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFgAAAAUBAMAAAD7IecQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMnZUZs0Qu91E7yKJmaurDqYVAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABlklEQVQoFX2TTyiDYRzHv8s8tt45UE5qe8sNk1yIg7VSDg6jXpHwXrRSuJAiWhxcd5QcpISknMxFuTiudpELmbVcXLBFQ+b3vH/2PO82nnre5/v7Pp/3eX+/Z78BZUMJlRl26D4zlWvfdoCEkEJFj0g/m7HrW/gTQkpqivSMFJvSnZKtGtWKcrT67MDyAI+VmWnYsPedYrZnmEqDuUfPi5LiwoZ9MR6Nw9USjiR+0HTa2KGSEeTuZbJV52sJrs0EO4F7RIEYKP8tVfmi7UWa3hNMc1TAC6OMNgewo0PFOrAC5Gl7k+ZsCLccFXC/DrqwHng+2sDhJeCNto9pPujYoMWnaWMHmpYmeQNGcADsupji8LKAD8FeiaBhFzgJL11eAOdwFySY0qBDal6ccB71dHnb6AKyEkwFshzqIn2Okwu8DiqwV8cqz8FKI0nQCAbTVw44izWK77AbHor7i0/+4mP3Z5p/Cphvb87EHfBcMETxsOFJj+o/twEoMYkzpLORmNwplY1EBfw1Klu0evMb71c55r+/1S+z8V/W/OfTdQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\sin{\\left (a + b \\right )}$$"
      ],
      "text/plain": [
       "sin(a + b)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sin(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAAUBAMAAACwpfa4AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3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SYBMWWzxn6TLpAWT6Osgvq81UE9bL2JdRPfpgDQek/tERFDrAE3ik2wbmGOodRSEYKOHZzCTcgO8A4kt4hUSojLACCrP+8RHM8Geg4tK5v02fUxWctz6A3VabAaS2n327oPD7Wx5tbV79bfPqr/f/21U1wd7hW+dbwOMMxd7Z8/Dmvm3lv7n9HJcsGfaMVKgU5T1MQLB1eeX5yys3HvyMVh6QM/s9hV3ixflzOMDpC79DNovo/Ndf3hlhWoyhX8XGOzEFaxomYHgoqV+2J3uJrDVFCt4WRxT8WJn3GGv6okm11j8309pkCE3TfqvkpNXYaUQkMsbpNPE7EEqnmpgMFVGkYf2HJHYy4ihjnE4T36lQxvkmQKmIqr7uQcCJ0LLen64Qr0f107yKklu1YY2IqPwl+x/SgSmeCF1yWAAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\sin{\\left (a \\right )} \\cos{\\left (b \\right )} + \\sin{\\left (b \\right )} \\cos{\\left (a \\right )}$$"
      ],
      "text/plain": [
       "sin(a)⋅cos(b) + sin(b)⋅cos(a)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand(sin(a+b), trig=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See `help(expand)` for a detailed explanation of the various types of expansions the `expand` functions can perform."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The opposite of product expansion is of course factoring. To factor an expression in SymPy use the `factor` function: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMEAAAAUBAMAAADGn0QzAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACtklEQVQ4EY2Vv2tTURTHv+/l10vyUmOlDrqkRZ2jf4B94sNBUIKlkxReQVERMQ6lg5XGRReHgi6KQybFyYCKQ4U+QSwihdZF3Lo4G6SoQzCec38k75QX6h1uzj3nez/f3PN+AU4NI8akyY+PqMOt6opTG6VQiAOjqqhogFNPU0zMnAS2dCUV4ay+amnEqbT98J4CfqBKWfNPhc7fxFobH3QuFTEB76dCuI3kTq+pVxtzPQreqsXRpAD39aoSo7iCspJLhFUst/BLIfLiH1oHFNjhk6LNpzkUI1T+wl/hmkRYhxdtf0chDor90iHbpqITCYU5Q7bHDjjLNYmwDuAugRALJFm/c+i2bo90yDWoWKDJX9x6rls23I88HXOWHSRiqPjYAQhxgvZ33Cv5gLWQDm5AqUwN+IxjzUsU0zBnoOglAY5wSiIGisOXY8ANcBUox+WeVyXpbgcvolS+BbzDWnyXYhpDhxu0uscpiRgqyk8IGIFmH9k6K8theOZRGG5yrK70WJeiYhuIcZqTeB2G18LwnIpVC39wmEAIBR7HIASVgX1NnmnILg0cgN+6njgDdx/WIYGwZ9gPTLfYgY5Iwm2eaUiHUkQp7hLG6L7Rw3bJa4AYzzgpEdahH7MDIegyefEyCrECSAc3oCRf6QeFLpYopmEdvgO3hlc6gbCKh8DNKtwAb+imaE1jnHfvPkOuQ6lyA6U/ma5To5iGcXCur85FwEVOSYRVTMGj1hKCzrmxuH6hxtKhQ+7bzhd6XLYpRef0vy4tTHGZhnHI9vv9CDjPKYmwitLsTFsh5CM/6BLvNA/rvIrtZLuk1k7EPxJhHZSAEfK15Vd1Qc9pb773SUHqm08oGHE8uUXE5u2dEa5CYd7eeyBSPx+KY9BOXVCTC4PeA+HUknuS8aRZmPssWdLxf31F/wED/Kt4r5jyqgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left(x + 1\\right) \\left(x + 2\\right) \\left(x + 3\\right)$$"
      ],
      "text/plain": [
       "(x + 1)⋅(x + 2)⋅(x + 3)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**3 + 6 * x**2 + 11*x + 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simplify"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `simplify` tries to simplify an expression into a nice looking expression, using various techniques. More specific alternatives to the `simplify` functions also exists: `trigsimp`, `powsimp`, `logcombine`, etc. \n",
    "\n",
    "The basic usages of these functions are as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMEAAAAUBAMAAADGn0QzAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACtklEQVQ4EY2Vv2tTURTHv+/l10vyUmOlDrqkRZ2jf4B94sNBUIKlkxReQVERMQ6lg5XGRReHgi6KQybFyYCKQ4U+QSwihdZF3Lo4G6SoQzCec38k75QX6h1uzj3nez/f3PN+AU4NI8akyY+PqMOt6opTG6VQiAOjqqhogFNPU0zMnAS2dCUV4ay+amnEqbT98J4CfqBKWfNPhc7fxFobH3QuFTEB76dCuI3kTq+pVxtzPQreqsXRpAD39aoSo7iCspJLhFUst/BLIfLiH1oHFNjhk6LNpzkUI1T+wl/hmkRYhxdtf0chDor90iHbpqITCYU5Q7bHDjjLNYmwDuAugRALJFm/c+i2bo90yDWoWKDJX9x6rls23I88HXOWHSRiqPjYAQhxgvZ33Cv5gLWQDm5AqUwN+IxjzUsU0zBnoOglAY5wSiIGisOXY8ANcBUox+WeVyXpbgcvolS+BbzDWnyXYhpDhxu0uscpiRgqyk8IGIFmH9k6K8theOZRGG5yrK70WJeiYhuIcZqTeB2G18LwnIpVC39wmEAIBR7HIASVgX1NnmnILg0cgN+6njgDdx/WIYGwZ9gPTLfYgY5Iwm2eaUiHUkQp7hLG6L7Rw3bJa4AYzzgpEdahH7MDIegyefEyCrECSAc3oCRf6QeFLpYopmEdvgO3hlc6gbCKh8DNKtwAb+imaE1jnHfvPkOuQ6lyA6U/ma5To5iGcXCur85FwEVOSYRVTMGj1hKCzrmxuH6hxtKhQ+7bzhd6XLYpRef0vy4tTHGZhnHI9vv9CDjPKYmwitLsTFsh5CM/6BLvNA/rvIrtZLuk1k7EPxJhHZSAEfK15Vd1Qc9pb773SUHqm08oGHE8uUXE5u2dEa5CYd7eeyBSPx+KY9BOXVCTC4PeA+HUknuS8aRZmPssWdLxf31F/wED/Kt4r5jyqgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left(x + 1\\right) \\left(x + 2\\right) \\left(x + 3\\right)$$"
      ],
      "text/plain": [
       "(x + 1)⋅(x + 2)⋅(x + 3)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# simplify (sometimes) expands a product\n",
    "simplify((x+1)*(x+2)*(x+3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAPBAMAAAArJJMAAAAAHlBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGjDitAAAACXRSTlMAVO8Qq5l2zWYZcMvdAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAHUlEQVQIHWNgAANGZQYGk5DJQDYbqQSr03QPsBkAJYgIYEZbtZEAAAAASUVORK5CYII=",
      "text/latex": [
       "$$1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# simplify uses trigonometric identities\n",
    "simplify(sin(a)**2 + cos(a)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAD4AAAAvBAMAAABJZWRJAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWZE3YkiMrsGmOkjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABaElEQVQ4EWNgIA0IGeBTz+zqj1eegSF/VB5fABIKnxD9kga8+kec5H/8YDCGxwmoo3qwO47nAVSc5QCIwQnlwSn2A1AmcwGIYQrlwalWOGsbiIXgQsXXwOWNgCzGr3AuhMGzAC7APoGBoeWXiwBDW3YCw3XVaykJQCm2AAYG5jSfaUDDWYFMhqVAM9YwhDAwrEvg+QPk8zUwMFgzdCUsZ2DgADkQJK/LMN+A4REDww8gn0uAgWEzw34DGaD4Aog8A292/AGG5wwM34B8pgkMDAYMdUAWA+8HiDxz3YH7FxjeIeQhLJh8At8ChvuXDaDyIPMZeP+C9HMuABIlDAH8Gxjkb16AyoPcJ8z2gSEZ6r4ohgCmBwz7b8D0cwcwcH7k+8ADVMa6AaifxfsAj2u2WdmV/75ynxzAhjK7Jad2AKXYLwAJDIAI31MYciABRISA4wdDDd8BqBAkfjHk4ekDrhBNyQkoH5S+AKXTe9mWCL8rAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\frac{1}{\\tan{\\left (x \\right )}}$$"
      ],
      "text/plain": [
       "  1   \n",
       "──────\n",
       "tan(x)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(cos(x)/sin(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### apart and together"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To manipulate symbolic expressions of fractions, we can use the `apart` and `together` functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f1 = 1/((a+1)*(a+2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIIAAAAvBAMAAADdrw/+AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiu91EiTJVJ+QZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/klEQVRIDe2UP0gjQRjF37pu/phEjgMLK0ELz1NwCekT7lAEC1OohSCGAzvlUohBLJJWbLbQ5mzs7k91gtgqtjY2sVPS2ikWFnca38xs3Gwcs0UQhGSamfm+37z55u3MAm/ePtqtbWFOzLSoAHzvKMiP0PFB3cV34MPs+FqptYfRWd1mDlRbbW3mV7PjRkuvZbdUIhD48ZoAuh2Z0gLRuT+VGrChVTCGADMvU1oghcR/F4hk6xWMnJqlZu84uBATPfAVGHSBmCpVLURNASGhkBRRPTAKzFQUsOeuVZ1fIXzCqB4o2kJBAsuEoovzPLZofgVLnFAA2C1/s0XvAcCxDQn8YvjUCd/IvAfIU0TyjAogPoJiA4D4Az0SwAE9/wRrn0M2fw3GX4YIIFnCgkjXAYhlOBPAGGDdIZbjMFEobA4XCsxAOdl7wxEBLNk4YlcPYI6BGtC1j3SFczZ/DTUAn2E+NgJZBiTAItPnGFD5BoW4ewrzAYYoh+15iz5EHUiARqU/4Kfh+AGfk+YtevL9PqA3i5CjnCwD4VzkMCHz3hZSwTpnlADWsZ0RY6+GvcvyNB0UwTN+i8mrnRWZf1awvtxPUbrCKAGkVq8nT9h7CsVq9Z8L6C+tpNV1DAT8D8dUdigB3cvSAPjt4i8793UHAtofiJTrUQUFAoE/sebAE+USwojXIqQHAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\frac{1}{\\left(a + 1\\right) \\left(a + 2\\right)}$$"
      ],
      "text/plain": [
       "       1       \n",
       "───────────────\n",
       "(a + 1)⋅(a + 2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAI4AAAAsBAMAAABBB53eAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEM3dMlTvq5l2ZiKJRLuWvIZ2AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABsElEQVRIDWNgoCEIEyDGcNZE/KoYK+qJMUe8/SN+cxgY5hNjDgPnqDl4A3I0fPAGD/3ST3u++gL8TgHJcpR9qiKsarCrYJ1AHReOmoM/HAmGj5AxCKgwMPzHDj4AzedRUlJOUlIqADL5sav6j98VCFmC7kEoxcsaNQdL8OzoewITpSR8GAsYzl+AGsS4AWYiJo2zoo+EqGUXYGBLwNQGF4Eqw13RQxWwGTCwf4HrwmRAleEuqKEKmD6SZQ53Tye0RQOziIGBGbNNsePENAGI22DK0CrWaxuYPqAqYGC4fwDdO7zZDPpQMezmMGYxcDyAqIApYGAwRzeGQWwBQx9eczg+MjBPAKo4pKRkpqSkDlbL0QDVgqD6BRgsQTwkZaj+YnvAIO8AUQ93zwwIH5nMY2CExSFMGao58gcY4qEaYApYGxh2I5sBZDP+YWCFhiIDTBmaOQEMj1g3gLXBFHgwMMxGN+crA59CNEQQpgzVHKYJXOY8KAq4bc60G6CZw6DJsLjgAIoytHTIWHlk+1wUBUzAMhTDHPE53pUXUJThruhhDoYox0kSVCaNUyuKBLIyAOpdmg617KB4AAAAAElFTkSuQmCC",
      "text/latex": [
       "$$- \\frac{1}{a + 2} + \\frac{1}{a + 1}$$"
      ],
      "text/plain": [
       "    1       1  \n",
       "- ───── + ─────\n",
       "  a + 2   a + 1"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apart(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f2 = 1/(a+2) + 1/(a+3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHwAAAAsBAMAAABVvsF6AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiiUS73TIuwQ1sAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB30lEQVRIDe2Wv0sCYRzGHzs0zx8ZQUGTUENFRRIuTSdF0RIKaUMESWMNNUQ3hf4DwQ21BIEEEQRFBO01NdQQEQ0tubYpDpFR9nrv+173+l4m1NDgDfe+3+d5Pu99+eqhwJ9dHZFGjnL1OqaUyXgjeDRZcsSB1UZweJq4PL/m6OSZiMp3X5vkyHpWTDpV7vGXaSf9H2qu9K+aauJ1xldxvgoE8en6Zp+ux8g25Jyq1Dm4ajUn/8OAauzdo0uu1BmdmrrJ85h9VRMIZ5mgGHZH2EfhexOELlp5igjdCoZQ8IYmgB7BYLh/ENqaYAgFx4eBeN7uMJxIVvN2m+05nomYuDo/x36xLVx5lLGdg6WIqXKcFE9EOTNaCzTNcfV4VsL9A8hQ8Qv3lwGlH+4LEQf2T2v50SwWavFgDHCXEEwT41DX73R9nUZapD8gixE8EM/2EgIpIrRcQMtTiDWvGggUqfJ1H4LyQSureU+CCFoOYZZieKgk40oZLjYhC+8EeZDWjiuXYR7AcNJP8JWdyBeliMBat1lxvC0Bj4HWtPfeR1MM92YRTnOOrxvYiuXMguN7RwczZPJTJ9srNMRwnKeuOWWt0eXnKfpxcDxTqbxbdnXDcUGUCo5LxpikOAnCS/gJnQmXnOfh0q8AAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\frac{1}{a + 3} + \\frac{1}{a + 2}$$"
      ],
      "text/plain": [
       "  1       1  \n",
       "───── + ─────\n",
       "a + 3   a + 2"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIIAAAAvBAMAAADdrw/+AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12VGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACs0lEQVRIDe2WzWsTQRjGn02y+dpuSCMo9LQkYC9KK3pS0OhfsKdCvSQIXrRgiGIugrnWi5YiWAsSPNSDivVqhKQFUU9G/wHr1UPpwdDi1/rOzs42231NFkoPgnPYd/eZ3zz77uzMvguEW6F0KiwCxpvEcU4Pa3oVt5thGRnHqTEyIyVtZO4N6GZXXhg31wbUYaeZFpLfBgDlkBzQhp8a/f06kH+2D23y2H15Jz+HzfWyVCIc79Ywnze2JKkc4ra2E2GsRFagP0JsOuhAV6ejOsQsxPrIdolPtduvltvtqhw6lZdx5HEdyEyjUpageoqr4NcJY2daGK/U8NnrUg7LwJTN4Ix0AVir9HDGlDkrBwt4ytCMpD1pHG0Z3dxKSnYqhwJyCwzOSIbjOC197sbhd0EHbfJSxIfYa6py2KtHv9ajvsLolv/JA5oBWj77aweU179oq238LesvsmMkcIgz0Iony0jKPcUDjes9BbzkHGaQ2oG+6naxQAHmbw/IWYMO6mNwGVgCbokuHvjQw3cPyAa2v3J4CFwrY1Y48MB8U/8BCUwIym/KoWMLB6NJHTwA8RQSoOLAFEkSX9ig0gMIAIX6e1tEqFsAF2uQwFmSw0USSG/TFIipFEB6AR0K1HyHI4/J0gWegy+S2SrhLRpDAGY3UKJAzXdA6oEHUGSLZJHwxBYdCBDF6hmFYBVdsn2AKZIYs2iE77AI3futUTmMA+d6EqAkmSKJT9DySLfIhgB9G6ZIh5pycGzh4AI0UUyRTFgYy/szqf9CfPVjwGGRXpYH1OmthovkRKN+hSaoRqMIwGt8rYrz3Rw2Yf70gDuUZLhIdhyHFq1RpjEEYObt+bkmxV2HdLFEggvwi9al5XIcCQQ3TrBIMjuLAXBC3o85ert7JMB+QFy/eN4NI4GRH7HhwB+u8vuFvy0h3AAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\frac{2 a + 5}{\\left(a + 2\\right) \\left(a + 3\\right)}$$"
      ],
      "text/plain": [
       "    2⋅a + 5    \n",
       "───────────────\n",
       "(a + 2)⋅(a + 3)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "together(f2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simplify usually combines fractions but does not factor: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIIAAAAvBAMAAADdrw/+AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12VGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACs0lEQVRIDe2WzWsTQRjGn02y+dpuSCMo9LQkYC9KK3pS0OhfsKdCvSQIXrRgiGIugrnWi5YiWAsSPNSDivVqhKQFUU9G/wHr1UPpwdDi1/rOzs42231NFkoPgnPYd/eZ3zz77uzMvguEW6F0KiwCxpvEcU4Pa3oVt5thGRnHqTEyIyVtZO4N6GZXXhg31wbUYaeZFpLfBgDlkBzQhp8a/f06kH+2D23y2H15Jz+HzfWyVCIc79Ywnze2JKkc4ra2E2GsRFagP0JsOuhAV6ejOsQsxPrIdolPtduvltvtqhw6lZdx5HEdyEyjUpageoqr4NcJY2daGK/U8NnrUg7LwJTN4Ix0AVir9HDGlDkrBwt4ytCMpD1pHG0Z3dxKSnYqhwJyCwzOSIbjOC197sbhd0EHbfJSxIfYa6py2KtHv9ajvsLolv/JA5oBWj77aweU179oq238LesvsmMkcIgz0Iony0jKPcUDjes9BbzkHGaQ2oG+6naxQAHmbw/IWYMO6mNwGVgCbokuHvjQw3cPyAa2v3J4CFwrY1Y48MB8U/8BCUwIym/KoWMLB6NJHTwA8RQSoOLAFEkSX9ig0gMIAIX6e1tEqFsAF2uQwFmSw0USSG/TFIipFEB6AR0K1HyHI4/J0gWegy+S2SrhLRpDAGY3UKJAzXdA6oEHUGSLZJHwxBYdCBDF6hmFYBVdsn2AKZIYs2iE77AI3futUTmMA+d6EqAkmSKJT9DySLfIhgB9G6ZIh5pycGzh4AI0UUyRTFgYy/szqf9CfPVjwGGRXpYH1OmthovkRKN+hSaoRqMIwGt8rYrz3Rw2Yf70gDuUZLhIdhyHFq1RpjEEYObt+bkmxV2HdLFEggvwi9al5XIcCQQ3TrBIMjuLAXBC3o85ert7JMB+QFy/eN4NI4GRH7HhwB+u8vuFvy0h3AAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\frac{2 a + 5}{\\left(a + 2\\right) \\left(a + 3\\right)}$$"
      ],
      "text/plain": [
       "    2⋅a + 5    \n",
       "───────────────\n",
       "(a + 2)⋅(a + 3)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(f2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to algebraic manipulations, the other main use of CAS is to do calculus, like derivatives and integrals of algebraic expressions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Differentiation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Differentiation is usually simple. Use the `diff` function. The first argument is the expression to take the derivative of, and the second argument is the symbol by which to take the derivative:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEgAAAAbBAMAAAAt2dQtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABYElEQVQoFY2SMUjEQBBFf+4kiSTGqGBhlUKwVcFO8IQUIihyYGWTTu1iIRaiXqWW1tpY2Qhq4VU2EQ6usrDWwtJG1FYCcXZn4xI8kptidubvY3aWGaDK2nNJFQLHH2pUQqbvfVdDl8ZPJQTYaR+QGfcBtYkxAnK9reaTXlsjN9YbkOoz+WvcAIslUAfwlk7nuZrmTnRIkRNjMMu+YIp3tRUh94xvxjUgoiKEZb7dpaN7MLEfc6ogKyOjb62zOgu4d7VNs1GAHt53pkZImWR1i9pLnNRWrXElr+X4h+L+mKFzqoSBaZnch+F2GK6IuI5XcXwKBxAEDKuOdOMfiMSFgug5it+EF5b/rgk5+ytWqXE7OYKVcKogN4XcItU4zfi2tYBRZvJKVoQLoWywTAWf9rrNgLMcqgd4Ecoqy6VjMSKG5LpwKPyjDimiAbPNFORi0snTsqX7K2AEOf7vlOtL6i9nYkLvuMXjdgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left(x + \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "       2\n",
       "(x + π) "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFgAAAAbBAMAAAAKd1XFAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBUq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABl0lEQVQ4EZWTvUvDUBTFTz9CY1swOCsWpyIIHdTJoZtuVkEEQXRwUpCABTtJFgc3BUUyKEHsoCBkSUVBKIijoyhKJ/8AHUSEivXel5eWp1XjGU7uue+X5D7yAoRVecANiyJqRfPhYUd/Dg0D0fw/4IoRHtZ2wrPqGAnrxzt1k5ci7+xdbCgKb2+3QMVIvPHioCB223OiewbETH2Jam2BG3qGvamrZsVFsoBI+YSHKW1zjom5uBJS4dS8bMMR8EoQ/asKY1+u6qaAJyl6s2szBQXWGqQM0CdhDwIeA1KOPhTbVODj7un1VepsSNjx4RHahpusp+Xo/hiRatKcYO7Sh9OGDy/Tk9GRF80j2x627T2u43jgSy8bcI4mDHTKiYFggz3IMSXh61rt9YYijUE9g50VwFnUOT6yCYmPQhtMu3PQXL8n4VQdH9wINgi8cDwAnqrjKHFNkrCWwyLHfjbWYeOOnF50MeVlLe6QJBy3cM9xi62lXz93QuyyBX85SKetFaroIKkaVaOS6IiqKqpRSd8e9OdvBXwCt6VWuiSpHUoAAAAASUVORK5CYII=",
      "text/latex": [
       "$$4 \\left(x + \\pi\\right)^{3}$$"
      ],
      "text/plain": [
       "         3\n",
       "4⋅(x + π) "
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(y**2, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For higher order derivatives we can do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAbBAMAAACekfw3AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiuzKJRN0MreaOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAByElEQVQ4EZWTPUjDYBCG3/6bNqmKKOgi6OCmQToKFgV/Fi2oi5NQFFy0W8HBdhF0kY5OtaM41cnRgigUBTs5d1I6iBVRB3/i3Zd8bVO1knfId/fePbkcfAGcSF1ecNJOvUVMOyQqOMs4Qx5wWHJGAP26U+LOKaDGmomDZqOe73G4I3Nl6TjPsZKTzs/TmwG0gkZ9rkEgAvWTewLk/iVPAjjt7NMRWXwGJoEB7qwNFZhrRRzycQmMGAZlfiKGgXkah4qsirOJGJVFJlK6IJSyNH8jAiWrygTpUadxMcCzNntL81lyxqZhGC+Aj6pCJqF9UBJMA2PYX7k3KxahXvUcrGeAtoRpiz2AcJTScAdwjhu910acoOjPk+Mqm7ZFLHHmLgE6tkRBTSa3h5LJKCc5r07PUJVjkvgq3sAkgFcOWXKPUDTMqZ3ohpIxvwqhdy6zJOFLt/MMrcwmiWeEYvATwZt3+auIi0KNGNfbS+TYNs9eX8yRRzdTewpWFQJZcsYG3AVKffwg+SbeZpAyjC+Kaa5nKr66Kwp14gjeAlmBvOU3Hi1vSbaxU8b2m+ih1Rpk3YQGh8KgvcdW5Nv+U63+qD/e9t9fS0O+AbbpaS6aluDxAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$12 \\left(x + \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "          2\n",
       "12⋅(x + π) "
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(y**2, x, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAbBAMAAACekfw3AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiuzKJRN0MreaOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAByElEQVQ4EZWTPUjDYBCG3/6bNqmKKOgi6OCmQToKFgV/Fi2oi5NQFFy0W8HBdhF0kY5OtaM41cnRgigUBTs5d1I6iBVRB3/i3Zd8bVO1knfId/fePbkcfAGcSF1ecNJOvUVMOyQqOMs4Qx5wWHJGAP26U+LOKaDGmomDZqOe73G4I3Nl6TjPsZKTzs/TmwG0gkZ9rkEgAvWTewLk/iVPAjjt7NMRWXwGJoEB7qwNFZhrRRzycQmMGAZlfiKGgXkah4qsirOJGJVFJlK6IJSyNH8jAiWrygTpUadxMcCzNntL81lyxqZhGC+Aj6pCJqF9UBJMA2PYX7k3KxahXvUcrGeAtoRpiz2AcJTScAdwjhu910acoOjPk+Mqm7ZFLHHmLgE6tkRBTSa3h5LJKCc5r07PUJVjkvgq3sAkgFcOWXKPUDTMqZ3ohpIxvwqhdy6zJOFLt/MMrcwmiWeEYvATwZt3+auIi0KNGNfbS+TYNs9eX8yRRzdTewpWFQJZcsYG3AVKffwg+SbeZpAyjC+Kaa5nKr66Kwp14gjeAlmBvOU3Hi1vSbaxU8b2m+ih1Rpk3YQGh8KgvcdW5Nv+U63+qD/e9t9fS0O+AbbpaS6aluDxAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$12 \\left(x + \\pi\\right)^{2}$$"
      ],
      "text/plain": [
       "          2\n",
       "12⋅(x + π) "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(y**2, x, 2) # same as above"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To calculate the derivative of a multivariate expression, we can do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x, y, z = symbols(\"x,y,z\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f = sin(x*y) + cos(y*z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\frac{d^3f}{dxdy^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAAUBAMAAABSee3BAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEM3dMnarIkSJZlS7me8N5bApAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADlElEQVRIDYVWXYhMYRh+zszsnvnbcVKrlJiGcOFiIzY/F5twpz0ulqRhifyUGrkg7DYlF27sUBsuZCKJUiPJjdjkpySmDUVkibhRu7V+1t943u/8zJzv7PLUO/u+z/d8z3vOe77ZM0AIB0JMkDDLjfXFxuIfecA1WQAkQjAHQlSQOBQom3WPnld3XUHsQ12puW7nioSOiO6mC+YECCMbKGF042zRoWJj9SXNdTJXJHRc0QmtTlaDRL9bpmyVNFvIDAUVUmmuCYoldLTrhFZHC0Fiulu6zTNtaB4NKqTSXI0hQEKD2aYRerlXIyJFh3CbR0b85uYKXxpyncslCQdG1/YjMsGmKq9J5a3PYL5l8eJMBVO7TiG24UKWgjyjde3+9baRt9FroYUbBG5zZtERKPGhr1j+eeVam1TI9TVJCQdTcd6ezzRdBpy8dAtNw0BvJfYx1o60PQ1oo+Ahr6eUXBjNJlJD2MSvTJYkUW/eVwKUeDawzDa/cTHkeo6khIPjOGvdYBqtACo3K4NIDwA5pEajJcRGj1mwKVjKo2IlRlKFgy1ZPGJTuSKi3nwRKyW+DzwGfrAMuV4jeQ2TFwhmwcI81kCmCCc38AGTbBi/SXbyisaiv9aJ4BPvHJEBqjI2vnDAwyQTudzMd7lcN1PnOSgxm9+BaEKu6CQp4UFEjszZEB9BZwfiQt8uAr+ME7UqczaHXBXQZ8V4rlVzVv6dy6GAErM5R+M1b3TVm4sRIQPijPnBqW6ki3fnv/cg+Z00x86L7uDHIJqGgHgbU8JrnqpiH6DE9ea6Kw5zh4SDqzxca5jK0XDydDe4m88cqUwV8e/XgV2seeBS1iY0WVylJHTgVgN8PEpcbx5ylcMmoRD/mR42y0wT7OPkLQNxOSu9ZZyMvUekdMvCA9ZnOO/KE/BF8Qp93NFSIkm4d24uPv2cs1Biztwde8j1BXdIKBjb1uQvS8YpurmxM/+NhLF7fQE9p3fg6IVLRdYc1pSu1pdl/ul/UwAiHVBwm0dqtRqbi3hVbfOq2tabf7rHcZ3FPRIa2us1n3oYjf9eB7ns/cdzm4c3KEZzNXl9EjqueMQWzCh6ecNf/8XSnDXkAPa7awanMDE014leLGnPZAnujWsmD14QLadtPpUs0/9Dc53oleq/9o/kO8Y19X5MxLuect13HVfrk5qr3IB3E75GksAPnsCKU6gvhc9f9LN/JwHXJKfL+AuGGQ+nZudVwQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$- x \\left(x y \\cos{\\left (x y \\right )} + 2 \\sin{\\left (x y \\right )}\\right)$$"
      ],
      "text/plain": [
       "-x⋅(x⋅y⋅cos(x⋅y) + 2⋅sin(x⋅y))"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(f, x, 1, y, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Integration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Integration is done in a similar fashion:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKAAAAAUBAMAAAD4uit9AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMnZUZs0Qu91E7yKJmaurDqYVAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACz0lEQVQ4EXVVX0hTURz+rtvu3bybWRD1pBcDH6rV6KHCoIYUBCqsh1ERxCWIIKRECqFBDCoifBk+SQSaYCARDJIMIRiDgihpBeKL0YoKwgKtSFaafffsnvtnzQN35/t9v+/3nfu75547oHZMeokub1AX60kPHcxXA2XMJfU+FwNNtsLL+fGUL1yoRsqKy4Z8FmrKzdRHp3z0WV8kgh4/VbTDgOHnZRQsSSTmqOELrWDQz5y3w/UMw76GoI4Kvb7RcdGzDhQglKvG6xk+9stxEsq2ztTUX2x+sGmXwWQDW1DbFoaKOPMG+j3E7JakoTo/k0Bv23NRR32c8riBaRPDE0oSeItzQBangZuG/ocCjWQvjho3UOhAwyKCKZIc0nA6odxXBqEZog64BEQDo9gNtT8+R90R3DZh4CpwBfhJIpwAnuGVeUBPzELrQyBLkkMaphFYChegLIk64BrwMJbCAFSIF24/wss7YBleBr6zsDEHmDjORjCGDQaURZLRdPrEeDpdJrvKsJWLrog6YILyRkOUbjGtHNSnayXLcMA1rKLIL7Q2Vw0ptO8wYi16iIsuizphiBFTWQIiZaZo+AjBisfQapkN8YeNbKcsS8hhGzp3uCrqRMuYRcMocFDohrEH+OgxtDZlLzejHVrZoms3JU3vxhIiFVEnNoUyagMlNMHalA4TGatfu+UoxT+0RR75WF+EuxQrUMUhN2U6iRfKXYQKog6YYW4OI0ls7e68RTyPO53duZa1zy1r7/f9LosW1Q/t8cN8/l/jfI9CzVRxSEP12848nrz8IurID/O6UHyXRyk4zkeLY7z8w3P0+BQhT4I09Iv5muUtZlbStQeNfI/MfcJFLlm0Q1UUypw78+PQlFIrkogaEjmzJiuvo599p5zEOiCDcFIzZLLO58v5wA7Fm3kSpb2s+G+eQqTttcNmHOSCSRcCXd6gLq79C/gHf4qzfkFAyyAAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\sin{\\left (x y \\right )} + \\cos{\\left (y z \\right )}$$"
      ],
      "text/plain": [
       "sin(x⋅y) + cos(y⋅z)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVAAAAA/BAMAAABEE43RAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsyme8Q6PJIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGz0lEQVRoBc1aXWwUVRQ+szvb7s9sd8NfJEY7FFP8CbKpBIOJMi/GB02cQKL4k3QFBIkP7IMRfJAuYkQepDWKsVFkIUZ80Q4/CUEfujExGh/oiqYICbGFqAmRpFAVAwKec+/89y7Lzmwnvcneuefce77zcWfunW9uAQhR5J4QwVGG9r0bZbbguWLXgsdGGjliRJoueLJ+LXhslJGJySizhciVvRwiOMrQzBXKJo2+Wg2RddV9DYJD4hN66m9WV6Q3G+S6Sbcy3qvfpBu7UqHwGXbqP7psBdjIzEBVtuHGEQ6fkeJEDwMM5QORpKB4oVFoOHyGzoni0u+tNMpWt78x0XD4DlEJ3069al0iDTqUR84VpDXdeuKTezdqOHbWCZDHPTHh8DkUm1H5H4CRoge7GSNuwPcaHIKO050ljCt+CbEJvG4cpLIHWyHxGRdOFGc0HNG3EKCaKhOkXDkFGYOBW5UcDp/BMKIhb03cUHA3zhVTbFFJcBZy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74H+3fHrrwwXmOgkNxserPIRNUFEcnEebFF3Wkd1ll5k40lXso/6zFF4UoPV+cQ4XNh3WbpEX3lHIFVgopO5g5GkKDFRgVyEF2BT/lPrqM7SiywxO8qj04fMGORK8DvgMdmH2LMccgUo4hJD4jHuZuMDVZzofHZMZL23hHIR8vARvubMozpLhlFOrisZ0QE8x1Oubd78IJCKHNL2TyR02gv6D2ncHYgjC/LOaO4GFvSL5CLAJewxRaObKNeVjKiBRNvZR8MeHNtRLJ5vY5sWik7uZjkDVV6iFoRILoJCRwBcNNp6kQLYjNaStRid3+KM0r8HiGh2m/ryPCIqo+h8nrmpK1gRExXJxX143r2A7iMd1XkWE+lKPVXIcqKwEz8oGVFlUhu5i4imSXQydzCSFCUmKpCLiSuZCbnERaOjF1li0pWQLVtER0rwI7xDPTshOYBEDSY6mZuND1SJiQrkotSzYPFm+6jOWXeYlXQlwAPrlBWTPwyfA+nh7vxzN77CjkUQKwF655LoJHcgijyoDlE/ousVSqJRLvsHTLvtI/pUAXwfJ5yBLUq4aBSIkulm6iNavKiMiVKiXOSFi0aRzBOFtdDnJSrPLccMETrKRV64aBToRVFUK31x75lOvBhXhfBuuQgivSiMaqHT9590knpHtYXoLYSSrnvAktVejz2DjE2qm0yi55jbnElt72qaA0dnEjk3F2mv7jKXJXA7n6El5v4z29PrZyhLojUnOm7/Aywn7A6s7fMGAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$x \\cos{\\left (y z \\right )} + \\begin{cases} 0 & \\text{for}\\: y = 0 \\\\- \\frac{1}{y} \\cos{\\left (x y \\right )} & \\text{otherwise} \\end{cases}$$"
      ],
      "text/plain": [
       "             ⎛⎧    0       for y = 0⎞\n",
       "             ⎜⎪                     ⎟\n",
       "x⋅cos(y⋅z) + ⎜⎨-cos(x⋅y)            ⎟\n",
       "             ⎜⎪──────────  otherwise⎟\n",
       "             ⎝⎩    y                ⎠"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(f, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By providing limits for the integration variable we can evaluate definite integrals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFAAAAAUBAMAAADo9qfkAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12VGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABuElEQVQoFXWTP2hTURTGf695r3lNkxgUh2Z6tEMHHVIKTqUEurkkINihQlNBLBU0Zqngkk0MDgotpNWh3bqoFToUOjRmKYKF4CBEBLN1aIb+kxYR43dNW/NK8sE57/u+c7jv3Hfvgw6wai2FaAys/uFki3VOr5wzQ6bgNsETn3cqPvjMCbgHCz6vKaKezwyWWIRHSZ/5T4Q0VQucl2ym2zbGW7oMXTfpfRqcwVyF8c9PsLfrq/LKijvXseYpFO0a9Eu7x0p9FfuVvUygdAuWpEcV2SrhPWekPCC+qwhllDaIHIWy2EfP05Sk3+rDVa4RSDhYCelvCrOqc6CUqsBx6PCjKEV5vOFSCcY0GSkIe3q6v5V+5OHQedowhhpxf5JK4mZETeMXrNj/FQ+uEjUHoFfTtcon+Gr6KGB7hGNmRiI9Hu7Jd3iogtlMIMMNIh7d4rvEZ3MzIn01Httz9GaraYakc4quhPuHmzv1FfFBNhuNXyLO3a0Yl2eneVbfyUsXjHe/vI8XfW30mqItmkeoKZuwltp2yTSX4gGT+dO6LkUnaNB3jJxVdc06QRf3RTl5VjX764CLv8JfPcJj8HGPsdwAAAAASUVORK5CYII=",
      "text/latex": [
       "$$2 \\cos{\\left (y z \\right )}$$"
      ],
      "text/plain": [
       "2⋅cos(y⋅z)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(f, (x, -1, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and also improper integrals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB0AAAAVBAMAAABI7vhRAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInarRM2ZVBDdiWbvuzJCz3LGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAr0lEQVQYGWNggADG/2DwAcplYHaAsSC0KCqXIR2VzzEBlc9WgMrvROUyrEblg7TrAx3wDSrMtIGBa12RtKIAkO8CxJwMDM8ZFjI9ADKZjgIJJSBmuMDcACQl9B0YGEC28xkAVQFB/wUG7gVAmm0DfwOQYmD7yMAJYvQ38DsAKQbGbwy7QLQmA88CEM1g3zADRN1mYF4AohneL08A0zCC9WgDjAmm2WGuhIkGwhhAGgDwdic2xV4k0wAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\sqrt{\\pi}$$"
      ],
      "text/plain": [
       "  ___\n",
       "╲╱ π "
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(exp(-x**2), (x, -oo, oo))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remember, `oo` is the SymPy notation for inifinity."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sums and products"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can evaluate sums using the function `Sum`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "n = Symbol(\"n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAADsAAAA9BAMAAADhUgydAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMs2Zq91U7yJ2iWZEELuNX9C8AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACFklEQVQ4Ec2Vz2sTQRTHv/nRpMnuklBPnhK8iYdW2lMlpgjSBg+KFytEsgVbb017bEESUARBSaBKoafgqT25oCh4ykWEXsxB8OTFP8AmodpYLOPszr7ZTXYz577Dznvfz763MzszPMC2KSB10HLckMeFm8AnPAwhQloFysh0xvFVGD0kzfE4MsCEpcBnmGgq8ABJBebfzrTHZytn/gjYGb/uJ5US0vMFf+3EY3806n+bOxuVhuLUecDTzLO/cnpyalWWdcQfL97XToI4zu6QqFVMcmU2KsekIZYn18M5ZpGIq+R5WGMDEpFyvdhi/xaJR7/JCx2/s5lQ3RV19keF8fako+JRlldho3aqwqiytorHmPJooeKrnvF2sOvWbDxQFcduXYX1XpCmrxdIzJnkeeM+briBseWp0ltGri6C6IIYh55lNNpC+Dmky6AqdiLtrSohGXfui+BjXYofpAekxSnwrUrf8OF3wm+YpBlFJ+Fw6fmrJvSWnuXA2Cb67La4ETO/msYAU2vrHU6i/UuO3eM74ZzJiHkXkz3UGLPzit4GMeeOGcY/aF0qGRwTXcTzQZkUfpUylv3dcEu2UO20whlX+b9+o9mLGjXeFbh9Bp76fx+9ZXcFlfGuoLIg1q5d3JEZIfirlazH7d96BQjil699LSiIUcIXRXFsYjbiFL8cVnyyi/IKpfOuMGL6AvYsV7O7wrD9B0+roo4Zb2oXAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\sum_{n=1}^{10} \\frac{1}{n^{2}}$$"
      ],
      "text/plain": [
       "  10    \n",
       " ____   \n",
       " ╲      \n",
       "  ╲   1 \n",
       "   ╲  ──\n",
       "   ╱   2\n",
       "  ╱   n \n",
       " ╱      \n",
       " ‾‾‾‾   \n",
       "n = 1   "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Sum(1/n**2, (n, 1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJ8AAAAPBAMAAAAIUwCQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWbdMokiu0RRNjIpAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACUElEQVQ4Ea2TP2gTUQDGf+k1TZo/7eHoYvwDXaqeRShODaVB0aEVmqn+CYoUVEiGDuJicNAOgg6Kk5jVyQ5FxCAJgpMOtzg4NSg4C61WQRu/995drt09wpfL+77fl/fevYPoylzNnddtbgFvab3Li0q7vQbtG63c46V22wqF9ls/zttv2TZDQkPqsPWG+n0VMBoy1SxM8rDf75eZ7qUbWd1tWeE9qV8xsC/A2k4SmpOLm7Yws37TfF8JmYUKF2AYPlEM010oW6Hu89sBXmVehcZ2ktAw4gqF6yrcD9mGDprtG8b+aCivT8sKX3qeTAfUA2fbjPnziI79aCifCb2/UG/BWI1iycTg20DMkgeF1nYZVyhayXiGq9da8EBDE5phD4owfvvVJSUwMSenNPNBobVdZnhAxz7FoLCDV1NhB+404TXM3GLI14r1y8mHJ0EMaMnWdpmEjn0hfNZ+qTBTS3V8vO8q3CStMzBjTCvkj8WAKTR2lBnQsW+Yy939ppB71zstRhpa8iOyO7BsTCtwKNi1ZGNHmYSO9/C0du7rmi0EnQkdSEYbZPUUnplCIx9hw08Kre0yu+nooUzoP07Mzc0fqYlc0RLLOoklM0NvSyNW+sGeQmu7zG46KlyApwKHQu52czp/LzWQ0yaVyP3QuJWj8LybzNDaLpPQbg9nGnqtspMCx0OWg+kmHFCGc0z5pEyhlVVSP+NN10Nxts0kNOnZ7TOMliksVRTJb2w181Vz+N75knz1rN6eg7qzMla92HMAi8dXms52mQGt7P+9/gFEfua1t0ciqgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$1.54976773116654$$"
      ],
      "text/plain": [
       "1.54976773116654"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Sum(1/n**2, (n,1, 10)).evalf()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJ8AAAAPBAMAAAAIUwCQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWbdiTJEuyIU2bFIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACpklEQVQ4Ea1TQWsTQRh9223TdLPZ5FBPHmqtIEIrS62V9tJQo1Q8ZNEGoShEoQp6aKQeDV0U1JPVgwiCGMRKrZTsQS9ejIIHT1bP1u5FUA9KGmnaYhvffNMUf4ALeftm3puXmfm+BRqP9fQ5aczTYI49CYEfcx+V/BI4OKtU8Qj0Xzy9pQhzsosBnLn5JCeNLqXAvIpTfLUtaOj1nf1wPHT4HD8ELmMg1B5t9NCsFlMR1gfrD77CWAX6RqsUgLiHRb7OqUDCMHAEkQoSd4BID6K7YOW1R4yGDycvimaHgd2YSGKNqyM68ENIDucmAxXUgBLsbgxx2bcC4ikYVYhHwFqAyb+iotk+IBO8LZtc1gj8pPJgtTKQYG4AEwEn1JGLBSRSsCsQj4C94Vpa0WzKZSCvj0duBNZejZeB2ypQwV7ukGPzARANCxjyYK9APNqYWZ3VCoQx55ELDBRJ9JHNmo9OmDlmCZSAKd7Ts5NAPwr4VURsXTzaCKs+qRXNAHudTXGPoY3AuosTrgUGCrTmjFKS6uMyiirQU4HKc0CMaH+/rBXN2B4puq3u7UBVn+nyThUogBuXSgHVpi4jYKA+sngEoilMaEUYjVn+WGpucavK9xl4vchAU4ESM64TIl4ZBANZFKMC5fEFmkLEtCKMKR7wE1hKbgey8abfpNOZPWcV5Bh4BYkqA9vT6eWj8QVEq6o5p8sCQ9RHRBEG7IAT8jL+CXzN+6GrSW2OcC2Mbap32ybH0tgteYhHgPvCF1E04xcbCXtYas6rI7N9m4tmJx0JFUi44B7yEfXRkeOYTXkXvYH2iNGeRFQrmn2fmzmGWzBWgJbh2oiUaGY0YJmWfvsCVlZ9/O+yn4nz9REMnj9OJh6BF2NKV4qwqXp9DXb2TJmT//n5CzSjDuDy+pXbAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$1.64493406684823$$"
      ],
      "text/plain": [
       "1.64493406684823"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Sum(1/n**2, (n, 1, oo)).evalf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Products work much the same way:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAC8AAAA9BAMAAADPFy0PAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMs2Zq91U7yJ2iWZEELuNX9C8AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABXElEQVQ4EWNgAAEhBgbOlQvATBRC2JWBYRdDCooYhJPKwBDDwH8BUyaVgfcjA3sCNgnGHwysG7BK/GVgnYBV4gcDO1YJoB38B7DpwOGqdAaGydj80RzvycBlZYtp0iAUYf+PAB+R3cdx5P+/DhDY/19NAFmCgeH9FzA///8FVHGG9VCJr2jiQ1zilEfnFEiCQfOgwbsJvD/AfkWVYEwIYuCAhCaqBC/vHwbuD1h0MLB9YGBRwCbBrMDAvwEc0KhGMbAvYMi/sACkBU2i/wDDIm4BLBJHGRhatoHE0XWAxQZe4j0k3axHS1ccwv//v2lg4JX6/1+tAeFUBgZw2v3AwAdKwShpF1kRPdjAkg4rAJV02AGwpMMOkCW4rSUnw1WhSJzfwN7AogQE2gwMyBK9U5GKT2QJBk+G41iNYihkMGYEG6WJahTHB4aYRJgWYEkHBzwODLM2QHmgkg4GAOSmp5CzUioBAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\prod_{n=1}^{10} n$$"
      ],
      "text/plain": [
       "  10   \n",
       "┬───┬  \n",
       "│   │ n\n",
       "│   │  \n",
       "n = 1  "
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Product(n, (n, 1, 10)) # 10!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Limits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Limits can be evaluated using the `limit` function. For example, "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAPBAMAAAArJJMAAAAAHlBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACGjDitAAAACXRSTlMAVO8Qq5l2zWYZcMvdAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAHUlEQVQIHWNgAANGZQYGk5DJQDYbqQSr03QPsBkAJYgIYEZbtZEAAAAASUVORK5CYII=",
      "text/latex": [
       "$$1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(sin(x)/x, x, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use 'limit' to check the result of derivation using the `diff` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKAAAAAUBAMAAAD4uit9AAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMnZUZs0Qu91E7yKJmaurDqYVAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACz0lEQVQ4EXVVX0hTURz+rtvu3bybWRD1pBcDH6rV6KHCoIYUBCqsh1ERxCWIIKRECqFBDCoifBk+SQSaYCARDJIMIRiDgihpBeKL0YoKwgKtSFaafffsnvtnzQN35/t9v+/3nfu75547oHZMeokub1AX60kPHcxXA2XMJfU+FwNNtsLL+fGUL1yoRsqKy4Z8FmrKzdRHp3z0WV8kgh4/VbTDgOHnZRQsSSTmqOELrWDQz5y3w/UMw76GoI4Kvb7RcdGzDhQglKvG6xk+9stxEsq2ztTUX2x+sGmXwWQDW1DbFoaKOPMG+j3E7JakoTo/k0Bv23NRR32c8riBaRPDE0oSeItzQBangZuG/ocCjWQvjho3UOhAwyKCKZIc0nA6odxXBqEZog64BEQDo9gNtT8+R90R3DZh4CpwBfhJIpwAnuGVeUBPzELrQyBLkkMaphFYChegLIk64BrwMJbCAFSIF24/wss7YBleBr6zsDEHmDjORjCGDQaURZLRdPrEeDpdJrvKsJWLrog6YILyRkOUbjGtHNSnayXLcMA1rKLIL7Q2Vw0ptO8wYi16iIsuizphiBFTWQIiZaZo+AjBisfQapkN8YeNbKcsS8hhGzp3uCrqRMuYRcMocFDohrEH+OgxtDZlLzejHVrZoms3JU3vxhIiFVEnNoUyagMlNMHalA4TGatfu+UoxT+0RR75WF+EuxQrUMUhN2U6iRfKXYQKog6YYW4OI0ls7e68RTyPO53duZa1zy1r7/f9LosW1Q/t8cN8/l/jfI9CzVRxSEP12848nrz8IurID/O6UHyXRyk4zkeLY7z8w3P0+BQhT4I09Iv5muUtZlbStQeNfI/MfcJFLlm0Q1UUypw78+PQlFIrkogaEjmzJiuvo599p5zEOiCDcFIzZLLO58v5wA7Fm3kSpb2s+G+eQqTttcNmHOSCSRcCXd6gLq79C/gHf4qzfkFAyyAAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\sin{\\left (x y \\right )} + \\cos{\\left (y z \\right )}$$"
      ],
      "text/plain": [
       "sin(x⋅y) + cos(y⋅z)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFMAAAAUBAMAAAADwRznAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHaZIu+JVM27RDKrZt2dj8xZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABlUlEQVQoFXWTPUvDUBSG31jbfDUxFBQEh9BKXYRmEkXEiBRRUDo46RJaEXEKgrMZHDo6uAiKXXS0/gMFN0EIRZyLCK5qQWg76IlJ9V6jBxLOc86T+8G9Af6Lfp/t3LPwO1/kCmmbQx72ORQ8DjnQLA5R5pEl3WYJGOSRpRUWKJdqEHImigYgTFRcDGW3oY41PGrl6MmML+XNSEhYSIklFKhcdNVV9QmyOQw0iU/oa0d71L1I0DwsJDwcUasKsas7ULtnBkziZyBlpF5FOxLEJoykiTcaoUXtKxd419ujlGKNapAO0BPUF6BuqF1AIR17NaAtbH1YoQoMmJSFQqBOoq/EjNqag9YhgRZA01zSKxSUJnAM+ZoKVUBMWlA6O8AdMW1LNAroMyKBtoUp1H1qFX1sqOuQnF0Dh8QVmtk9xXJPSDjASHnWppZwk7cxv3mL88ZDjfiCOtnMDI0SClKwFlrMH8EebCDQ6aU9IdhFLL6vSyTQddF92Yx5QSFYcBChEFxCJTv9VYm9elc7FGQ7JvwUYj/MJ3CTXM0O35peAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$y \\cos{\\left (x y \\right )}$$"
      ],
      "text/plain": [
       "y⋅cos(x⋅y)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(f, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\displaystyle \\frac{\\mathrm{d}f(x,y)}{\\mathrm{d}x} = \\frac{f(x+h,y)-f(x,y)}{h}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "h = Symbol(\"h\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFMAAAAUBAMAAAADwRznAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHaZIu+JVM27RDKrZt2dj8xZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABlUlEQVQoFXWTPUvDUBSG31jbfDUxFBQEh9BKXYRmEkXEiBRRUDo46RJaEXEKgrMZHDo6uAiKXXS0/gMFN0EIRZyLCK5qQWg76IlJ9V6jBxLOc86T+8G9Af6Lfp/t3LPwO1/kCmmbQx72ORQ8DjnQLA5R5pEl3WYJGOSRpRUWKJdqEHImigYgTFRcDGW3oY41PGrl6MmML+XNSEhYSIklFKhcdNVV9QmyOQw0iU/oa0d71L1I0DwsJDwcUasKsas7ULtnBkziZyBlpF5FOxLEJoykiTcaoUXtKxd419ujlGKNapAO0BPUF6BuqF1AIR17NaAtbH1YoQoMmJSFQqBOoq/EjNqag9YhgRZA01zSKxSUJnAM+ZoKVUBMWlA6O8AdMW1LNAroMyKBtoUp1H1qFX1sqOuQnF0Dh8QVmtk9xXJPSDjASHnWppZwk7cxv3mL88ZDjfiCOtnMDI0SClKwFlrMH8EebCDQ6aU9IdhFLL6vSyTQddF92Yx5QSFYcBChEFxCJTv9VYm9elc7FGQ7JvwUYj/MJ3CTXM0O35peAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$y \\cos{\\left (x y \\right )}$$"
      ],
      "text/plain": [
       "y⋅cos(x⋅y)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit((f.subs(x, x+h) - f)/h, h, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "OK!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can change the direction from which we approach the limiting point using the `dir` keyword argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABMAAAALBAMAAABv+6sJAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEKvvZom7mXYyzVQi3UQ6SGZXAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAaklEQVQIHWNgYBBgAAIQwaj82YGBIayogYGBbQLHLwapDQxTGRg8GRj2J6xkYGA5wACUYP0LJBgcQEyGfBDRAGYm/wNqd2BwZGDgiDE+wMBxgIGdgSGcYb4dgytQolxtAwNjvXEAUDncNgBJUBUwaYAbUgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\infty$$"
      ],
      "text/plain": [
       "∞"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1/x, x, 0, dir=\"+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACMAAAALBAMAAAAHCCkxAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEM3dMqvvZom7mXZUIkRJD0iWAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAfklEQVQIHWNggAMBEAtMQIUYw74VMDB0Lt0AV8LA6cD9iUHoAIMHQqiEgeH8BBUGBvYLDELGIKDCANTA8RmkC6gdCkC8+SAChCEAxJr2j4GBsQAmwlDIwMDdm3aBgfsCXIiLgaGLwT+PoQIuwsC4KvIAA+P6tAaEENThAgwMAMSLGqu/gFQwAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$-\\infty$$"
      ],
      "text/plain": [
       "-∞"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1/x, x, 0, dir=\"-\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series expansion is also one of the most useful features of a CAS. In SymPy we can perform a series expansion of an expression using the `series` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVoAAAAwBAMAAACiZ6/NAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWaJ3SJEuzID+9VZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFlElEQVRoBdVYXWgcVRQ+m7s7+zObyUptXwo2WkQQlSENgeBDQy1BWrCLJmofqgNpCuKDEcUFiXQRBX+QLmhWIm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8ZWKZ464FY0ygBnbek2uRCu1qg1QS2eOuAWTq3sPndYmnXWV7Vaw/U3dWD1XofzZ4JaPHXCrXFo7RHXkBxLtVoLfQ9qzls3Vp5Vt58f+ykgQky2+k7ABFk8wS8+FzAS0UcX6yCOvlz3afVaHnaHz7n/05bSF/I5DB93B10xBKftoKsyk/BQQDSz+IJPfK7JgCz7+iF7HdIF8bFPKxl9qOVzw37TJ9b9nzShq5eM6i7CIkF4Au3OCOeUffcA3ALP40oboQPg56ApWy+Eu3G9rhlwl8JI0Hz0nsgYTzBLvKfLZeh2gCP1HwD2xAxYshC62KehXmwH9xjGrSxAiMMTVBi3WBIeqePaOh92FEokJtRupqL21m3n3IKjNW7113G7FXP7KMjUGLcY8dm+K+jWorUOulPlzqqd9dvz5I8Tq+WgyA8nQcEA9cXG3ZDy1/Rr+GGBTOrlSdhKwgeU9gaa75MxomHC/SZJoaDCu80N6Ti2sW7JrBsDU0U6bg3glE1RKEjUebejINabCVTWDWLjTNwZdFumOBQMAutWwx+PT9meAiXdNJYowpdkkgPM2ErBjQcP/j4cUTnzdifotUWU0pMoomkTwB30cVKyhPOWIjxBdHdwyj6jCFoNdwdlG6SS/EtM/2h6ZIbUJiui+eKvBPiCvxTMa8qx/WJ66hDkCuKuKL0FSBrfSmboPFMjdYLwBG83vBIvCHHKvpONxhqI6nFKGoT+r1v/ACMLtG2aUvPeAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$1 + x + \\frac{x^{2}}{2} + \\frac{x^{3}}{6} + \\frac{x^{4}}{24} + \\frac{x^{5}}{120} + \\mathcal{O}\\left(x^{6}\\right)$$"
      ],
      "text/plain": [
       "         2    3    4     5        \n",
       "        x    x    x     x     ⎛ 6⎞\n",
       "1 + x + ── + ── + ── + ─── + O⎝x ⎠\n",
       "        2    6    24   120        "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series(exp(x), x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default it expands the expression around $x=0$, but we can expand around any value of $x$ by explicitly include a value in the function call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAvwAAAAoBAMAAABwRjOsAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEIl2mSJE3e9UMqtmzbsXyEShAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAJmElEQVRoBe1aXYwkVRU+1T3dM93z1/wYNtlsaJklEhJiLzsmJrDSLmM0LOLEB1GyYTpxkzUmOpvg+POwMhj/EAxNgKg8uB3ezQzBDRvRbKO7cR/EHUOERBy3X0xIRHZZQYLrZDw/t6pu3bq35lZ1v5hwH26dc+75zvnu6Tu3qm4NwPstZwX+kNPf4R6sOwZymK/e/4kc3qFrMRSiCwMpczA3vxgyGOJa3hgCrEO/oiuF5OoKfDY/sBgK8xQGMseji5VmfrIpxJcaKVMxQ3no5f/5bnCXT+7SPR/X3LxR889qKBR9gYQaUKe3ysNwbU83ZMn1Xx3u28eDH9nturU05wDrTgB/Tar5tVO/u91rLdwIv9GC+6Luhns0FIq+QHStvZWEAkw+uv9F0+bUq9tXHGOTHmv2lcqqA50wL3nVLgFJKi6OSS+AFVhbjG2+qINwPgaR5AtE16tT5Z/1qFuUrvq0qzT6RCL3pFBeD1pJi13z+SXtSGW9nDkaD34dljqx5osCc/X7A2FfuvzNmMGOUrXjcnnNNRDbJ7qxnCXVUxyzvC1j3wEoW8wW0wltNXmjzH3WGwj1x1NTm2gCeC1KYl+9c14jrM2n8o6mOMTZJxxg0/9l05BTPwCB54PnA1pkb9QBo1zeQCgFqfLjA+O1ixqLTLHSmRpYHcaOWc0J41K76uGFkHMJmKYc1uSEOJWYQfnO8B3ECYCzhC+1sQubN8rcfGIgQKUfhj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ap/L/zeAHxnUoqVPOjPC2IX4ZRnwFV/+Q5Z/o2RIUsk30C8F00IKueMrv4j8MdDx9dTfM9fQw5V+AYOjNB+ITLJ1aIXn4PyT67Ja7Hcfy93Oj6BNf0Byi/MQVb70nhrr1Arycn7kdUfme3Z7D+iRUFnO4i+upYqsfc33hhRdOP1Lg1smbD3HF943zRfY9bY7X9TRlGPG6IdcBfhNoQzl/+ddw78/PW+WaGOSH8gEj48+BeuEuEERBKhkHPrmivpHL2+Z87w17n7fZs22TzaDAk4/KNTvIDm4dpdXP+PGu+V/TVv9M432Zo96DQcfb1eV4YXsb76O5295DvdwYkFyli1f6ubF8vsv4YO62AqlzJ3wf8P9Rgf8BNeLlKiPUeWYAAAAASUVORK5CYII=",
      "text/latex": [
       "$$e + e \\left(x - 1\\right) + \\frac{e}{2} \\left(x - 1\\right)^{2} + \\frac{e}{6} \\left(x - 1\\right)^{3} + \\frac{e}{24} \\left(x - 1\\right)^{4} + \\frac{e}{120} \\left(x - 1\\right)^{5} + \\mathcal{O}\\left(\\left(x - 1\\right)^{6}; x\\rightarrow1\\right)$$"
      ],
      "text/plain": [
       "                         2            3            4            5             \n",
       "                ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1)     ⎛       6\n",
       "ℯ + ℯ⋅(x - 1) + ────────── + ────────── + ────────── + ────────── + O⎝(x - 1) \n",
       "                    2            6            24          120                 \n",
       "\n",
       "        \n",
       "       ⎞\n",
       "; x → 1⎠\n",
       "        "
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series(exp(x), x, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And we can explicitly define to which order the series expansion should be carried out:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 5780 characters]",
      "text/latex": [
       "$$e + e \\left(x - 1\\right) + \\frac{e}{2} \\left(x - 1\\right)^{2} + \\frac{e}{6} \\left(x - 1\\right)^{3} + \\frac{e}{24} \\left(x - 1\\right)^{4} + \\frac{e}{120} \\left(x - 1\\right)^{5} + \\frac{e}{720} \\left(x - 1\\right)^{6} + \\frac{e}{5040} \\left(x - 1\\right)^{7} + \\frac{e}{40320} \\left(x - 1\\right)^{8} + \\frac{e}{362880} \\left(x - 1\\right)^{9} + \\mathcal{O}\\left(\\left(x - 1\\right)^{10}; x\\rightarrow1\\right)$$"
      ],
      "text/plain": [
       "                         2            3            4            5            6\n",
       "                ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1) \n",
       "ℯ + ℯ⋅(x - 1) + ────────── + ────────── + ────────── + ────────── + ──────────\n",
       "                    2            6            24          120          720    \n",
       "\n",
       "            7            8            9                      \n",
       "   ℯ⋅(x - 1)    ℯ⋅(x - 1)    ℯ⋅(x - 1)     ⎛       10       ⎞\n",
       " + ────────── + ────────── + ────────── + O⎝(x - 1)  ; x → 1⎠\n",
       "      5040        40320        362880                        "
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series(exp(x), x, 1, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The series expansion includes the order of the approximation, which is very useful for keeping track of the order of validity when we do calculations with series expansions of different order:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMQAAAAwBAMAAAC8i8hXAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWbdMiJEibtHdKBDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADm0lEQVRYCcVW30sUUR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pCVDtuAfEcr7xH4froTxzkrGzpHWQLZsvNEmzS+AdBPIFl+sjY2muOORlgogW1yxRrIREjnOFKRfdHX17TQ4Wo0UXhFRjoVDb7JqLMcb/nJhvofDO3R9EBajpdIax4LS2mLR5Ij/kfoLzgzy5MB/1+MAAAAASUVORK5CYII=",
      "text/latex": [
       "$$1 - \\frac{x^{2}}{2} + \\frac{x^{4}}{24} + \\mathcal{O}\\left(x^{5}\\right)$$"
      ],
      "text/plain": [
       "     2    4        \n",
       "    x    x     ⎛ 5⎞\n",
       "1 - ── + ── + O⎝x ⎠\n",
       "    2    24        "
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = cos(x).series(x, 0, 5)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAF8AAAAcBAMAAAD1rn4EAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsyme8Q6PJIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACCUlEQVQ4EY1Sv2tTURT+Xsx7SV5+9FKh0MnXCm61obhYB99SHBv8A0wUwbFO6qJDUcShtCiCwcEIuuhglg7i0Afi4CBJ7SDSwS4OTiYiCCLEc867971cSfFdOOd833fOx7l5N0DG4+3vZpzUY5dxOpvhnR67jV6Yy2CpdPXQHaw23Pr/HSWVzLxSuJKQf4C7O7P5ULSPaWcdKKb2VGe04G5/GDUI5IdJw+sDOYpJ50hwQWFxi1o8pc9NqvmBYVL9QNM95yswxc1aR0twW24E3DVUqjG4w2oXKPFtespMvH/xkvC+oZah2PeJl3nDrWTg+2hE+CTF9PGVE4HoZkMzYlo+oPSZwpk/df4SVT5NBadVWC9uCTOGL3KPWp/ENxQzuBHckwG6YgRPeUM/FG4MO8J6DSoPKC5iTT0TCfTacFDqMvPa7Uff2u06wSeKhdecNigUHjOUs9qhMhUIBsyGsxEJlZ+s8gbgh2ROtEHuFQvGsMZrZrkVGyp/4j7lXghfLSInd0g2HPtFb7otQ2coP80NMKct9JWanR2ci6nZUBgtr+yFotF3d3+XB/mr2kDvcnR++pOmxoC3mxtRPMHffWlu4bqeP+SlTZdqTX5JKtxPISEntCiTasuSnPTfbukpqRykmFChbtFJZNkSi5FFJ5FZS7xmsYnE74/J+e4YOQw+H2tU1RjJCv8Cl4Bl3Hr5MZEAAAAASUVORK5CYII=",
      "text/latex": [
       "$$x + \\mathcal{O}\\left(x^{2}\\right)$$"
      ],
      "text/plain": [
       "     ⎛ 2⎞\n",
       "x + O⎝x ⎠"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = sin(x).series(x, 0, 2)\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAF8AAAAcBAMAAAD1rn4EAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsyme8Q6PJIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACCUlEQVQ4EY1Sv2tTURT+Xsx7SV5+9FKh0MnXCm61obhYB99SHBv8A0wUwbFO6qJDUcShtCiCwcEIuuhglg7i0Afi4CBJ7SDSwS4OTiYiCCLEc867971cSfFdOOd833fOx7l5N0DG4+3vZpzUY5dxOpvhnR67jV6Yy2CpdPXQHaw23Pr/HSWVzLxSuJKQf4C7O7P5ULSPaWcdKKb2VGe04G5/GDUI5IdJw+sDOYpJ50hwQWFxi1o8pc9NqvmBYVL9QNM95yswxc1aR0twW24E3DVUqjG4w2oXKPFtespMvH/xkvC+oZah2PeJl3nDrWTg+2hE+CTF9PGVE4HoZkMzYlo+oPSZwpk/df4SVT5NBadVWC9uCTOGL3KPWp/ENxQzuBHckwG6YgRPeUM/FG4MO8J6DSoPKC5iTT0TCfTacFDqMvPa7Uff2u06wSeKhdecNigUHjOUs9qhMhUIBsyGsxEJlZ+s8gbgh2ROtEHuFQvGsMZrZrkVGyp/4j7lXghfLSInd0g2HPtFb7otQ2coP80NMKct9JWanR2ci6nZUBgtr+yFotF3d3+XB/mr2kDvcnR++pOmxoC3mxtRPMHffWlu4bqeP+SlTZdqTX5JKtxPISEntCiTasuSnPTfbukpqRykmFChbtFJZNkSi5FFJ5FZS7xmsYnE74/J+e4YOQw+H2tU1RjJCv8Cl4Bl3Hr5MZEAAAAASUVORK5CYII=",
      "text/latex": [
       "$$x + \\mathcal{O}\\left(x^{2}\\right)$$"
      ],
      "text/plain": [
       "     ⎛ 2⎞\n",
       "x + O⎝x ⎠"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand(s1 * s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to get rid of the order information we can use the `removeO` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAG8AAAAwBAMAAADtMzlxAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEHarIkSJZt3NVLsyme8Q6PJIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACRUlEQVRIDc2Vv2vUYBjHv8klubt4XsMVOqdVCg6HtS3oIJhBORfx8B/o6XCLSG/TSQ8VsTjo2MOhFR0VuiuYyU0aFNqhFBwVRe78hagYn8vv5p6noA76Dnmf5/t9PnnfhDxvgGBUjjY6YZS/GvVDeSmb6/7HbJqJN7GVyUZC/aE1ooXCBq4ITiDrTdndfcUXdWlJ5Z18T0BrFtcE/4AtGJFc+ST5u261YCmfJXB9VXJIrzragLXpCU93WCcUDcvwePs6LguvrbbvxLSN9sYIp0zNnzmHZ9PzI04gKK3i1dIdzpvAJXuJM0KNNjkwHc4/i0XrAWeEmoLyGu9auMsbsTpmx1F+/pAXdubr7s48ySo/kpAJTOsgVPaF31P7mGSISFpYfYoGZ+vf9vS1DueE2vhUbZO1ldnJ+kWZ+78d0/7D/f07MOyn39h2tFW5n6R7RWDUT346CFCWb9JYcikcS40+pUavt/ym15uhGrGfqIwdyVuV+4nlkIBiP/FcDMr9JHAxKPaTxMVg3E+ntp4MS59z9dr2YzfVk2cMJGUGi02KbqUFaTQOI/NnUJzUAQoWqq8A9W1WjGP6i7+P4/xc7aJAh1KDPQpfA8fcPBHl5UEAtlhwxZJB4ksDFB0WJPO2JaxI8kILNenU1r/IHK4BLQks0Vctjb0eTFcCtyWK9DowAQFUPRk0PRy/Pzf3c5YreQTN4fShRt/afprYD6DiQXXI5IZ2o/2yS8Z3zjzZvnCE04damU6ILnDeP8xUrPj+V0b+K+kXPYuKl+Zu0xMAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\frac{x^{5}}{24} - \\frac{x^{3}}{2} + x$$"
      ],
      "text/plain": [
       " 5    3    \n",
       "x    x     \n",
       "── - ── + x\n",
       "24   2     "
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand(s1.removeO() * s2.removeO())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But note that this is not the correct expansion of $\\cos(x)\\sin(x)$ to $5$th order:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANsAAAAwBAMAAABqLhIyAAAAMFBMVEX///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sY3fuPsuziWHBaMq1waCKVWV8/eWl2tolmWONftyDT2ehxfstMZPMTOMbOGgHbjR04c9PNFTLCJ9l5jCrcZw65O7Bx4dTEbvxccnkgvRCSL6AV2B/YzYb+c0Dk2bNBv/JIYOqQXxQOf6HF8czMveOAjeBY7Bz6Z+o0/Kkt6UTzwiU67R6fbtgfiOpM6B65D/cYflSW9KB74nL68fMbxMbbOxM4Ru6s8OfeVpMz1CM1OFH4iW3mvS94/GIb0fm6EnNmC883Qxf3BFrwcvuyWXLcRQhLtw0WBRsSgfhQmhVYCe9Ri6MrWWBtTsycPyWjUK2xLSJo9s7qRvAnjKzoSElhuRMsScZQMb844ktvjyDdTovsrt1/KeU3yehzjdA/EAa/cqSmHA0oj1xJgc11wek3zlc1ekBBWLm+Z4WclEfL8qeAWLcFRma/WVKiHsXI4vvDO/9Gp1PNpx4WDctdtjuzRwPfyfM93Mtdk5R4G4ROeM0Ma5e5u5W4B+K17yApSWmkdMl7XklBy2NXhY7dE/p7ndBPOV7QqrNwxSN/QBgxMHN/yu5YqsfjYXw+CefXR3Z5tVe7/iP0Lu0QQYvWiWkQAAAAASUVORK5CYII=",
      "text/latex": [
       "$$x - \\frac{2 x^{3}}{3} + \\frac{2 x^{5}}{15} + \\mathcal{O}\\left(x^{6}\\right)$$"
      ],
      "text/plain": [
       "       3      5        \n",
       "    2⋅x    2⋅x     ⎛ 6⎞\n",
       "x - ──── + ──── + O⎝x ⎠\n",
       "     3      15         "
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(cos(x)*sin(x)).series(x, 0, 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Linear algebra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Matrices are defined using the `Matrix` class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "m11, m12, m21, m22 = symbols(\"m11, m12, m21, m22\")\n",
    "b1, b2 = symbols(\"b1, b2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGgAAAAyBAMAAABCJ4MDAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMA74lUMhBEqyJ2u93NZplQnf8bAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABo0lEQVRIDe2WsUrDUBSGf5s0VmNF1MXJbuJSKT5AFeuewb1FfABXnQoubnYSfAAFFyk+QcAH0c2hFqpg7Rbvudfc60lPh9ZJyIEL4f73a8pP8hFsJh+YanaSd6w1DqdicNSoYX06hE7v55ApTRcRbF3enAXbd1KPUqahwuI9mteoAX5slsN5Vn2IVKShgzDGVQXP8F+7ejkGLCu059WvGihqtlCPcA54XbMcxbKw7Y9SCHWgB2xIEMvCVmlooTfgEcGoKNwJLIP3aaFVFIdYji8kiGUIOymk7lkeYKlSESCeoaoYU0TQxcIpvP6eAPGs/GIhujAz3l6amJKOcaI2Mq+Gt6JCtaRR+/7T7u0YVOp9dWhJDO3PJclgDJIOC3uZvyecELZy6KeUvIg/FyEJMX3kpExX7oRIMiRhuuGZJEuSIQnz11hZUibKUsuQXnk3VpaUibLUMuSQlSVlsixJhhnIylKLUpIlyTADWVlqUUqyJBlyyMlSi1KSJcmQQ06WlImyJBlyyPVImSRLLcMJsqRMlCXJcJIsKfsvspzp022Wj8RvbKDHm//7t/sAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left[\\begin{matrix}m_{11} & m_{12}\\\\m_{21} & m_{22}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡m₁₁  m₁₂⎤\n",
       "⎢        ⎥\n",
       "⎣m₂₁  m₂₂⎦"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = Matrix([[m11, m12],[m21, m22]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB4AAAAyBAMAAAC5cHbcAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMA74lUMhC73c2rRHaZImaqCQggAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA9klEQVQoFWOQ//+JAQb0/39kEHZxhXEZQlwMGUTgPBDDEcRnTSyACYL5DBwTUPn8Bqj89QGofHUYF2IeQ+fuw1ARiPk/AuIvMHiAhMB85q8M/Ao+0+F8ngaGeAUGczif4wDDegMkPv8DhvMByPwFrHIMSHzOB2wHkPmsp88BzUKYB+Rg4RuCBCH+BbHu1M9G4YPEkOXpyKdLfDAdOwrzHzg+dBlmwPjg+Khl8L8A9T84PqoY7i+A8iHxwWAPTAXg8OMHxwdDNUw/JD6YDsD4kPh4A+QihTeXApcDMt/D2AxmHkghg/z//0ASET9gQSL46OkdLT8AAPfcTiuXVltrAAAAAElFTkSuQmCC",
      "text/latex": [
       "$$\\left[\\begin{matrix}b_{1}\\\\b_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡b₁⎤\n",
       "⎢  ⎥\n",
       "⎣b₂⎦"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = Matrix([[b1], [b2]])\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `Matrix` class instances we can do the usual matrix algebra operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false
   },
   "output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{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡    2                               ⎤\n",
       "⎢ m₁₁  + m₁₂⋅m₂₁    m₁₁⋅m₁₂ + m₁₂⋅m₂₂⎥\n",
       "⎢                                    ⎥\n",
       "⎢                                 2  ⎥\n",
       "⎣m₁₁⋅m₂₁ + m₂₁⋅m₂₂   m₁₂⋅m₂₁ + m₂₂   ⎦"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJEAAAAyBAMAAACufiRQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMA74lUMhC73c2rRHaZImaqCQggAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACnklEQVRIDe2Xv2sTYRjHvya5Jj3TJqjgIOhtKnaoOHRwqNAKDg7n4qQEBxftEP0HPBAhBYdOdQhIKO7tbDsEHFoRasBVMf+BJaWggXg+z/O+6f16cx6mk+0LB+/7vN9+evfcm3wILvk9jD1yvl/F2cXbY4NgLS5UcW58DhMKQrIe1TPh7p8fHVMklFZGR8I7y+FFdK5JldloOb66qQv78Y1grUnrblAyzTTJPjBtqpomXR6dkB1NyqW0U5OWNz/IX9hXv2027WtP4mBNKjW3VBsMOUWyfrq1Du4AuYmnqL3HdXqpnroUU5Nqr6wBZpbcWI4r+hTkD1Bx7r4BFooevjv4gcLHulwKBE2ad9HLdSa9aE4qmlT2UHMwB7i1Fii+CuSpJXzRuNJoPGs0+ADswOoVO4VBNCcVTSq1sT7LJMwD28CFCInK+p5ew94vtqb60ZyqqD5VuvjsCmkHeA57YIXvKSD9RpnOAfWC7i6Uk4omtayLENIZWH2UvS9mUh/3ukBxA4jkpKJIk92JtpDoxqf3UHIcM+ktlgDMANEcV4af4E+7NKc+2XWcXkF+95aZ9GCrC0y34zmuaBJNaHDHD0fo3VFNd1x21/DuMKXejFTU06kdOo/ByFcpRZcaD4cT+t8vbzwOVpxRlRDp64tmkJja/rXBV1AZzk75/t5wTg2jjKqESMH2P81OSNnadhz6dGJOPgv/jTnZhgWPnkiG/qYzmJNzqeZkG7I9o6SkOTmXbk6xYQZzci7dnGJDTaL70k+XNCfn/mJOtmGCZDCnWJNdqr4LKklzsg0TJIM5xZrsUkUymJNtmCAZzCnWTDUn2zBBMpiTc+nmZBsmSHImoubkVao5xYYZzMm5dHOyDbOYk3PHxJxH96vsyH4p/gHeHTH4ApAfnQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left[\\begin{matrix}b_{1} m_{11} + b_{2} m_{12}\\\\b_{1} m_{21} + b_{2} m_{22}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡b₁⋅m₁₁ + b₂⋅m₁₂⎤\n",
       "⎢               ⎥\n",
       "⎣b₁⋅m₂₁ + b₂⋅m₂₂⎦"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And calculate determinants and inverses, and the like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKYAAAAMBAMAAAAaIdvMAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMARImrIna7EFTvMt3NZpneUCSWAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABq0lEQVQoFWWTsWvUYBjGf0mvvVxij3CjDgbUtTh4Xexwg4KTvVJ01kHdSv4AoZ2kUugVwdmOHYqeopOCoVPpn+BghUIdesKdVhyv7/udfm9iH3jge35JHt43IQRXbt1rB9ce4nWe+Et6aN24vdxt3U8NniPhzCNWl3gOcTGxkbmVHHVF2eNu/XcabcEuhMffwROXhO4kBYsZh8RHfWcjYWeqUFcq47VP1H4yO+T1TbjKAkY0Kc1XU3o58xD1J/Yk6cSnau18+lF1APX6iOgPUw9gA97zpmNEk1J6cAIvrdOTJK2N1HJTSdGQZp9G13W+ZTHFE5e0cwDvCE7rfk4jOpBzqZJmQZJxuZO7iXRHI5q08wWyTbO4Y52ekMg06rIaGfK6BrW1SecHMIIk6ZTV5H1fyDLfaYQ5KVOXJcsewl7onib8hq7/l2iSzqDP9HWiH9u+08is3KKu6BIsw+aXSedXuWZEk+7u9e+7e0CLi85GKiedKAu2jblU7dySDyL2ivfXn6g9+O8g/8nu+rPcqEtCvWonv7pqD2iMx0O1kcrp7uc2r8bjEtMk9AyNtKqM6qgiUQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$m_{11} m_{22} - m_{12} m_{21}$$"
      ],
      "text/plain": [
       "m₁₁⋅m₂₂ - m₁₂⋅m₂₁"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAa4AAABSCAMAAAA7DMB+AAAAPFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAo1xBWAAAAE3RSTlMAMquZdlQQQOkwRM3d7yJmu4lsc/NKdgAAAAlwSFlzAAAOxAAADsQBlSsOGwAACQdJREFUeAHtXemisygMpS6db7rZTt//XQeQcE3CIgho+11+3LYhQA5HMYblCuFJp6sn41d8vB449d3teFb9WuTtgfGXLm/fHDDjl64DkiLE6a1TR42rS9fUT6dh6MUwPE+0ZSHCuV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Y1PnxBFp9x9fRWVu9ekzUEbkpcZyalDV/PTJwQ+fiLasasVtkzOzSOdc6sGCI8xGDY/fULg4ydWsxFVZLtfoyV+FDQXNmjzI/dtpF5qeL7Xubuanz5hTjP3gMwX08k544FbRzxQs1lX59yqkR3JqULXDqdPCHT8RKAXU7Pw7lfjgVvvHDbmPtWapdwQSIJJVehCZwskGLNUTR340fETy4o2fseTc8YDt474xsqTi2fRFXv+wq6JmF7I2tSBv8Ql4rCHTc7B/GPdaUiHJVqURxdeHMO2Me5w+oRAx0/40KbL6eQc7KA2jnh6hdtK5NBFn79s0/gOp08IdPzEtj4JlU7YhIyihKE6E/Jy6KKLY9g2xh1OnxB1J92hR1M2UqMoIRuBoMa0zzy68POX0WV3P1O9igM/On4irQ8StI0H7vTOWTXLKCEbgZj2KkEWXez5a17OzYfY4fQJ/b+KViFup+SMIEIf5ZmRRRd7/hK64CgIqpcw8CejgTaTC1YrQKKEpI/yms2iizVFTPE4uSkDP3lOk/OKmAHyfxVV2NjqaCZfRPoor6ISdLFtjDucPiHQ8RN5fVG31GHo4jCbnz4h8PET3KL9JQemq/npE/O/G92fE68FbATyagYzSgyGjgbwaRAOhZgo9Tm9ucGYQcfIn+nSseSSu7wbnz4h/1f2MbqzohXqXU8mOVmm/oHyoE7/Lpeanj6h/y9AOduPWZP+N9e1Lsump08IevzEMTv8yFaFT58oa3nLNY1lLT9ObexMiHqmNWyqHoi1Nf8PHzi4lSipunoAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left[\\begin{matrix}\\frac{1}{m_{11}} + \\frac{m_{12} m_{21}}{m_{11}^{2} \\left(m_{22} - \\frac{m_{12} m_{21}}{m_{11}}\\right)} & - \\frac{m_{12}}{m_{11} \\left(m_{22} - \\frac{m_{12} m_{21}}{m_{11}}\\right)}\\\\- \\frac{m_{21}}{m_{11} \\left(m_{22} - \\frac{m_{12} m_{21}}{m_{11}}\\right)} & \\frac{1}{m_{22} - \\frac{m_{12} m_{21}}{m_{11}}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡ 1          m₁₂⋅m₂₁                -m₁₂        ⎤\n",
       "⎢─── + ────────────────────  ───────────────────⎥\n",
       "⎢m₁₁      2 ⎛      m₁₂⋅m₂₁⎞      ⎛      m₁₂⋅m₂₁⎞⎥\n",
       "⎢      m₁₁ ⋅⎜m₂₂ - ───────⎟  m₁₁⋅⎜m₂₂ - ───────⎟⎥\n",
       "⎢           ⎝        m₁₁  ⎠      ⎝        m₁₁  ⎠⎥\n",
       "⎢                                               ⎥\n",
       "⎢          -m₂₁                       1         ⎥\n",
       "⎢   ───────────────────         ─────────────   ⎥\n",
       "⎢       ⎛      m₁₂⋅m₂₁⎞               m₁₂⋅m₂₁   ⎥\n",
       "⎢   m₁₁⋅⎜m₂₂ - ───────⎟         m₂₂ - ───────   ⎥\n",
       "⎣       ⎝        m₁₁  ⎠                 m₁₁     ⎦"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.inv()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solving equations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For solving equations and systems of equations we can use the `solve` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEoAAAAUBAMAAADYerbFAAAALVBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAOrOgAAAADnRSTlMAdt3NMolEEFTvq5lmIsfapuIAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAB3SURBVCgVYxAyYSAEDqsxhBFSA5QPQ1bFMR2Ljs4CVFVVu55jqGJfvQ5NFQMjpioGBrlBrqpYCQSAgTuYXV9ngBb+6KHKvfLxGgYmdVRVu+ZZHUCNbbB8L6oqMA8lTYBFAohRxS5AjCoeLIqA7hJSwSaOIiakBgD0ZimSClhTrgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left [ -1, \\quad 1\\right ]$$"
      ],
      "text/plain": [
       "[-1, 1]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(x**2 - 1, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjEAAABLCAMAAAC2lyZIAAAAPFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAo1xBWAAAAE3RSTlMAMquZdlQQQOkwRM3du2aJ7yJs4cVMPgAAAAlwSFlzAAAOxAAADsQBlSsOGwAACPhJREFUeAHtnOnaoygQhYkmTk8WTcb7v9fBpUpFwTosytNJfjSYTlEvh/MpIom6tP2rUPJXM4Sk+FcOYXwyBczQppHI4zCiXnD2iLlJjktbXvXrBrA8gc8e9dEcmajvZ7LFy113NilapS7thXomLR/SDx74uRyZqPtnskXOXXs55lKSFPmUOTKROmeyxc7t55i6Ji3yKXNkInXOZIud288xJXwZI+3SlTkyUW/PZIud288xkS+NJGxQmSMTdehMtti5/RwTb/pNmoaXOTJRr85ki53byzEfZOmGVEtc5shEXT6TLXpuL8fcrqRFPmWOTKTOmWzRc3s5prmTFvmUOTKROmeyRc/t5ZhXRVrEKD/X2fq+d4ORmWIgcV/C2D5BLIG512Pj5Zi4k6kiyhkrRyayTBhbmD7Rc/s4pnqRFDHKT5TbvxyZSJ0wtjB94uf2ccy9IS1ilCXyBNSaMEcmgg1jC9Mnfm4fx0BPuUk2W1n1p83HtVKXEG3iMwUjcY+D2Hp9/FnCcysjuY9jig+LYVZub3RSPNjk3c3uQk5ddiYcSfVMwUiszcR2PEt4bmUI4eMY22SqKsqiBR0znGLUoym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uWvpIjE/X6TLaEuQHHPL1/Qoo0jF/myES9PJMtYW7AMS90+Z+kS1jmyETdPZMtYW7AMY18GwOJlrzMkYk6fSZbwtyAY27OzTGk07FljkykwJlsCXMDjrHvFSONji9zZCIVzmRLmBtwDCnxK79agZ9jvnr4PTr/c4yHaF8d8nPMVw+/R+d/jvEQ7atDfo756uH36PzPMR6ifXXIzzFfPfwenf85xkO0rw75Oearh9+j84Nj+q1QO/ulPBr/hfxdCjyHLXP6ywrX/uXx0zB/lyC/3uwoUA9GUf8DYe6QJALMc1YAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left [ - i \\sqrt{- \\frac{1}{2} + \\frac{\\sqrt{5}}{2}}, \\quad i \\sqrt{- \\frac{1}{2} + \\frac{\\sqrt{5}}{2}}, \\quad - \\sqrt{\\frac{1}{2} + \\frac{\\sqrt{5}}{2}}, \\quad \\sqrt{\\frac{1}{2} + \\frac{\\sqrt{5}}{2}}\\right ]$$"
      ],
      "text/plain": [
       "⎡        _____________         _____________        ___________       ________\n",
       "⎢       ╱         ___         ╱         ___        ╱       ___       ╱       _\n",
       "⎢      ╱    1   ╲╱ 5         ╱    1   ╲╱ 5        ╱  1   ╲╱ 5       ╱  1   ╲╱ \n",
       "⎢-ⅈ⋅  ╱   - ─ + ───── , ⅈ⋅  ╱   - ─ + ───── , -  ╱   ─ + ───── ,   ╱   ─ + ───\n",
       "⎣   ╲╱      2     2       ╲╱      2     2      ╲╱    2     2     ╲╱    2     2\n",
       "\n",
       "___⎤\n",
       "__ ⎥\n",
       "5  ⎥\n",
       "── ⎥\n",
       "   ⎦"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(x**4 - x**2 - 1, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "System of equations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH4AAAAVBAMAAAByPkciAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAZpkQ3Ynvq81UMrtEdiLw+n06AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABrklEQVQ4EZ2UMUjDUBCG/+Sl2NI2uugqHcQuQiY7lSoiTpWAi4sguIgg1EFxDIK4Zupog6tDuzmI2KmbtFS0k6i7g1pEJ/HeS1KTl4iQGy69/97HXe5eCkApIJmd3XKOLSejieo45JTXBDybmSRWtQhNbZKTLLMlCXKYttkqoQukx/Cl2aEMSPEUsO6hMTzy//EXQMNwSyfiP4GuHeDLg5e7HvWo1USjMfXLc9DFVFboBHsnvufyYxbFplLV6Il57hDXv1lHnm+KbZPTP4Cm6a6u7tAgjNQw80QJz6L1dbuIbMvP61SfeBwB5zskMqRHOX4myjPsYrzn817/GKt6lyiQi+eRG6Lp+Dxofg0bOCGBvz9143DvW7Q+VAv3fhq4BKYN9/35JcoYj8gbv+kYPlsDLd03uj/FwP3p2td45rnA/CuWf1g81Vbujf8Q84dmM5qbuDrclR7KhQmedfevHnztQVviwsjYfv9bBHz/YBsDJ8CLhOxOJYEmINmovqS7oRlSD1FphwQKBP/X9896oeOLuArFPHA7Oo7oQlDC8k3fCQsUddpcyiT+/1sDfgDNr2XH9LJoSQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left \\{ x : 1, \\quad y : 0\\right \\}$$"
      ],
      "text/plain": [
       "{x: 1, y: 0}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve([x + y - 1, x - y - 1], [x,y])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In terms of other symbolic expressions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {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v+ICl24fdxbxmifII23p2Ne93aTdCP+yc89ZET2OGP2AT9oIYnyLFod1skR2L0anmbfgcuO/KYUGVgcob7BpmXAsnsD0/tkT5CHCqeRveAV7G3FmobEYVe6yr3bgj69JZIrfGqeZt2GTIhv4lYeiuRSOlyKnmbcjqnDZoMfai53u2WFOKzGqFDAOqr9BO9wOjtlaJrGqFDBUbEihlrylFVjXTsEV5oH0KGuxRQIqUIquaadhIU3gcVtXOqpSUqURWNd38MzMeoTksXgctTrGvB3spk2KlyKoWMn+SgvyjhrTyHoTjhGE38JEyKVaJ7GpfRkx57QEprW5q8hmF2smp/mYKaawUWdV2HbXVZ+yJMzatrHx3AmviZ58PzRTSWCmyqu3g4r9Q1gIGAlZHdQAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\left \\{ x : \\frac{a}{2} + \\frac{c}{2}, \\quad y : \\frac{a}{2} - \\frac{c}{2}\\right \\}$$"
      ],
      "text/plain": [
       "⎧   a   c     a   c⎫\n",
       "⎨x: ─ + ─, y: ─ - ─⎬\n",
       "⎩   2   2     2   2⎭"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve([x + y - a, x - y - c], [x,y])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Further reading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* http://sympy.org/en/index.html - The SymPy projects web page.\n",
    "* https://github.com/sympy/sympy - The source code of SymPy.\n",
    "* http://live.sympy.org - Online version of SymPy for testing and demonstrations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Versions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "application/json": {
       "Software versions": [
        {
         "module": "Python",
         "version": "2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]"
        },
        {
         "module": "IPython",
         "version": "3.2.1"
        },
        {
         "module": "OS",
         "version": "Darwin 14.1.0 x86_64 i386 64bit"
        },
        {
         "module": "numpy",
         "version": "1.9.2"
        },
        {
         "module": "matplotlib",
         "version": "1.4.3"
        },
        {
         "module": "sympy",
         "version": "0.7.6"
        }
       ]
      },
      "text/html": [
       "<table><tr><th>Software</th><th>Version</th></tr><tr><td>Python</td><td>2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]</td></tr><tr><td>IPython</td><td>3.2.1</td></tr><tr><td>OS</td><td>Darwin 14.1.0 x86_64 i386 64bit</td></tr><tr><td>numpy</td><td>1.9.2</td></tr><tr><td>matplotlib</td><td>1.4.3</td></tr><tr><td>sympy</td><td>0.7.6</td></tr><tr><td colspan='2'>Sat Aug 15 11:37:37 2015 JST</td></tr></table>"
      ],
      "text/latex": [
       "\\begin{tabular}{|l|l|}\\hline\n",
       "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n",
       "Python & 2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)] \\\\ \\hline\n",
       "IPython & 3.2.1 \\\\ \\hline\n",
       "OS & Darwin 14.1.0 x86\\_64 i386 64bit \\\\ \\hline\n",
       "numpy & 1.9.2 \\\\ \\hline\n",
       "matplotlib & 1.4.3 \\\\ \\hline\n",
       "sympy & 0.7.6 \\\\ \\hline\n",
       "\\hline \\multicolumn{2}{|l|}{Sat Aug 15 11:37:37 2015 JST} \\\\ \\hline\n",
       "\\end{tabular}\n"
      ],
      "text/plain": [
       "Software versions\n",
       "Python 2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]\n",
       "IPython 3.2.1\n",
       "OS Darwin 14.1.0 x86_64 i386 64bit\n",
       "numpy 1.9.2\n",
       "matplotlib 1.4.3\n",
       "sympy 0.7.6\n",
       "Sat Aug 15 11:37:37 2015 JST"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%reload_ext version_information\n",
    "\n",
    "%version_information numpy, matplotlib, sympy"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
