100% found this document useful (1 vote)
403 views

Python Versus Matlab: Examples in Civil Engineering

The document discusses Python versus MATLAB, providing examples of how each handles different programming paradigms like scripting, procedural, functional, object oriented, interface oriented, and aspect oriented programming. It also covers the types systems of each. The document is presented as a slideshow by Fedor Baart comparing features of Python and MATLAB for various engineering and scientific domains like geophysics and civil engineering.

Uploaded by

NiranjanAryan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
403 views

Python Versus Matlab: Examples in Civil Engineering

The document discusses Python versus MATLAB, providing examples of how each handles different programming paradigms like scripting, procedural, functional, object oriented, interface oriented, and aspect oriented programming. It also covers the types systems of each. The document is presented as a slideshow by Fedor Baart comparing features of Python and MATLAB for various engineering and scientific domains like geophysics and civil engineering.

Uploaded by

NiranjanAryan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

A bit about myself Learn a new language? What do people like?

Examples

Python versus Matlab: examples from geophysics


to civil engineering

Fedor Baart

May 13, 2013

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Introduction

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

1 A bit about myself

2 Learn a new language?

3 What do people like?

4 Examples

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Outline

1 A bit about myself

2 Learn a new language?

3 What do people like?

4 Examples

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Senior consultant at
Deltares, coastal systems
and software data
integration
1 day a week at Hydraulic
Engineering at Delft 5 June 2013 Phd: Confidence in
University of Technology coastal forecasts

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Outline

1 A bit about myself

2 Learn a new language?

3 What do people like?

4 Examples

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

When to extend your toolbox?

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

What does your toolbox look like?

src: Norm Abram

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

What determines the language?


Paradigma
Data types (dict, list, strings, numbers, matrices, vectors)
Syntax (Keywords, whitespace, braces)
Type system (int a = 1 vs a = 1)

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Evolution of computer paradigms

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Scripting

Python Matlab

>>> 1 + 1 1 >> 1 + 1
>>> print ( " Hello World " ) >> disp ( Hello World ) ;

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Procedural

Python
Matlab
def add (a , b ) :
function total = add (a , b )
return a + b #
total = a + b
significant
end % ends the function 3
whitespace

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Functional

Python
Matlab
# anonymous function
# applied to each element 2
% apply anonymous function
>>> map ( lambda x : x **2 ,
% to each element 2
[1 ,2 ,3])
>> arrayfun ( @ ( x ) ( x ^2) , [1
[1 ,4 ,9]
2 3])
# recursive
ans =
# add (1 + reduce ( add ,
1 4 9
[2 ,3]) )
% reduce not in matlab
>>> reduce ( add , [1 ,2 ,3]) 7
6

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Object Oriented

Python Matlab

# numbers are objects % numbers are not objected


>>> 2.5. as_integer_ratio () oriented
(5 , 2) >> rat (2.5)
>>> f = interp1d ([0 ,1 ,2 ,3] , 4 ans =
[0 ,1 ,4 ,9] , kind = cubic 3 + 1/( -2)
) % This is an objected 5
# which can be called oriented part of matlab
>>> f (1.5) >> Hs = spectrum . periodogram
2.25 % which has properties
# and has properties >> get ( Hs , WindowName )
>>> f . fill_value 9 ans =
nan Rectangular 10

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Interface Oriented

Python

class SomeModel ( object ) :


implements ( IModel ) :
Matlab
def initialize ( self , config ) :
# initialize
% Not used in
def update ( self , dt ) 5
matlab
# do a timestep
...
# Register Model
registerUtility ( SomeModel () , IModel )
# get a list of all the models
models = getUtilitiesFor ( IModel ) 10

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Aspect Oriented

Python

@cache Matlab
def slow_calculation () :
time . sleep (100) 3 % Not used in
return 1+1 matlab
slow_calculation () # slow
slow_calculation () # fast

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Types

Python
Python
Types: Everything is an object
Types: Everything is an array
Type safety:
Weak dynamic, typed
# strong , dynamic
>> x = 1
>>> x = 1
>> x = a
>>> x = a
>> x = 1 + a
>>> x = 1 + a 4
98
TypeError

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Outline

1 A bit about myself

2 Learn a new language?

3 What do people like?

4 Examples

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

What aspects determine if people like their language?


People (What are people using it for? )
Help (Documentation, community)
Tools (What tools are available? )
Speed (How does it scale?)
Libraries (What libraries can you reuse?)

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Whats popular where?

python
science
matlab
geospatial
engineering
web
science
system
games

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

People and paradigma

scientists programmers

less more
abstract abstract

script function object interface aspects

FOTRAN / C

Matlab

Python / Ruby

Java / C#

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

python

help ( function ) 1

most python modules have a doc matlab


website: help function
1 docs.python.org
All documentation in matlab gui.
2 docs.scipy.org
3 matplotlib.sourceforge.net
4 readthedocs.org

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Most popular python IDE (@SO) Screenshot

15 Spyder

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Most popular python IDE (@SO)


Screenshot

3 emacs
4 PyCharm
15 Spyder

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Screenshot
Most popular python IDE (@SO)
new ipython notebook
1 vim
2 PyDev
3 emacs
4 PyCharm
15 Spyder

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Python Matlab
Startup do nothing and shutdown Startup do nothing and shutdown
time. time.
$time python -c " pass " $ time matlab -r exit 1
real 0 m0 .038 s real 1 m8 .891 s
user 0 m0 .022 s user 0 m11 .889 s
sys 0 m0 .012 s 4 sys 0 m3 .184 s

Other languages
C: 0.000s, Java: 0.3s, Perl 0.001s, Bash 0.001s

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Matlab
Python
>> x = zeros (1000) ;
>>> timeit . timeit ( a = x . dot ( 1 >> tic ; for i =1:10; a = x * x ; end
x ) number =10) ; toc
1. 594820 97 62 57 324 2 Elapsed time is 1.796690 3
seconds .

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Outline

1 A bit about myself

2 Learn a new language?

3 What do people like?

4 Examples

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

BMI
fortran + web

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Kustviewer
data + gis + web

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Argus
notebook (calculator) + computer vision

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering
A bit about myself Learn a new language? What do people like? Examples

Leap
Interaction + 2d, 3d plotting

Fedor Baart
Python versus Matlab: examples from geophysics to civil engineering

You might also like