{ "cells": [ { "cell_type": "markdown", "id": "62bac615", "metadata": {}, "source": [ "# Sympy pour le calcul formel" ] }, { "cell_type": "markdown", "id": "d199cfd8", "metadata": {}, "source": [ "> **Importez les bibliothèques utiles**" ] }, { "cell_type": "code", "execution_count": 1, "id": "d8696c37", "metadata": {}, "outputs": [], "source": [ "import math\n", "import sympy" ] }, { "cell_type": "markdown", "id": "5c170b7c", "metadata": {}, "source": [ "## Premiers essais\n", "> **Testez les commandes suivantes**" ] }, { "cell_type": "markdown", "id": "ad76441b", "metadata": {}, "source": [ "Avec la bibliothèque math \n", "Ces instructions fonctionnent de la même manière avec la bibliothèque Numpy" ] }, { "cell_type": "code", "execution_count": null, "id": "dc98c6bc", "metadata": {}, "outputs": [], "source": [ "math.sqrt(9)" ] }, { "cell_type": "code", "execution_count": null, "id": "85f541b7", "metadata": {}, "outputs": [], "source": [ "math.sqrt(8)" ] }, { "cell_type": "markdown", "id": "5ecbd569", "metadata": {}, "source": [ "Avec la bibliothèque Sympy" ] }, { "cell_type": "code", "execution_count": null, "id": "6d933928", "metadata": { "scrolled": true }, "outputs": [], "source": [ "sympy.sqrt(9)" ] }, { "cell_type": "code", "execution_count": null, "id": "0bd354eb", "metadata": {}, "outputs": [], "source": [ "sympy.sqrt(8)" ] }, { "cell_type": "code", "execution_count": null, "id": "3222c90a", "metadata": {}, "outputs": [], "source": [ "m = 3/2\n", "print(m)" ] }, { "cell_type": "code", "execution_count": null, "id": "57173084", "metadata": {}, "outputs": [], "source": [ "k = sympy.Rational(3,2)\n", "print(k)" ] }, { "cell_type": "markdown", "id": "70060fa7", "metadata": {}, "source": [ "## Expressions" ] }, { "cell_type": "markdown", "id": "80fc7f06", "metadata": {}, "source": [ "Il est possible de mettre en forme des équations avec Sympy.\n", "> **Testez l'exemple suivant**" ] }, { "cell_type": "code", "execution_count": null, "id": "55ede9e3", "metadata": {}, "outputs": [], "source": [ "x, y = sympy.symbols('x y')\n", "expr = x**2 - 4 * x + 5\n", "expr" ] }, { "cell_type": "code", "execution_count": null, "id": "baf3c438", "metadata": {}, "outputs": [], "source": [ "expr.subs(x, 1)" ] }, { "cell_type": "markdown", "id": "ea30ab0a", "metadata": {}, "source": [ "> **A quoi sert la fonction *subs* ?**" ] }, { "cell_type": "markdown", "id": "07ca4ddb", "metadata": {}, "source": [ "Réponse : " ] }, { "cell_type": "markdown", "id": "d67ec2e7", "metadata": {}, "source": [ "### Développer et factoriser" ] }, { "cell_type": "code", "execution_count": null, "id": "2ba58ca8", "metadata": {}, "outputs": [], "source": [ "x*expr" ] }, { "cell_type": "code", "execution_count": null, "id": "9d6f8f67", "metadata": {}, "outputs": [], "source": [ "expand_exp = sympy.expand(x*expr)\n", "expand_exp" ] }, { "cell_type": "code", "execution_count": null, "id": "8dd656a8", "metadata": {}, "outputs": [], "source": [ "factor_exp = sympy.factor(expand_exp)\n", "factor_exp" ] }, { "cell_type": "markdown", "id": "14528e6a", "metadata": {}, "source": [ "## Fonctions" ] }, { "cell_type": "markdown", "id": "f4995b2f", "metadata": {}, "source": [ "Soit la fonction $f(x,y) = x^2 + y$" ] }, { "cell_type": "code", "execution_count": null, "id": "f2438c85", "metadata": {}, "outputs": [], "source": [ "f = sympy.Function('f')\n", "f = x**2 + y\n", "f" ] }, { "cell_type": "code", "execution_count": null, "id": "9a195faa", "metadata": {}, "outputs": [], "source": [ "f.subs(x, 4)" ] }, { "cell_type": "code", "execution_count": null, "id": "8117f3ce", "metadata": {}, "outputs": [], "source": [ "f.subs(y, 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "f081c66b", "metadata": {}, "outputs": [], "source": [ "f.subs({x:1, y: 2})" ] }, { "cell_type": "markdown", "id": "1e4ce429", "metadata": {}, "source": [ "### Vectorisation et affichage" ] }, { "cell_type": "markdown", "id": "6cf71007", "metadata": {}, "source": [ "Soit la fonction $g(x) = \\sin(\\frac{x}{2} + \\sin(x))$" ] }, { "cell_type": "code", "execution_count": null, "id": "39411d59", "metadata": {}, "outputs": [], "source": [ "g = sympy.Function('g')\n", "g = sympy.sin(x/2 + sympy.sin(x))\n", "g" ] }, { "cell_type": "markdown", "id": "58bfb293", "metadata": {}, "source": [ "On souhaite afficher cette fonction sur l'intervalle $[-\\pi, \\pi]$" ] }, { "cell_type": "code", "execution_count": null, "id": "e76793e6", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "xlin = np.linspace(-np.pi, np.pi, 21)" ] }, { "cell_type": "markdown", "id": "62e6aa1d", "metadata": {}, "source": [ "> Que donnent les instructions suivantes ?" ] }, { "cell_type": "code", "execution_count": null, "id": "5b57a106", "metadata": {}, "outputs": [], "source": [ "result = g.subs(x, xlin)\n", "result" ] }, { "cell_type": "code", "execution_count": null, "id": "a00b04c3", "metadata": {}, "outputs": [], "source": [ "type(g)" ] }, { "cell_type": "markdown", "id": "5dc7437a", "metadata": {}, "source": [ "> **Testez les instructions suivantes**" ] }, { "cell_type": "code", "execution_count": null, "id": "80c38e77", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from sympy.utilities.lambdify import lambdify\n", "func = lambdify([x], g)\n", "type(func)" ] }, { "cell_type": "code", "execution_count": null, "id": "43fac308", "metadata": {}, "outputs": [], "source": [ "yres = func(xlin)\n", "type(yres), yres.shape" ] }, { "cell_type": "markdown", "id": "72bb1bea", "metadata": {}, "source": [ "> **Affichez la fonction g(x) en fonction de xlin**" ] }, { "cell_type": "code", "execution_count": null, "id": "e817b93c", "metadata": {}, "outputs": [], "source": [ "# TO DO" ] }, { "cell_type": "markdown", "id": "f5d532b0", "metadata": {}, "source": [ "## Expressions avec des dérivées" ] }, { "cell_type": "markdown", "id": "d5e9d586", "metadata": {}, "source": [ "La plupart des problèmes de physique font intervenir des équations différentielles nécessitant alors la possibilité de déclarer des expressions incluant des dérivées." ] }, { "cell_type": "markdown", "id": "1f55c1bf", "metadata": {}, "source": [ "Dans la suite de cette exercice, nous nous intéresserons à l'équation différentielle régissant la décharge d'une capacité dans une résistance selon une constante de temps $\\tau$ : $$V_s(t) + \\tau \\cdot \\frac{d V_s(t)}{dt} = 0$$" ] }, { "cell_type": "markdown", "id": "b2f027c2", "metadata": {}, "source": [ "On déclare au préalable les fonctions et les symboles dont nous aurons besoin. " ] }, { "cell_type": "code", "execution_count": 8, "id": "eedcc563", "metadata": {}, "outputs": [], "source": [ "t, tau = sympy.symbols('t tau')\n", "vs = sympy.Function('V_s')(t)" ] }, { "cell_type": "markdown", "id": "69061b85", "metadata": {}, "source": [ "On définit ensuite la dérivée de $V_s(t)$ et l'expression précédente :" ] }, { "cell_type": "code", "execution_count": 9, "id": "77e08e40", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\tau \\frac{d}{d t} V_{s}{\\left(t \\right)} + V_{s}{\\left(t \\right)}$" ], "text/plain": [ "tau*Derivative(V_s(t), t) + V_s(t)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dvs = sympy.Derivative(vs, t)\n", "exp = vs + tau * dvs\n", "exp" ] }, { "cell_type": "markdown", "id": "97ce005b", "metadata": {}, "source": [ "### Résolution d'une équation différentielle" ] }, { "cell_type": "markdown", "id": "fc78d8cf", "metadata": {}, "source": [ "Lorsqu'une solution analytique peut être calculée, il est possible d'utiliser la fonction ***dsolve*** de Sympy pour obtenir cette expression." ] }, { "cell_type": "code", "execution_count": 11, "id": "e36eb4e8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vs_t = C1*exp(-t/tau)\n" ] } ], "source": [ "result = sympy.dsolve(exp, vs)\n", "vs_t = result.rhs\n", "print(f'vs_t = {vs_t}')" ] }, { "cell_type": "markdown", "id": "d1436101", "metadata": {}, "source": [ "> La solution donnée est-elle juste ? " ] }, { "cell_type": "markdown", "id": "21785300", "metadata": {}, "source": [ "Il est également possible d'ajouter des conditions initiales." ] }, { "cell_type": "code", "execution_count": 12, "id": "72ef30b9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vs_t = 5*exp(-t/tau)\n" ] } ], "source": [ "init_conds = {vs.subs(t,0): 5}\n", "result = sympy.dsolve(exp, vs, ics=init_conds)\n", "vs_t = result.rhs\n", "print(f'vs_t = {vs_t}')" ] }, { "cell_type": "markdown", "id": "ced0731e", "metadata": {}, "source": [ "> Quelle est la différence avec l'expression précédente ?" ] }, { "cell_type": "markdown", "id": "c4fd0734", "metadata": {}, "source": [ "## Calculs de limites\n", "Soit la fonction $g(x) = \\sin(\\frac{x}{2} + \\sin(x))$.\n", "\n", "On souhaite calculer la limite de cette fonction $g(x)$ en $\\pi$." ] }, { "cell_type": "code", "execution_count": null, "id": "5c9b0710", "metadata": {}, "outputs": [], "source": [ "g = sympy.Function('g')\n", "g = sympy.sin(x/2 + sympy.sin(x))\n", "g" ] }, { "cell_type": "code", "execution_count": null, "id": "6739399f", "metadata": {}, "outputs": [], "source": [ "lg = sympy.limit(g, x, sympy.pi)\n", "lg" ] }, { "cell_type": "markdown", "id": "a4e1ca24", "metadata": {}, "source": [ "## Dérivées" ] }, { "cell_type": "code", "execution_count": null, "id": "7bb1b568", "metadata": {}, "outputs": [], "source": [ "f = x**2 + y\n", "f" ] }, { "cell_type": "code", "execution_count": null, "id": "914889a3", "metadata": {}, "outputs": [], "source": [ "dfx = sympy.diff(f, x)\n", "dfx" ] }, { "cell_type": "code", "execution_count": null, "id": "e3649806", "metadata": {}, "outputs": [], "source": [ "dfy = sympy.diff(f, y)\n", "dfy" ] }, { "cell_type": "code", "execution_count": null, "id": "bee61453", "metadata": {}, "outputs": [], "source": [ "k = sympy.Function('k')\n", "l = sympy.Function('l')\n", "k = k(x)\n", "l = l(x + k)\n", "k, l" ] }, { "cell_type": "code", "execution_count": null, "id": "59f1532d", "metadata": {}, "outputs": [], "source": [ "dfkl = sympy.diff(l, x)\n", "dfkl" ] }, { "cell_type": "markdown", "id": "4d172a7d", "metadata": {}, "source": [ "## Intégrales" ] }, { "cell_type": "code", "execution_count": null, "id": "b3e23608", "metadata": {}, "outputs": [], "source": [ "inte_f = sympy.integrate(f, x)\n", "inte_f" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }