{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# First Step in SageMath\n",
    "\n",
    "The objective of this class is to present tools and classical data structures to interact with SageMath."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## Find help\n",
    " \n",
    " To find help, just type the name of the mathematical concept/object that you wish to use (with first letter in upper and lower case) followed by the question mark and press shift+Enter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Permutations?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you don't know the name of the mathematical function, you can guess it by writing the first letters and hit the TAB key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Permu<TAB>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also see the source code of a program by putting two question marks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Permutations??"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - You can find numerous tutorials in the page Help of the interface jupyter. There are and complete API of the available objects.\n",
    "\n",
    " - Try to find a programming tutorial of Python, a tutorial on linear algebra, a tutorial to use symmetric functions and the API on binary trees.\n",
    " \n",
    "### Reference\n",
    " \n",
    "  http://doc.sagemath.org/html/en/tutorial/tour.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Polynomials"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Roots of a polynomial\n",
    " \n",
    " Look in the documentation how to write a polynomial and define the following one.\n",
    " $$\n",
    " P = x^3- 3\\cdot x^2 + 1.\n",
    " $$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw this polynomial on the interval $[1,3]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determine the roots of this polynomial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What do you notice? Do you find this normal?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to find the approximated values of roots when we specify the interval where to look for the root. Look for the function that allows to do this and determine an approximate value of the three roots by specifying the three intervals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - multivariate polynomials\n",
    "\n",
    "Find in the documentation the way to define the ring of polynomials $R$ with two variables $y$ and $t$ with rationals coefficients."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define the polynomial $Q = y^2 - 1 - 2\\cdot t + t^3$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show it in 3 dimensions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - polynomial interpolation\n",
    " \n",
    " Find the (minimal degree) polynomial that passes through the following points: $[1,2]$, $[3,2]$, $[-1,-1]$ and $[4,3]$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Polynomial factorization\n",
    "\n",
    "Find the factorizations of the polynomial $54\\cdot x^4+36\\cdot x^3-102\\cdot x^2-72\\cdot x-12$ over the following rings: $\\mathbb{Z}, \\mathbb{Q}$, and the algebraic numbers. With a bit more search, for $\\mathbb{Q}[\\sqrt{2}], GF(5)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matrix calculus\n",
    "\n",
    "- Inverse of matrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Look in the documentation how to define a matrix with rational coefficients. Define the matrix:\n",
    "$$\n",
    "M = \n",
    "\\left(\n",
    "\\begin{array}{ccc}\n",
    "2 & 4 & 1 \\\\\n",
    "8 & 9 & 2 \\\\ \n",
    "3 & 7 & 3\n",
    "\\end{array}\n",
    "\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Do the same thing but in the Symbolic ring."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Verify that it is invertible by computing the determinant:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute now the inverse of this matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Kernel\n",
    " \n",
    " Now define the following matrix:\n",
    " $$\n",
    " N = \\left( \n",
    " \\begin{array}{ccc}\n",
    " 1 & 2 & 3 \\\\\n",
    " 4 & 5 & 6 \\\\\n",
    " 7 & 8 & 9\n",
    " \\end{array}\n",
    " \\right)\n",
    " $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determine now the kernel of the kernel:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get a basis for the kernel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Solution of linear equations\n",
    " \n",
    "Look up how to determine the solution(s) of the following linear equation:\n",
    " $$\n",
    " \\left(\\begin{array}{rrr}\n",
    "2 & 4 & 1 \\\\\n",
    "8 & 9 & 2 \\\\\n",
    "3 & 7 & 3\n",
    "\\end{array}\\right) \\times x = \n",
    "\\left(\\begin{array}{r}\n",
    "2 \\\\\n",
    "2 \\\\\n",
    "1\n",
    "\\end{array}\\right)\n",
    " $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Diagonalization of a matrix\n",
    " \n",
    " Let $M$ be the matrix\n",
    "$$\n",
    " M=\n",
    " \\left(\\begin{array}{rrr}\n",
    "0 & 1 & 3 \\\\\n",
    "1 & -1 & 0 \\\\\n",
    "1 & 0 & 1\n",
    "\\end{array}\\right)\n",
    "$$\n",
    " \n",
    " \n",
    " \n",
    " Find how to diagonalize $M$ by using the eigenmatrices and its diagonalization (if possible)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Representation in Jordan form\n",
    " \n",
    "Define the following matrix $M=\n",
    "\\left(\\begin{array}{rrr}\n",
    "5 & 1 & -1 \\\\\n",
    "-1 & 7 & -4 \\\\\n",
    "0 & 0 & 3\n",
    "\\end{array}\\right)\n",
    "$.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the matrix is diagonalizable, we can use eigenmatrix to compute the diagonal matrix. Otherwise we should not do it!\n",
    "\n",
    "Try to diagonalize the matrix $M$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show the eigenmatrix, what do you notice?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, we see that the eigenmatrix is not invertible. This is disapointing! In fact $M$ is not diagonalizable and we should not have used the method eigenmatrix_left.\n",
    "\n",
    "In fact, we must put the matrix in the Jordan form. Look for the function and give the eigenmatrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Verify that you really obtained the Jordan form."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise :** Compute $M^{11}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Derivation and Integration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Define the following functions: $$sin(x)$$ \n",
    " $$fx^3+x-1$$\n",
    " $$\\ln(x^4-3)$$ \n",
    " Look in the documentation how to derivate a function and compute the first derivative of each functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Compute the third derivative of the functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Compute the following integral \n",
    "    $$\n",
    "    \\int x\\sin(x^3)dx\n",
    "    $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - and this one :\n",
    " $$\n",
    " \\int_0^1 \\frac{x}{x^2+1} dx\n",
    " $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Differencial equations\n",
    "\n",
    "## Basic commands\n",
    "\n",
    " - We begin by defining a variable $t$ and a function $x$ that depends on this variable:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t = var('t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = function('x')(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read in the documentation how the function 'desolve' works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, take the differential equation of the first order:\n",
    "\n",
    "$$y'+ 3y = e^x$$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "desolve?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Vous pouvez obtenir la solution particulière vérifiant y(0) = 1 avec la commande :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " **Exercise :** "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Resolve the following differential equation of the first order:\n",
    " $$\n",
    " y'-2y/(x-1)=(x+1)^3\n",
    " $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Knowing that the second derivative of the function $x$ w.r.t. $t$ is obtained with *diff(x,t,2)*, resolve the following equation of the second order with constant coefficients:\n",
    " $$\n",
    " y\"+3y=t^2-7t+31\n",
    " $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Graph theory\n",
    "\n",
    " - Digraph\n",
    " \n",
    " Look how to define the directed graph with vertices $V = \\{0, 1, 2, 3, 4\\}$ and arcs are $E = \\{ (0,1), (0,2), (0,3), (1,0), (2,4) \\}$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Shortest path\n",
    "\n",
    "Determine the shortest path from 1 to 4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Combinatorics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - Cartesian product\n",
    " \n",
    " Look in the documentation how to define the following cartesian products:\n",
    " $$\n",
    " \\{3,4,5\\} \\times \\{6,7,8\\} \\times \\{ 3 \\}\n",
    " $$\n",
    "\n",
    "List the elements of this product."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - subsets\n",
    " \n",
    " Also, list all subsets of \\{1,2,3\\}."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List now the subsets of size 2 of $\\{1, 2, 3 \\}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 7.5.beta3",
   "language": "",
   "name": "sagemath"
  },
  "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
}
