Python pour le calcul scientifique
Transcription
Python pour le calcul scientifique
Python pour le calcul scientifique O. Wilk Python pour le calcul scientifique Introduction Des bases Commentaires, chaines Listes Types et opérations O. Wilk Entrée - Sortie Fonctions Le calcul scientifique Calcul scientifique/Math/Cnam Numpy Scipy scipy.sparse 2016 La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Introduction Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Introduction O. Wilk Un langage libre et portable sur n’importe quel système (Windows, Mac, Linux) Introduction Des bases Commentaires, chaines Listes permettant de relier des mondes et des langages différents : Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse réseau système C La visualisation Matplotlib visualisation Java VTK Python calcul Fortran entrée/sortie O. Wilk: Python pour le calcul scientifique Bibliographie Cuda, GTK OpenCL web IHM Calcul scientifique/Math/Cnam Introduction 1989 : Guido van Rossum développe Python, 1991 : Python est publié, 1994 : premières applications scientifiques. Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes 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 Classement TIOBE / moteurs de recherche du web. O. Wilk: Python pour le calcul scientifique Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique Introduction 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 (Seuls 3 langages progressent : Python, Objective-C : langage phare du développement pour Mac OS X, C# roi des langages .NET pour Microsoft) Python élu langage de l’année 2010 par TIOBE. O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Introduction O. Wilk Pourquoi Python s’impose-t-il ? Introduction Pour sa lisibilité, Des bases rien a déclarer, gestion automatique de la mémoire, Commentaires, chaines orienté "objet", interprété et partiellement compilé (alternative à Matlab, Scilab, Octave, ... mais pas simplement), Types et opérations Listes Entrée - Sortie Fonctions 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". Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie 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 ... O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Introduction O. Wilk Introduction Un grand choix de bibliothèques et de logiciels ouverts, permettant de faire du : Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions 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 (BioPython, MMTK, ...), ... O. Wilk: Python pour le calcul scientifique Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Des bases Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam 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, ... 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 Savoir chercher de l’information : La visualisation Matplotlib sous le prompt python : help(), help(nom d’une variable, d’une fonction ou d’un package), ... Bibliographie sous le prompt ipython : help() ou nom-package.nom-fonction?, sur la toile : docs.python.org, www.python.org/, pypi.python.org/, ... O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Pour commencer (2/2) O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Respecter quelques règles : Entrée - Sortie Fonctions 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. Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Commentaires, chaines de caractères (1/2) Python pour le calcul scientifique CodePython.py O. Wilk # −∗− coding : 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 . " Introduction Des bases # ====================================================== # Une chaine de c a r a c t è r e s avec passage à l a l i g n e Commentaires, chaines Listes Types et opérations chaine = " " " 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 " " " Entrée - Sortie Fonctions Le calcul scientifique p r i n t chaine p r i n t " pour t r a i t e r une chaine de c a r a c t è r e s : " print " chaine . f i n d , chaine . r e p l a c e , chaine . s p l i t , Numpy Scipy ... " scipy.sparse La visualisation # ====================================================== # Transformer un nombre en chaine de c a r a c t è r e s Matplotlib Bibliographie a = 12345 ; b = −6789.01 ; c = 1.01 e−6 p r i n t " " " A j o u t e r une chaine 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 print print print print " " " " format format format format s f d e : %s %s %s : %12.2 f %12.2 f %12.2 f : %6d %6d %6d : %12.3e %12.3e %12.3e O. Wilk: Python pour le calcul scientifique " " " " % % % % ( str (a ) , str (b ) , str ( c ) ) (a, b, c) (a, b, c) (a, b, c) Calcul scientifique/Math/Cnam Commentaires, chaines de caractères (2/2) Python pour le calcul scientifique O. Wilk Introduction [wilk @localhost ∼]$ python CodePython.py Des bases A f f i c h e r un commentaire d u r a n t l ’ exé c u t i o n . Commentaires, chaines Listes Le f e u n ’ a p l u s de fumée quand i l e s t devenu flamme . Types et opérations Entrée - Sortie Fonctions D j a l a l a l−Din Rumi Le calcul scientifique pour t r a i t e r une chaine de c a r a c t è r e s : chaine . f i n d , chaine . r e p l a c e , chaine . s p l i t , Numpy ... A j o u t e r une chaine 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. format s : 12345 −6789.01 1.01 e−06 format f : format d : format e : 12345.00 12345 1.234 e+04 −6789.01 −6789 −6.789e+03 0.00 0 1.010e−06 O. Wilk: Python pour le calcul scientifique Scipy scipy.sparse La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique Listes (... dico.) CodePython.py O. Wilk # −∗− coding : 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.14 , " t o t o " ] print 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 print " l e d e r n i e r " , a[ −1] print " l e s compo . 0 e t 1 " , a [ 0 : 2 ] Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie p r i n t " On e n l ève l a composante 1 " a[1:2] = [] print a Fonctions Le calcul scientifique Numpy p r i n t " l o n g e u r de l a l i s t e " , len ( a ) Scipy scipy.sparse p r i n t " On a j o u t e un é l ément " a . append ( " t i t i " ) print a La visualisation Matplotlib Bibliographie [wilk @localhost ∼]$ python CodePython.py Cré a t i o n d ’ une ’ L i s t e ’ [1 , 3.14 , ’ toto ’ ] On r écupè r e l e second 3.14 le dernier toto l e s compo . 0 e t 1 On e n l ève l a composante 1 [1 , ’ toto ’ ] 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 , ’ toto ’ , ’ t i t i ’ ] O. Wilk: Python pour le calcul scientifique [1 , 3.14] Calcul scientifique/Math/Cnam Python pour le calcul scientifique Types et opérations O. Wilk CodePython.py # −∗− coding : U t f 8 −∗− Introduction 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 " print 1 . + 2 . , 1. −2. , 1 . ∗ 2 . , 1 . / 2 . , 2∗∗2 Des bases Commentaires, chaines Listes Types et opérations 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 ) Entrée - Sortie Fonctions Le calcul scientifique b = int (a. real ) ; c = float (b) # Conversion . . . Numpy Scipy import math scipy.sparse p r i n t " P i = %.60 f " % ( math . p i ) La visualisation [wilk @localhost ∼]$ python CodePython.py Bibliographie Matplotlib 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 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Tests et boucles (1/3) Python pour le calcul scientifique CodePython.py O. Wilk # −∗− coding : u t f 8 −∗− # if 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 print " " p r i n t " On s o r t ! " quit () else : print " " p r i n t " t e s t s : ( 0 < 1 ) " , ( 0 < 1 ) , type ( ( 0 == 1 ) ) print " 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 Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib p r i n t " On c o n t i n u e hors du t e s t " Bibliographie [wilk @localhost ∼]$ python CodePython.py tests tests tests tests : : : : (0 (0 (0 (0 < 1) True < t y p e ’ bool ’ > > 1) False == 1 ) and ( 1 == 1 ) False == 1 ) o r ( 1 == 1 ) True On c o n t i n u e hors du t e s t O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Tests et boucles (2/3) CodePython.py O. Wilk # −∗− coding : u t f 8 −∗− # while Introduction i = 0 while i < 3 : print i i = i + 1 Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie while 1 : try : Fonctions Le calcul scientifique 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 . . . " Numpy Scipy scipy.sparse La visualisation Matplotlib print x Bibliographie [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 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Tests et boucles (3/3) Python pour le calcul scientifique O. Wilk Introduction CodePython.py # −∗− coding : u t f 8 −∗− # for Des bases Commentaires, chaines Listes Types et opérations f o r i i n xrange ( 0 , 5 ) : print i p r i n t [ j f o r j i n xrange ( 0 , 5 ) ] Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy [wilk @localhost ∼]$ python CodePython.py 0 1 2 3 4 scipy.sparse La visualisation Matplotlib Bibliographie [0 , 1 , 2 , 3 , 4] O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique Entrée - Sortie (1/5) O. Wilk Introduction CodePython.py (PAS SOUS JUPYTER) Des bases # −∗− coding : u t f 8 −∗− # Récupé r e r des arguments en e n t r ée Commentaires, chaines Listes Types et opérations 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 . . . Entrée - Sortie Fonctions Le calcul scientifique 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 . . . Numpy Scipy scipy.sparse print " " 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 ) La visualisation Matplotlib Bibliographie [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 ’ ] < t y p e ’ l i s t ’ > O. Wilk: Python pour le calcul scientifique [ ’ 2 a_In_Out . py ’ , ’1 ’ , ’2 ’] Calcul scientifique/Math/Cnam Python pour le calcul scientifique Entrée - Sortie (2/5) O. Wilk CodePython.py (PAS SOUS JUPYTER) Introduction # −∗− coding : u t f 8 −∗− # Récupé r e r des arguments en cours d ’ exé c u t i o n Des bases Commentaires, chaines Listes # Un nombre Types et opérations Entrée - Sortie x = input ( " Frapper au c l a v i e r un nombre : " ) Fonctions p r i n t x , type ( x ) Le calcul scientifique Numpy # Une chaine de c a r a c t è r e s Scipy print " " a = raw_input ( " Frapper au c l a v i e r une chaine 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 ) scipy.sparse La visualisation Matplotlib Bibliographie [wilk @localhost ∼]$ python CodePython.py 1 2 Frapper au c l a v i e r un nombre : 123.1 < t y p e ’ f l o a t ’ > 123.1 Frapper au c l a v i e r une chaine 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 ’ > O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Entrée - Sortie (3/5) Python pour le calcul scientifique O. Wilk CodePython.py # −∗− coding : u t f 8 −∗− p r i n t " Ouverture 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 ) " Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions x = 1.1 ; y = 2 Le calcul scientifique f f f f f f . . . . . . write ( write ( write ( write ( write ( write ( str ( x ) str ( y ) ’ \n ’ ) str ( x ) ’ \n ’ ) str ( y ) ) ) Numpy Scipy scipy.sparse ) ) p r i n t " On ferme l e f i c h i e r " La visualisation Matplotlib Bibliographie f . close ( ) [wilk @localhost ∼]$ cat tmp.dat 1.12 1.1 2 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Entrée - Sortie (4/5) Python pour le calcul scientifique CodePython.py O. Wilk # −∗− coding : 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 ’ ) print " " , f . read ( ) f . close ( ) p r i n t " V2 " f = open ( ’ tmp . d a t ’ , ’ r ’ ) print "1 " , f . readline () print "2 " , f . readline () f . close ( ) p r i n t " V3 " f = open ( ’ tmp . d a t ’ , ’ r ’ ) print " " , f . readlines ( ) f . close ( ) Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib [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 Bibliographie 1.1 V3 [ ’1.12\n’ , ’1.1\n’ , ’2 ’] O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Entrée - Sortie (5/5) Python pour le calcul scientifique O. Wilk CodePython.py # −∗− coding : u t f 8 −∗− # L i r e un f i c h i e r a s c i i c o nt e n a nt une m a t r i c e : # v o i r a u s s i ’ l o a d ’ , ’ save ’ , ’ savez ’ Introduction Des bases import numpy as np Commentaires, chaines a = np . l o a d t x t ( ’ mat . d a t ’ , d e l i m i t e r = ’ , ’ ) Types et opérations p r i n t type ( a ) , a . shape , a . s i z e print " " print a Listes Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy [wilk @localhost ∼]$ cat mat.dat # commentaire 1, 2, 3 4, 5, 6 7, 8, 9 scipy.sparse La visualisation Matplotlib Bibliographie [wilk @localhost ∼]$ python CodePython.py < t y p e ’ numpy . ndarray ’ > ( 3 , 3 ) 9 [ [ 1. [ 4. [ 7. 2. 5. 8. 3.] 6.] 9.]] O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Fonctions (1/2) Python pour le calcul scientifique CodePython.py # −∗− coding : U t f 8 −∗− # Une f o n c t i o n dans l e corps 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 return b O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations 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 ) ] Entrée - Sortie Fonctions # 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 Le calcul scientifique Numpy Scipy scipy.sparse p r i n t [ F i b o n a c c i _2 . F ib o n ac c i _2 ( i ) f o r i i n xrange ( 1 , 1 0 ) ] La visualisation Matplotlib [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 etc . . . " " " a, b = 0, 1 f o r i i n xrange ( n ) : a , b = b , a+b return b Bibliographie [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 ] O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Fonctions (2/2) Python pour le calcul scientifique O. Wilk CodePython.py # −∗− coding : U t f 8 −∗− # Fonction à p l u s i e u r s v a r i a b l e s i n et out def t e s t ( a , b ) : c = a + b d = a ∗ b Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie return c , d a = 2.0 ; b = 1.0 Fonctions Le calcul scientifique Numpy [ c , d] = test (a, b) Scipy scipy.sparse print 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 La visualisation Matplotlib Bibliographie os . system ( ’ pwd ’ ) [wilk @localhost ∼]$ python CodePython.py 2.0 1.0 3.0 2.0 / home / w i l k / Bureau / Python / Expose_LaTeX / Python_tests_LaTeX O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Pour le Calcul Scientifique Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy, Scipy et Matplotlib Python pour le calcul scientifique O. Wilk Introduction Des bases 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 performances. O. Wilk: Python pour le calcul scientifique Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Numpy Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (1/6) : des tableaux. Python pour le calcul scientifique O. Wilk [wilk @localhost ∼]$ python Introduction >>> import numpy as np Des bases >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dtype = ’ f l o a t 3 2 ’ ) >>> p r i n t a [ [ 1.5 2. 3. ] [ 4. 5. 6. ] ] Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions >>> a . shape , a . s i z e , a . dtype ( 2 , 3 ) , 6 , dtype ( ’ f l o a t 3 2 ’ ) >>> type ( a ) <type ’ numpy . n d a r r a y ’ > Le calcul scientifique Numpy Scipy scipy.sparse >>> b = np . zeros ( ( 2 , 3 ) , dtype = ’ f l o a t 3 2 ’ ) >>> b array ( [ [ 0. , 0. , 0.] , [ 0. , 0. , 0 . ] ] , dtype= f l o a t 3 2 ) La visualisation Matplotlib Bibliographie >>> b = np . ones ( ( 2 , 3 ) , dtype = ’ f l o a t 3 2 ’ ) >>> b array ( [ [ 1. , 1. , 1.] , [ 1. , 1. , 1 . ] ] , dtype= f l o a t 3 2 ) Qq. types : int32, int64, float32, float64, complex64, complex128, ... O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (1/6) : Python pour le calcul scientifique des tableaux (suite). O. Wilk Introduction [wilk @localhost ∼]$ python Des bases >>> import numpy as np Commentaires, chaines Listes >>> np . arange ( 0 , 1 , 0 . 1 ) array ( [ 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 ) array ( [ 0. , 0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.8 , 0.9 , Types et opérations Entrée - Sortie Fonctions 1. ] ) Le calcul scientifique Numpy Scipy >>> x = np . l i n s p a c e ( 0 , np . p i , 10 ) scipy.sparse >>> np . s i n ( x ) a r r a y ( [ 0.00000000e+00 , 8.66025404e−01, 8.66025404e−01, 1.22464680e−16]) 3.42020143e−01, 9.84807753e−01, 6.42787610e−01, La visualisation 6.42787610e−01, 9.84807753e−01, 3.42020143e−01, Matplotlib Bibliographie >>> 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 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (2/6) : Python pour le calcul scientifique des opérations. O. Wilk [wilk @localhost ∼]$ python >>> import numpy as np Introduction >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dtype = ’ f l o a t 3 2 ’ ) Des bases Commentaires, chaines >>> c = np . ones ( ( 2 , 3 ) , dtype = ’ f l o a t 3 2 ’ ) >>> c array ( [ [ 1. , 1. , 1.] , [ 1. , 1. , 1 . ] ] , dtype= f l o a t 3 2 ) >>> a∗c array ( [ [ 1.5 , [ 4. , Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique # p r o d u i t terme à terme 2. , 5. , Numpy 3. ] , 6 . ] ] , dtype= f l o a t 3 2 ) >>> np . d o t ( a , c ) 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 ) array ( [ [ 1.5 , 4. ] , [ 2. , 5. ] , [ 3 . , 6 . ] ] , dtype= f l o a t 3 2 ) >>> np . d o t ( np . t r a n s p o s e ( a ) , c ) array ( [ [ 5.5 , 5.5 , 5.5] , [ 7. , 7. , 7. ] , [ 9 . , 9 . , 9 . ] ] , dtype= f l o a t 3 2 ) O. Wilk: Python pour le calcul scientifique Scipy scipy.sparse # produit entre 2 matrices La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Numpy (3/6) : des fonctions. Python pour le calcul scientifique O. Wilk [wilk @localhost ∼]$ python Introduction >>> import numpy as np Des bases >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] , dtype = ’ f l o a t 3 2 ’ ) Commentaires, chaines Listes >>> np .sum( a ) 21.5 >>> np . min ( a ) 1.5 >>> np . max( a ) 6.0 >>> np . mean ( a ) 3.5833333333333335 >>> np . median ( a ) 3.5 # ou a . sum ( ) Types et opérations Entrée - Sortie # ou a . min ( ) # ou a . max ( ) Fonctions Le calcul scientifique Numpy # ou a . mean ( ) # ou a . median ( ) >>> 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 ] ) Scipy scipy.sparse La visualisation Matplotlib Bibliographie et aussi : np.exp, np.log, np.log10, np.sqrt, np.sin, np.cos, np.tan, np.arcsin, np.arccos, np.arctan, np.sign ... O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (3/6) : Python pour le calcul scientifique des fonctions (suite). [wilk @localhost ∼]$ python O. Wilk Array Creation 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 , zeros_like Introduction Des bases Commentaires, chaines 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 Listes Types et opérations Entrée - Sortie Manipulations a r r a y s p l i t , column stack , 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 , repeat , reshape , r e s i z e , squeeze , swapaxes , take , transpose , v s p l i t , v s t a c k Fonctions Le calcul scientifique Numpy Scipy scipy.sparse Questions a l l , any , nonzero , where La visualisation Matplotlib Ordering 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 Operations choose , compress , cumprod , cumsum , i n n e r , r e a l , sum Bibliographie f i l l , imag , prod , put , putmask , Basic S t a t i s t i c s cov , mean , std , v a r Basic L i n e a r Algebra cross , dot , o u t e r , svd , v d o t O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (4/6) : des affectations (attention). Python pour le calcul scientifique O. Wilk [wilk @localhost ∼]$ python Introduction >>> import numpy as np Des bases Commentaires, chaines >>> x = np . a r r a y ( [ 3 0 , 2 0 , 1 0 ] ) Listes Types et opérations >>> 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 False >>> z [ 1 ] = 20 Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie >>> y a r r a y ( [ 1 0 , 22 , 3 0 ] ) >>> z a r r a y ( [ 1 0 , 20 , 3 0 ] ) O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (5/6) : des extractions. Python pour le calcul scientifique O. Wilk [wilk @localhost ∼]$ python >>> import numpy as np Introduction >>> a = np . a r r a y ( [ ( 1 . 5 , 2 , 3 , 4 ) , ( 4 . 5 , 5 , 6 , 7 ) ] ) >>> a array ( [ [ 1.5 , 2. , 3. , 4. ] , [ 4.5 , 5. , 6. , 7. ] ] ) Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie >>> a [ : , 1 ] array ( [ 2. , 5.]) >>> a [ 1 , : ] array ( [ 4.5 , 5. , Fonctions Le calcul scientifique 6. , 7.]) Numpy Scipy >>> a [ 1 , 0 : 3 ] array ( [ 4.5 , 5. , 6.]) >>> a [ 1 , 1 : 3 ] array ( [ 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 scipy.sparse La visualisation Matplotlib Bibliographie >>> 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 array ( [ [ 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) O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Numpy (6/6) : Intégrer du code C ou fortran Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions C’est possible (f2py, swig, ...) Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Scipy Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Scipy (1/3) : prolonge Numpy. Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines [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 ) Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse >>> np . l o g ( −1.) , sp . l o g ( −1.) ( nan , 3.1415926535897931 j ) La visualisation ... Bibliographie Matplotlib O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Scipy (2/3) : des modules spécialisés. Python pour le calcul scientifique O. Wilk Sous module cluster constants fftpack integrate interpolate io linalg maxentropy ndimage odr optimize signal sparse spatial special stats weave Description Clustering algorithms Physical and mathematical constants Fast Fourier Transform routines Integration and ordinary differential equation solvers Interpolation and smoothing splines Input and Output Linear algebra Maximum entropy methods N-dimensional image processing Orthogonal distance regression Optimization and root-finding routines Signal processing Sparse matrices and associated routines Spatial data structures and algorithms Special functions Statistical distributions and functions CC++ integration O. Wilk: Python pour le calcul scientifique 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 Calcul scientifique/Math/Cnam Scipy (3/3) : Stockage morse Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Importer le module "scipy.sparse" : Listes Types et opérations Entrée - Sortie >>> import scipy.sparse as sp_sp Fonctions Le calcul scientifique Numpy Scipy scipy.sparse Les différentes classes, modules et fonctions, un petit exemple. O. Wilk: Python pour le calcul scientifique La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Les différentes classes Python pour le calcul scientifique O. Wilk Différents stockages morses : csc_matrix: Compressed Sparse Column format, Introduction Des bases Commentaires, chaines csr_matrix: Compressed Sparse Row format, Listes bsr_matrix: Block Sparse Row format, Entrée - Sortie Types et opérations Fonctions lil_matrix: List of Lists format, dok_matrix: Dictionary of Keys format, coo_matrix: COOrdinate format, dia_matrix: DIAgonal format. Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib 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. O. Wilk: Python pour le calcul scientifique Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique Quelques fonctions O. Wilk Introduction Des bases Commentaires, chaines et aussi pour construire une matrice morse : Listes Types et opérations Entrée - Sortie eye : 1 sur la k ième diagonale, sinon 0, Fonctions identity : matrice identité stockée morse, Le calcul scientifique 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. O. Wilk: Python pour le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique Exemples O. Wilk import numpy as np import s c i p y . sparse as sp_sp row = print # [[4 # [0 # [0 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 ] ) sp_sp . c o o _ m a t r i x ( ( data , ( row , c o l ) ) , shape = ( 3 , 3 ) ) . todense ( ) 0 0] 7 0] 0 5]] Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie p r i n t sp_sp . eye ( 3 , 3 , k = 1 ) . todense ( ) # [ [ 0. 1. 0 . ] # [ 0. 0. 1 . ] # [ 0. 0. 0.]] Fonctions Le calcul scientifique Numpy Scipy 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 . spdiags ( data , diags , 3 , 3 ) . todense ( ) # [ [ 1 8 0] # [4 2 9] # [0 5 3 ] ] scipy.sparse La visualisation Matplotlib Bibliographie 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 ] ] # [ [ 1 2 5] # [3 4 6] # [0 0 7 ] ] O. Wilk: Python pour le calcul scientifique ) . todense ( ) Calcul scientifique/Math/Cnam Des modules Python pour le calcul scientifique O. Wilk extract : pour extraire différentes parties d’une matrice morse, Introduction sparsetools : différentes routines pour manipuler les matrices ... produits, Des bases ... Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions en relation avec (A une matrice stockée morse, x un vecteur) : Le calcul scientifique Numpy Scipy LaDiagonale = A.diagonal(), UnProduitMatriceVecteur = A.dot(x) = A._mul_vector(x), ... scipy.sparse La visualisation Matplotlib Bibliographie linalg : algèbre linéaire pour des matrices morses (sous-modules : eigen (valeurs propres), isolve (méthodes itératives), dsolve (méthodes directes). O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Python pour le calcul scientifique scipy.sparse.linalg O. Wilk matmat, matvec, Introduction cg : gradient conjugué, Des bases Commentaires, chaines bicg : gradient bi-conjugué, Listes Types et opérations bicgstab : gradient biconjugué stabilisé, Entrée - Sortie Fonctions 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), Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie lsqr : Large sparse QR, minres : (experimental) qmr : quasi-minimisation du résidu, splu, spilu : LU et LU incomplet (SuperLU). O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Qq exemples Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Pour A une matrice stockée morse (stockage au format CSC ou CSR pour Umfpack) : Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique >>> [Val, Vect] = sp_sp.linalg.eigs(A, k = 10) # les 10 premières v.p., Numpy Scipy scipy.sparse >>> 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. O. Wilk: Python pour le calcul scientifique La visualisation Matplotlib Bibliographie Calcul scientifique/Math/Cnam Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Visualiser sous Python Types et opérations Entrée - Sortie Fonctions Matplotlib (courbes, images ... haute qualité, 2D), Le calcul scientifique Numpy Scipy autres (plutôt orienté 3D, vtk) : Mayavi, Paraview, ... scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (1/4) : un point de vue. Python pour le calcul scientifique O. Wilk Introduction Visualisation 2D de qualité (png, pdf, eps, ...), Des bases Commentaires, chaines Listes Matlab-like (pylab : pyplot + numpy), intégration de commandes LaTeX, doc. bien faite (http://matplotlib.sourceforge.net/). Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (2/4) : visu. intéractive. Python pour le calcul scientifique O. Wilk [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 Introduction Des bases Commentaires, chaines Listes Types et opérations plt . figure () Entrée - Sortie Fonctions p l t . subplot (211) Le calcul scientifique p l t . plot (x , y , color= ’ r ’ ) p l t . t i t l e ( ’ Titre 1 ’ , color= ’g ’ ) Numpy Scipy scipy.sparse p l t . subplot (212) p l t . p l o t ( x , y , marker= ’ o ’ ) p l t . t i t l e ( ’ Titre 2 ’ , color= ’b ’ ) La visualisation Matplotlib Bibliographie p l t . show ( ) O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (3/4) : sauvegarde auto. png, ... Python pour le calcul scientifique O. Wilk [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 ’ , usetex =True ) x = np . a r r a y ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) y = x∗x Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique p l t . ion ( ) # mode dynamique ( non s t a n d a r t ) Numpy Scipy plt . figure () scipy.sparse p l t . p l o t ( x , y , marker= ’ o ’ ) p l t . xlim (2 , 8) La visualisation p l t . t i t l e ( r ’ $ \ v a r p h i $ en \ LaTeX ’ ) Bibliographie Matplotlib p l t . draw ( ) p l t . s a v e f i g ( ’ image . png ’ ) plt . ioff () # pour r e v e n i r en mode s t a n d a r t et aussi : plt.imshow, plt.colorbar, plt.legend, ... O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (4/6) : nos exemples 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 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (4/6) : Python pour le calcul scientifique nos exemples (1/9) O. Wilk [wilk @localhost ∼]$ python Introduction # −∗− coding : 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 Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie n _ l i g , n _ c o l = 3 , 4 ; num = 1 Fonctions p l t . ion ( ) # 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 =(16 , 8 ) ) # c r é a t i o n d ’ une f i g u r e plt . clf () # On n e t t o i e l a f i g u r e Le calcul scientifique Numpy Scipy scipy.sparse # −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 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 ) ) La visualisation Matplotlib Bibliographie 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.00.0 O. Wilk: Python pour le calcul scientifique 0.5 1.0 1.5 2.0 2.5 3.0 3.5 Calcul scientifique/Math/Cnam Matplotlib (4/6) : Python pour le calcul scientifique nos exemples (2-3/9) O. Wilk [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 . polar ( theta , r ) Introduction Des bases 90° 135° Commentaires, chaines 45° Listes Types et opérations 2.53.0 1.52.0 0.51.0 0° 180° Entrée - Sortie Fonctions Le calcul scientifique 225° Numpy 315° Scipy 270° scipy.sparse [wilk @localhost ∼]$ python La visualisation 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 ) Bibliographie Matplotlib 20 15 10 5 00 O. Wilk: Python pour le calcul scientifique 5 10 15 20 Calcul scientifique/Math/Cnam Matplotlib (4/6) : Python pour le calcul scientifique nos exemples (4-5/9) [wilk @localhost ∼]$ python O. Wilk 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 . semilogy ( x , np . exp ( x ) ) plt . grid () # v o i r a u s s i : p l t . semilogx 109 108 107 106 105 104 103 102 101 100 0 Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie 5 15 10 20 Fonctions Le calcul scientifique [wilk @localhost ∼]$ python Numpy ; num += 1 Scipy scipy.sparse La visualisation Matplotlib Bibliographie + y ∗∗ 3 ) ∗ np . exp(−x ∗∗ 2 −y ∗∗ 2 ) cmap= " h o t " ) 8 , colors= ’ black ’ , l i n e w i d t h =.5) 3 0 0.00 00 50 0.7 50 -0.2 -0.5 0.250 00 1 00 1 0 0.5 2 0.5 p l t . s u b p l o t ( n _ l i g , n_col , num) 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 p l t . contourf (X, Y, f (X, Y) , 8 , C = p l t . contour (X, Y, f (X, Y) , p l t . c l a b e l (C, f o n t s i z e =10) # v o i r aussi : p l t . contour 2 33 O. Wilk: Python pour le calcul scientifique 2 1 0 1 2 3 Calcul scientifique/Math/Cnam Matplotlib (4/6) : Python pour le calcul scientifique nos exemples (6-7/9) [wilk @localhost ∼]$ python O. Wilk ax1 = p l t . s u b p l o t 2 g r i d ( ( 3 , 4 ) , ( 1 , 1 ) , colspan =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 =False ) 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 ( ’ equal ’ ) # même dimension en x qu ’ en y ( un c e r c l e sera rond ) p l t . axis ( " o f f " ) p l t . t r i c o n t o u r f ( triang ,Z,20) Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib [wilk @localhost ∼]$ python Bibliographie 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 , bins , patches = p l t . h i s t ( phi , 20 , c o l o r = " green " ) # v o i r aussi : p l t . hist2d 140 120 100 80 60 40 20 060 O. Wilk: Python pour le calcul scientifique 70 80 90 100 110 120 130 Calcul scientifique/Math/Cnam Matplotlib (4/6) : Python pour le calcul scientifique nos exemples (8-9/9) [wilk @localhost ∼]$ python O. Wilk ax2 = p l t . s u b p l o t 2 g r i d ( ( 3 , 4 ) , ( 2 , 0 ) , colspan =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 " ) 800 p l t . xlim (2 , 8) x 700 x2 p l t . x l a b e l ( " abscisse " ) 600 Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie x3 500 400 300 200 100 02 Fonctions Le calcul scientifique Numpy Scipy scipy.sparse 4 3 5 abscisse [wilk @localhost ∼]$ python 7 6 8 La visualisation 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 . axis ( " 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 " , . . . 0 # v o i r a u s s i : p l t . imread , p l t . imsave 0.8 100 200 300 400 500 0 O. Wilk: Python pour le calcul scientifique 100 200 300 400 500 Matplotlib Bibliographie 0.7 0.6 0.5 0.4 0.3 0.2 0.1 Calcul scientifique/Math/Cnam Matplotlib (5/6) : Python pour le calcul scientifique le 3D, un exemple O. Wilk [wilk @localhost ∼]$ python Introduction 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 Des bases Commentaires, chaines Listes def l o r e n z ( x , y , z , s =10 , r =28 , b =2.667) : " " " 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 ) Types et opérations Attracteur de Lorenz Entrée - Sortie 50 T = 1 0 . ; d t = 0.01 ; npas = i n t ( T / d t ) 30 20 10 x , y , z = np . zeros ( [ 3 , npas ] ) # Conditions i n i t i a l e s x [ 0 ] , y [ 0 ] , z [ 0 ] = ( 0 . , 1. , 1.05) Fonctions 40 Le calcul scientifique Numpy Scipy 10 5 0 5 10 15 20 10 0 10 20 scipy.sparse La visualisation Matplotlib 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 Bibliographie fig = plt . figure () 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 O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Matplotlib (6/6) : Bcp d’autres exemples. 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 Site web : http://matplotlib.sourceforge.net/gallery.html O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam Bibliographie Python pour le calcul scientifique O. Wilk Introduction Des bases Commentaires, chaines Listes Types et opérations Entrée - Sortie 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 Fonctions Le calcul scientifique Numpy Scipy scipy.sparse La visualisation Matplotlib Bibliographie O. Wilk: Python pour le calcul scientifique Calcul scientifique/Math/Cnam