• Aucun résultat trouvé

[PDF] Formation sur la programmation python cnam | Cours programmation

N/A
N/A
Protected

Academic year: 2021

Partager "[PDF] Formation sur la programmation python cnam | Cours programmation"

Copied!
58
0
0

Texte intégral

(1)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Python pour le calcul scientifique

O. Wilk

Calcul scientifique/Math/Cnam

(2)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

(3)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

Un langage

libre et portable sur n’importe quel

système (Windows, Mac, Linux)

permettant de

relier des mondes et des langages

différents :

réseau

système

visualisation

C

Java

VTK

calcul

Python

Cuda,

OpenCL

Fortran

GTK

web

(4)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

1989 : Guido van Rossum développe Python,

1991 : Python est publié,

1994 : premières applications scientifiques.

Un langage qui sort du lot :

Programming

Web

Position

Position

Position

Language

Rate

Jan 2011

Jan 2006

Jan 1996

Java

18 %

1

1

5

C

16 %

2

2

1

C++

9 %

3

3

2

PHP

8 %

4

4

-Python

6 %

5

8

22

C#

6 %

6

7

-(Visual) Basic

6 %

7

5

3

Objective-C

3 %

8

44

-Perl

3 %

9

6

6

Ruby

2 %

10

20

-Lisp

1 %

13

14

13

Ada

1 %

20

17

12

(5)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

(Seuls 3 langages progressent : Python, Objective-C : langage phare du

développement pour Mac OS X, C# roi des langages .NET pour Microsoft)

(6)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

Pourquoi Python s’impose-t-il ?

Pour sa lisibilité,

rien a déclarer, gestion automatique de la mémoire,

orienté "objet", interprété et partiellement compilé (alternative à

Matlab, Scilab, Octave, ... mais pas simplement),

même si ce n’est pas le plus rapide, on peut l’interfacer avec d’autres

langages plus performants : facile à interfacer avec le Fortran (f2py),

le C (swig, boost), Cuda et OpenCL (pycuda, pyopencl), ...

Des librairies déjà très optimisées et un grand choix de bibliothèques,

d’autres langages accessibles par python : Jython ⇒ Java,

une plateforme très "communautaire".

Python permet d’intégrer facilement des codes de calcul

existants (Fortran, C, ...), de les rendre accessibles

(MacroCommandes, Gui), de traiter graphiquement leurs

résultats et de poursuivre leurs développements ...

(7)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Introduction

Un grand choix de bibliothèques et de logiciels ouverts,

permettant de faire du :

web (Zope, Plone, Django, ...), bases de données

(MySQL, Oracle, ...), réseaux (PyRO, ...), Gui (Gtk, Qt,

WxWidgets, ...), graphique (gnuplot, matplotlib, VTK,

MayaVi, ...), calcul scientifique ( E.F. (FiPy, Getfem, ...),

systèmes Dynamiques (SimPy), ...), mathématiques

(Sage), statistiques (MDP), bioinformatique

(8)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Des bases

(9)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Pour commencer (1/2)

Choisir son interface, son environnement de

programmation :

python,

idle, ipython (complétion automatique, ...),

bpython, spyder, jupyter (notebook), ...

des environnement de dév. : eclipse+PyDev, ...

Savoir chercher de l’information :

sous le prompt python : help(), help(nom d’une

variable, d’une fonction ou d’un package), ...

sous le prompt ipython : help() ou

nom-package.nom-fonction?,

sur la toile : docs.python.org, www.python.org/,

pypi.python.org/, ...

(10)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Pour commencer (2/2)

Respecter quelques règles :

commencer en tout début de ligne (le décalage "4

espaces" sert pour les test et les boucles),

éviter les tabulations n’étant pas constituées

d’espace d’un caractère.

(11)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Commentaires, chaines de caractères (1/2)

CodePython.py

# −∗− c o d i n g : u t f 8 −∗−

# M e t t r e un commentaire dans l e code python

p r i n t " A f f i c h e r un commentaire d u r a n t l ’ exé c u t i o n . "

# ====================================================== # Une c h a i n e de c a r a c t è r e s avec passage à l a l i g n e c h a i n e = " " " Le f e u n ’ a p l u s de fumée

quand i l e s t devenu flamme . D j a l a l a l−Din Rumi " " " p r i n t ch ai ne p r i n t " pour t r a i t e r une ch ai ne de c a r a c t è r e s : " p r i n t " c h a i n e . f i n d , c h a i n e . r e p l a c e , c h a i n e . s p l i t , . . . " # ====================================================== # T r a n s f o r m e r un nombre en c h a i n e de c a r a c t è r e s a = 12345 ; b = −6789.01 ; c = 1.01 e−6 p r i n t " " " A j o u t e r une ch ai ne de c a r a c t è r e s " " " + s t r ( a ) + " " " p u i s une a u t r e " " " +s t r ( b ) + " " " e t l a d e r n i è r e " " " + s t r ( c ) + " " " . " " " # ====================================================== # A f f i c h e r en f o r m a t a n t , a t t e n t i o n aux e r r e u r s é v e n t u e l l e s p r i n t " f o r m a t s : %s %s %s " % (s t r ( a ) , s t r ( b ) , s t r ( c ) ) p r i n t " f o r m a t f : %12.2 f %12.2 f %12.2 f " % ( a , b , c ) p r i n t " f o r m a t d : %6d %6d %6d " % ( a , b , c )

(12)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Commentaires, chaines de caractères (2/2)

[wilk @localhost ∼]$ python CodePython.py

A f f i c h e r un commentaire d u r a n t l ’ exé c u t i o n . Le f e u n ’ a p l u s de fumée

quand i l e s t devenu flamme . D j a l a l a l−Din Rumi pour t r a i t e r une c h a i n e de c a r a c t è r e s : c h a i n e . f i n d , c h a i n e . r e p l a c e , c h a i n e . s p l i t , . . . A j o u t e r une c h a i n e de c a r a c t è r e s 12345 p u i s une a u t r e −6789.01 e t l a d e r n i è r e 1.01 e−06. f o r m a t s : 12345 −6789.01 1.01 e−06 f o r m a t f : 12345.00 −6789.01 0.00 f o r m a t d : 12345 −6789 0

(13)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Listes (... dico.)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− p r i n t " Cré a t i o n d ’ une ’ L i s t e ’ " a = [ 1 , 3 . 1 4 , " t o t o " ] p r i n t a

p r i n t " On r écupè r e l e second " , a [ 1 ] # l a numé r a t o t i o n commence à 0

p r i n t " l e d e r n i e r " , a[ −1] p r i n t " l e s compo . 0 e t 1 " , a [ 0 : 2 ] p r i n t " On e n l ève l a composante 1 " a [ 1 : 2 ] = [ ] p r i n t a p r i n t " l o n g e u r de l a l i s t e " , len ( a ) p r i n t " On a j o u t e un é l ément " a . append ( " t i t i " ) p r i n t a

[wilk @localhost ∼]$ python CodePython.py Cré a t i o n d ’ une ’ L i s t e ’ [ 1 , 3 . 1 4 , ’ t o t o ’ ] On r écupè r e l e second 3.14 l e d e r n i e r t o t o l e s compo . 0 e t 1 [ 1 , 3 . 1 4 ] On e n l ève l a composante 1 [ 1 , ’ t o t o ’ ] l o n g e u r de l a l i s t e 2 On a j o u t e un é l ément [ 1 , ’ t o t o ’ , ’ t i t i ’ ]

(14)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Types et opérations

CodePython.py # −∗− c o d i n g : U t f 8 −∗− p r i n t " Quelques o p e r a t i o n s " p r i n t " 1 . + 2 . , 1. −2. , 1 . ∗ 2 . , 1 . / 2 . , 2∗∗2 " p r i n t 1 . + 2 . , 1. −2. , 1 . ∗ 2 . , 1 . / 2 . , 2∗∗2 p r i n t " Un complexe : " a = complex ( 1 . 1 , 2 . 2 ) p r i n t a . r e a l , a . imag , type ( a ) b = i n t ( a . r e a l ) ; c = f l o a t ( b ) # Conversion . . . import math p r i n t " P i = %.60 f " % ( math . p i )

[wilk @localhost ∼]$ python CodePython.py Quelques o p e r a t i o n s 1 . + 2 . , 1. −2. , 1 . ∗ 2 . , 1 . / 2 . , 2∗∗2 3 . 0 −1.0 2 . 0 0 . 5 4 Un complexe : 1 . 1 2 . 2 < t y p e ’ complex ’ > P i = 3.141592653589793115997963468544185161590576171875000000000000

(15)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Tests et boucles (1/3)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− # i f i f ( 0 == 1 ) :

# i l f a u t i n d e n t e r pour que Python sache que l ’ on e s t dans l e t e s t

p r i n t " " p r i n t " On s o r t ! " q u i t ( ) else : p r i n t " " p r i n t " t e s t s : ( 0 < 1 ) " , ( 0 < 1 ) , type ( ( 0 == 1 ) ) p r i n t " t e s t s : ( 0 > 1 ) " , ( 0 > 1 ) p r i n t " t e s t s : ( 0 == 1 ) and ( 1 == 1 ) " , ( 0 == 1 ) and ( 1 == 1 ) p r i n t " t e s t s : ( 0 == 1 ) o r ( 1 == 1 ) " , ( 0 == 1 ) or ( 1 == 1 ) # l a f i n de l ’ i n d e n t a t i o n marque l a f i n du t e s t p r i n t " On c o n t i n u e hors du t e s t "

[wilk @localhost ∼]$ python CodePython.py

t e s t s : ( 0 < 1 ) True < t y p e ’ bool ’ > t e s t s : ( 0 > 1 ) Fa ls e

t e s t s : ( 0 == 1 ) and ( 1 == 1 ) Fa ls e t e s t s : ( 0 == 1 ) o r ( 1 == 1 ) True On c o n t i n u e hors du t e s t

(16)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Tests et boucles (2/3)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− # w h i l e i = 0 while i < 3 : p r i n t i i = i + 1 while 1 : t r y :

x = f l o a t ( raw_input ( " Frapper au c l a v i e r un nombre

. . . ( a u t r e chose vous f e r a recommencer ) : " ) )

break except V a l u e E r r o r :

p r i n t " Ce n ’ e s t pas c o r r e c t ! Recommencez . . . " p r i n t x

[wilk @localhost ∼]$ python CodePython.py 0

1 2

Frapper au c l a v i e r un nombre ( a u t r e chose vous f e r a recommencer ) : a z e r t y Ce n ’ e s t pas c o r r e c t ! Recommencez . . .

Frapper au c l a v i e r un nombre ( a u t r e chose vous f e r a recommencer ) : 123.1 123.1

(17)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Tests et boucles (3/3)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− # f o r f o r i i n xrange ( 0 , 5 ) : p r i n t i p r i n t [ j f o r j i n xrange ( 0 , 5 ) ]

[wilk @localhost ∼]$ python CodePython.py 0 1 2 3 4 [ 0 , 1 , 2 , 3 , 4 ]

(18)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Entrée - Sortie (1/5)

CodePython.py (PAS SOUS JUPYTER) # −∗− c o d i n g : u t f 8 −∗−

# Récupé r e r des arguments en e n t r ée

import sys # On i m p o r t e un package

# i l va nous s e r v i r a r écupé r e r des arguments d ’ e n t r ée . . . L i s t e = sys . argv # On r écupè r e l e s arguments d ’ e n t r ée

# python C e F i c h i e r . py arg1 arg2 . . .

p r i n t " "

p r i n t " Les arguments s o n t dans l a l i s t e s u i v a n t e : " , L i s t e

p r i n t [ L i s t e [ j ] f o r j i n xrange ( 0 , len ( L i s t e ) ) ] , type ( L i s t e )

[wilk @localhost ∼]$ python CodePython.py 1 2

Les arguments s o n t dans l a l i s t e s u i v a n t e : [ ’ 2 a_In_Out . py ’ , ’ 1 ’ , ’ 2 ’ ] [ ’ 2 a_In_Out . py ’ , ’ 1 ’ , ’ 2 ’ ] < t y p e ’ l i s t ’ >

(19)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Entrée - Sortie (2/5)

CodePython.py (PAS SOUS JUPYTER) # −∗− c o d i n g : u t f 8 −∗−

# Récupé r e r des arguments en co ur s d ’ exé c u t i o n # Un nombre

x = input ( " Frapper au c l a v i e r un nombre : " ) p r i n t x , type ( x )

# Une c h a i n e de c a r a c t è r e s

p r i n t " "

a = raw_input ( " Frapper au c l a v i e r une c hai ne de c a r a c t è r e s ( sans ’ ’ ) : " )

p r i n t a , a [ 0 : 3 ] , a [ 1 : 3 ] , type ( a )

[wilk @localhost ∼]$ python CodePython.py 1 2 Frapper au c l a v i e r un nombre : 123.1 123.1 < t y p e ’ f l o a t ’ >

Frapper au c l a v i e r une c h a i n e de c a r a c t è r e s ( sans ’ ’ ) : a z e r t y a z e r t y aze ze < t y p e ’ s t r ’ >

(20)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Entrée - Sortie (3/5)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− p r i n t " O u v e r t u r e du f i c h i e r en e c r i t u r e " f =open ( ’ tmp . d a t ’ , ’w ’ )

p r i n t " On é c r i t dans l e f i c h i e r ( avec des s a u t s de l i g n e s ) "

x = 1 . 1 ; y = 2 f . w r i t e ( s t r ( x ) ) f . w r i t e ( s t r ( y ) ) f . w r i t e ( ’ \ n ’ ) f . w r i t e ( s t r ( x ) ) f . w r i t e ( ’ \ n ’ ) f . w r i t e ( s t r ( y ) ) p r i n t " On ferme l e f i c h i e r " f . c l o s e ( )

[wilk @localhost ∼]$ cat tmp.dat 1.12

1 . 1 2

(21)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Entrée - Sortie (4/5)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− p r i n t " D i f f é r e n t e s maniè r e s de l i r e " p r i n t " V1 " f =open ( ’ tmp . d a t ’ , ’ r ’ ) p r i n t " " , f . read ( ) f . c l o s e ( ) p r i n t " V2 " f =open ( ’ tmp . d a t ’ , ’ r ’ ) p r i n t " 1 " , f . r e a d l i n e ( ) p r i n t " 2 " , f . r e a d l i n e ( ) f . c l o s e ( ) p r i n t " V3 " f =open ( ’ tmp . d a t ’ , ’ r ’ ) p r i n t " " , f . r e a d l i n e s ( ) f . c l o s e ( )

[wilk @localhost ∼]$ python CodePython.py D i f f é r e n t e s maniè r e s de l i r e V1 1.12 1 . 1 2 V2 1 1.12 2 1 . 1 V3 [ ’ 1 . 1 2 \ n ’ , ’ 1 . 1 \ n ’ , ’ 2 ’ ]

(22)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Entrée - Sortie (5/5)

CodePython.py # −∗− c o d i n g : u t f 8 −∗− # L i r e un f i c h i e r a s c i i c o n t e n a n t une m a t r i c e : # v o i r a u s s i ’ l o a d ’ , ’ save ’ , ’ savez ’ import numpy as np a = np . l o a d t x t ( ’ mat . d a t ’ , d e l i m i t e r = ’ , ’ ) p r i n t type ( a ) , a . shape , a . s i z e p r i n t " " p r i n t a

[wilk @localhost ∼]$ cat mat.dat # commentaire

1 , 2 , 3 4 , 5 , 6 7 , 8 , 9

[wilk @localhost ∼]$ python CodePython.py < t y p e ’ numpy . ndarray ’ > ( 3 , 3 ) 9

[ [ 1 . 2 . 3 . ] [ 4 . 5 . 6 . ] [ 7 . 8 . 9 . ] ]

(23)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Fonctions (1/2)

CodePython.py # −∗− c o d i n g : U t f 8 −∗− # Une f o n c t i o n dans l e co rp s du s c r i p t . . . def F i b o n a c c i ( n ) : a , b = 0 , 1 f o r i i n xrange ( n ) : a , b = b , a+b r e t u r n b p r i n t [ F i b o n a c c i ( i ) f o r i i n xrange ( 1 , 1 0 ) ]

# i m p o r t e r une f o n c t i o n q u i n ’ e s t pas dans l e même r é p e r t o i r e

import sys

sys . path . append ( " Bib_Perso " ) # On a j o u t e l e l e chemin du r é p e r t o i r e

import F i b o n a c c i _ 2

p r i n t [ F i b o n a c c i _ 2 . F i b o n a c c i _ 2 ( i ) f o r i i n xrange ( 1 , 1 0 ) ]

[wilk @localhost ∼]$ cat Bib_Perso/Fibonacci2.py

def F i b o n a c c i _ 2 ( n ) : " " " I l e s t p o s s i b l e de documenter l e s f o n c t i o n s e t c . . . " " " a , b = 0 , 1 f o r i i n xrange ( n ) : a , b = b , a+b r e t u r n b

[wilk @localhost ∼]$ python CodePython.py [ 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 5 5 ] [ 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 5 5 ]

(24)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Fonctions (2/2)

CodePython.py # −∗− c o d i n g : U t f 8 −∗− # F o n c t i o n à p l u s i e u r s v a r i a b l e s i n e t o u t def t e s t ( a , b ) : c = a + b d = a ∗ b r e t u r n c , d a = 2 . 0 ; b = 1 . 0 [ c , d ] = t e s t ( a , b ) p r i n t a , b , c , d # exé c u t e r des f o n c t i o n s d ’ a u t r e s l o g i c i e l s # ( hors python , sous s h e l l u n i x / l i n u x par ex )

import os

os . system ( ’ pwd ’ )

[wilk @localhost ∼]$ python CodePython.py 2 . 0 1 . 0 3 . 0 2 . 0

(25)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

(26)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy, Scipy et Matplotlib

Numpy : Un package incontournable pour

commencer à faire du calcul scientifique sous

Python,

Scipy : un ensemble d’extensions permettant de

compléter Numpy,

Matplotlib : le package permettant de faire des

visualisations 2D (mais pas que ...) de très haute

qualité, facilement et avec de très bonnes

(27)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy

(28)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (1/6) :

des tableaux.

[wilk @localhost ∼]$ python >>> import numpy as np >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dt ype = ’ f l o a t 3 2 ’ ) >>> p r i n t a [ [ 1 . 5 2 . 3 . ] [ 4 . 5 . 6 . ] ] >>> a . shape , a . s i z e , a . dty pe ( 2 , 3 ) , 6 , dty pe ( ’ f l o a t 3 2 ’ ) >>> type ( a ) <type ’ numpy . n d a r r a y ’ > >>> b = np . ze ro s ( ( 2 , 3 ) , dty pe = ’ f l o a t 3 2 ’ ) >>> b a r r a y ( [ [ 0 . , 0 . , 0 . ] , [ 0 . , 0 . , 0 . ] ] , d typ e = f l o a t 3 2 ) >>> b = np . ones ( ( 2 , 3 ) , dty pe = ’ f l o a t 3 2 ’ ) >>> b a r r a y ( [ [ 1 . , 1 . , 1 . ] , [ 1 . , 1 . , 1 . ] ] , d typ e = f l o a t 3 2 )

(29)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (1/6) :

des tableaux (suite).

[wilk @localhost ∼]$ python >>> import numpy as np >>> np . arange ( 0 , 1 , 0 . 1 ) a r r a y ( [ 0 . , 0 . 1 , 0 . 2 , 0 . 3 , 0 . 4 , 0 . 5 , 0 . 6 , 0 . 7 , 0 . 8 , 0 . 9 ] ) >>> np . l i n s p a c e ( 0 , 1 , 11 ) a r r a y ( [ 0 . , 0 . 1 , 0 . 2 , 0 . 3 , 0 . 4 , 0 . 5 , 0 . 6 , 0 . 7 , 0 . 8 , 0 . 9 , 1 . ] ) >>> x = np . l i n s p a c e ( 0 , np . p i , 10 ) >>> np . s i n ( x )

a r r a y ( [ 0.00000000e +00 , 3.42020143e−01, 6.42787610e−01, 8.66025404e−01, 9.84807753e−01, 9.84807753e−01, 8.66025404e−01, 6.42787610e−01, 3.42020143e−01, 1.22464680e−16])

>>> np . s i n ( np . p i ) # à comparer à l a d e r n i è r e v a l e u r obtenue p r écédemment 1.2246467991473532e−16

(30)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (2/6) :

des opérations.

[wilk @localhost ∼]$ python >>> import numpy as np >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dt ype = ’ f l o a t 3 2 ’ ) >>> c = np . ones ( ( 2 , 3 ) , dty pe = ’ f l o a t 3 2 ’ ) >>> c a r r a y ( [ [ 1 . , 1 . , 1 . ] , [ 1 . , 1 . , 1 . ] ] , d typ e = f l o a t 3 2 )

>>> a∗c # p r o d u i t terme à terme

a r r a y ( [ [ 1 . 5 , 2 . , 3 . ] , [ 4 . , 5 . , 6 . ] ] , d typ e = f l o a t 3 2 ) >>> np . d o t ( a , c ) # p r o d u i t e n t r e 2 m a t r i c e s Traceback ( most r e c e n t c a l l l a s t ) : F i l e " < s t d i n > " , l i n e 1 , i n <module> V a l u e E r r o r : m a t r i c e s are not a l i g n e d >>> np . t r a n s p o s e ( a ) a r r a y ( [ [ 1 . 5 , 4 . ] , [ 2 . , 5 . ] , [ 3 . , 6 . ] ] , d typ e = f l o a t 3 2 ) >>> np . d o t ( np . t r a n s p o s e ( a ) , c ) a r r a y ( [ [ 5 . 5 , 5 . 5 , 5 . 5 ] , [ 7 . , 7 . , 7 . ] , [ 9 . , 9 . , 9 . ] ] , d typ e = f l o a t 3 2 )

(31)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (3/6) :

des fonctions.

[wilk @localhost ∼]$ python >>> import numpy as np >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dt ype = ’ f l o a t 3 2 ’ ) >>> np .sum( a ) # ou a . sum ( ) 21.5 >>> np .min ( a ) # ou a . min ( ) 1 . 5 >>> np .max( a ) # ou a . max ( ) 6 . 0 >>> np . mean ( a ) # ou a . mean ( ) 3.5833333333333335 >>> np . median ( a ) # ou a . median ( ) 3 . 5 >>> x = np . a r r a y ( [ 3 0 , 2 0 , 1 0 ] ) >>> i n d e x = np . a r g s o r t ( x ) >>> x [ i n d e x ] a r r a y ( [ 1 0 , 20 , 3 0 ] )

et aussi : np.exp, np.log, np.log10, np.sqrt, np.sin, np.cos, np.tan, np.arcsin, np.arccos, np.arctan, np.sign ...

(32)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (3/6) :

des fonctions (suite).

[wilk @localhost ∼]$ python A r r a y C r e a t i o n

arange , a r r a y , copy , empty , e m p t y _ l i k e , eye , f r o m f i l e , f r o m f u n c t i o n , i d e n t i t y , l i n s p a c e , logspace , mgrid , o g r i d , ones , o n e s _ l i k e , r , zeros , z e r o s _ l i k e

Conversions

astype , a t l e a s t 1d , a t l e a s t 2d , a t l e a s t 3d , mat M a n i p u l a t i o n s

a r r a y s p l i t , column s t a c k , concatenate , d i a g o n a l , d s p l i t , dstack , h s p l i t , hstack , item , newaxis , r a v e l , r e p e a t , reshape , r e s i z e , squeeze , swapaxes , take , transpose , v s p l i t , v s t a c k

Questions

a l l , any , nonzero , where

O r d e r i n g

argmax , argmin , a r g s o r t , max, min , ptp , s e a r c h s o r t e d , s o r t

O p e r a t i o n s

choose , compress , cumprod , cumsum , i n n e r , f i l l , imag , prod , put , putmask , r e a l , sum

B as ic S t a t i s t i c s cov , mean , s t d , v a r B as ic L i n e a r A l g e b r a

(33)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (4/6) :

des affectations (attention).

[wilk @localhost ∼]$ python >>> import numpy as np >>> x = np . a r r a y ( [ 3 0 , 2 0 , 1 0 ] ) >>> y = x >>> y i s x True >>> y [ 1 ] = 22 >>> x a r r a y ( [ 1 0 , 22 , 3 0 ] ) >>> z = y . copy ( ) >>> z i s y F al se >>> z [ 1 ] = 20 >>> y a r r a y ( [ 1 0 , 22 , 3 0 ] ) >>> z a r r a y ( [ 1 0 , 20 , 3 0 ] )

(34)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (5/6) :

des extractions.

[wilk @localhost ∼]$ python >>> import numpy as np >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 , 4 ) , ( 4 . 5 , 5 , 6 , 7 ) ] ) >>> a a r r a y ( [ [ 1 . 5 , 2 . , 3 . , 4 . ] , [ 4 . 5 , 5 . , 6 . , 7 . ] ] ) >>> a [ : , 1 ] a r r a y ( [ 2 . , 5 . ] ) >>> a [ 1 , : ] a r r a y ( [ 4 . 5 , 5 . , 6 . , 7 . ] ) >>> a [ 1 , 0 : 3 ] a r r a y ( [ 4 . 5 , 5 . , 6 . ] ) >>> a [ 1 , 1 : 3 ] a r r a y ( [ 5 . , 6 . ] ) >>> a [0 , −1] # d e r n i è r e v a l e u r de l a premi è r e l i g n e 4 . 0 >>> i = np . a r r a y ( [ 0 , 2 ] ) >>> a [ : , i ] # r écupè r a t i o n à l ’ a i d e d ’ un " a r r a y " pour l e s i n d i c e s a r r a y ( [ [ 1 . 5 , 3 . ] , [ 4 . 5 , 6 . ] ] )

et aussi : np.diag, np.linalg.det, np.linalg.eig, ... ("linalg" pour algèbre linéaire, voir plutôt scipy)

(35)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Numpy (6/6) :

Intégrer du code C ou fortran

(36)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Scipy

(37)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Scipy (1/3) :

prolonge Numpy.

[wilk @localhost ∼]$ python >>> import numpy as np >>> import s c i p y as sp >>> np . s q r t ( − 1 . ) , sp . s q r t ( −1.) ( nan , 1 j ) >>> np . l o g ( − 1 . ) , sp . l o g ( −1.) ( nan , 3.1415926535897931 j ) ...

(38)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Scipy (2/3) :

des modules spécialisés.

Sous module

Description

cluster

Clustering algorithms

constants

Physical and mathematical constants

fftpack

Fast Fourier Transform routines

integrate

Integration and ordinary differential equation solvers

interpolate

Interpolation and smoothing splines

io

Input and Output

linalg

Linear algebra

maxentropy

Maximum entropy methods

ndimage

N-dimensional image processing

odr

Orthogonal distance regression

optimize

Optimization and root-finding routines

signal

Signal processing

sparse

Sparse matrices and associated routines

spatial

Spatial data structures and algorithms

special

Special functions

stats

Statistical distributions and functions

weave

CC++ integration

(39)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Scipy (3/3) : Stockage morse

Importer le module "scipy.sparse" :

>>>

import scipy.sparse as sp_sp

Les différentes classes, modules et fonctions,

un petit exemple.

(40)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Les différentes classes

Différents stockages morses :

csc_matrix: Compressed Sparse Column format,

csr_matrix: Compressed Sparse Row format,

bsr_matrix: Block Sparse Row format,

lil_matrix: List of Lists format,

dok_matrix: Dictionary of Keys format,

coo_matrix: COOrdinate format,

dia_matrix: DIAgonal format.

Les formats "csc_matrix" et "csr_matrix" sont à

privilégier lors des opérations de factorisation, de

résolution, ...

Les autres sont plus souples pour construire ou

manipuler les matrices.

(41)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Quelques fonctions

et aussi pour construire une matrice morse :

eye : 1 sur la k ième diagonale, sinon 0,

identity : matrice identité stockée morse,

kron, kronsum : produit et somme de kronecker de matrices morses,

spdiags : matrice diagonale stockée morse,

tril, triu : partie "lower" et "upper" d’une matrice morse,

bmat : création d’une matrice bloc à partir de matrices morses, voir

aussi hstack et vstack.

(42)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Exemples

import numpy as np import s c i p y . sparse as sp_sp row = np . a r r a y ( [ 0 , 2 , 1 ] ) ; c o l = np . a r r a y ( [ 0 , 2 , 1 ] ) ; data = np . a r r a y ( [ 4 , 5 , 7 ] )

p r i n t sp_sp . c o o _ m a t r i x ( ( data , ( row , c o l ) ) , shape = ( 3 , 3 ) ) . todense ( )

# [ [ 4 0 0 ] # [ 0 7 0 ] # [ 0 0 5 ] ] p r i n t sp_sp . eye ( 3 , 3 , k = 1 ) . todense ( ) # [ [ 0 . 1 . 0 . ] # [ 0 . 0 . 1 . ] # [ 0 . 0 . 0 . ] ] data = np . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ) ; d i a g s = np . a r r a y ( [ 0 , − 1 , 1 ] )

p r i n t sp_sp . s p d i a g s ( data , diags , 3 , 3 ) . todense ( )

# [ [ 1 8 0 ] # [ 4 2 9 ] # [ 0 5 3 ] ]

A = sp_sp . c o o _ m a t r i x ( [ [ 1 , 2 ] , [ 3 , 4 ] ] )

B = sp_sp . c o o _ m a t r i x ( [ [ 5 ] , [ 6 ] ] ) ; C = sp_sp . c o o _ m a t r i x ( [ [ 7 ] ] )

p r i n t A . todense ( ) ; p r i n t B . todense ( ) ; p r i n t C . todense ( )

# [ [ 1 2 ] [ [ 5 ] [ [ 7 ] ]

# [ 3 4 ] ] [ 6 ] ]

p r i n t sp_sp . bmat ( [ [ A , B ] , [ None , C ] ] ) . todense ( ) # [ [ 1 2 5 ]

# [ 3 4 6 ] # [ 0 0 7 ] ]

(43)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Des modules

extract : pour extraire différentes parties d’une matrice morse,

sparsetools : différentes routines pour manipuler les matrices ...

produits,

...

en relation avec (A une matrice stockée morse, x un vecteur) :

LaDiagonale = A.diagonal(),

UnProduitMatriceVecteur = A.dot(x) = A._mul_vector(x),

...

linalg : algèbre linéaire pour des matrices morses (sous-modules :

eigen (valeurs propres), isolve (méthodes itératives), dsolve

(méthodes directes).

(44)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

scipy.sparse.linalg

matmat, matvec,

cg : gradient conjugué,

bicg : gradient bi-conjugué,

bicgstab : gradient biconjugué stabilisé,

cgs : gradient conjugué carré,

eigs, eigsh : valeurs et vecteurs propres d’une matrice carrée stockée

morse non herm. et herm. (Arpack),

factorized : LU,

gmres, lgmres : minimisation du résidu généralisé,

lobpcg : valeurs propres (utilisant un préconditionnement),

lsqr : Large sparse QR,

minres : (experimental)

qmr : quasi-minimisation du résidu,

splu, spilu : LU et LU incomplet (SuperLU).

(45)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Qq exemples

Pour A une matrice stockée morse

(stockage au format CSC ou CSR pour Umfpack) :

>>>

[Val, Vect] = sp_sp.linalg.eigs(A, k = 10) # les 10 premières v.p.,

>>>

sp_sp.linalg.use_solver(useUmfpack = True) # ... sinon SuperLU

>>>

solve = sp_sp.linalg.factorized( A ) # factorisation LU

>>>

x1 = solve( b1 ) # descente-remontée, b1 scd. membre,

>>>

x2 = solve( b2 ) # même chose sur un autre scd membre.

(46)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Visualiser sous Python

Matplotlib (courbes, images ... haute qualité, 2D),

autres (plutôt orienté 3D, vtk) : Mayavi, Paraview,

...

(47)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (1/4) :

un point de vue.

Visualisation 2D de qualité (png, pdf, eps, ...),

Matlab-like (pylab : pyplot + numpy),

intégration de commandes LaTeX,

(48)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (2/4) :

visu. intéractive.

[wilk @localhost ∼]$ python

import numpy as np import m a t p l o t l i b . p y p l o t as p l t x = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) y = x∗x p l t . f i g u r e ( ) p l t . s u b p l o t ( 2 1 1 ) p l t . p l o t ( x , y , c o l o r = ’ r ’ ) p l t . t i t l e ( ’ T i t r e 1 ’ , c o l o r = ’ g ’ ) p l t . s u b p l o t ( 2 1 2 ) p l t . p l o t ( x , y , marker= ’ o ’ ) p l t . t i t l e ( ’ T i t r e 2 ’ , c o l o r = ’ b ’ ) p l t . show ( )

