Scientific Computing using Python · 2019-04-10 · Python is free and open source, with...

73
Scientific Computing using Python Swaprava Nath Dept. of CSE IIT Kanpur mini-course webpage: https://swaprava.wordpress.com/a-short-course-on-python/ Disclaimer: the contents of this lecture series are based on several texts and online resources

Transcript of Scientific Computing using Python · 2019-04-10 · Python is free and open source, with...

Page 1: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Scientific Computing using Python

Swaprava Nath

Dept. of CSEIIT Kanpur

mini-course webpage: https://swaprava.wordpress.com/a-short-course-on-python/

Disclaimer: the contents of this lecture series are based on several texts and online resources

Page 2: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Outline of the Talk

1 Part 1: Preliminaries of Python

2 Part 2: Scientific Libraries

3 Part 3: Object Oriented Programming

1 / 24 Swaprava Nath Python

Page 3: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Outline of the Talk

1 Part 1: Preliminaries of Python

2 Part 2: Scientific Libraries

3 Part 3: Object Oriented Programming

2 / 24 Swaprava Nath Python

Page 4: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 5: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 6: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 7: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 8: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 9: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

What is Python?

Python is a general purpose programming language conceived in 1989 byDutch programmer Guido van Rossum

Python is free and open source, with development coordinated through thePython Software Foundation, www.python.org

Python has experienced rapid adoption in the last decade, and is now one ofthe most popular programming languages

Typical application domains:I scientific computing – simulationsI web developmentI graphical user interfacesI gamesI data processing

Commercial usage: Google, Dropbox, Reddit, YouTube, Walt DisneyAnimations, Cisco, Intel etc.

Academic usage: companion of many courses – for 101 CS courses or forsupportive computing

3 / 24 Swaprava Nath Python

Page 10: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Relative popularity of Python

Image courtesy: www.quantecon.org

The plot, produced using Stack Overflow Trends, shows one measure of therelative popularity of Python

4 / 24 Swaprava Nath Python

Page 11: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 12: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 13: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutines

I object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 14: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objects

I functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 15: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 16: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 17: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Features

High-level language, useful for rapid development

Relatively small core language – supported by many libraries

Multiparadigm language – the style of building the structure and elements ofcomputer programs can be

I procedural – based on routines/subroutinesI object-oriented – created on abstract objectsI functional – treats computation as the evaluation of mathematical functions

Usually implemented as interpreted as opposed to compiled (e.g. in C)

Syntax and design of a python code makes it easier to read, debug, anddevelop

5 / 24 Swaprava Nath Python

Page 18: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 19: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 20: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 21: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 22: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 23: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Compiled vs Interpreted

The compiler takes a program as a whole and translates it, but interpretertranslates a program statement by statement

Intermediate code or target code is generated in case of a compiler –interpreter doesn’t create intermediate code

Compiler is comparatively faster than Interpreter as the compiler takes thewhole program at one go while interpreter compiles each line of code

Compiler presents all errors concurrently, and it’s difficult to detect the errors– in contrast, interpreter display errors of each statement one by one, and it’seasier to detect errors

I Suitable for very large codes – one can check certain snippets of it withoutrunning the whole code again

I Step-by-step execution also helps in identifying errors

6 / 24 Swaprava Nath Python

Page 24: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant libraries

I numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,

integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 25: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on them

I scipy – mathematical operations, e.g., linear algebra, optimization,integration etc.

I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 26: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,

integration etc.

I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 27: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,

integration etc.I matplotlib – all kinds of plotting, 2D and 3D

I pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 28: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,

integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 29: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Suitability for Scientific Computation

Availability of relevant librariesI numpy – numerical computation, e.g., arrays, matrices, and operations on themI scipy – mathematical operations, e.g., linear algebra, optimization,

integration etc.I matplotlib – all kinds of plotting, 2D and 3DI pandas – for data handling

used in data science, machine learning, artificial intelligence, computationalbiology, computational physics, quantitative economics etc.

7 / 24 Swaprava Nath Python

Page 30: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Example in action – 2D plot

A plot of surprise in the Brexit election

0.0 0.2 0.4 0.6 0.8 1.0

wG = 1− wI , weight of global observation

0.0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Fractionof

shocked

minoritypopulation

Intra-conn prob = 0.8, Inter-conn prob = 0.8

Bias = 0.01

Bias = 0.05

Bias = 0.1

