#!/usr/bin/env python # coding: utf-8 # # Annonce # La branche étudiante de l'IEEE propose, ce jeudi 25 février de 13 h 30 à 17 h 30, une formation à Mathematica pour donner les bases de ce logiciel. L'inscription est nécessaire. # http://ieee.aees.be/fr/accueil/25-francais/activites/conferences/151-formation-mathematica # # Initialisation # Pour que la division soit comme en Python 3: # In[1]: from __future__ import division # Importer toutes les fonctions et quelques variables de Sympy: # In[2]: from sympy import * from sympy.abc import a,b,c,k,n,t,u,v,w,x,y,z init_printing(pretty_print=True, use_latex='mathjax') # # Plusieurs choses ne sont pas dans les notes de cours # Calculer le pgcd de $$p(x)=x^5 - 20x^4 + 140x^3 - 430x^2 + 579x - 270$$ et $$q(x)=x^6 - 25x^5 + 243x^4 - 1163x^3 + 2852x^2 - 3348x + 1440$$ # In[5]: p = x**5 - 20*x**4 + 140*x**3 - 430*x**2 + 579*x - 270 q = x**6 - 25*x**5 + 243*x**4 - 1163*x**3 + 2852*x**2 - 3348*x + 1440 # In[3]: gcd(4,6) # In[8]: gcd(p,q) # # 7 Calcul différentiel et intégral # ### 7.1 Limites # In[ ]: from sympy.abc import x # In[ ]: limit(1/x, x, 0, dir='+') # In[ ]: oo # In[ ]: limit(1/x, x, oo) # In[ ]: # ### 7.2 Sommes # Calculer la somme des nombres 134,245,325,412,57. # In[9]: sum([134, 245, 325, 412, 57]) # In[10]: sum([134, 245, 325, 412, 57, x]) # In[11]: sum([134, 245, 325, 412, 57, x, []]) # In[ ]: # Caluler la somme # $$\sum_{i=0}^n i$$ # In[13]: from sympy.abc import i summation(i, (i,0,n)) # In[15]: summation(i**2, (i,0,2016)) # Calculer la somme # $$\sum_{k=1}^\infty {1 \over k^6}$$ # In[16]: summation(1/k**6, (k, 1, oo)) # ### 7.3 Produit # Calculer le produit # $$\prod_{n=1}^{2016} 2n+1$$ # In[17]: product(2*n+1, (n,1,2016)) # In[ ]: # ### 7.4 Calcul différentiel # Calculer la dérivée de $$x^5+bx$$ # In[19]: b,x # In[20]: diff(x**5+b*x, b) # In[21]: diff(x**5+b*x, x) # Calculer la dérivée de $$\arcsin(x)$$ # In[22]: diff(asin(x), x) # ### 7.5 Calcul intégral # Calculer l'intégrale $$\int\log(x)\, dx$$ # In[23]: integrate(log(x), x) # Calculer l'intégrale $$\int a^x\, dx$$ # In[24]: integrate(a**x, x) # Calculer l'intégrale $$\int x^a\, dx$$ # In[25]: integrate(x**a, x) # In[30]: log(100, 10) # In[31]: get_ipython().run_line_magic('pinfo', 'log') # Calculer l'intégrale # $$\int \sec^2(x)\,dx$$ # In[26]: integrate(sec(x)**2, x) # In[37]: integrate(integrate(x**2*y, x), y) # $$\int_0^5\int_0^2 x^2y\,dx\,dy$$ # In[39]: integrate(x**2*y, (x,0,2), (y,0,5)) # ### 7.6 Sommes, produits, dérivées et intégrales non évaluées # In[40]: A = Sum(1/k**6, (k,1,oo)) B = Product(2*n+1, (n,1,21)) C = Derivative(asin(x), x) D = Integral(log(x), x) # In[41]: Eq(A, A.doit()) # In[42]: Eq(B, B.doit()) # In[43]: Eq(C, C.doit()) # In[44]: Eq(D, D.doit()) # In[ ]: # ### 7.7 Développement en séries # Calculer la série de Taylor de $\tan(x)$ en $x_0=0$ d'ordre 14. # In[45]: series(tan(x), x, 0, 14) # In[51]: series(sin(x), x, 0, 10) # ### 7.8 Équations différentielles # In[55]: from sympy import E A = Derivative(E**x, x) Eq(A, A.doit()) # Trouver une fonction $f(x)$ telle que ${d\over dx} f(x) = f(x)$ # In[56]: f = Function('f') # In[58]: f(x) # In[61]: eq = Eq(Derivative(f(x),x), f(x)) # In[62]: dsolve(eq) # Trouver une fonction $f(x)$ telle que ${d^2\over dx^2} f(x) = -f(x)$ # In[64]: eq2 = Eq(Derivative(f(x),x,x), -f(x)) # In[65]: dsolve(eq2) # In[66]: Derivative(f(x),x,x,x,x,x) # In[67]: Derivative(f(x),x,5) # In[68]: f(x).diff(x,5) # Résoudre$$y''-4y'+5y=0$$. # In[69]: from sympy.abc import x,y eq = Eq(y(x).diff(x,x)-4*y(x).diff(x)+5*y(x),0) dsolve(eq, y(x)) # # 8 Algèbre linéaire # ### 8.1 Définir une matrice # Définir la matrice # $$M=\begin{bmatrix} # 2& 9& 3\\ 4& 5& 10\\ 2& 0& 3 # \end{bmatrix}$$ # In[70]: Matrix([[2, 9, 3], [4, 5, 10], [2, 0, 3]]) # In[107]: M = Matrix(3,3,[2, 9, 3, 4, 5, 10, 2, 0, 3]) # Définir la matrice # $$N=\begin{bmatrix} # 2& 9& 3\\ 4& 5& 10\\ -6& -1& -17 # \end{bmatrix}$$ # In[73]: N = Matrix(3,3,[2,9,3,4,5,10,-6,-1,-17]); N # Définir le vecteur # $$v=\begin{bmatrix} # 5\\ 2\\ 1 # \end{bmatrix}$$ # In[75]: v = Matrix([5,2,1]); v # ### 8.2 Opérations de base # In[76]: M, N # In[77]: M + N # In[78]: M * 3 # In[79]: M * N # In[80]: M * v # In[81]: M ** -1 # In[82]: N ** -1 # In[83]: M.transpose() # ### 8.3 Accéder aux coefficients # In[84]: M # In[85]: M[1,1] # In[88]: M[2,1] # In[90]: M.row(0) # In[92]: M.col(1) # In[94]: M # In[96]: M[0,0] = pi # In[ ]: # ### 8.4 Construction de matrices particulières # In[102]: zeros(4,6) # In[103]: ones(3,8) # In[104]: eye(5) # In[105]: diag(3,4,5) # In[106]: diag(3,4,5,M) # ### 8.5 Matrice échelonnée réduite # Calculer la forme échelonnée réduite de $M$ et $N$. # In[108]: M # In[110]: M.rref() # In[112]: N # In[111]: N.rref() # ### 8.6 Noyau # Calculer le noyau des matrices $M$ et $N$. # In[113]: M.nullspace() # In[114]: N.nullspace() # ### 8.7 Déterminant # Calculer le déterminant des matrices $M$ et $N$. # In[115]: M.det() # In[116]: N.det() # ### 8.8 Polynôme caractéristique # Calculer le polynôme caractérisque de la matrice $M$ et de $N$. # In[117]: from sympy.abc import x # In[118]: M.charpoly(x) # In[119]: M.charpoly(x).as_expr() # In[120]: N.charpoly(x).as_expr() # ### 8.9 Valeurs propres et vecteurs propres # Calculer les valeurs propres et vecteurs propres de # $$K=\begin{bmatrix} # 93& 27& -57\\ -40& 180& -140\\ -15& 27& 51 # \end{bmatrix}$$ # In[121]: K = Matrix(3,3,[93,27,-57,-40,180,-140,-15,27,51]) K # In[122]: K.eigenvals() # In[123]: K.eigenvects() # En général, les racines peuvent être plus compliquées: # In[124]: M.eigenvals() # In[ ]: