• No results found

Computational Mathematics with Python

N/A
N/A
Protected

Academic year: 2021

Share "Computational Mathematics with Python"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Boolean Arrays Classes

Computational Mathematics with Python

Basics

Olivier Verdier and Claus Führer

(2)

Introduction and Motivation Concepts Practical Information

1 Introduction and Motivation Python vs Other Languages Examples and Demo

2 Concepts Basic Types Variables Lists For Loop 3 Practical Information

(3)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages Examples and Demo

1 Introduction and Motivation Python vs Other Languages Examples and Demo

2 Concepts Basic Types Variables Lists For Loop 3 Practical Information

(4)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages

Examples and Demo

Why Python?

Python is. . .

Free and open source

It is a scripting language, meaning that it is interpreted

It is modern: object oriented, exception handling, dynamic typing etc. Plenty of libraries, in particular scientific ones: linear algebra;

visualisation tools: plotting, image analysis; differential equations solving; symbolic computations; statistics ; etc.

Many possible usages: Scientific computing (of course :-)), scripting, web sites, text parsing, etc.

(5)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages

Examples and Demo

Python vs language XX

Java, C++ Object oriented compiled languages. Very limited and extremely verbose. Low level compared to python. Few scientific libraries.

C, FORTRAN Very low level compiled language. Useful in some CPU critical situations.

php, ruby Other interpreted languages. PHP is web oriented. Ruby is as flexible as python but has no scientific library.

MATLAB Tool for matrix computation that evolved for scientific computing. The scientific library is huge but it is not a programming language. Extremely expensive.

(6)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages

Examples and Demo

Examples

Python may be used in interactivemode:

>>>x = 3 >>>y = 5 >>>p r i n t x + y 8 Here we solve " 1 2 3 4 # · x = " 2 1 # >>>M = a r r a y( [ [1 . , 2 .], [3 . , 4 .] ] ) >>>V = a r r a y( [2 . , 1 .] )

(7)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages

Examples and Demo

More examples

Computing e and 2100:

>>>p r i n t exp(1j*pi) # s h o u l d r e t u r n - 1 : -) ( -1+1 . 2 2 4 6 4 6 7 9 9 1 5 e-16j) >>>p r i n t 2* *100 1 2 6 7 6 5 0 6 0 0 2 2 8 2 2 9 4 0 1 4 9 6 7 0 3 2 0 5 3 7 6 L Computing ζ(x) =P k=1 1

kx. For x = 2 we know that ζ(2) = π

2 6 : # for x = 2 : >>>p r i n t s c i p y . s p e c i a l . z e t a(2 . , 1) 1 . 6 4 4 9 3 4 0 6 6 8 5 >>>p r i n t pi* *2/6 1 . 6 4 4 9 3 4 0 6 6 8 4 8 2 2 6 4

(8)

Introduction and Motivation Concepts Practical Information

Python vs Other Languages

Examples and Demo

Demo

(9)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

1 Introduction and Motivation Python vs Other Languages Examples and Demo

2 Concepts Basic Types Variables Lists For Loop 3 Practical Information

(10)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Numbers

A number may be an integer, a real numberor acomplex number. The usual operations are

+and-addition and substraction

*and/multiplication and division

** power 2* * (2+2) # 16

(11)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Strings

Strings are “lists” of characters, enclosed by simple or double quotes:

’ v a l i d s t r i n g ’

" s t r i n g w i t h d o u b l e q u o t e s "

You may also use triple quotesfor strings including multiple lines:

""" T h i s is a long ,

(12)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Strings

Strings are “lists” of characters, enclosed by simple or double quotes:

’ v a l i d s t r i n g ’

" s t r i n g w i t h d o u b l e q u o t e s "

You may also use triple quotesfor strings including multiple lines:

""" T h i s is a long ,

(13)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Concept: Variable

Variables

A variable is a reference to an object. An object may have several references. One uses theassignment operator =to assign a value to a variable.

Example

x = [3 , 4] # a l i s t o b j e c t is c r e a t e d

y = x # t h i s o b j e c t now has two l a b e l s : x and y

del x # we d e l e t e one of the l a b e l s

(14)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Concept: Lists

Lists

A python list is an orderedlist of objects, enclosed in square brackets. One accesses elements of a list using zero-basedindices inside square brackets.

(15)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

List Examples

Example L1 = [1 , 2] L1[0] # 1 L1[1] # 2 L1[2] # r a i s e s I n d e x E r r o r L2 = [’ a ’, 1 , [3 , 4] ] L2[0] # ’ a ’ L2[2] [0] # 3 L2[ -1] # l a s t e l e m e n t : [ 3 , 4 ] L2[ -2] # s e c o n d to l a s t : 1

(16)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

List Utilities

range(n)creates a list with n elements, starting with zero: p r i n t r a n g e(5)

[0 , 1 , 2 , 3 , 4]

len(L) gives thelength of a list:

len( [’ a ’, 1 , 2 , 34] ) # r e t u r n s 4

Useappend to append an element to a list: L = [’ a ’, ’ b ’, ’ c ’]

L[ -1] # ’ c ’

L .a p p e n d(’ d ’)

L # L is now [ ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’]

(17)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

List Utilities

range(n)creates a list with n elements, starting with zero: p r i n t r a n g e(5)

[0 , 1 , 2 , 3 , 4]