Bias = 0.2

Used pandas, numpy, matplotlib

8 / 24 Swaprava Nath Python

Page 31: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Example in action – 3D plot

X

40 30 20 10 0 10 20 30 40

Y

4030

2010

010203040

Z

1007550250255075100

Used matplotlib9 / 24 Swaprava Nath Python

Page 32: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Setting up Your Python Environment

Native python2.7 is available in any linux distribution

Need to install the libraries

An alternative python distribution is by anaconda – www.anaconda.com

We will use the native python with libraries and an IDE

Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder

jupyter-notebook is useful for testing snippets of code and displaying

spyder is useful for a long codebase development

Examples

10 / 24 Swaprava Nath Python

Page 33: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Setting up Your Python Environment

Native python2.7 is available in any linux distribution

Need to install the libraries

An alternative python distribution is by anaconda – www.anaconda.com

We will use the native python with libraries and an IDE

Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder

jupyter-notebook is useful for testing snippets of code and displaying

spyder is useful for a long codebase development

Examples

10 / 24 Swaprava Nath Python

Page 34: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Setting up Your Python Environment

Native python2.7 is available in any linux distribution

Need to install the libraries

An alternative python distribution is by anaconda – www.anaconda.com

We will use the native python with libraries and an IDE

Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder

jupyter-notebook is useful for testing snippets of code and displaying

spyder is useful for a long codebase development

Examples

10 / 24 Swaprava Nath Python

Page 35: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Setting up Your Python Environment

Native python2.7 is available in any linux distribution

Need to install the libraries

An alternative python distribution is by anaconda – www.anaconda.com

We will use the native python with libraries and an IDE

Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder

jupyter-notebook is useful for testing snippets of code and displaying

spyder is useful for a long codebase development

Examples

10 / 24 Swaprava Nath Python

Page 36: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Setting up Your Python Environment

Native python2.7 is available in any linux distribution

Need to install the libraries

An alternative python distribution is by anaconda – www.anaconda.com

We will use the native python with libraries and an IDE

Convenient to use an IDE – integrated development environmentI jupyter-notebookI spyder

jupyter-notebook is useful for testing snippets of code and displaying

spyder is useful for a long codebase development

Examples

10 / 24 Swaprava Nath Python

Page 37: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Some Introductory Python Programs

Task 1: finding if a number is even/odd – if-else clause

Task 2: finding the smallest of three numbers

Task 3: finding if a natural number is prime or not – while loop and for loop

Notice the indentation and absence of any braces or brackets

Makes the code clutter-free and more readable

11 / 24 Swaprava Nath Python

Page 38: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h =, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 39: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h =, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 40: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h =, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 41: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h = n, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 42: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h = max{1, n}, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 43: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h = max{1, n}, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 44: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

More Examples of Loops and ConditionalsFinding square root of a non-negative integer n – handling exceptions

xt+1 =1

2

(xt +

n

xt

)

Finding r-th root of any non-negative number n – bisection method

h = max{1, n}, l = 0, guess =h+ l

2if guessr > n : h = guess

else : l = guess

Generalization of the square-root finding algorithm – Newton-Raphsonmethod for finding a real root of a polynomial f

xt+1 = xt −f(xt)

f ′(xt)

12 / 24 Swaprava Nath Python

Page 45: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Python Standard Data Types

Python data types: mutable and immutable

Primitive data types: int, float, bool

Lists, Tuples, Strings

Dictionaries, Sets

Functions

Example of a function – the root finding algorithm as a function

Example of recursion of a function – checking a palindrome

Exercise: solve the ‘Tower of Hanoi’ problem using recursion

13 / 24 Swaprava Nath Python

Page 46: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Python Standard Data Types

Python data types: mutable and immutable

Primitive data types: int, float, bool

Lists, Tuples, Strings

Dictionaries, Sets

Functions

Example of a function – the root finding algorithm as a function

Example of recursion of a function – checking a palindrome

Exercise: solve the ‘Tower of Hanoi’ problem using recursion

13 / 24 Swaprava Nath Python

Page 47: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Outline of the Talk

1 Part 1: Preliminaries of Python

2 Part 2: Scientific Libraries

3 Part 3: Object Oriented Programming

14 / 24 Swaprava Nath Python

Page 48: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Numerical Computation: numpy

Goal: faster array processing

I Example: create an array with million entries and process them – add,compute average

