{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<span style=\"font-size:8pt\"><i>ENSAM-Bordeaux, Mathématiques et informatique. Date : le 14/11/22. Auteur : Éric Ducasse. Version : 1.4</i></span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sympy as sb\n",
    "sb.init_printing()\n",
    "from IPython.display import display # pour remplacer print\n",
    "print(\"Version de sympy :\", sb.__version__) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <span style=\"color:#0066BB\"> **Calcul formel : TD n°1, seconde partie** </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-danger\"> <span style=\"color:#800000\"> <i>Pour chaque exercice, faire exécuter la partie <b>exemples</b> cellule par cellule $\\left(\\mathtt{Maj+Entrée}\\right)$, avant de passer à la partie <b>Travail à faire</b>.</i> </span> </div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color: #0000BB\"> *Exercice 4* </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 4.1 Objectifs </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#005500\"><i> Savoir dériver symboliquement des expressions mathématiques et remplacer dans celle-ci un symbole par un autre symbole ou une expression.</i> </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 4.2 Exemples </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### On peut récrire une expression en remplaçant un symbole par un autre symbole ou une expression, à l'aide de la méthode $~$<span style=\"color:#B00000\"><b><tt>replace</tt></b></span>  :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,t = sb.symbols(\"a,t\")\n",
    "T = 3+2*a*sb.sin(t)-4*sb.sin(a*t)\n",
    "T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Syntaxe : <span style=\"color:#800000\"><b><tt>expression.replace(old, new)</tt></b></span>, où <span style=\"color:#800000\"><b><tt>old</tt></b></span> désigne un symbole ou une fonction, renvoie une <u>nouvelle expression</u>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[T, T.replace(t,0), T.replace(t,sb.pi) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = sb.symbols(\"n\", integer=True)\n",
    "[ T, T.replace(t,sb.pi).replace(a,n) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ T, T.replace(t,sb.pi).replace(a,sb.symbols(\"n\", integer=True)+sb.S(1)/2) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ T, T.replace(sb.sin, sb.cos), T.replace(sb.sin, sb.exp) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T.replace(sb.sin, lambda x:x ) # Fonction identité"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\bullet$ Ne fonctionne pas lorsque que <span style=\"color:#0000B0\"><b><tt>old</tt></b></span> est un nombre ou une partie de sous-expression* :<br><br>$\\hspace{10mm}$ <span style=\"font-size:8pt\"> $(*)$ Pour certaines sous-expressions, cela fonctionne néanmoins mais le pourquoi du comment n'est pas exigible.</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ T.replace(2*a,10), T.replace(4,a) ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### On dérive une expression à l'aide de la méthode  <span style=\"color:#B00000;font-family:Courier New;font-style:normal;font-size:12pt\"> $~$diff </span>  :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y,a,b,c,t,w = sb.symbols(\"x,y,a,b,c,t,omega\")\n",
    "wave = sb.cos( a*x + b*y ) * sb.exp( sb.I*w*t )\n",
    "wave"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Dérivée première par rapport à $x$ :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wave.diff(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Dérivée seconde par rapport à $x$ :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wave.diff(x,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Laplacien $\\displaystyle\\Delta=\\frac{\\partial^2}{\\partial x^2}+\\frac{\\partial^2}{\\partial y^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lap = (wave.diff(x,2)+wave.diff(y,2)).factor() ; lap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Opérateur de propagation acoustique $\\displaystyle\\frac{1}{c^2}\\,\\frac{\\partial^2}{\\partial t^2}-\\Delta$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(wave.diff(t,2)/c**2-lap).simplify()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 4.3 Travail à faire </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$a)$ Définir l'expression <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">D0</span> $\\displaystyle=(x+b)\\,\\exp(-a\\,x)$. Faire afficher les dérivées première et seconde de <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">D0</span> par rapport à $x$, notées respectivement <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">D1</span> et <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">D2</span>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$b)$ Vérifier que la fonction $\\displaystyle x\\mapsto (x+b)\\,\\exp(-a\\,x)$ est solution de l'équation différentielle $\\displaystyle f^{\\prime\\prime}(x)+2\\,a\\,f^{\\prime}(x)+a^2\\,f(x)=0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$c)$ Calculer $\\displaystyle f^{\\prime\\prime}(x)+2\\,a\\,f^{\\prime}(x)+a^2\\,f(x)$ **<span style=\"color:purple\">sous forme factorisée<span/>** pour les fonctions $f$ suivantes :<br /> $\\hspace{6cm}$\n",
    "$\\displaystyle x\\mapsto\\exp(-c\\,x)\\quad$ et $\\displaystyle \\quad x\\mapsto(a^2-d^2)\\,\\cos(d\\,x)+2\\,a\\,d\\,\\sin(d\\,x)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$d)$ Même question pour $x\\mapsto f(x-x_0)$ et $x\\mapsto f(-x)$, où $f(x)=\\mathtt{D0}=(x+b)\\,\\exp(-a\\,x)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color: #0000BB\"> *Exercice 5* </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 5.1 Objectifs </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  <span style=\"color:#005500\"> <i>Savoir intégrer symboliquement des expressions mathématiques et déterminer la valeur exacte d’une intégrale.</i> </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 5.2 Exemples </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#007070\"><i>Primitives</i></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Calcul direct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = sb.symbols(\"t\")\n",
    "[ (t**n).integrate(t) for n in range(10) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = sb.symbols(\"alpha\")\n",
    "sb.integrate(t**a,t)  # équivalent à (t**a).integrate(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Si on spécifie que $\\beta>0$ :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = sb.symbols(\"beta\", positive=True)\n",
    "(t**b).integrate(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Calcul en 2 temps : définition de l'intégrale sans la calculer, puis calcul (déconseillé en général)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\star$ Intégrale non calculée :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pne = sb.Integral(sb.log(t)/t**2,t) ; pne"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\star$ Calcul de l'intégrale par la méthode <b><tt>doit</tt></b> :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pev = pne.doit() ; pev.together()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\star$ Vérification :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pev.diff(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#007070\"><i>Intégrales</i></span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n,m = sb.symbols(\"n,m\", integer=True, positive=True)\n",
    "ps = sb.integrate( sb.cos(2*sb.pi*n*t) * sb.cos(2*sb.pi*m*t), (t, 0, 1) ) ; ps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#007070\"><i>Intégrales généralisées</i></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Transformées de Laplace : $\\displaystyle F(p)=\\int_0^{\\infty}f(t)\\,\\exp(-p\\,t)\\;\\mathbb{d}t$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p,w,a = sb.symbols(\"p,omega,a\", positive=True)\n",
    "sb.integrate( sb.sin(w*t) * sb.exp(-p*t), (t, 0, sb.oo) ).together()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ sb.integrate( f_de_t * sb.exp(-p*t), (t,0,sb.oo) ).simplify() \\\n",
    "      for f_de_t in [sb.sin(w*t), sb.cos(w*t), sb.sin(w*t)*sb.exp(-a*t)] ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* $\\displaystyle \\int_{-1}^{1}\\frac{1}{\\sqrt{1-t^2}}\\;\\mathbb{d}t$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.integrate( 1 / sb.sqrt(1-t**2), (t,-1,1) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 5.3 Travail à faire </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$a)$ Déterminer une primitive de $\\displaystyle t\\mapsto\\frac{1}{\\sqrt{2\\,\\pi}}\\,\\exp\\!\\left(\\frac{-t^2}{2}\\right)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$b)$ Déterminer pour tout réel $x$ : $\\displaystyle \\frac{1}{\\sqrt{2\\,\\pi}}\\int_{-\\infty}^{x}\\exp\\!\\left(\\frac{-t^2}{2}\\right)\\,\\mathbb{d}t$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$c)$ Vérifier que $\\displaystyle \\frac{1}{\\sqrt{2\\,\\pi}}\\int_{-\\infty}^{\\infty}t^2\\,\\exp\\!\\left(\\frac{-t^2}{2}\\right)\\,\\mathbb{d}t=1$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$d)$ Plus généralement, déterminer $\\displaystyle I_n=\\frac{1}{\\sqrt{2\\,\\pi}}\\int_{-\\infty}^{\\infty}t^{2\\,n}\\,\\exp\\!\\left(\\frac{-t^2}{2}\\right)\\,\\mathbb{d}t$, où $n$ est un entier naturel non nul."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$e)$ Vérifier que $\\displaystyle I_n=\\frac{(2\\,n)!}{2^n\\,n!}$, en utilisant la fonction <b><tt>sympy.factorial</tt></b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color: #0000BB\"> *Exercice 6* </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 6.1 Objectifs </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#005500\"> <i>Savoir créer une fonction indéfinie et la manipuler.</i> </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 6.2 Exemples </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y,z,t = sb.symbols(\"x,y,z,t\")\n",
    "f = sb.Function(\"f\")\n",
    "[ f(e) for e in [x,y,z,t,1,sb.S(2)/5] ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x).diff(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.integrate(f(x),x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ sb.integrate(f(x).diff(x),x), sb.integrate(f(x),x).diff(x) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x,y,z).diff(x,3,y,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sb.Function(\"g\")\n",
    "f(g(x)).diff(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(sb.sqrt(1-x**2)).diff(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.sqrt(1-f(x)**2).diff(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 6.3 Travail à faire </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "L'objet de cet exercice est de retrouver l'expression du Laplacien en coordonnées polaires"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$a)$ Définir les symboles réels $x$, $y$ et $a$, le symbole $r$ positif ou nul (« non négatif ») , ainsi que les fonctions <b><tt>F_c</tt></b> et <b><tt>F_p</tt></b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\star$ Le Laplacien en coordonnées cartésiennes s'écrit :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try :\n",
    "    delta_Fc = F_c(x,y).diff(x,2) + F_c(x,y).diff(y,2) \n",
    "    display(delta_Fc)\n",
    "except Exception as e :\n",
    "    print(\"Erreur :\",e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$b)$ Définir les expressions $R=\\sqrt{x^2+y^2}$ et $A=\\mathrm{arctan}_2(y,x)$ (fonction <b><tt>sympy.atan2</tt></b>),<br />$\\hspace{4mm}$ puis en déduire $\\displaystyle\n",
    "\\Delta F_p(R,A) = \\frac{\\partial^2}{\\partial x^2}\\,F_p(R,A)+\\frac{\\partial^2}{\\partial y^2}\\,F_p(R,A)$.<br />$\\hspace{6mm}$Une fois le résultat obtenu, le développer, puis le simplifier, puis le développer à nouveau."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$c)$ Arranger le résultat précédent en remplaçant dans l'ordre $A$ par $a$, $x$ par $(r\\,\\cos(a))$ et $y$ par $(r\\,\\sin(a))$, puis en simplifiant deux fois de suite. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color: #0000BB\"> *Exercice 7* </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 7.1 Objectifs </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#005500\"> <i>Savoir obtenir le développement limité d’une expression par rapport à l’un de ses paramètres, <br />au voisinage d’une valeur donnée et à un ordre donné.</i> </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 7.2 Exemples </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### *Développement limité en zéro*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,h =sb.symbols(\"x,h\")\n",
    "sb.exp(x).series(x,0,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f =sb.Function(\"f\")\n",
    "f(x).series(x,0,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### *Développement limité ailleurs qu'en zéro*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Le résultat est un peu lourd :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.log(x).series(x,1,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On peut préférer plutôt :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.log(1+h).series(h,0,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### *Développements limités généralisés*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(1/(1-sb.cos(x))).series(x,0,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "((1+x**2)/(x-2)**2).series(x,sb.oo,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### *Récupérer le résultat sans le $O(h^n)$ :*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Il faut utiliser la méthode <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">replace</span> de la façon suivante :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = sb.symbols(\"n\", integrer=True, positive=True)\n",
    "[sb.O(x**2), sb.O(h**n,h)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[ e.replace(sb.O, lambda *X:0) for e in [sb.O(x**2), sb.O(h**n,h)] ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* « <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">lambda *X:0</span> » désigne la fonction qui renvoie l'entier 0 et qui prend un nombre quelconque d'arguments :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fonction_nulle = lambda *X:0\n",
    "[fonction_nulle(), fonction_nulle(3), fonction_nulle(x,n), fonction_nulle(1,2,n), fonction_nulle(1,2,3,n)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Enlever le $O(h^n)$ peut faciliter la manipulation du résultat : "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DL_en1_ord4 = sb.log(x).series(x,1,4)\n",
    "DL_en1_ord4 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[DL_en1_ord4 .expand(),DL_en1_ord4 .factor()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DL_en1_ord4_sans_O = DL_en1_ord4.replace(sb.O, lambda *X:0)\n",
    "DL_en1_ord4_sans_O"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[DL_en1_ord4_sans_O.expand(), DL_en1_ord4_sans_O.factor()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 7.3 Travail à faire </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "L'objet de cet exercice est l'étude d'une courbe paramétrée définie par $\\displaystyle\\left\\lbrace\\begin{array}{lll}\n",
    "x(t) & = & 7\\,\\sin^2(t) \\\\ y(t) & = & 2\\,\\sin(t)\\,(1+\\cos(3\\,t))\n",
    "\\end{array}\\right .$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$a)$ Définir les expressions symboliques <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">X</span> $=x(t)$ et <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">Y</span> $=y(t)$. Vérifier que ces expressions sont $2\\,\\pi$-périodiques et étudier leurs parités. <br /> $\\hspace{5mm}$Montrer que la courbe paramétrée passe par un même point en $t=0$ et en $t=\\pi$, ainsi que pour $\\displaystyle t=\\frac{\\pi}{6}$ et $\\displaystyle t=\\frac{5\\,\\pi}{6}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$b)$ Définir les expressions symboliques <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">dX</span> $=x^{\\prime}(t)$ et <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">dY</span> $=y^{\\prime}(t)$. Vérifier que <span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">dX</span> s'annule pour $\\displaystyle t\\in\\left\\lbrace 0,\\frac{\\pi}{2},\\pi\\right\\rbrace$ et que <br /><span style=\"color:#0000B0;font-family:Courier New;font-style:normal;font-weight:bold;font-size:12pt\">dY</span> s'annule pour $\\displaystyle t\\in\\left\\lbrace \\mathrm{arccos}\\!\\left(\\frac{1+\\sqrt{7}}{4}\\right),\\frac{\\pi}{3},\\mathrm{arccos}\\!\\left(\\frac{1-\\sqrt{7}}{4}\\right),\\pi\\right\\rbrace$ (fonction <b><tt>sympy.acos</tt></b>)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$\\bullet$ Tracé de la courbe paramétrée (la fonction <b><tt>sympy.lambdify</tt></b> sera détaillée plus tard) :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "try :\n",
    "    X_num,Y_num = sb.lambdify(t, X, \"numpy\"),sb.lambdify(t, Y, \"numpy\")\n",
    "    def tracer() :\n",
    "        T = np.linspace(-np.pi,np.pi,510)\n",
    "        plt.plot(X_num(T),Y_num(T),\"-m\",linewidth=2)\n",
    "        plt.grid() ; plt.axis(\"equal\")\n",
    "    tracer()\n",
    "except Exception as e :\n",
    "    print(\"Erreur : X et Y non définis correctement,\",e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$c)$ Faire des développements limités de $X$ et $Y$ à l'ordre 4 lorsque $t$ est au voisinage de $0$. Faire tracer ensuite la courbe paramétrée correspondante, pour $t$ dans l'intervalle $[-0.6,0.6]$. On rappellera la fonction <b><tt>tracer</tt></b> pour superposer les deux courbes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$d)$ Faire de même lorque $t$ est au voisinage de $\\pi$ (point singulier)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <span style=\"color: #0000BB\"> *Exercice 8* </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 8.1 Objectifs </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#005500\"> <i>Savoir déterminer une limite et aussi calculer une somme finie ou infinie.</i> </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 8.2 Exemples </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#008080\"> *Limites* </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = sb.symbols(\"x\", real=True)\n",
    "a = sb.symbols(\"a\", positive=True)\n",
    "F = (1+a*x)/(2-3*x) ; F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "F.limit(x, sb.oo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "F.limit(x, sb.S(2)/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(sb.limit.__doc__[:749])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "F.limit(x, sb.S(2)/3, dir=\"+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "F.limit(x, sb.S(2)/3, dir=\"-\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Limite calculée en 2 temps (déconseillé en général)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "limite_non_evaluee = sb.Limit(F, x, sb.S(2)/3, dir=\"-\") ; limite_non_evaluee # Noter la majuscule à Limit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "limite_non_evaluee.doit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:#008080\"> *Sommes* </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = sb.symbols(\"r\", positive=True)\n",
    "k,n = sb.symbols(\"k,n\", integer=True, nonnegative=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "somme = sb.summation(r**n, (n, 0, sb.oo) ) ; somme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "un_moins_r = sb.symbols(\"umr\", positive=True)\n",
    "somme.replace( r , 1-un_moins_r ).simplify().replace( un_moins_r , 1-r )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "somme_partielle = sb.summation(r**k, (k, 0, n) ) ; somme_partielle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rm1 = sb.symbols(\"rm1\", nonzero=True)\n",
    "S = somme_partielle.replace( r , rm1+1 ).simplify().replace( rm1 , r-1 ) ; S"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tous les algorithmes ne sont pas implémentés :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try :\n",
    "    somme_partielle.limit(n,sb.oo)\n",
    "except Exception as e :\n",
    "    print(\"Erreur :\",e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try :\n",
    "    S.limit(n,sb.oo)\n",
    "except Exception as e :\n",
    "    print(\"Erreur :\",e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logr = sb.symbols(\"lambda\", negative=True) # log(p) < 0, pour spécifier que p = exp(log(p)) < 1\n",
    "etape1 = S.replace(r,sb.exp(logr)) ; etape1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "etape2 = etape1.limit(n,sb.oo) ; etape2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "etape3 = etape2.replace(logr,sb.log(r)) ; etape3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "etap4 = etape3.together() ; etap4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Somme calculée en 2 temps (déconseillé en général)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "somme_non_evaluee = sb.Sum(r**n,(n,0,sb.oo)) ; somme_non_evaluee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "somme_non_evaluee.doit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:purple\"> 8.3 Travail à faire </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Loi de probabilité discrète : la loi de Poisson "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$a)$ Définir $\\displaystyle \\mathtt{P_k}=\\frac{\\mu^k}{k!}\\,\\exp(-\\mu)$, en spécifiant que $\\mu$ est un nombre strictement positif et que $k$ est un entier naturel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$b)$ Vérifier que $\\displaystyle \\sum_{k=0}^{\\infty}\\mathtt{P_k}=1$ et calculer $\\displaystyle \\;\\mathtt{EP}=\\sum_{k=0}^{\\infty}k\\,\\mathtt{P_k}\\;$, $\\displaystyle \\;\\mathtt{VP}=\\sum_{k=0}^{\\infty}(k-\\mathtt{EP})^2\\,\\mathtt{P_k}\\;$, ainsi que $\\displaystyle \\mathtt{R_n}=\\sum_{k=n+1}^{\\infty}\\mathtt{P_k}=1-\\sum_{k=0}^{n}\\mathtt{P_k}$ pour $n$ entier naturel non nul."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(sb.lowergamma.__doc__[:235])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
