{ "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", "# Calcul diff\u00e9rentiel et int\u00e9gral" ], "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": [ "Cette section concerne le calcul diff\u00e9rentiel et int\u00e9gral. On trouvera d'autres exemples dans le tutoriel de Sympy sur le m\u00eame sujet: \n", "\n", "## Limites\n", "\n", "Pour calculer la limite d'une expression lorsqu'une variable tend vers une valeur, on utilise la fonction limit de sympy avec la syntaxe limit(expression, variable, valeur). :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import limit, sin, S\n", "from sympy.abc import x" ], "outputs": [], "metadata": {} }, { "source": [ "Par exemple, pour \u00e9valuer la limite lorsque x tend vers 0 de l'expression $(\\sin(x)-x)/x^3$, on \u00e9crit:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit((sin(x)-x)/x**3, x, 0)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-1/6" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La limite de $f(x)=2x+1$ lorsque $x \\to 5/2$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(2*x+1, x, S(5)/2) # la fonction S permet de cr\u00e9er un nombre rationel" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour calculer la limite **\u00e0 gauche** en un point, on doit sp\u00e9cifier l'option dir=\"-\" :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(1/x, x, 0, dir=\"-\")" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-oo" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour calculer la limite **\u00e0 droite** en un point, on doit sp\u00e9cifier l'option dir=\"+\" :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(1/x, x, 0, dir=\"+\")" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "oo" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Lorsque la direction n'est pas sp\u00e9cifi\u00e9e, c'est la limite \u00e0 droite (dir=\"+\") qui est calcul\u00e9e par d\u00e9faut:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(1/x, x, 0)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "oo" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "En sympy, tout comme dans SageMath, le symbole oo repr\u00e9sente l'infini $\\infty$. Les deux o coll\u00e9s resemblent au symbole de l'infini 8 \u00e0 l'horizontal. Les op\u00e9rations d'addition, de soustraction, de multiplication, etc. sont possibles avec l'infini oo tant qu'elle soient bien d\u00e9finies. On doit l'importer pour l'utiliser:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import oo\n", "oo" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "oo" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "5 - oo" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-oo" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "oo - oo # nan signifie \"Not A Number\"" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "nan" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On peut calculer la limite d'une expression lorsque x tend vers **plus l'infini**:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(1/x, x, oo)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "et aussi lorsque x tend vers **moins l'infini**:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit(4+x*exp(x), x, -oo)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Sympy proc\u00e8de \u00e0 des simplifications lorsque possible:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "limit((1+1/x)**x, x, oo)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "E" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Sommes\n", "\n", "En Python, il existe une fonction (sum) que l'on a pas besoin d'importer et qui permet de calculer la somme des valeurs d'une liste:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sum([1,2,3,4,5])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "15" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cette fonction sum permet aussi de calculer une somme impliquant des variables et expressions symboliques de SymPy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import tan\n", "from sympy.abc import x,z\n", "sum([1,2,3,4,5,x,tan(z)])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x + tan(z) + 15" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Par contre, sum ne permet pas de calculer des sommes infinies ou encore des s\u00e9ries donn\u00e9es par un terme g\u00e9n\u00e9ral. En SymPy, il existe une autre fonction (summation) pour calculer des sommes possiblement infinies d'expressions symboliques:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import summation" ], "outputs": [], "metadata": {} }, { "source": [ "Pour calculer la somme d'une s\u00e9rie dont le terme g\u00e9n\u00e9ral est donn\u00e9 par une expression qui d\u00e9pend de n pour toutes les valeurs enti\u00e8res de n entre debut et fin (debut et fin inclus), on utilise la syntaxe summation(expression (n,debut,fin)) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import n\n", "summation(n, (n,1,5))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "15" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Le d\u00e9but et la fin de l'intervalle des valeurs de n peut \u00eatre donn\u00e9 par des variables symboliques:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a,b\n", "summation(n, (n,1,b))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "b b\n", "-- + -\n", "2 2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "summation(n, (n,a,b))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 2\n", " a a b b\n", "- -- + - + -- + -\n", " 2 2 2 2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour faire la somme d'une s\u00e9rie pour tous les nombres entiers de 1 \u00e0 l'infini, on utilise le symbole oo :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import oo\n", "summation(1/n**2, (n, 1, oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "pi\n", "---\n", " 6" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Si la s\u00e9rie est divergente, elle sera \u00e9valu\u00e9e \u00e0 oo ou encore elle restera non \u00e9valu\u00e9e:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "summation(n, (n,1,oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "oo" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "summation((-1)**n, (n,1,oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " oo\n", " ___\n", " \\ \n", " \\ n\n", " / (-1)\n", " /__,\n", "n = 1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Sympy peut aussi calculer une double somme. Il suffit de sp\u00e9ficier l'intervalle des valeurs pour chacune des variables en terminant avec la variable dont la somme est effectu\u00e9e en dernier:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import m,n\n", "summation(n*m, (n,1,m), (m,1,10))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1705" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Les doubles sommes fonctionnent aussi avec des intervalles infinis:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "summation(1/(n*m)**2, (n,1,oo), (m,1,oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 4\n", "pi\n", "---\n", " 36" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Produit\n", "\n", "Comme pour la somme, le calcul d'un produit dont le terme g\u00e9n\u00e9ral est donn\u00e9 par une expression qui d\u00e9pend de n pour toutes les valeurs enti\u00e8res de n entre debut et fin (debut et fin inclus), on utilise la syntaxe product(expression (n,debut,fin)) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import product\n", "from sympy.abc import n,b\n", "product(n, (n,1,5))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "120" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "product(n, (n,1,b))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "b!" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Voici un autre exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "product(n*(n+1), (n, 1, b))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "RisingFactorial(2, b)*b!" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Calcul diff\u00e9rentiel\n", "\n", "Pour d\u00e9river une fonction par rapport \u00e0 une variable x, on utilise la fonction diff de sympy avec la syntaxe diff(fonction, x) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import diff" ], "outputs": [], "metadata": {} }, { "source": [ "Faisons quelques importations de fonctions et variables pour la suite:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import sin,cos,tan,atan,pi\n", "from sympy.abc import x,y" ], "outputs": [], "metadata": {} }, { "source": [ "On calcule la d\u00e9riv\u00e9e de $\\sin(x)$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(sin(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "cos(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Voici quelques autres exemples:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(cos(x**3), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 / 3\\\n", "-3*x *sin\\x /" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(atan(2*x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "--------\n", " 2\n", "4*x + 1" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(1/tan(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "- tan (x) - 1\n", "-------------\n", " 2\n", " tan (x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour calculer la i-\u00e8me d\u00e9riv\u00e9e d'une fonction, on ajoute autant de variables que n\u00e9cessaire ou bien on sp\u00e9cifie le nombre de d\u00e9riv\u00e9es \u00e0 faire:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(sin(x), x, x, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-cos(x)" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(sin(x), x, 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-cos(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela fonctionne aussi avec des variables diff\u00e9rentes:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "diff(x**2*y**3, x, y, y)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "12*x*y" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Calcul int\u00e9gral\n", "\n", "Le calcul d'une int\u00e9grale ind\u00e9finie se fait avec la fonction integrate avec la syntaxe integrate(f, x) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import integrate" ], "outputs": [], "metadata": {} }, { "source": [ "Par exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(1/x, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "log(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Le calcul d'une int\u00e9grale d\u00e9finie se fait aussi avec la fonction integrate avec la syntaxe integrate(f, (x, a, b)) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(1/x, (x, 1, 57))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "log(57)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Voici quelques autres exemples:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import exp\n", "integrate(cos(x)*exp(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " x x\n", "e *sin(x) e *cos(x)\n", "--------- + ---------\n", " 2 2" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(x**2, (x,0,1))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1/3" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "L'int\u00e9grale d'une fonction rationnelle:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate((x+1)/(x**2+4*x+4), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 1\n", "log(x + 2) + -----\n", " x + 2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "L'int\u00e9grale d'une fonction exponentielle polynomiale:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(5*x**2 * exp(x) * sin(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 x 2 x x x\n", "5*x *e *sin(x) 5*x *e *cos(x) x 5*e *sin(x) 5*e *cos(x)\n", "-------------- - -------------- + 5*x*e *cos(x) - ----------- - -----------\n", " 2 2 2 2" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Deux int\u00e9grales non \u00e9l\u00e9mentaires:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import erf\n", "integrate(exp(-x**2)*erf(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " ____ 2\n", "\\/ pi *erf (x)\n", "--------------\n", " 4" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Calculer l'int\u00e9grale de $x^2 \\cos(x)$ par rapport \u00e0 $x$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(x**2 * cos(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "x *sin(x) + 2*x*cos(x) - 2*sin(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Calculer l'int\u00e9grale d\u00e9finie de $x^2 \\cos(x)$ par rapport \u00e0 $x$ sur l'intervalle de $0$ \u00e0 $\\pi/2$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(x**2 * cos(x), (x, 0, pi/2))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", " pi\n", "-2 + ---\n", " 4" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Sommes, produits, d\u00e9riv\u00e9es et int\u00e9grales non \u00e9valu\u00e9es\n", "\n", "Les fonctions summation, product, diff et integrate ont tous un \u00e9quivalent qui retourne un r\u00e9sultat non \u00e9valu\u00e9. Elles s'utilisent avec la m\u00eame syntaxe, mais portent un autre nom et commencent avec une majuscule: Sum, Product, Derivative, Integral." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Sum, Product, Derivative, Integral, sin, oo\n", "from sympy.abc import n, x\n", "Sum(1/n**2, (n, 1, oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " oo\n", "____\n", "\\ \n", " \\ 1\n", " \\ --\n", " / 2\n", " / n\n", "/___,\n", "n = 1" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Product(n, (n,1,10))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 10\n", "_____\n", "| | n\n", "| |\n", "n = 1" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Derivative(sin(x**2), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "d / / 2\\\\\n", "--\\sin\\x //\n", "dx" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Integral(1/x**2, (x,1,oo))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " oo\n", " /\n", " |\n", " | 1\n", " | -- dx\n", " | 2\n", " | x\n", " |\n", "/\n", "1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour les \u00e9valuer, on ajoute .doit() :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Sum(1/n**2, (n, 1, oo)).doit()" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", "pi\n", "---\n", " 6" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Product(n, (n,1,10)).doit()" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "3628800" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Derivative(sin(x**2), x).doit()" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " / 2\\\n", "2*x*cos\\x /" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Integral(1/x**2, (x,1,oo)).doit()" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela est utile pour \u00e9crire des \u00e9quations:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "A = Sum(1/n**2, (n, 1, oo))\n", "B = Product(n, (n,1,10))\n", "C = Derivative(sin(x**2), x)\n", "D = Integral(1/x**2, (x,1,oo))\n", "from sympy import Eq\n", "Eq(A, A.doit())" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " oo\n", "____\n", "\\  2\n", " \\ 1 pi\n", " \\ -- = ---\n", " / 2 6\n", " / n\n", "/___,\n", "n = 1" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(B, B.doit())" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 10\n", "_____\n", "| | n = 3628800\n", "| |\n", "n = 1" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(C, C.doit())" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "d / / 2\\\\ / 2\\\n", "--\\sin\\x // = 2*x*cos\\x /\n", "dx" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(D, D.doit())" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " oo\n", " /\n", " |\n", " | 1\n", " | -- dx = 1\n", " | 2\n", " | x\n", " |\n", "/\n", "1" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Int\u00e9grales multiples\n", "\n", "Pour faire une int\u00e9grale double, on peut int\u00e9grer le r\u00e9sultat d'une premi\u00e8re int\u00e9gration comme ceci:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x,y\n", "integrate(integrate(x**2+y**2, x), y)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 3 3\n", "x *y x*y\n", "---- + ----\n", " 3 3" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Mais, il est plus commode d'utiliser une seule fois la commande integrate et sympy permet de le faire:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(x**2+y**2, x, y)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 3 3\n", "x *y x*y\n", "---- + ----\n", " 3 3" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour les int\u00e9grales d\u00e9finies multiples, on sp\u00e9cifie les intervalles pour chaque variable entre parenth\u00e8ses. Ici, on fait l'int\u00e9grale sur les valeurs de x dans l'intervalle [0,y], puis pour les valeurs de y dans l'intervalle [0,10] :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "integrate(x**2+y**2, (x,0,y), (y,0,10))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "10000/3" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## D\u00e9veloppement en s\u00e9ries\n", "\n", "On calcule la s\u00e9rie de Taylor d'une expression qui d\u00e9pend de x au point x0 d'ordre n avec la syntaxe series(expression, x, x0, n). Par exemple, la s\u00e9rie de Maclaurin (une s\u00e9rie de Maclaurin est une s\u00e9rie de Taylor au point $x_0=0$) de $\\cos(x)$ d'ordre 14 est:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import series, cos\n", "from sympy.abc import x\n", "series(cos(x), x, 0, 14)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 4 6 8 10 12\n", " x x x x x x / 14\\\n", "1 - -- + -- - --- + ----- - ------- + --------- + O\\x /\n", " 2 24 720 40320 3628800 479001600" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Par d\u00e9faut, le d\u00e9veloppement est eff\u00e9ctu\u00e9e en 0 et est d'ordre 6:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "series(cos(x), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 4\n", " x x / 6\\\n", "1 - -- + -- + O\\x /\n", " 2 24" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "De fa\u00e7on \u00e9quivalente, on peut aussi utilise la syntaxe expression.series(x, x0, n) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "(1/cos(x**2)).series(x, 0, 14)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 4 8 12\n", " x 5*x 61*x / 14\\\n", "1 + -- + ---- + ------ + O\\x /\n", " 2 24 720" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Le d\u00e9veloppement de Taylor de $\\log$ se fait en $x_0=1$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import log\n", "series(log(x), x, 0)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "log(x)" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "series(log(x), x, 1)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2 3 4 5\n", " (x - 1) (x - 1) (x - 1) (x - 1) / 6 \\\n", "-1 - -------- + -------- - -------- + -------- + x + O\\(x - 1) ; x -> 1/\n", " 2 3 4 5" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## \u00c9quations diff\u00e9rentielles\n", "\n", "Une \u00e9quation diff\u00e9rentielle est une relation entre une fonction inconnue et ses d\u00e9riv\u00e9es. Comme la fonction est inconnue, on doit la d\u00e9finir de fa\u00e7on abstraite comme ceci:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Function\n", "f = Function(\"f\")" ], "outputs": [], "metadata": {} }, { "source": [ "D\u00e9j\u00e0, cela permet d'\u00e9crire f et f(x) :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "f" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "f" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x\n", "f(x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "f(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On peut d\u00e9finir les d\u00e9riv\u00e9es de f \u00e0 l'aide de la fonction Derivative de sympy:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Derivative\n", "Derivative(f(x), x) # ordre 1" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "d\n", "--(f(x))\n", "dx" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Derivative(f(x), x, x) # ordre 2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", " d\n", "---(f(x))\n", " 2\n", "dx" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "En utilisant, Eq on peut d\u00e9finir une \u00e9quation impliquant la fonction f et ses d\u00e9riv\u00e9es, c'est-\u00e0-dire une \u00e9quation diff\u00e9rentielle:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(f(x), Derivative(f(x),x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " d\n", "f(x) = --(f(x))\n", " dx" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Puis, on peut la r\u00e9soudre avec la fonction dsolve de sympy avec la syntaxe dsolve(equation, f(x)) et trouver quelle fonction f(x) est \u00e9gale \u00e0 sa propre d\u00e9riv\u00e9e:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import dsolve\n", "dsolve(Eq(f(x), Derivative(f(x),x)), f(x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " x\n", "f(x) = C1*e" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Voici un autre exemple qui trouve une fonction \u00e9gale \u00e0 l'oppos\u00e9 de sa d\u00e9riv\u00e9e d'ordre 2:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(f(x), -Derivative(f(x),x,x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " 2\n", " d\n", "f(x) = - ---(f(x))\n", " 2\n", " dx" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "dsolve(Eq(f(x), -Derivative(f(x),x,x)), f(x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "f(x) = C1*sin(x) + C2*cos(x)" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "R\u00e9soudre une \u00e9quation diff\u00e9rentielle ordinaire comme $f''(x) + 9 f(x) = 1$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "dsolve(Eq(Derivative(f(x),x,x) + 9*f(x), 1), f(x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "f(x) = C1*sin(3*x) + C2*cos(3*x) + 1/9" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Pour d\u00e9finir la d\u00e9riv\u00e9e, on peut aussi utiliser .diff()`. L'exemple pr\u00e9c\u00e9dent s'\u00e9crit:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "dsolve(Eq(f(x).diff(x, x) + 9*f(x), 1), f(x))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "f(x) = C1*sin(3*x) + C2*cos(3*x) + 1/9" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Finalement, voici un exemple impliquant deux \u00e9quations:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import x,y,t\n", "eq1 = Eq(Derivative(x(t),t), x(t)*y(t)*sin(t))\n", "eq2 = Eq(Derivative(y(t),t), y(t)**2*sin(t))\n", "systeme = [eq1, eq2]\n", "systeme" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " d d 2\n", "[--(x(t)) = x(t)*y(t)*sin(t), --(y(t)) = y (t)*sin(t)]\n", " dt dt" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "dsolve(systeme)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " C1\n", " -e -1\n", "set([x(t) = ---------------, y(t) = -----------])\n", " C1 C1 - cos(t)\n", " C2*e - cos(t)" ] }, "metadata": {} } ], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "python2", "name": "python2" } } }