I Classic python must use loop which is much slower than C or FortranI numpy does precisely that – accesses the optimized C or Fortran code by

sending the relevant portion of the python codeI the relevant portions are sent in batches – therefore your code should be

vectorized as much as possibleI and use numpy functions for them

A ton of useful functions on arraysI linspace, amax, argmax, ones, zeros, sum, mean, var, std,

cumsum, cumprod and many moreI random gives a bunch of random variables – randn, beta, binomial,

dirichlet, exponential

15 / 24 Swaprava Nath Python

Page 49: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Numerical Computation: numpy

Goal: faster array processingI Example: create an array with million entries and process them – add,

compute average

I Classic python must use loop which is much slower than C or FortranI numpy does precisely that – accesses the optimized C or Fortran code by

sending the relevant portion of the python codeI the relevant portions are sent in batches – therefore your code should be

vectorized as much as possibleI and use numpy functions for them

A ton of useful functions on arraysI linspace, amax, argmax, ones, zeros, sum, mean, var, std,

cumsum, cumprod and many moreI random gives a bunch of random variables – randn, beta, binomial,

dirichlet, exponential

15 / 24 Swaprava Nath Python

Page 50: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Numerical Computation: numpy

Goal: faster array processingI Example: create an array with million entries and process them – add,

compute averageI Classic python must use loop which is much slower than C or FortranI numpy does precisely that – accesses the optimized C or Fortran code by

sending the relevant portion of the python code

I the relevant portions are sent in batches – therefore your code should bevectorized as much as possible

I and use numpy functions for them

A ton of useful functions on arraysI linspace, amax, argmax, ones, zeros, sum, mean, var, std,

cumsum, cumprod and many moreI random gives a bunch of random variables – randn, beta, binomial,

dirichlet, exponential

15 / 24 Swaprava Nath Python

Page 51: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Numerical Computation: numpy

Goal: faster array processingI Example: create an array with million entries and process them – add,

compute averageI Classic python must use loop which is much slower than C or FortranI numpy does precisely that – accesses the optimized C or Fortran code by

sending the relevant portion of the python codeI the relevant portions are sent in batches – therefore your code should be

vectorized as much as possibleI and use numpy functions for them

A ton of useful functions on arraysI linspace, amax, argmax, ones, zeros, sum, mean, var, std,

cumsum, cumprod and many moreI random gives a bunch of random variables – randn, beta, binomial,

dirichlet, exponential

15 / 24 Swaprava Nath Python

Page 52: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Numerical Computation: numpy

Goal: faster array processingI Example: create an array with million entries and process them – add,

compute averageI Classic python must use loop which is much slower than C or FortranI numpy does precisely that – accesses the optimized C or Fortran code by

sending the relevant portion of the python codeI the relevant portions are sent in batches – therefore your code should be

vectorized as much as possibleI and use numpy functions for them

A ton of useful functions on arraysI linspace, amax, argmax, ones, zeros, sum, mean, var, std,

cumsum, cumprod and many moreI random gives a bunch of random variables – randn, beta, binomial,

dirichlet, exponential

15 / 24 Swaprava Nath Python

Page 53: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Scientific Tools: scipy

numpy ⊂ scipy

built on top of numpy for more focused scientific programming, e.g.,I linear algebraI numerical integrationI interpolationI optimizationI distributions and random number generationI signal processing

similar ideas of using python over C and Fortran subroutines

particularly useful for statistical methods – scipy.stats

two specific functions – bisect and newton

16 / 24 Swaprava Nath Python

Page 54: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Plotting: matplotlib

Features:

high quality 2D and 3D plots

output in all the usual formats (PDF, PNG, EPS, SVG etc.)

LATEX integration

fine grained control over all aspects of presentation

animation

and many more

Some example usage of matplotlib

17 / 24 Swaprava Nath Python

Page 55: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Data Handling: pandas

Pandas is a package of fast, efficient data analysis tools for Python

Similar to numpy that defines the basic array data type and fundamentaloperations on arrays

pandas defines fundamental structures for working with data, and

endows them with methods that facilitate operations such asI reading in dataI adjusting indicesI working with dates and time seriesI sorting, grouping, re-ordering, slicing, and general data manipulationsI dealing with missing value

Two fundamental data types: series and dataframeI series: an array of (possibly dissimilar) objects with generalized index – not

as space consuming as dictionariesI dataframe: a matrix with generalized index – and various methods for data

handling

18 / 24 Swaprava Nath Python

