{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*ENSAM-Bordeaux, Mathématiques et informatique. Date : le 12/10/19. Auteur : Éric Ducasse. Version : 1.0*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
*On pourra faire exécuter ce notebook cellule par cellule $\\left(\\mathtt{Maj+Entrée}\\right)$ ou intégralement par $\\mathtt{\\;Kernel\\rightarrow Restart\\;\\&\\;Run\\;All}$.*
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sympy as sb\n", "sb.init_printing() # Pour avoir de jolies formules mathématiques à l'écran" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Version de sympy : {sb.__version__}\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# **Réécriture d'expressions mathématiques**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Avec* sympy, *on utilise un* **Dictionnaire** (*classe* dict) *de* Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Cela consiste à pouvoir remplacer une ou plusieurs sous-expressions par d'autres.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Voir aussi le tutoriel **sympy**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/tutorial/index.html#tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "et en particulier :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/modules/core.html?highlight=replace#sympy.core.basic.Basic.replace
et
\n", "https://docs.sympy.org/latest/modules/core.html?highlight=replace#sympy.core.basic.Basic.xreplace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. *Manipulation de dictionnaires*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1 **Création et ajout d'items**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ensemble non ordonné d'associations **Clef/Objet**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico = {\"c\":\"truc\",\"b\":sb.pi,\"a\":[-2.34,5.67]} ; dico" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict( ((\"c\",\"truc\"),(\"b\",sb.pi),(\"a\",-2.34)) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico = dict()\n", "for key,val in ((\"c\",\"truc\"),(\"b\",sb.pi),(\"a\",-2.34)) :\n", " dico[key] = val\n", "dico" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict.fromkeys([1,2,3],99)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 **Méthodes disponibles**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[m for m in dir(dico) if not m.startswith(\"__\")]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3 ** Accès à la *valeur* par la *clef* **" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico[\"b\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try :\n", " dico[\"d\"]\n", "except :\n", " print(\"Erreur d'exécution !\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[ dico.get(k) for k in [\"a\",\"b\",\"c\",\"d\"] ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.4 ** Itérables **" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### a/ *Liste des clefs*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Attention** : un dictionnaire n'est pas ordonné !" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(dico.keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(dico)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### b/ *Liste des valeurs*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico.values()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(dico.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### c/ *Itérations sur un dictionnaire*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dico.items()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for k,v in dico.items() :\n", " print(f\"'{k}' -> {v}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. *Substitution dans une expression symbolique*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1 **Programmation standard, avec affectation**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x,y = sb.symbols(\"x,y\")\n", "a = x*(y-1)\n", "b = (a+x).expand()\n", "[a,b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2 **Réécriture en utilisant une règle, avec la méthode xreplace**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a,x,y = sb.symbols(\"a,x,y\")\n", "regle = {a:x*(y-1)}\n", "b = a+x\n", "print(\"L'application de la règle donne :\",b.xreplace(regle).expand())\n", "[a,b]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variante avec la méthode $\\mathtt{replace}$, qui n'utilise pas de dictionnaire mais ne peut faire qu'un seul remplacement à la fois :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b.replace(a,x*(y-1)).expand()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 **Exemple de séparation expression littérale/application numérique**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Volume et surface d'un cylindre de rayon $r$ et de hauteur $h$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r,h = sb.symbols(\"r,h\", real=True, positive=True)\n", "V = sb.pi*r**2*h\n", "S = ( 2 * sb.pi*r**2 + 2*sb.pi*r * h ).factor()\n", "(V,S)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Deux applications numériques (sans affectation)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "AN1,AN2 = {r:2e-3,h:3e-2},{r:2.5e-3,h:2e-2}\n", "for regle in (AN1,AN2) : print([float(e.xreplace(regle)) for e in (r,h,V,S)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "AN1,AN2 = {r:2e-3,h:3e-2},{r:2.5e-3,h:2e-2}\n", "for regle in (AN1,AN2) :\n", " print(f\"r = {1e3*regle[r]:.1f} mm ; h = {1e3*regle[h]:.1f} mm -> \" + \\\n", " f\"V ~ {1e9*float(V.xreplace(regle)):.2f} mm^3 et \" + \\\n", " f\"S ~ {1e6*float(S.xreplace(regle)):.2f} mm^2\")\n", "(V,S)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Minimimisation de la surface $S$, pour un volume $V$ donné" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vol = sb.symbols(\"V\", real=True, positive=True)\n", "regle_h = sb.solve(vol - V, h, dict=True)[0] ; regle_h" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S_de_V = S.xreplace(regle_h).expand() ; S_de_V " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dSdr = S_de_V.diff(r).factor() ; dSdr" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "regle_r_opt = sb.solve(dSdr,r,dict=True)[0] ; regle_r_opt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(h/r).xreplace(regle_h).xreplace(regle_r_opt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Surface minimale pour $2\\,r=h$ (diamètre et hauteur identiques)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 Outil de réécriture plus puissant : voir le notebook UEF-MINI_CM_substitutions_patterns.ipynb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
*Ce dernier point ne sera pas exigible*
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }