{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*ENSAM-Bordeaux, Mathématiques et informatique. Date : le 11/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": [ "# **Calcul formel et calcul numérique**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Voir aussi le tutoriel **sympy** :\n", "https://docs.sympy.org/latest/tutorial/index.html#tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "et en particulier : https://docs.sympy.org/latest/tutorial/intro.html#what-is-symbolic-computation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. *Nombres irrationnels* " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "liste = [sb.pi,sb.E,sb.sqrt(2),(1+sb.sqrt(5))/2] ; liste" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[n.evalf() for n in liste]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[sb.sin(sb.pi),np.sin(np.pi)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[sb.exp(sb.I*sb.pi),np.exp(1j*np.pi)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[[sb.pi/n,sb.cos(sb.pi/n),sb.sin(sb.pi/n)] for n in range(1,7)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[sb.sqrt(2)**2-2,np.sqrt(2)**2-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. *Manipulation d'expressions algébriques*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1 Développer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a,b = sb.symbols(\"a,b\")\n", "((a+b)**2).expand()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2 Factoriser" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(a**2+2*a-3).factor()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 Simplifier" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(1-sb.cos(a)**2).simplify()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = sb.symbols(\"n\")\n", "[sb.sin(sb.pi*n),sb.cos(sb.pi*n)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = sb.symbols(\"n\",integer=True)\n", "[sb.sin(sb.pi*n),sb.cos(sb.pi*n)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = sb.sqrt(a**2)\n", "[A,sb.refine(A,sb.Q.positive(a))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 Décomposer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Décomposition en éléments simples dans $\\mathbb{R}$*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = sb.symbols(\"x\")\n", "((2*x-7)/((x-1)*(x**2+4))).apart()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Décomposition en éléments simples dans $\\mathbb{C}$*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(sb.apart)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "((2*x-7)/((x-1)*(x**2+4))).apart(full=True).doit()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Décomposition en éléments simples avec paramètres* (ici, transformée de Laplace usuelle)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w = sb.symbols(\"omega\",positive=True)\n", "p = sb.symbols(\"p\",complex=True)\n", "TL = ((a*w**2-b*p)/(p*(p**2+w**2)))\n", "(TL,TL.apart(p))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. *Dérivation et intégration formelles d'expressions*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.1 Dérivation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/tutorial/calculus.html#derivatives" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m,s = sb.symbols(\"m,sigma\",real=True)\n", "G = sb.exp(-((x-m)/s)**2/2)\n", "G.diff(x).simplify()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.diff(G,x).simplify() # Variante" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[sb.exp(-x**2/2).diff(x,n) for n in (1,2,3,4)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2 Intégration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/tutorial/calculus.html#integrals" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.integrate(G,(x,-sb.oo,sb.oo))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "G.integrate( (x,-sb.oo,sb.oo) ) # Variante" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Il faut spécifier que $\\sigma>0$ et redéfinir G* :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = sb.symbols(\"sigma\",positive=True)\n", "G_bis = sb.exp(-((x-m)/s)**2/2).simplify() # Redéfinition nécessaire pour prendre en compte l'hypothèse\n", "G_bis.integrate( (x,-sb.oo,sb.oo) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*$\\dots$ ou juste que l'intégrale est supposée définie pour les paramètres donnés* :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.integrate(G, (x,-sb.oo,sb.oo), conds=\"none\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = sb.symbols(\"t\",real=True)\n", "sb.integrate(sb.exp(-t**2), (t,0,x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.3 Sommation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k,n = sb.symbols(\"k,n\",integer=True,positive=True)\n", "p = sb.symbols(\"p\",positive=True) \n", "sb.summation(p**k,(k,0,n))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.summation(p**k,(k,0,sb.oo))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.summation(p**k/sb.factorial(k),(k,0,sb.oo))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. *Fonctions indéfinies*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f = sb.Function(\"f\") ; f(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f(x).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f(a*x+t).diff(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f(sb.sin(x)).diff(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variante :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "g, h = sb.Function(\"g\"), sb.Function(\"h\")\n", "g(h(x)).diff(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. *Développements limités* " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/tutorial/calculus.html#series-expansion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.exp(x).series(x,0,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sb.sin(sb.pi/3+2*x).series(x,0,3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(1/sb.cos(sb.sqrt(x))).series(x,0,3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(1/sb.sin(3*x)).series(x,0,5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a,h = sb.symbols(\"a,h\",real=True)\n", "f(a+h).series(h,0,3).doit()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. *Matrices et vecteurs*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://docs.sympy.org/latest/tutorial/matrices.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.1 Déterminant" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "P = sb.Matrix([[1,1,1],[-2,-1,0],[0,1,a]]) ; P,P.det()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.2 Inverse" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(a-2)*P.inv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.3 Produits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Matrice diagonale*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "D = sb.diag(2,1,a) ; D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Produit matriciel* : nous préconisons l'opérateur @ qui désigne la même opération en sympy et en numpy, contrairement à l'opérateur * (produit matriciel en sympy, produit terme-à-terme en numpy)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "M = ((a-2)*P@D@P.inv()).expand() ; M" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Simplification **en place** *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "M.simplify() ; M" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Vecteur = liste** (ou tuple)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = (1,-1,x) ; u" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = (1,-1,x)\n", "P.dot(u)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try : P*u\n", "except Exception as e : print(\"Erreur :\",e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try : P@u\n", "except Exception as e : print(\"Erreur :\",e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = sb.Matrix([[a,2,0],[1,a,1],[0,2,a]]) ; A" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A.dot((x,1,0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Avec sympy, la notion de vecteur n'existe pas.\n", "Un vecteur est assimilé à une **matrice-colonne** !" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u = (1,-1,x) ; U = sb.Matrix(u) ; U" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "U.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rappelons que « transposer un vecteur » n'a pas de sens !" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(U,U.transpose(),U.T)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(P@U, A@sb.Matrix((x,1,0)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Produit scalaire : **" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V = P.row(2) ; V" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(V.dot(u),V.dot(U),V.dot(U.T),V.T.dot(U))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peu importe qu'il s'agisse de matrices-lignes ou de matrices-colonnes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S = V@U ; S" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "S.shape # S est une matrice 1x1 !" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "W = A.col(1) ; W" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "W.dot(u)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.4 Valeurs propres" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "M.eigenvals()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(M.eigenvals())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(M.eigenvals().items())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "M.eigenvects(simplify=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A.eigenvals()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A.eigenvects(simplify=True)" ] }, { "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 }