Python, une introduction. O. Wilk. Introduction. Des bases Commentaires, chaines. Types et opérations Entrée - Sortie Fonctions

Documents pareils
Introduction à MATLAB R

Calcul Scientifique avec 3

Cours 7 : Utilisation de modules sous python

Python - introduction à la programmation et calcul scientifique

Les bases du langage Python

Initiation à la programmation en Python

Pourquoi R devient incontournable en recherche, enseignement et développement

WDpStats Procédure d installation

Calcul Formel et Numérique, Partie I

Optimisation de logiciels de modélisation sur centre de calcul

Programmation en Java IUT GEII (MC-II1) 1

SHERLOCK 7. Version du 01/09/09 JAVASCRIPT 1.5

Gnuplot. Chapitre Lancer Gnuplot. 3.2 Options des graphes

Bases de SQL. Hacks 1-6 CHAPITRE UN

Découverte de Python

Projet de programmation (IK3) : TP n 1 Correction

Exercices types Algorithmique et simulation numérique Oral Mathématiques et algorithmique Banque PT

Plan du cours. Historique du langage Nouveautés de Java 7

L informatique en BCPST

INTRODUCTION A JAVA. Fichier en langage machine Exécutable

M2-Images. Rendu Temps Réel - OpenGL 4 et compute shaders. J.C. Iehl. December 18, 2013

TP 1. Prise en main du langage Python

Présentation du langage et premières fonctions

Table des matières. 1. Installation de VMware ESXI Pré-requis Installation... 3

Cours d Algorithmique et de Langage C v 3.0

Programmation Web. Madalina Croitoru IUT Montpellier

MATLAB : COMMANDES DE BASE. Note : lorsqu applicable, l équivalent en langage C est indiqué entre les délimiteurs /* */.

Aide - mémoire gnuplot 4.0

calls.paris-neuroscience.fr Tutoriel pour Candidatures en ligne *** Online Applications Tutorial

Séance 0 : Linux + Octave : le compromis idéal

Cours intensif Java. 1er cours: de C à Java. Enrica DUCHI LIAFA, Paris 7. Septembre Enrica.Duchi@liafa.jussieu.fr

Installation et Mise en œuvre de MySQL

3.2. Matlab/Simulink Généralités

Comment reproduire les résultats de l article : POP-Java : Parallélisme et distribution orienté objet

Cours Informatique Master STEP

Introduction à l informatique en BCPST

I. Programmation I. 1 Ecrire un programme en Scilab traduisant l organigramme montré ci-après (on pourra utiliser les annexes):

Cours d algorithmique pour la classe de 2nde

Telecharger gratuitement convertisseur de fichier word en pdf

Génération de code binaire pour application multimedia : une approche au vol

INFO-F-404 : Techniques avancées de systèmes d exploitation

Comment Accéder à des Bases de Données MySQL avec Windows lorqu'elles sont sur un Serveur Linux

Évaluation et implémentation des langages

How to Login to Career Page

SaltStack pour la gestion de parc

Cours de Programmation 2

Calcul Formel et Numérique, Partie I

MapReduce. Malo Jaffré, Pablo Rauzy. 16 avril 2010 ENS. Malo Jaffré, Pablo Rauzy (ENS) MapReduce 16 avril / 15

Info0101 Intro. à l'algorithmique et à la programmation. Cours 3. Le langage Java

La programmation orientée objet et le langage C++

Cours 1 : Introduction. Langages objets. but du module. contrôle des connaissances. Pourquoi Java? présentation du module. Présentation de Java

La place de SAS dans l'informatique décisionnelle

Introduction au langage C

Guide Installation Serveur Extensive Testing

Once the installation is complete, you can delete the temporary Zip files..

ÉdIteur officiel et fournisseur de ServIceS professionnels du LogIcIeL open Source ScILab

1 Installer des packages

Jérôme FESSY. IUT de Paris 5. Base de Données. Cours Introductif. Base de Données

pyensae StockPrices September 1, Manipulation de séries financières avec la classe StockPrices

SysFera. Benjamin Depardon

Algorithmique et Programmation, IMA

lundi 3 août 2009 Choose your language What is Document Connection for Mac? Communautés Numériques L informatique à la portée du Grand Public

Mysql avec EasyPhp. 1 er mars 2006

Notre Catalogue des Formations IT / 2015

as Architecture des Systèmes d Information

SUGARCRM Sugar Open Source Guide d Installation de French SugarCRM Open Source Version 4.2

L envoi d un formulaire par courriel. Configuration requise Mail Texte Mail HTML Check-list

Contrôle Non Destructif : Implantation d'algorithmes sur GPU et multi-coeurs. Gilles Rougeron CEA/LIST Département Imagerie Simulation et Contrôle

Efficient Object Versioning for Object- Oriented Languages From Model to Language Integration

Configurer la supervision pour une base MS SQL Server Viadéis Services

Environnement Informatique EMSE Linux

pythonocc: une plateforme de développement agile d applications CAO.

Pratique et administration des systèmes

Get Instant Access to ebook Cest Maintenant PDF at Our Huge Library CEST MAINTENANT PDF. ==> Download: CEST MAINTENANT PDF

Comment Créer une Base de Données Ab Initio

Exercices sur SQL server 2000

Guide Installation Serveur Extensive Testing

Programming Server-Side Web Applications with Object-Oriented PHP NC Group Syllabus. Duration: 75 hours 1-2-2

«Clustering» et «Load balancing» avec Zope et ZEO

Déploiement OOo en environnement Windows Terminal Server

THE FRENCH EXPERIENCE 1

1. Structure d'un programme FORTRAN 95

Mendeley, pour gérer sa bibliographie et la partager. Patricia Volland-Nail

Stéphane DERACO, DSI CNRS l Argos Devops : de l hyperviseur aux conteneurs l 11/12/2014 DOCKER

AUVRAY Clément (168187) HOMBERGER Alexandre (186897) GLADE. Langages, outils et méthodes pour la programmation avancée Page 1 sur 12

Lesson Plan Physical Descriptions. belle vieille grande petite grosse laide mignonne jolie. beau vieux grand petit gros laid mignon

td3a correction session7az

Exemple PLS avec SAS

Lamia Oukid, Ounas Asfari, Fadila Bentayeb, Nadjia Benblidia, Omar Boussaid. 14 Juin 2013

Logitech Tablet Keyboard for Windows 8, Windows RT and Android 3.0+ Setup Guide Guide d installation

Anticiper et prédire les sinistres avec une approche Big Data

Bacula. It comes by night and sucks the vital essence from your computers. Guillaume Arcas

Quick Start Guide This guide is intended to get you started with Rational ClearCase or Rational ClearCase MultiSite.

CATALOGUE DES FORMATIONS LANGUES

Oracle Database SQL Developer Guide D'Installation Release 4.0 E

L installation a quelque peu changée depuis les derniers tutos, voici une actualisation.

Rapport de Mini-Projet en ArcGIS Engine

Transcription:

Plusieurs parties : Calcul scientifique/math/cnam Février 2011 : des bases, du calcul scientifique (numpy, scipy,...), de la visualisation. Le module scipy plus en détails, couplage fortran-python (f2py) et le C... Des applications : avec GetFem,... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam Un langage libre et portable sur n importe quel système (Windows, Mac, Linux) permettant de relier des mondes et des langages différents : système réseau visualisation C Java VTK calcul Python Cuda, OpenCL Fortran GTK web entrée/sortie IHM : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

