Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Python Scientific lecture notes potx
PREMIUM
Số trang
139
Kích thước
4.8 MB
Định dạng
PDF
Lượt xem
1504

Tài liệu Python Scientific lecture notes potx

Nội dung xem thử

Mô tả chi tiết

Python Scientific lecture notes

Release 2010

EuroScipy tutorial team

Editors: Emmanuelle Gouillart, Gaël Varoquaux

July 09, 2010

Contents

1 Scientific computing: why Python? 1

1.1 The scientist’s needs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.3 Existing solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Building blocks of scientific computing with Python 3

3 A (very short) introduction to Python 6

3.1 First steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3.2 Basic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.3 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.4 Defining functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.5 Reusing code: scripts and modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

3.6 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

3.7 Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3.8 Exceptions handling in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

3.9 Object-oriented programming (OOP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

4 NumPy: creating and manipulating numerical data 41

4.1 Creating NumPy data arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

4.2 Graphical data representation : matplotlib and Mayavi . . . . . . . . . . . . . . . . . . . . . . . . . 42

4.3 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

4.4 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

4.5 Manipulating the shape of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

4.6 Exercises : some simple array creations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

4.7 Real data: read/write arrays from/to files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

4.8 Simple mathematical and statistical operations on arrays . . . . . . . . . . . . . . . . . . . . . . . . 53

4.9 Fancy indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

4.10 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.11 Synthesis exercises: framing Lena . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

5 Getting help and finding documentation 63

6 Matplotlib 67

i

6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6.2 IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6.3 pylab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6.4 Simple Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

6.5 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

6.6 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

6.7 Ticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

6.8 Figures, Subplots, and Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

6.9 Other Types of Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

6.10 The Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

7 Scipy : high-level scientific computing 87

7.1 Scipy builds upon Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.2 File input/output: scipy.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.3 Signal processing: scipy.signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

7.4 Special functions: scipy.special . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

7.5 Statistics and random numbers: scipy.stats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

7.6 Linear algebra operations: scipy.linalg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.7 Numerical integration: scipy.integrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7.8 Fast Fourier transforms: scipy.fftpack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

7.9 Interpolation: scipy.interpolate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

7.10 Optimization and fit: scipy.optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

7.11 Image processing: scipy.ndimage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

7.12 Summary exercices on scientific computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

8 Sympy : Symbolic Mathematics in Python 119

8.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

8.2 What is SymPy? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

8.3 First Steps with SymPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

8.4 Algebraic manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

8.5 Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

8.6 Equation solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

8.7 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

9 3D plotting with Mayavi 126

9.1 A simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

9.2 3D plotting functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

9.3 Figures and decorations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

9.4 Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Bibliography 135

ii

CHAPTER 1

Scientific computing: why Python?

authors Fernando Perez, Emmanuelle Gouillart

1.1 The scientist’s needs

• Get data (simulation, experiment control)

• Manipulate and process data.

• Visualize results... to understand what we are doing!

• Communicate on results: produce figures for reports or publications, write presentations.

1.2 Specifications

• Rich collection of already existing bricks corresponding to classical numerical methods or basic actions: we

don’t want to re-program the plotting of a curve, a Fourier transform or a fitting algorithm. Don’t reinvent the

wheel!

• Easy to learn: computer science neither is our job nor our education. We want to be able to draw a curve, smooth

a signal, do a Fourier transform in a few minutes.

• Easy communication with collaborators, students, customers, to make the code live within a labo or a company:

the code should be as readable as a book. Thus, the language should contain as few syntax symbols or unneeded

routines that would divert the reader from the mathematical or scientific understanding of the code.

• Efficient code that executes quickly... But needless to say that a very fast code becomes useless if we spend too

much time writing it. So, we need both a quick development time and a quick execution time.

• A single environment/language for everything, if possible, to avoid learning a new software for each new prob￾lem.

1.3 Existing solutions

Which solutions do the scientists use to work?

1

Python Scientific lecture notes, Release 2010

Compiled languages: C, C++, Fortran, etc.

• Advantages:

– Very fast. Very optimized compilers. For heavy computations, it’s difficult to outperform these languages.

– Some very optimized scientific libraries have been written for these languages. Ex: blas (vector/matrix

operations)

• Drawbacks:

– Painful usage: no interactivity during development, mandatory compilation steps, verbose syntax (&, ::,

}}, ; etc.), manual memory management (tricky in C). These are difficult languages for non computer

