{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Tracer une fonction" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from __future__ import division, print_function # Python 3\n", "from sympy import init_printing\n", "init_printing(use_latex='mathjax',use_unicode=False) # Affichage des r\u00e9sultats" ], "outputs": [], "metadata": {} }, { "source": [ "La librairie Sympy utilise [matplotlib](http://matplotlib.org/), une autre librairie de Python, pour faire des dessins. Pour activer l'affichage des graphiques dans Jupyter, on \u00e9crit d'abord ceci dans une cellule:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": {} }, { "source": [ "## Tracer une fonction $\\RR\\to\\RR$\n", "\n", "On importe la fonction plot qui permet de dessiner des fonctions:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import plot " ], "outputs": [], "metadata": {} }, { "source": [ "Un premier exemple. Par d\u00e9faut, l'intervalle pour les $x$ est $[-10,10]$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin\n", "from sympy.abc import x\n", "plot(sin(x))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Un deuxi\u00e8me exemple sur l'intervalle $[-100, 100]$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot(sin(x)/x, (x,-100,100))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "On trace une parabole de couleur rouge dans l'intervale $[-5, 5]$ avec un titre:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot(x**2+x-6, (x,-5,5), line_color='red', title='Youpi')" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Tracer plusieurs fonctions $\\RR\\to\\RR$\n", "\n", "On trace plusieurs fonctions sur le m\u00eame intervalle de la fa\u00e7on suivante. Dans cet exemple, on a aussi sp\u00e9cifier une limite inf\u00e9rieure et sup\u00e9rieure pour l'axe des $y$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot(x, x**2, x**3, (x, -2, 2), ylim=(-2,2))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Pour dessiner les trois fonctions avec des couleurs diff\u00e9rentes, il faut cr\u00e9er un dessin \u00e0 la fois et ensuite les combiner. L'option show=False permet d'\u00e9viter d'afficher les dessins interm\u00e9diaires:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "p1 = plot(x, (x, -1, 1), show=False, line_color='b')\n", "p2 = plot(x**2, (x, -1, 1), show=False, line_color='r')\n", "p3 = plot(x**3, (x, -1, 1), show=False, line_color='g')" ], "outputs": [], "metadata": {} }, { "source": [ "On ajoute \u00e0 p1 les graphes p2 et p3 :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "p1.extend(p2)\n", "p1.extend(p3)" ], "outputs": [], "metadata": {} }, { "source": [ "Maintenant p1 contient les trois graphes:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "print(p1)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Plot object containing:\n", "[0]: cartesian line: x for x over (-1.0, 1.0)\n", "[1]: cartesian line: x**2 for x over (-1.0, 1.0)\n", "[2]: cartesian line: x**3 for x over (-1.0, 1.0)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On affiche le graphe des trois fonctions:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "p1.show()" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Tracer une fonction $\\RR^2\\to\\RR$\n", "\n", "On importe la fonction plot3d du sous-module sympy.plotting :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.plotting import plot3d" ], "outputs": [], "metadata": {} }, { "source": [ "Un premier exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x,y\n", "plot3d(x**2+y**2)" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Un deuxi\u00e8me exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot3d(sin(x*10)*cos(y*4), (x, -1, 1), (y, -1, 1))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "On trouvera d'autres exemples en consultant la documentation de plot? et plot3d? ou dans la section Plotting du tutoriel de Sympy: \n", "\n", "## Dessiner une fonction $\\RR\\to\\RR^2$\n", "\n", "Dans cette section et les suivantes, on aura utilisera les fonctions et variables symboliques suivantes:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin, cos\n", "from sympy.abc import u, v" ], "outputs": [], "metadata": {} }, { "source": [ "La fonction plot_parametric permet de tracer des fonctions param\u00e9tr\u00e9s $\\RR\\to\\RR^2$. Par exemple, on trace la [courbe de Lissajous](https://en.wikipedia.org/wiki/Lissajous_curve) lorsque $a=3$ et $b=2$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.plotting import plot_parametric\n", "plot_parametric(cos(3*u), sin(2*u), (u, -5, 5))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Dessiner une fonction $\\RR\\to\\RR^3$\n", "\n", "La fonction plot3d_parametric_line permet de tracer des courbes dans l'espace 3d. Par exemple, on trace une h\u00e9lice:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.plotting import plot3d_parametric_line\n", "plot3d_parametric_line(cos(u), sin(u), u, (u, -15, 15))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Dessiner une fonction $\\RR^2\\to\\RR^3$\n", "\n", "La fonction plot3d_parametric_surface permet de tracer des surfaces dans $\\RR^3$. Par exemple, on trace un tore:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.plotting import plot3d_parametric_surface\n", "X = cos(u)*(5+2*cos(v))\n", "Y = sin(u)*(5+2*cos(v))\n", "Z = 2*sin(v)\n", "plot3d_parametric_surface(X, Y, Z, (u, -.5, 4), (v, -5, 5))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Dessiner les solutions d'une \u00e9quation implicite" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import plot_implicit, Eq\n", "from sympy.abc import x, y" ], "outputs": [], "metadata": {} }, { "source": [ "La fonction plot_implicit permet de tracer les solutions d'une \u00e9quation implicite:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "eq = Eq(x**2+y**2+x*y-2*x, 5)\n", "eq" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x**2 + x*y - 2*x + y**2 == 5" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot_implicit(eq)" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "On peut modifier les \u00e9tendues des variables x et y de la fa\u00e7on suivante (le dessin n'est pas affich\u00e9 dans ces notes):" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot_implicit(eq, (x,-2,5), (y,-5,3))" ], "outputs": [], "metadata": {} }, { "source": [ "## Tracer une r\u00e9gion de $\\RR^2$\n", "\n", "La fonction plot_implicit peut aussi servir \u00e0 dessiner une r\u00e9gion de points qui satisfont une in\u00e9galit\u00e9:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot_implicit(y > 2*x+1)" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Pour tracer la r\u00e9gion d\u00e9finie par plusieurs in\u00e9galit\u00e9s, on utilise la fonction And de sympy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import And\n", "plot_implicit(And(y>2*x+1, y<5*x, x+y<5))" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "## Dessiner une fonction complexe avec mpmath\n", "\n", "[mpmath](http://mpmath.org/) est une librairie Python pour faire des calculs en pr\u00e9cision arbitraire sur les nombres flottants. Elle permet aussi de faire des [dessins de fonctions complexes](http://mpmath.googlecode.com/svn/gallery/gallery.html).\n", "\n", "La fa\u00e7on d'importer la librairie mpmath n'est pas exactement la m\u00eame selon qu'on utilise une installation normale de SymPy ou qu'on utilise SageMath:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import mpmath # Sympy (installation normale)\n", "import mpmath # SageMath" ], "outputs": [], "metadata": {} }, { "source": [ "Rappelons que sans la ligne suivante, les dessins ne s'afficheront pas:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": {} }, { "source": [ "La syntaxe des arguments n'est pas exactement la m\u00eame que pour la fonction plot de SymPy. Il faut d\u00e9finir une fonction Python avec la commande def ou encore sur une ligne avec lambda. Par exemple, la fonction identit\u00e9 peut s'\u00e9crire lambda z:z en Python.\n", "\n", "On trace la fonction identit\u00e9 pour comprendre la signification de l'image obtenue:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "mpmath.cplot(lambda z: z, [-10, 10], [-10, 10])" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Les couleurs de l'arc en ciel doivent \u00eatre interpr\u00e9t\u00e9s comme l'argument d'un nombre complexe (rouge pour un nombre r\u00e9el positif). Le module du nombre complexe est repr\u00e9sent\u00e9 par la transparence (0=noir opaque, $\\infty$ = oo = blanc transparent).\n", "\n", "De la m\u00eame fa\u00e7on, on ne peut pas utiliser le I de sympy avec mpmath, il faut utiliser les nombres complexes de Python. Le dessin suivant illustre la multiplication par le nombre complexe $i$, c'est-\u00e0-dire une rotation de 90 degr\u00e9s:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "I = complex(0,1) # le nombre complexe I de Python\n", "mpmath.cplot(lambda z: I*z, [-10, 10], [-10, 10])" ], "outputs": [], "metadata": {} }, { "source": [ "Les pixels en rouges sont envoy\u00e9s sur la droite r\u00e9elle positive par la fonction lambda z: I*z.\n", "\n", "\n", "\n", "Le dessin suivant permet de voir les cinq racines cinqui\u00e8me de l'unit\u00e9:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "mpmath.cplot(lambda z: z**5-1, [-2, 2], [-2, 2])" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "Cela permet aussi d'\u00e9tudier les z\u00e9ros de la fonction zeta de Riemann:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from mpmath import zeta\n", "mpmath.cplot(zeta, [-10, 10], [-50, 50])" ], "outputs": [], "metadata": {} }, { "source": [ "\n", "\n", "mpmath offre aussi sa propre fonction de dessin mpmath.plot ainsi qu'une fonction pour dessiner des surfaces en 3d mpmath.splot. On trouvera d'autres exemples dans la page suivante de la documentation de Sympy: " ], "cell_type": "markdown", "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "python2", "name": "python2" } } }