Scientific Programming in Python



Similar documents
Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

CUDAMat: a CUDA-based matrix class for Python

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

(!' ) "' # "*# "!(!' +,

CIS 192: Lecture 13 Scientific Computing and Unit Testing

2+2 Just type and press enter and the answer comes up ans = 4

CRASH COURSE PYTHON. Het begint met een idee

Modeling with Python

ANSA and μeta as a CAE Software Development Platform

Programming Languages & Tools

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Vectors VECTOR PRODUCT. Graham S McDonald. A Tutorial Module for learning about the vector product of two vectors. Table of contents Begin Tutorial

Postprocessing with Python

Computational Mathematics with Python

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Row Echelon Form and Reduced Row Echelon Form

Part VI. Scientific Computing in Python

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

Python for Scientific Computing.

Python. Python. 1 Python. M.Ulvrova, L.Pouilloux (ENS LYON) Informatique L3 Automne / 25

Computational Mathematics with Python

Introduction to MATLAB (Basics) Reference from: Azernikov Sergei

Python for Chemistry in 21 days

Computational Mathematics with Python

Introduction to Matlab

SCIENTIFIC COMPUTING AND PROGRAMMING IN THE CLOUD USING OPEN SOURCE PLATFORMS: AN ILLUSTRATION USING WEIGHTED VOTING SYSTEMS

Bachelor Degree in Informatics Engineering Master courses

Recall that two vectors in are perpendicular or orthogonal provided that their dot

Chemical and Biological Engineering Calculations using Python 3. Jeffrey J. Heys

Advanced Topics: Biopython

An Introduction to APGL

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Introduction to Python

Computational Physics

Tutorial Program. 1. Basics

Session 15 OF, Unpacking the Actuary's Technical Toolkit. Moderator: Albert Jeffrey Moore, ASA, MAAA

Bachelor of Games and Virtual Worlds (Programming) Subject and Course Summaries

18.06 Problem Set 4 Solution Due Wednesday, 11 March 2009 at 4 pm in Total: 175 points.

Today's Topics. COMP 388/441: Human-Computer Interaction. simple 2D plotting. 1D techniques. Ancient plotting techniques. Data Visualization:

K-Means Clustering Tutorial

Programming Exercise 3: Multi-class Classification and Neural Networks

:Introducing Star-P. The Open Platform for Parallel Application Development. Yoel Jacobsen E&M Computing LTD

Linear Algebra Review. Vectors

Solution of Linear Systems

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

MATLAB Programming Tricks

Python Programming: An Introduction to Computer Science

From mathematics to a nice figure in a LaTeX document

Availability of the Program A free version is available of each (see individual programs for links).

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

Basic Concepts in Matlab

How To Develop Software

Robot Task-Level Programming Language and Simulation

VB.NET Programming Fundamentals

KNIME TUTORIAL. Anna Monreale KDD-Lab, University of Pisa

MATHEMATICS FOR ENGINEERS BASIC MATRIX THEORY TUTORIAL 2

Why (and Why Not) to Use Fortran

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VI

Some programming experience in a high-level structured programming language is recommended.

A few useful MATLAB functions

Fall 2012 Q530. Programming for Cognitive Science

Introduction Installation Comparison. Department of Computer Science, Yazd University. SageMath. A.Rahiminasab. October9, / 17

Beginner s Matlab Tutorial

Python Programming: An Introduction to Computer Science

Using MATLAB to Measure the Diameter of an Object within an Image

Chaco: A Plotting Package for Scientists and Engineers. David C. Morrill Enthought, Inc.

Data Mining: Algorithms and Applications Matrix Math Review

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

MEng, BSc Computer Science with Artificial Intelligence

Component Ordering in Independent Component Analysis Based on Data Power

Signal Processing First Lab 01: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

Tentative NumPy Tutorial

Chapter 12. Development Tools for Microcontroller Applications

Python programming guide for Earth Scientists. Maarten J. Waterloo and Vincent E.A. Post

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Sample Solutions for Assignment 2.

1 Topic. 2 Scilab. 2.1 What is Scilab?

Chapter 15 Functional Programming Languages

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

SAGE, the open source CAS to end all CASs?

Arithmetic and Algebra of Matrices

Adaptive Sampling and the Autonomous Ocean Sampling Network: Bringing Data Together With Skill

Linear Algebra Notes

Matrix Multiplication

Analysis Programs DPDAK and DAWN

Solving Systems of Linear Equations

Plug. & Play. Various ECUs tested by automated sequences. dspace Magazine 3/2009 dspace GmbH, Paderborn, Germany info@dspace.com

Introduction Our choice Example Problem Final slide :-) Python + FEM. Introduction to SFE. Robert Cimrman

Big Data Paradigms in Python

Transcription:

UCSD March 9, 2009

What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is:

What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is: cross platform

What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is: cross platform object oriented

What is Python? Python in a very high level (scripting) language which has gained widespread popularity in recent years. It is: cross platform object oriented open source

