1 / 18

Initiation à la programmation en Python

Initiation à la programmation en Python. Romain Brette 11 mars 2009. Calcul scientifique: SciPy et Pylab. SciPy = bibliothèque scientifique Pylab = bibliothèque graphique Utilisation:.  Matlab. from scipy import * from pylab import *. Documentation: http://docs.scipy.org/doc/

munin
Télécharger la présentation

Initiation à la programmation en Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Initiation à la programmation en Python Romain Brette 11 mars 2009

  2. Calcul scientifique: SciPy et Pylab • SciPy = bibliothèque scientifique • Pylab = bibliothèque graphique • Utilisation:  Matlab from scipy import * from pylab import * Documentation: http://docs.scipy.org/doc/ http://matplotlib.sourceforge.net/ (voir la gallerie d’exemples) http://mathesaurus.sourceforge.net/matlab-numpy.html

  3. SciPy

  4. Vecteurs et matrices • Type de base défini par SciPy: array(= vecteur ou matrice) from scipy import * x=array([1,2,3]) M=array([[1,2,3],[4,5,6]]) M=ones((3,2)) z=2*x+1 y=dot(M,x) vecteur (1,2,3) 1 2 3 4 5 6 matrice 1 1 1 1 1 1 matrice produit matriciel

  5. Opérations x+y x-y x*y x/y x**2 exp(x) sqrt(x) dot(x,y) dot(M,x) M.T M.max() M.sum() size(x) M.shape élément par élément x² produit scalaire produit matriciel transposition maximum somme nombre total d’éléments

  6. Indexation Indexation des vecteurs  listes (premier élément = 0) x[i] M[i,j] x[i:j] M[i,:] M[:,i] x[[1,3]] x[1:3]=[0,1] M[1,:]+=x (i+1)e élement tranche de x[i] à x[j-1] (i+1)e ligne (i+1)e colonne éléments x[1] et x[3] x[1]=0, x[3]=1 ajouter le vecteur x à la 2e ligne de M est une « vue » sur la matrice M  copie ( référence) M[i,:] y=M[0,:] y[2]=5 x=z x[1]=3 M[0,2] vaut 5 copie: z[1] vaut 3 x=z.copy()

  7. Construction x=array([1,2,3]) M=array([[1,2,3],[4,5,6]]) x=ones(5) M=zeros((3,2)) M=eye(3) M=diag([1,3,7]) x=rand(5) x=randn(5) x=arange(10) x=linspace(0,1,100) à partir de listes vecteur de 1 matrice nulle matrice identité matrice diagonale vecteur aléatoire dans [0,1] vecteur aléatoire gaussien 0,1,2,...,9 100 nombres entre 0 et 1

  8. Vecteurs booléens x=array([1,2,3,4]) x>2.5 A(B) x[x>2.5]+=1 M=rand(3,3) i,j=where(M>.5) i=where(x>2.5)[0] [False, False, True, True] = tous les aij tels que bij est True ajoute 1 à chaque élément > 2.5 indices (ligne, colonne) des éléments > .5 indices des éléments > 2.5

  9. Algèbre linéaire from scipy import linalg help(linalg) d=linalg.det(M) liste des fonctions Quelques fonctions: det norm lstsq solve eigvals lu ...

  10. Bibliothèque scientifique import scipy help(scipy) liste des modules de SciPy Exemple: optimisation (scipy.optimize) from scipy import optimize def f(x): return (x-1)*(x-3) print optimize.fsolve(f,0) résout f(x)=0 point initial

  11. Vectorisation • Comment écrire des programmes efficaces? • Remplacer les boucles par des opérations vectorielles for i in range(1000000): X[i]=1 X=ones(1000000) for i in range(1000000): X[i]=X[i]*2 X=X*2 for i in range(999999): Y[i]=X[i+1]-X[i] Y=X[1:]-X[:-1] for i in range(1000000): if X[i]>0.5: Y[i]=1 Y[X>.5]=1

  12. Pylab

  13. Pylab plus d’exemples: http://matplotlib.sourceforge.net/gallery.html import pylab help(pylab) from pylab import * plot([1,2,3],[4,5,6]) show() plot(x,y,’r’) plot(x,y,’.’) xlabel(’Temps (ms)’) figure() subplot(211) plot(x,y) subplot(212) plot(u,v) x y affiche la figure en rouge avec des points (pas de lignes) légende pour l’axe horizontal 1 colonne nouvelle figure sous-figure 211 2 lignes 212

  14. Exercices

  15. Exercice 1 – Analyse de notes • Ecrire une fonction qui analyse une liste de notes (entre 0 et 20) en affichant: • Le nombre de notes • La moyenne • Le nombre de notes au-dessus de 10 • Le nombre de notes au-dessous de 10 • La meilleure note • La plus mauvaise note • L’histogramme des notes (pylab.hist) • Essayer avec des notes aléatoires from scipy import * -> fonction random.randint

  16. Exercice 2 - Triangle de Pascal Ci,j=Ci-1,j-1+Ci-1,j Ecrire une fonction qui affiche les n premières lignes du triangle de Pascal. Ecrire le calcul de manière vectorielle (ligne = vecteur).

  17. Exercice 3 – Dessiner un cercle • Un cercle (x(t),y(t)) est solution d’un système différentiel: • Ecrire un programme qui dessine un cercle en intégrant les équations différentielles (x(t+dt)=x(t)+dt*(-y(t))) • La même chose en l’écrivant sous forme vectorielle: dx/dt = -y dy/dt = x t = angle en radian X=(x,y) dX/dt=MX 0 -1 1 0 M=

  18. Exercice 4 – Carrés presques magiques [[ 3. 4. 2. 5. 1.] [ 1. 2. 5. 3. 4.] [ 4. 5. 3. 1. 2.] [ 2. 3. 1. 4. 5.] [ 5. 1. 4. 2. 3.]] Somme identique sur les lignes et les colonnes • Ecrire une fonction qui détermine si un carré (= matrice) est presque magique. • Ecrire une fonction qui renvoie un carré presque magique dit « élémentaire »: • Ecrire une fonction qui calcule un carré presque magique en échangeant un grand nombre de fois des lignes et des colonnes aléatoirement à partir d’un carré élémentaire. [[ 1. 2. 3. 4. 5.] [ 2. 3. 4. 5. 1.] [ 3. 4. 5. 1. 2.] [ 4. 5. 1. 2. 3.] [ 5. 1. 2. 3. 4.]]

More Related