1989 : Guido van Rossum développe 1991 : Python est publié, 1994 : premières applications scientifiques. Aujourd hui : 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. (Seuls 3 langages progressent : 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. : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam Pourquoi Python s impose-t-il? 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... 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 (Bio MMTK,...),... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

Pour commencer (1/2) Choisir son interface, son environnement de programmation : python, ipython (complétion automatique,...), bpython, spyder, des environnement de dév. : idle, eclipse+pydev. Savoir chercher de l information : sous le prompt Python : help(), help(nom d une variable, d une fonction ou d un package), sur la toile : http ://docs.python.org, http ://www.python.org/doc/quickref.html. : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam Pour commencer (2/2) Respecter quelques règles : commencer en tout début de ligne (le décalage sert pour les test et les boucles), éviter les tabulations n etant pas constituées d espace d un caractère. de caractères (1/2) # Mettre un commentaire dans l e code python p r i n t " A f f i c h e r un commentaire durant l exé cution. " # ====================================================== # Une chaine de c aract ères avec passage à l a l i g n e chaine = " " " Le feu n a plus de fumée quand i l est devenu flamme. D j a l a l al Din Rumi " " " 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 ères : " p r i n t " chaine. f i n d, chaine. replace, chaine. s p l i t,... " # ====================================================== # Transformer un nombre en chaine de c a r a c t è res a = 12345 ; b = 6789.01 ; c = 1.01e 6 p r i n t " " " A j o u t e r une chaine de c a r a c t ères " " " + s t r ( a)+ " " " puis une autre " " " + s t r ( b)+ " " " et l a d e r n i è re " " " + s t r ( c )+ " " ". " " " # ====================================================== # Afficher en formatant, attention aux erreurs éventuelles p r i n t " format s : %s %s %s " % ( s t r ( a ), s t r ( b ), s t r ( c ) ) p r i n t " format f : %12.2 f %12.2 f %12.2 f " % ( a, b, c ) p r i n t " format d : %6d %6d %6d " % ( a, b, c ) p r i n t " format e : %12.3e %12.3e %12.3e " % ( a, b, c ) : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

de caractères (2/2) A f f i c h e r un commentaire durant l exé cution. Le feu n a plus de fumée quand i l est devenu flamme. D j a l a l al Din Rumi pour t r a i t e r une chaine de c a r a c t è res : chaine. f i n d, chaine. replace, chaine. s p l i t,... A j o u t e r une chaine de c aract è res 12345 puis une autre 6789.01 et l a d e r n i è re 1.01e 06. format s : 12345 6789.01 1.01e 06 # coding : Utf8 p r i n t " Quelques operations " 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. real, 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 " Pi = %.60 f " % ( math. p i ) format f : 12345.00 6789.01 0.00 format d : 12345 6789 0 format e : 1.234e+04 6.789e+03 1.010e 06 Quelques operations 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 <type complex > Pi = 3.141592653589793115997963468544185161590576171875000000000000 : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam Tests et boucles (1/3) # 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 est 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) or (1 == 1) ", (0 == 1) or (1 == 1) # la f i n de l indentation marque la f i n du test p r i n t " On continue hors du t e s t " Tests et boucles (2/3) # while 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 clavier un nombre... ( autre chose vous f e r a recommencer ) : " ) ) break except ValueError : p r i n t " Ce n est pas c o r r e c t! Recommencez... " p r i n t x t e s t s : (0 < 1) True <type bool > t e s t s : (0 > 1) False t e s t s : (0 == 1) and (1 == 1) False t e s t s : (0 == 1) or (1 == 1) True On continue hors du t e s t 0 1 2 Frapper au c l a v i e r un nombre ( autre chose vous f e r a recommencer ) : a z e r t y Ce n est pas c o r r e c t! Recommencez... Frapper au c l a v i e r un nombre ( autre chose vous f e r a recommencer ) : 123.1 123.1 : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

Tests et boucles (3/3) (1/5) # f o r for i in xrange ( 0, 5 ) : p r i n t i p r i n t [ j for j in xrange ( 0, 5 ) ] 0 1 2 3 4 # Récupé rer des arguments en entrée import sys # On importe 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è re l e s arguments d e n t r ée # python CeFichier. py arg1 arg2... p r i n t " " p r i n t " Les arguments sont dans la l i s t e suivante : ", Liste p r i n t [ L i s t e [ j ] for j in xrange ( 0, len ( L i s t e ) ) ], type ( L i s t e ) 1 2 [ 0, 1, 2, 3, 4] Les arguments sont dans l a l i s t e suivante : [ 2 a_in_out. py, 1, 2 ] [ 2 a_in_out. py, 1, 2 ] <type l i s t > : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (2/5) (3/5) # Récupé rer des arguments en cours d exécution # Un nombre x = i n p u t ( " Frapper au c l a v i e r un nombre : " ) p r i n t x, type ( x ) # Une chaine de c aract ères p r i n t " " a = raw_input ( " Frapper au c l a v i e r une chaine de c a r a c t è res ( sans ) : " ) p r i n t a, a [ 0 : 3 ], a [ 1 : 3 ], type ( a ) 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. dat, w ) p r i n t " On é c r i t dans l e f i c h i e r ( avec des sauts 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 ) ) 1 2 p r i n t " On ferme l e f i c h i e r " Frapper au c l a v i e r un nombre : 123.1 123.1 <type float > f. close ( ) Frapper au c l a v i e r une chaine de c a r a c t è res ( sans ) : a z e rty azerty aze ze <type str > [wilk@localhost ]$ cat tmp.dat 1.12 1.1 2 : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(4/5) p r i n t " D i f f é rentes manières de l i r e " p r i n t " V1" f = open ( tmp. dat, r ) p r i n t " ", f. read ( ) f. close ( ) p r i n t " V2" f = open ( tmp. dat, 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. close ( ) p r i n t " V3" f = open ( tmp. dat, r ) p r i n t " ", f. r e a d l i n e s ( ) f. close ( ) D i f f é rentes manières 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 ] (5/5) # L i r e un f i c h i e r a s c i i contenant une matrice : # v o i r aussi load, save, savez import numpy as np a = np. l o a d t x t ( mat. dat, d e l i m i t e r =, ) p r i n t type ( a ), a. shape, a. s ize 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 <type numpy. ndarray > ( 3, 3) 9 [ [ 1. 2. 3. ] [ 4. 5. 6. ] [ 7. 8. 9. ] ] : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (1/2) # coding : Utf8 # Une f o n c t i o n dans l e corps du s c r i p t... def Fibonacci ( n ) : a, b = 0, 1 for i in xrange ( n ) : a, b = b, a+b return b p r i n t [ Fibonacci ( i ) for i in xrange ( 1, 1 0 ) ] # importer une fonction qui n est pas dans le même ré pertoire import sys sys. path. append ( " Bib_Perso " ) # On ajoute le le chemin du ré pertoire import Fibonacci_2 p r i n t [ Fibonacci_2. Fibonacci_2 ( i ) for i in xrange ( 1, 10)] (2/2) # coding : Utf8 # 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 return 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 [wilk@localhost ]$ cat Bib_Perso/Fibonacci2.py def Fibonacci_2 ( n ) : " " " I l est p o s sible de documenter l e s f o n c t i o n s etc... " " " a, b = 0, 1 for i in xrange ( n ) : a, b = b, a+b return b # exé c u t e r des f o n c t i o n s d autres l o g i c i e l s # ( hors python, sous shell unix / linux par ex ) import os os. system ( pwd ) [ 1, 2, 3, 5, 8, 13, 21, 34, 55] [ 1, 2, 3, 5, 8, 13, 21, 34, 55] 2.0 1.0 3.0 2.0 / home / wilk / Bureau / Python / Expose_LaTeX / Python_tests_LaTeX : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

et Python et le Calcul Scientifique : "the fundamental package needed for scientific computing with Python", : "a collection of mathematical algorithms and convenience functions built on the extension for Python",... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (1/5) : des tableaux. >>> a = np. array ( [ ( 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. ] ] >>> a. ndim, a. shape, a. size, a. dtype ( 2, ( 2, 3 ), 6, dtype ( f l o a t 3 2 ) ) >>> type ( a ) <type numpy. ndarray > >>> 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 ) >>> 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,... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(2/5) : des opérations. (3/5) : des fonctions. >>> a = np. array ( [ ( 1. 5, 2, 3 ), ( 4, 5, 6 ) ], dtype = f l o a t 3 2 ) >>> 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 # p r o d u i t terme à terme array ( [ [ 1.5, 2., 3. ], [ 4., 5., 6. ] ], dtype= f l o a t 3 2 ) >>> np. dot ( a, c ) # p r o d u i t entre 2 matrices Traceback ( most recent c a l l l a s t ) : F i l e " < s t d i n > ", l i n e 1, in <module> ValueError : matrices are not aligned >>> np. transpose ( a ) array ( [ [ 1.5, 4. ], [ 2., 5. ], [ 3., 6. ] ], dtype= f l o a t 3 2 ) >>> np. dot ( np. transpose ( a ), c ) array ( [ [ 5.5, 5.5, 5. 5 ], [ 7., 7., 7. ], [ 9., 9., 9. ] ], dtype= f l o a t 3 2 ) >>> a = np. array ( [ ( 1. 5, 2, 3 ), ( 4, 5, 6 ) ], dtype= 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. array ( [ 3 0, 2 0, 1 0 ] ) >>> index = np. a r g s o r t ( x ) >>> x [ index ] array ( [ 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... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (4/5) : des affectations (attention). (5/5) : des extractions. >>> x = np. array ( [ 3 0, 2 0, 1 0 ] ) >>> y = x >>> y is x True >>> y [ 1 ] = 22 >>> x array ( [ 1 0, 22, 3 0 ] ) >>> z = y. copy ( ) >>> z is y False >>> z [ 1 ] = 20 >>> y array ( [ 1 0, 22, 3 0 ] ) >>> z array ( [ 1 0, 20, 3 0 ] ) >>> a = np. array ( [ ( 1. 5, 2, 3, 4 ), ( 4, 5, 6, 7 ) ], dtype= f l o a t 3 2 ) >>> a array ( [ [ 1.5, 2., 3., 4. ], [ 4., 5., 6., 7. ] ], dtype= f l o a t 3 2 ) >>> a [ :, 1 ] array ( [ 2., 5. ], dtype= f l o a t 3 2 ) >>> a [ 1, : ] array ( [ 4., 5., 6., 7. ], dtype= f l o a t 3 2 ) >>> a [ 1, 0 : 3 ] array ( [ 4., 5., 6. ], dtype= f l o a t 3 2 ) >>> a [ 1, 1 : 3 ] array ( [ 5., 6. ], dtype= f l o a t 3 2 ) et aussi : np.diag, np.linalg.det, np.linalg.eig,... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(1/5) : prolonge. >>> import scipy as sp >>> np. s q r t ( 1.), sp. s q r t ( 1.) ( nan, 1 j ) >>> np. log ( 1.), sp. log ( 1.) ( nan, 3.1415926535897931 j )... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (2/5) : des modules spécialisés. (3/5) : ne pas oublier la doc. 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 >>> import scipy as sp >>> help ( ) Welcome to Python 2. 7! This is the o n l i n e help u t i l i t y. I f t h i s is your f i r s t time using Python, you should d e f i n i t e l y check out the t u t o r i a l on the I n t e r n e t at h t t p : / / docs. python. org / t u t o r i a l /. Enter the name of any module, keyword, or t o p i c to get help on w r i t i n g Python programs and using Python modules. To q u i t t h i s help u t i l i t y and return to the interpreter, j u s t type " quit ". To get a l i s t of available modules, keywords, or topics, type " modules ", " keywords ", or " topics ". Each module also comes with a one l i n e summary of what i t does ; to l i s t the modules whose summaries contain a given word such as "spam", type " modules spam". help > scipy. l i n a l g... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(4/5) : ne pas oublier la doc. (5/5) : un exemple.... Help on package scipy. l i n a l g i n scipy : NAME scipy. l i n a l g FILE / usr / l i b 6 4 / python2. 7 / s i t e packages / scipy / l i n a l g / i n i t. py DESCRIPTION Linear algebra r o u t i n e s ======================= : Linear Algebra Basics : : i n v Find the inverse of a square m a t r i x solve Solve a l i n e a r system of equations solve_banded Solve a l i n e a r system of equations with a banded matrix solveh_banded Solve a l i n e a r system of equations with a Hermitian or symmetric banded matrix, returning the Cholesky decomposition as w e l l det Find the determinant of a square m a t r i x norm matrix and v e c t o r norm lstsq Solve l i n e a r least squares problem pinv Pseudo inverse ( Moore Penrose ) using lstsq >>> import scipy. sparse as sp_sp >>> row = np. array ( [ 0, 2, 1, 0, 1, 2 ] ) >>> c o l = np. array ( [ 0, 0, 1, 2, 2, 2 ] ) >>> data = np. array ( [ 1, 2, 3, 4, 5, 6 ] ) >>> sp_sp. csc_matrix ( ( data, ( row, c o l ) ), shape =(3,3) ). todense ( ) matrix ( [ [ 1, 0, 4 ], [ 0, 3, 5 ], [ 2, 0, 6 ] ] ) >>> p t r _ i n d = np. array ( [ 0, 2, 3, 6 ] ) >>> i n d _ l i g = np. array ( [ 0, 2, 1, 0, 1, 2 ] ) >>> data = np. array ( [ 1, 2, 3, 4, 5, 6 ] ) >>> sp_sp. csc_matrix ( ( data, i n d _ l i g, p t r _ i n d ), shape =(3,3) ). todense ( ) matrix ( [ [ 1, 0, 4 ], [ 0, 3, 5 ], [ 2, 0, 6 ] ] )... Il faut une séance complète pour présenter! : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (1/4) : un point de vue. Visualiser sous Python Visualisation 2D de qualité (png, pdf,...), Matlab-like (pylab : pyplot + numpy), (courbes, images... 2D), (données sur maillages... 3D (2D)),... intégration de commandes LaTeX, doc. bien faite (http ://matplotlib.sourceforge.net/). : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(2/4) : visu. intéractive. (3/4) : sauvegarde auto. png,... import numpy as np import matplotlib. pyplot as p l t x = np. array ( [ 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. subplot (211) 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. subplot (212) 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 ( ) import numpy as np import matplotlib. pyplot 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. rc ( t e x t, usetex=true ) x = np. array ( [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] ) y = x x # t u r n i n t e r a c t i v e mode o f f f o r dynamic updates. p l t. i o f f ( ) p l t. f i g u r e ( ) p l t. p l o t ( x, y, marker= o ) p l t. xlim ( 2, 8) p l t. t i t l e ( r $ \ varphi$ en \ LaTeX ) p l t. draw ( ) p l t. s a v e f i g ( image. png ) p l t. ion ( ) et aussi : plt.imshow, plt.colorbar, plt.legend,... : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam (4/4) : qq. exemples. (1/3) : un point de vue. 2 : Visualisation 3D (2D) (png, pdf,...), IHM ou non, "Help user guide" du logiciel "mayavi2". Site web : http ://matplotlib.sourceforge.net/gallery.html : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam

(2/3) : IHM (intéractivité). (3/3) : une animation ds le corps du script. import enthought. mayavi. mlab as mlab x, y, z, t r i, u =... # Coordonnées des sommets, connectivit é # des é l éments triangles et champ associé. T i t r e =... # Chaines de c a r a c t è res donnant un t i t r e # à l a f i g u r e mlab. figure ( size =(600,200)) ml = mlab. triangular_mesh ( x, y, z, t r i, scalars = u, colormap = Blues ) mlab. view ( 0., 0., 1., [ x0, y0, z0 ] ) # Controle des paramê tres "caméra " mlab. c o l o r b a r ( t i t l e = u, o r i e n t a t i o n = v e r t i c a l ) mlab. t i t l e ( T i t r e, size = 0.1, h e i g h t = 0.9 ) mlab. s a v e f i g ( image_0. png ) mlab_ tri = ml. mlab_source for i in xrange ( 0, 100): # Calcul de u u =... # On change u dans l a v i s u a l i s a t i o n et on sauve une image m l a b _ t r i. set ( s c a l a r s = u ) ; mlab. s a v e f i g ( image_ + s t r ( i ). png ) : Calcul scientifique/math/cnam : Calcul scientifique/math/cnam 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 : Calcul scientifique/math/cnam