(49)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (3/4) :

sauvegarde auto. png, ...

[wilk @localhost ∼]$ python

import numpy as np import m a t p l o t l i b . p y p l o t as p l t import m a t p l o t l i b as mpl # Pour a v o i r du t e x t e ( t i t r e ) en LaTeX mpl . r c ( ’ t e x t ’ , u s e t e x =True ) x = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) y = x∗x

p l t . i o n ( ) # mode dynamique ( non s t a n d a r t ) p l t . f i g u r e ( ) p l t . p l o t ( x , y , marker= ’ o ’ ) p l t . x l i m ( 2 , 8 ) p l t . t i t l e ( r ’ $ \ v a r p h i $ en \ LaTeX ’ ) p l t . draw ( ) p l t . s a v e f i g ( ’ image . png ’ ) p l t . i o f f ( ) # pour r e v e n i r en mode s t a n d a r t et aussi : plt.imshow, plt.colorbar,

(50)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

(51)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (4/6) :

nos exemples (1/9)

[wilk @localhost ∼]$ python # −∗− c o d i n g : u t f −8 −∗− import numpy as np import m a t p l o t l i b . p y p l o t as p l t import m a t p l o t l i b as mpl n _ l i g , n _ c o l = 3 , 4 ; num = 1 p l t . i o n ( ) # p l t . i o f f ( ) : mode dynamique ou s t a n d a r t p l t . f i g u r e ( num=1 , f i g s i z e = ( 1 6 , 8 ) ) # c r é a t i o n d ’ une f i g u r e p l t . c l f ( ) # On n e t t o i e l a f i g u r e # −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 x = np . l i n s p a c e ( 0 , np . p i , 1 0 ) y = np . l i n s p a c e ( 0 , np . p i , 1 0 ) [ X , Y ] = np . meshgrid ( x , y ) p l t . q u i v e r ( X , Y , np . cos ( X ) , np . s i n ( Y ) ) 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5

(52)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (4/6) :

nos exemples (2-3/9)

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num , p o l a r =True ) ; num += 1 r = np . arange ( 0 , 3 . 0 , 0 . 0 1 ) ; t h e t a = 2∗np . p i ∗ r p l t . p o l a r ( t h e t a , r ) 0° 45° 90° 135° 180° 225° 270° 315° 0.51.01.52.0 2.53.0

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 x = np . arange ( 0 , 2 0 , 1 ) p l t . bar ( x , x ) 0 5 10 15 20 0 5 10 15 20

(53)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (4/6) :

nos exemples (4-5/9)

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 x = np . arange ( 0 , 2 0 , 1 ) p l t . s e m i l o g y ( x , np . exp ( x ) ) p l t . g r i d ( ) # v o i r a u s s i : p l t . s e m i l o g x 0 5 10 15 20 100 101 102 103 104 105 106 107 108 109

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 n = 256 x = np . l i n s p a c e (−3, 3 , n ) y = np . l i n s p a c e (−3, 3 , n ) [ X , Y ] = np . meshgrid ( x , y ) def f ( x , y ) : r e t u r n ( 1 − x / 2 + x ∗∗ 5 + y ∗∗ 3 ) ∗ np . exp(−x ∗∗ 2 −y ∗∗ 2 ) p l t . c o n t o u r f ( X , Y , f ( X , Y ) , 8 , cmap= " h o t " ) C = p l t . c o n t o u r ( X , Y , f ( X , Y ) , 8 , c o l o r s = ’ b l a c k ’ , l i n e w i d t h = . 5 ) p l t . c l a b e l (C, f o n t s i z e =10) # v o i r a u s s i : p l t . c o n t o u r 3 2 1 0 1 2 3 3 2 1 0 1 2 3 -0.500 -0.250 0.000 0.250 0.500 0.500 0.750

(54)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (4/6) :

nos exemples (6-7/9)

[wilk @localhost ∼]$ python

ax1 = p l t . s u b p l o t 2 g r i d ( ( 3 , 4 ) , ( 1 , 1 ) , c o l s p a n =2) ; num += 2 t h e t a = np . l i n s p a c e ( 1 . e−9, 2∗np . p i , 100 , e n d p o i n t = F al se ) xc = 3∗np . cos ( t h e t a ) yc = 1∗np . s i n ( t h e t a ) t r i a n g = mpl . t r i . T r i a n g u l a t i o n ( xc , yc ) Z = ( t r i a n g . x∗∗2 + t r i a n g . y ∗∗2)

p l t . a x i s ( ’ e qua l ’ ) # même dimension en x qu ’ en y ( un c e r c l e sera rond ) p l t . a x i s ( " o f f " )

p l t . t r i c o n t o u r f ( t r i a n g , Z , 2 0 )

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 p h i = 100 + 10 ∗ np . random . randn ( 1 0 0 0 ) n , b i n s , patches = p l t . h i s t ( p h i , 20 , c o l o r = " green " ) # v o i r a u s s i : p l t . h i s t 2 d 60 70 80 90 100 110 120 130 0 20 40 60 80 100 120 140