Why should I care?

Why should I care? You may need to use a computer to run simulations crunch data display data...

Why should I care? You may need to use a computer to run simulations crunch data display data... Python s 3 rd -party libraries can help you with these tasks.

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want:

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP)

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython machine learning functions PyML, mlpy, SHOGUN

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython machine learning functions PyML, mlpy, SHOGUN neural nets Fast Artificial Neural Network (FANN) Library

Python s Scientific Libraries Python is enhanced by a large set of scientific libraries that are being actively developed. Suppose you want: standard science and engineering functions or plotting (MATLAB) SciPy, Matplotlib a computer algebra system (Mathematica) SAGE data processing Modular toolkit for Data Processing (MDP) bioinformatics functions Biopython machine learning functions PyML, mlpy, SHOGUN neural nets Fast Artificial Neural Network (FANN) Library artificial intelligence or robotics routines Python Robotics (Pyro)

Is it hard to learn?

Is it hard to learn?

Python vs MATLAB Advantages of MATLAB:

Python vs MATLAB Advantages of MATLAB: already widely used

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box Advantages of Python:

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box Advantages of Python: open source means no limits on use

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box Advantages of Python: open source means no limits on use appears to approximately superset MATLAB s functionality

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box Advantages of Python: open source means no limits on use appears to approximately superset MATLAB s functionality modern language with support for object orientation

Python vs MATLAB Advantages of MATLAB: already widely used designed specifically for scientific computing easy to find documentation good IDE with debugging and profiling support out of the box Advantages of Python: open source means no limits on use appears to approximately superset MATLAB s functionality modern language with support for object orientation support for calling functions in other languages

Python Intro - Basics To get information on an object from the interpreter h e l p <o b j e c t > Commenting: Inline comments are preceded with # Block comments are surrounded with Code blocks are denoted with indentation: i f x == 2 : p r i n t x Python is dynamically typed: a = h e l l o # a i s a s t r i n g a = 4 # a i s now an i n t e g e r

Python Intro - Vectors and Matrices Many of these functions come from SciPy. from s c i p y import Vectors: N = 5 # a s c a l a r v = [ 1, 2, 3 ] # a l i s t v = a r r a y ( [ 1, 2, 3 ] ) # a column v e c t o r v = a r r a y ( [ [ 1 ], [ 2 ], [ 3 ] ] )# a column v e c t o r v = a r r a y ( [ [ 1, 2, 3 ] ] ) # a column v e c t o r v = t r a n s p o s e ( v ) # t r a n s p o s e a v e c t o r # ( row to column # or column to row ) v = arange ( 4,4) # a v e c t o r i n # a s p e c i f i e d range : v = p i arange ( 4,4)/4 v = arange ( 4,4,.5) # arange ( s t a r t, stop, s t e p ) v = [ ] # empty l i s t

Python Intro Matrices: m = z e r o s ( [ 2, 3 ] ) # a m a t r i x o f z e r o s v = ones ( [ 1, 3 ] ) # a m a t r i x o f ones v = rand ( 3, 1 ) # rand m a t r i x ( s e e a l s o randn ) m = eye ( 3 ) # i d e n t i t y m a t r i x (3 x3 )

Python Intro - Vectors and Matrices v = a r r a y ( [ 5, 6, 7 ] ) # a c c e s s a v e c t o r element v [ 2 ] # v e c t o r ( i n d e x ) a r r a y s a r e # zero i n d e x e d l e n ( v ) # number o f e l e m e n t s i n # a v e c t o r m = a r r a y ( [ [ 1, 2, 3 ], \ [ 4, 5, 6 ] ] ) # a 2 x3 m a t r i x m[ 1, 2 ] == m[ 1 ] [ 2 ] # a c c e s s a m a t r i x element # m a t r i x [ row, column ]

blah m[ 1, : ] # a c c e s s a m a t r i x row # ( bottom row ) m[ :, 0 ] # a c c e s s a m a t r i x column # ( l e f t column ) m. r e s h a p e ( [ 4, 1 ] ) # t u r n m a t r i x i n t o a column # v e c t o r ( c o n c a t e n a t e rows ) m. shape # s i z e o f a m a t r i x [ rows, c o l s ] m. shape [ 0 ] # number o f rows m. shape [ 1 ] # number o f columns z e r o s (m. shape ) # c r e a t e a new m a t r i x with # s i z e o f m m = a r r a y ( [ [ h e l l o, sum ], \ [ 1, 2 ] ] ) # put whatever you want # i n t o an a r r a y m[ 0, 1 ] (m[ 1 ] ) # c a l l sum on bottom row o f m