len(L) gives thelength of a list:

len( [’ a ’, 1 , 2 , 34] ) # r e t u r n s 4

Useappend to append an element to a list: L = [’ a ’, ’ b ’, ’ c ’]

L[ -1] # ’ c ’

L .a p p e n d(’ d ’)

L # L is now [ ’ a ’ , ’ b ’ , ’ c ’ , ’ d ’]

(18)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

List Utilities

range(n)creates a list with n elements, starting with zero: p r i n t r a n g e(5)

[0 , 1 , 2 , 3 , 4]

len(L) gives thelength of a list:

len( [’ a ’, 1 , 2 , 34] ) # r e t u r n s 4

Useappend to append an element to a list: L = [’ a ’, ’ b ’, ’ c ’]

L[ -1] # ’ c ’

(19)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Comprehensive lists

A convenient way to build up lists is to use the comprehensive lists construct, possibly with a conditional inside.

Definition

The syntax of a comprehensive list is [ <e x p r> for <x> in <l i s t> ]

Example

L = [2 , 3 , 10 , 1 , 5]

L2 = [x*2 for x in L] # [ 4 , 6 , 20 , 2 , 10 ]

(20)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Comprehensive Lists in Maths

Mathematical Notation

This is very close to the mathematical notation for sets. Compare: S2 = {2x; x ∈ S }

and

L2 = [2*x for x in L]

(21)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Operations on Lists

Adding two lists concatenates(sammanfoga) them: L1 = [1 , 2]

L2 = [3 , 4]

L = L1 + L2 # [ 1 , 2 , 3 , 4 ]

Logically, multiplying a list with an integer concatenates the list with itself several times: n*L is equivalent to L + L + · · · + L

| {z }

n times

. L = [1 , 2]

(22)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Operations on Lists

Adding two lists concatenates(sammanfoga) them: L1 = [1 , 2]

L2 = [3 , 4]

L = L1 + L2 # [ 1 , 2 , 3 , 4 ]

Logically, multiplying a list with an integer concatenates the list with itself several times: n*L is equivalent to L + L + · · · + L

| {z }

n times

. L = [1 , 2]

(23)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Concept: for loop

for loop

A for loopallows to loop through a list using an index variable. This variable is successively equal to all the elements in the list.

Example

L = [1 , 2 , 10] for s in L:

p r i n t s * 2 ,

(24)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Concept: for loop

for loop

A for loopallows to loop through a list using an index variable. This variable is successively equal to all the elements in the list.

Example

L = [1 , 2 , 10] for s in L:

p r i n t s * 2 ,

(25)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Indentation

The part to be repeated in the for loop has to be properly indented: for elt in m y _ l i s t:

d o _ s o m e t h i n g( ) s o m e t h i n g _ e l s e( ) etc

(26)

Introduction and Motivation Concepts Practical Information Basic Types Variables Lists For Loop

Repeating a Task

One typical useof the forloop is to repeat a certain task a fixed number of time:

n = 30

for i in r a n g e(n) :

(27)

Introduction and Motivation Concepts Practical Information

1 Introduction and Motivation Python vs Other Languages Examples and Demo

2 Concepts Basic Types Variables Lists For Loop 3 Practical Information

(28)

Introduction and Motivation Concepts Practical Information

Python Shell

Start a python session by typingscipythonin a unix shell Check that it is working with: plot(rand(4));show()

A window should appear with a graph; you should be able to type other commands without having to close the graph window when you want to quit, writeexit()

When you want to run python at home please follow the installation instruction

(29)

Introduction and Motivation Concepts Practical Information

Executing Scripts

You often want to execute the contents of a file.

We recommand to useKate on the Linux machines (but any other good editor will do)

Save your files in (for example) in $HOME/course/

Type (once) in ipython: cd course

To execute the contents of a file namedfile.py just write execfile(’file.py’) in ipython.

(30)

Introduction and Motivation Concepts Practical Information

Getting Help

Some tips on how to use ipython:

To gethelp on an object just type ? after it and then return Use the arrow keys to reuse the last executed commands

We will see later that you may use the tabulation key forcompletion in general

References

Related documents

I, Garrett Langford, Manager of Planning and Zoning for the City of Mesquite, Texas, hereby certify that the attached Agenda for the Planning and Zoning

In our contribution to the Code Saturne project, we enhanced capability of mesh multiplication package to be able to create full hybrid meshes up to hundreds of billion cells

Within 45 days following the full implementation of the Target Pharmacy Computer System, which shall occur by March 31, 2010, when filling a prescription for an LEP Customer,

However, the m axim um increase o f 2.42 tim es was recorded with the adoption o f balanced nutrition along with im proved cultivar, thus confirm ing the role

on the relationship between attitudes, effective beliefs, and consumption of fast food among students of Hamedan University of Medical Sciences, Hamedan, Iran, it

During the year ended  March , CLC acquired an interest in real estate properties from Government of Canada departments and a Crown corporation for an aggregate

Chest HRCT showed signi fi cant improvement at one year in mean modi fi ed Brody scores for bronchiectasis, mucous plugging, airway wall thickness, and total Brody scores..

This paper aims to apply Sperber &amp; Wilson’s Relevance Theory (1986; 1995; 1987) and the two stage incongruity-resolution theory of humour (Attardo 1994) to explain how humorous