scientists.

Scripting languages: Matlab

• Advantages:

– Very rich collection of libraries with numerous algorithms, for many different domains. Fast execution

because these libraries are often written in a compiled language.

– Pleasant development environment: comprehensive and well organized help, integrated editor, etc.

– Commercial support is available.

• Drawbacks:

– Base language is quite poor and can become restrictive for advanced users.

– Not free.

Other script languages: Scilab, Octave, Igor, R, IDL, etc.

• Advantages:

– Open-source, free, or at least cheaper than Matlab.

– Some features can be very advanced (statistics in R, figures in Igor, etc.)

• Drawbacks:

– fewer available algorithms than in Matlab, and the language is not more advanced.

– Some softwares are dedicated to one domain. Ex: Gnuplot or xmgrace to draw curves. These programs

are very powerful, but they are restricted to a single type of usage, such as plotting.

What about Python?

• Advantages:

– Very rich scientific computing libraries (a bit less than Matlab, though)

– Well-thought language, allowing to write very readable and well structured code: we “code what we think”.

– Many libraries for other tasks than scientific computing (web server management, serial port access, etc.)

– Free and open-source software, widely spread, with a vibrant community.

• Drawbacks:

– less pleasant development environment than, for example, Matlab. (More geek-oriented).

– Not all the algorithms that can be found in more specialized softwares or toolboxes.

2 Chapter 1. Scientific computing: why Python?

CHAPTER 2

Building blocks of scientific computing

with Python

author Emmanuelle Gouillart

• Python, a generic and modern computing language

– Python language: data types (string, int), flow control, data collections (lists, dictionaries), patterns,

etc.

– Modules of the standard library.

– A large number of specialized modules or applications written in Python: web protocols, web framework,

etc. ... and scientific computing.

– Development tools (automatic tests, documentation generation)

• IPython, an advanced Python shell

http://ipython.scipy.org/moin/

3

Python Scientific lecture notes, Release 2010

• Numpy : provides powerful numerical arrays objects, and routines to manipulate them.

>>> import numpy as np

>>> t = np.arange(10)

>>> t

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> print t

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

>>> signal = np.sin(t)

http://www.scipy.org/

• Scipy : high-level data processing routines. Optimization, regression, interpolation, etc:

>>> import numpy as np

>>> import scipy

>>> t = np.arange(10)

>>> t

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> signal = t**2 + 2*t + 2+ 1.e-2*np.random.random(10)

>>> scipy.polyfit(t, signal, 2)

array([ 1.00001151, 1.99920674, 2.00902748])

http://www.scipy.org/

• Matplotlib : 2-D visualization, “publication-ready” plots

http://matplotlib.sourceforge.net/

4 Chapter 2. Building blocks of scientific computing with Python

Python Scientific lecture notes, Release 2010

• Mayavi : 3-D visualization

http://code.enthought.com/projects/mayavi/

• and many others.

5

CHAPTER 3

A (very short) introduction to Python

authors Chris Burns, Christophe Combelles, Emmanuelle Gouillart, Gaël Varoquaux

Python for scientific computing

We introduce here the Python language. Only the bare minimum necessary for getting started with Numpy

and Scipy is addressed here. To learn more about the language, consider going through the excellent tutorial

http://docs.python.org/tutorial. Dedicated books are also available, such as http://diveintopython.org/.

3.1 First steps

Python is a programming language, as are C, Fortran, BASIC, PHP, etc. Some specific features of Python are as

follows:

• an interpreted (as opposed to compiled) language. Contrary to e.g. C or Fortran, one does not compile Python

code before executing it. In addition, Python can be used interactively: many Python interpreters are available,

from which commands and scripts can be executed.

• a free software released under an open-source license: Python can be used and distributed free of charge, even

for building commercial software.

• multi-platform: Python is available for all major operating systems, Windows, Linux/Unix, MacOS X, most

likely your mobile phone OS, etc.

• a very readable language with clear non-verbose syntax