Page 56: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Outline of the Talk

1 Part 1: Preliminaries of Python

2 Part 2: Scientific Libraries

3 Part 3: Object Oriented Programming

19 / 24 Swaprava Nath Python

Page 57: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Programming in Practice: Simulations

We typically use simulations to understand the performance/behavior of asystem

Examples:

1. manufacturing process simulation2. stock market simulation3. statistical processes simulation

Typical steps of simulation:I Create a computational environment that mimics the real worldI Generate (synthetic) or load data from sourcesI Test hypotheses

Examples:

1. simulating a 2D random walk2. simulation of a discrete random variable

20 / 24 Swaprava Nath Python

Page 58: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Programming in Practice: Simulations

We typically use simulations to understand the performance/behavior of asystem

Examples:

1. manufacturing process simulation2. stock market simulation3. statistical processes simulation

Typical steps of simulation:I Create a computational environment that mimics the real worldI Generate (synthetic) or load data from sourcesI Test hypotheses

Examples:

1. simulating a 2D random walk2. simulation of a discrete random variable

20 / 24 Swaprava Nath Python

Page 59: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Programming in Practice: Simulations

We typically use simulations to understand the performance/behavior of asystem

Examples:

1. manufacturing process simulation2. stock market simulation3. statistical processes simulation

Typical steps of simulation:I Create a computational environment that mimics the real worldI Generate (synthetic) or load data from sourcesI Test hypotheses

Examples:

1. simulating a 2D random walk2. simulation of a discrete random variable

20 / 24 Swaprava Nath Python

Page 60: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 61: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed too

I object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 62: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a class

I example: class rectangle, create an object of class rectangle, hasparameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 63: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 64: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 65: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 66: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

object and classWhy do we need an object-oriented code?

Purpose: reuse a code that we need quite often

data and function/method bound together, e.g., a listI list instance l, method l.append() or l.sort()I when the object is passed, the methods are passed tooI object is an instance of a classI example: class rectangle, create an object of class rectangle, has

parameters/data length and height, function of that data is area, longer side,shorter side, length of diagonal, area of the largest ellipse inscribed inside it

abstract data types – defined by a class

object: collection of data and function, as defined in the class

Example: simulation of LUDO game

21 / 24 Swaprava Nath Python

Page 67: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Simulation using OOP

discrete random variable with finite state space

define the class of discrete random variables

an instance of the class is a random variable, which has a distribution –probability masses

how to draw a random variable of a given distribution

inverse CDF method – true for discrete random variable too

22 / 24 Swaprava Nath Python

Page 68: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Simulation using OOP

discrete random variable with finite state space

define the class of discrete random variables

an instance of the class is a random variable, which has a distribution –probability masses

how to draw a random variable of a given distribution

inverse CDF method – true for discrete random variable too

22 / 24 Swaprava Nath Python

Page 69: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Simulation using OOP

discrete random variable with finite state space

define the class of discrete random variables

an instance of the class is a random variable, which has a distribution –probability masses

how to draw a random variable of a given distribution

inverse CDF method – true for discrete random variable too

22 / 24 Swaprava Nath Python

Page 70: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Summary

This series of lectures discussed

The basic syntaxes of python and its standard data types, loops andconditionals

some implementations of numerical techniques

Four important set of scientific libraries

Object-oriented programming in python

23 / 24 Swaprava Nath Python

Page 71: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Summary

This series of lectures discussed

The basic syntaxes of python and its standard data types, loops andconditionals

some implementations of numerical techniques

Four important set of scientific libraries

Object-oriented programming in python

23 / 24 Swaprava Nath Python

Page 72: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

Summary

This series of lectures discussed

The basic syntaxes of python and its standard data types, loops andconditionals

some implementations of numerical techniques

Four important set of scientific libraries

Object-oriented programming in python

23 / 24 Swaprava Nath Python

Page 73: Scientific Computing using Python · 2019-04-10 · Python is free and open source, with development coordinated through the Python Software Foundation, Python has experienced rapid

References

[online course materials] Python for scientific computing, by SwapravaNath: http://scientificcomputing.is-great.net/

[book] Introduction to Computation and Programming using Python, byJohn Guttag, Prentice Hall of India.

[online course materials] Lectures in Quantitative Economics, Thomas J.Sargent and John Stachurski:https://lectures.quantecon.org/py/index.html

24 / 24 Swaprava Nath Python