(55)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (4/6) :

nos exemples (8-9/9)

[wilk @localhost ∼]$ python

ax2 = p l t . s u b p l o t 2 g r i d ( ( 3 , 4 ) , ( 2 , 0 ) , c o l s p a n =3) ; num += 3 x = np . arange ( 0 , 1 0 , 1 ) p l t . p l o t ( x , x , c o l o r = " red " , marker= " < " , l a b e l = " $x$ " ) p l t . p l o t ( x , x ∗∗2 , c o l o r = " green " , marker= " o " , l a b e l = " $x ^2$ " ) p l t . p l o t ( x , x ∗∗3 , c o l o r = " b l u e " , marker= " > " , l a b e l = " $x ^3$ " ) p l t . legend ( l o c = " upper l e f t " ) p l t . x l i m ( 2 , 8 ) p l t . x l a b e l ( " a b s c i s s e " ) 2 3 4 abscisse5 6 7 8 0 100 200 300 400 500 600 700 800 x x2 x3

[wilk @localhost ∼]$ python

p l t . s u b p l o t ( n _ l i g , n_col , num ) ; num += 1 #M1 = np . arange ( 0 , 2 5 6 , 1 ) . reshape ( [ 1 6 , 1 6 ] ) # p l t . a x i s ( " o f f " )

image = p l t . imread ( " A n t i l o p e . png " )

p l t . imshow ( image [ : , : , 0 ] , cmap= " gray " ) ; p l t . c o l o r b a r ( ) ; p l t . draw ( ) # v o i r a u s s i : cmap=" h o t " , cmap=" j e t " , . . . # v o i r a u s s i : p l t . imread , p l t . imsave 0 100 200 300 400 500 0 100 200 300 400 500 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8

(56)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (5/6) :

le 3D, un exemple

[wilk @localhost ∼]$ python

import numpy as np

import m a t p l o t l i b . p y p l o t as p l t from m p l _ t o o l k i t s . mplot3d import Axes3D def l o r e n z ( x , y , z , s =10 , r =28 , b = 2 . 6 6 7 ) : " " " x_pt , y_pt , z _ p t " " " r e t u r n ( s ∗( y − x ) , r ∗x − y − x∗z , x∗y − b∗z ) T = 1 0 . ; d t = 0.01 ; npas = i n t ( T / d t ) x , y , z = np . z er os ( [ 3 , npas ] ) # C o n d i t i o n s i n i t i a l e s x [ 0 ] , y [ 0 ] , z [ 0 ] = ( 0 . , 1 . , 1 . 0 5 ) f o r i i n xrange ( npas−1) : x_pt , y_pt , z _ p t = l o r e n z ( x [ i ] , y [ i ] , z [ i ] ) x [ i + 1 ] = x [ i ] + x _ p t∗ d t y [ i + 1 ] = y [ i ] + y _ p t∗ d t z [ i + 1 ] = z [ i ] + z _ p t∗ d t f i g = p l t . f i g u r e ( ) ax = f i g . gca ( p r o j e c t i o n = ’ 3d ’ ) ax . p l o t ( x , y , z ) ; ax . s e t _ t i t l e ( " A t t r a c t e u r de Lorenz " ) p l t . show ( ) p l t . s a v e f i g ( " . . / PS/ 7 e _ M a t p l o t l i b . p d f " ) # v o i r a u s s i : ax . p l o t _ s u r f a c e , ax . p l o t _ w i r e f r a m e 10 5 0 5 10 15 20 10 010 20 10 20 30 40 50 Attracteur de Lorenz

(57)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Matplotlib (6/6) :

Bcp d’autres exemples.

(58)

Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie

Bibliographie

Pierre Puiseux, Université de Pau et des Pays de l’Adour, Traduction française du tutoriel écrit par Guido Van Rossum http : //python.developpez.com/cours/TutoVanRossum/.

Exposés journée Python à Autrans, 2010 : Sylvain Faure, Loïc Gouarin, Thierry Dumont, Pierre Raybaut

Références

Documents relatifs

Langage compilé (à compiler, ou langage de « bas niveau ») rustique (rédaction complète, puis compilation, puis exécution), contraignant (typage statique, ordre des fonctions

L'auteur explique comment ces objets vivants, usuels mais aussi dotés d'une signification sociale et symbolique au XVIIIe siècle, sont devenus des objets inanimés mais

The study aimed to identify the electronic management in the Algerian Telecom Corporation in the valley and its role in raising the level of performance of

Mais c'est aussi l'occasion de constater l'existence d'une autre opposition qui est, elle, indépendante du contexte : que ce soit dans cette Maison brahmane ou dans les centres

:فظنلأا جاتنلإا او لاعفلا ءادلأا ةينقتلا هذه دعت ةدعاقل لأا جنل ةيساس ةرادلإا ماظن حا ا اهفيرعت تم ثيح ،ةيئيبل ةدحتلما مملأل يئيبلا جمانبرلا لبق نم (

L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des

L'un des principes méthodologiques qui pourrait être retenu, c'est de détacher les pratiques socio-spatiales observées dans un quartier des caractéristiques socio-spatiales

Pierre, entrepreneur, 9 rue Delaborde, « timbre dans la galerie », « règlement échappement » (1900-1901) ; Société anonyme d’électricité et d’automobiles, Mors, 48 rue