Python Intro - Vectors and Matrices Arithmetic operations performed on arrays are done element by element. a = a r r a y ( [ 1, 2, 3, 4 ] ) # v e c t o r 2 a # s c a l a r m u l t i p l i c a t i o n a / 4 # s c a l a r d i v i s i o n b = a r r a y ( [ 5, 6, 7, 8 ] ) # v e c t o r a + b # p o i n t w i s e v e c t o r a d d i t i o n a b # p o i n t w i s e v e c t o r s u b t r a c t i o n a 2 # p o i n t i s e v e c t o r s q u a r i n g a b # p o i n t w i s e v e c t o r m u l t i p l y a / b # p o i n t w i s e v e c t o r d i v i d e l o g ( a ) # p o i n t w i s e l o g a r i t h m around ( a r r a y ( [ [. 6 ], \ [. 5 ] ] ) ) # p o i n t w i s e r o u n d i n g # (. 5 rounds to 0)

Vector Operations a = a r r a y ( [ 1, 4, 6, 3 ] ) # v e c t o r sum ( a ) # sum o f v e c t o r e l e m e n t s mean ( a ) # mean o f v e c t o r e l e m e n t s v a r ( a ) # v a r i a n c e s t d ( a ) # s t a n d a r d d e v i a t i o n max( a ) # maximum a = a r r a y ( [ [ 1, 2, 3 ], \ [ 4, 5, 6 ] ] ) # m a t r i x mean ( a, 0 ) # mean o f each column amax ( a, 1 ) # max o f each row amax ( a ) # to o b t a i n max o f m a t r i x # note we use 2 # d i f f e r e n t max f u n c t i o n s

Matrix Operations dot ( t r a n s p o s e ( a r r a y ( [ 1, 2, 3 ] ) ), \ a r r a y ( [ 4, 5, 6 ] ) ) # row v e c t o r 1 x3 t i m e s column # v e c t o r 3 x1 r e s u l t s i n a # s i n g l e number, a l s o known # as dot / i n n e r product dot ( a r r a y ( [ [ 1 ], [ 2 ], [ 3 ] ] ), \ a r r a y ( [ [ 4, 5, 6 ] ] ) ) # column v e c t o r 3 x1 t i m e s row # v e c t o r 1 x3 r e s u l t s i n 3 x3 # matrix, a l s o known # as o u t e r p r oduct a = rand ( 3, 2 ) # 3 x2 m a t r i x b = rand ( 2, 4 ) # 2 x4 m a t r i x dot ( a, b ) # 3 x4 m a t r i x

Saving Your Work import c P i c k l e as p i c k l e # t h i s module l e t s you # s a v e and r e l o a d o b j e c t s f = open ( s a v e f i l e, w ) # open a r c h i v e f i l e p i c k l e. dump( obj, f ) # dump o b j e c t to a r c h i v e f. c l o s e ( ) # c l o s e a r c h i v e f i l e del o b j # c l e a r o b j e c t # from memory f = open ( s a v e f i l e, r ) # open a r c h i v e f i l e o b j = p i c k l e. l o a d ( f ) # read o b j e c t # from a r c h i v e f. c l o s e ( ) # c l o s e a r c h i v e f i l e

Relations and Control Example: given a list v, create a new list u with values equal to v if they are greater than 0, and equal to 0 if they less than or equal to 0. Using a for loop: v = [3,5, 2,5, 1,0] u = [ 0 ] l e n ( v ) # u i s a l l z e r o s f o r i i n range ( l e n ( v ) ) : i f v ( i ) > 0 : u ( i ) = v ( i ) Using list comprehension: v = [3,5, 2,5, 1,0] u = [ max( e, 0 ) f o r e i n v ]

Importing Functions Save the following code to mylib.py : def myfunc ( a, b ) : return a+b 2 Import and use myfunc. Note, we might need to configure PYTHONPATH. from mylib import myfunc myfunc ( 1, 2 ) myfunc ( b=2, a=1) # same a s above Python also supports class creation: c l a s s MyClass : def i n i t ( s e l f ) : p r i n t h e l l o! m y c l a s s = MyClass ( )

Plotting The library matplotlib / pylab is your friend: from p y l a b import xs = arange ( 2,2,.01) p l o t ( xs, s i n ( xs ) ) show ( )

Imaging We use the Python Imaging Library as well as matplotlib / pylab from p y l a b import import Image im = Image. open ( my image. j p g ) im. show ( ) # we can d i s p l a y the image ima = a r r a y ( im ) # t y p e c a s t i n g to a r r a y # e x t r a c t s p i x e l v a l u e s imr = Image. f r o m s t r i n g ( RGB,\ ( ima. shape [ 1 ], ima. shape [ 0 ] ), \ ima. t o s t r i n g ( ) ) # c o n v e r t a r r a y i n t o image img = ima. mean ( 2 ) # a v e r a g e c o l o r i n t e n s i t i e s # f o r each p i x e l imshow ( img ) autumn ( ) # s e t d e f a u l t colormap to autumn show ( )

How do I start? Many guides and tutorials are available online:

How do I start? Many guides and tutorials are available online: Dive Into Python python introduction for programmers

How do I start? Many guides and tutorials are available online: Dive Into Python python introduction for programmers A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html

Questions?