• a language for which a large variety of high-quality packages are available for various applications, from web

frameworks to scientific computing.

• a language very easy to interface with other languages, in particular C and C++.

6

Python Scientific lecture notes, Release 2010

• Some other features of the language are illustrated just below. For example, Python is an object-oriented lan￾guage, with dynamic typing (an object’s type can change during the course of a program).

See http://www.python.org/about/ for more information about distinguishing features of Python.

Start the Ipython shell (an enhanced interactive Python shell):

• by typing “Ipython” from a Linux/Mac terminal, or from the Windows cmd shell,

• or by starting the program from a menu, e.g. in the Python(x,y) or EPD menu if you have installed one these

scientific-Python suites.

If you don’t have Ipython installed on your computer, other Python shells are available, such as the plain Python shell

started by typing “python” in a terminal, or the Idle interpreter. However, we advise to use the Ipython shell because

of its enhanced features, especially for interactive scientific computing.

Once you have started the interpreter, type

>>> print "Hello, world!"

Hello, world!

The message “Hello, world!” is then displayed. You just executed your first Python instruction, congratulations!

To get yourself started, type the following stack of instructions

>>> a = 3

>>> b = 2*a

>>> type(b)

<type ’int’>

>>> print b

6

>>> a*b

18

>>> b = ’hello’

>>> type(b)

<type ’str’>

>>> b + b

’hellohello’

>>> 2*b

’hellohello’

Two objects a and b have been defined above. Note that one does not declare the type of an object before assigning

its value. In C, conversely, one should write:

int a;

a = 3;

In addition, the type of an object may change. b was first an integer, but it became a string when it was assigned

the value hello. Operations on integers (b=2*a) are coded natively in the Python standard library, and so are some

operations on strings such as additions and multiplications, which amount respectively to concatenation and repetition.

3.1. First steps 7

Python Scientific lecture notes, Release 2010

A bag of Ipython tricks

• Several Linux shell commands work in Ipython, such as ls, pwd, cd, etc.

• To get help about objects, functions, etc., type help object. Just type help() to get started.

• Use tab-completion as much as possible: while typing the beginning of an object’s name (variable, func￾tion, module), press the Tab key and Ipython will complete the expression to match available names. If

many names are possible, a list of names is displayed.

• History: press the up (resp. down) arrow to go through all previous (resp. next) instructions starting with

the expression on the left of the cursor (put the cursor at the beginning of the line to go through all previous

commands)

• You may log your session by using the Ipython “magic command” %logstart. Your instructions will be

saved in a file, that you can execute as a script in a different session.

In [1]: %logstart commandes.log

Activating auto-logging. Current session state plus future input saved.

Filename : commandes.log

Mode : backup

Output logging : False

Raw input log : False

Timestamping : False

State : active

3.2 Basic types

3.2.1 Numerical types

Integer variables:

>>> 1 + 1

2

>>> a = 4

floats

>>> c = 2.1

complex (a native type in Python!)

>>> a=1.5+0.5j

>>> a.real

1.5

>>> a.imag

0.5

and booleans:

>>> 3 > 4

False

>>> test = (3 > 4)

>>> test

False

>>> type(test)

<type ’bool’>

8 Chapter 3. A (very short) introduction to Python

Python Scientific lecture notes, Release 2010

A Python shell can therefore replace your pocket calculator, with the basic arithmetic operations +, -, \*, /, %

(modulo) natively implemented:

>>> 7 * 3.

21.0

>>> 2**10

1024

>>> 8%3

2

Warning: Integer division

>>> 3/2

1

Trick: use floats:

>>> 3/2.

1.5

>>> a = 3

>>> b = 2

>>> a/b

1

>>> a/float(b)

1.5

• Scalar types: int, float, complex, bool:

>>> type(1)

<type ’int’>

>>> type(1.)

<type ’float’>

>>> type(1. + 0j )

<type ’complex’>

>>> a = 3

>>> type(a)

<type ’int’>

• Type conversion:

>>> float(1)

1.0

3.2.2 Containers

Python provides many efficient types of containers, in which collections of objects can be stored.

Lists

A list is an ordered collection of objects, that may have different types. For example

3.2. Basic types 9

Tải ngay đi em, còn do dự, trời tối mất!