Computational Mathematics with Python



Similar documents
Computational Mathematics with Python

Computational Mathematics with Python

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

Introduction to Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

PYTHON Basics

Exercise 1: Python Language Basics

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Method To Solve Linear, Polynomial, or Absolute Value Inequalities:

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

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

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

Using Casio Graphics Calculators

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Programming Languages & Tools

Introduction to Perl

Certified PHP Developer VS-1054

The Julia Language Seminar Talk. Francisco Vidal Meca

3.GETTING STARTED WITH ORACLE8i

1998. (R. Bird and P. Wadler, Introduction to Functional Programming, Prentice

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Computer Programming I & II*

Microsoft Access 3: Understanding and Creating Queries

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Euler s Method and Functions

Linear Equations and Inequalities

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

Java Crash Course Part I

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

AN INTRODUCTION TO UNIX

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

Exercise 4 Learning Python language fundamentals

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Introduction to Matlab

Introduction to Parallel Programming and MapReduce

Introduction. Chapter 1

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

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

Modeling with Python

Welcome to Introduction to programming in Python

Systems Programming & Scripting

McGraw-Hill The McGraw-Hill Companies, Inc.,

Python for Scientific Computing.

A Comparison of C, MATLAB, and Python as Teaching Languages in Engineering

Moving from CS 61A Scheme to CS 61B Java

Introduction to Python

FEEG Applied Programming 5 - Tutorial Session

Informatica e Sistemi in Tempo Reale

Basic Use of the TI-84 Plus

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

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

MATLAB Programming. Problem 1: Sequential

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

ESPResSo Summer School 2012

Government Girls Polytechnic, Bilaspur

6.170 Tutorial 3 - Ruby Basics

Scientific Programming, Analysis, and Visualization with Python. Mteor 227 Fall 2015

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

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

For another way to generate recursive sequences, see Calculator Note 1D.

Classroom Tips and Techniques: The Student Precalculus Package - Commands and Tutors. Content of the Precalculus Subpackage

Facebook Twitter YouTube Google Plus Website

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Tutorial on C Language Programming

Introduction to Python

Object Oriented Software Design

The C Programming Language course syllabus associate level

VHDL Test Bench Tutorial

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

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

Alternative Programming Languages

Sources: On the Web: Slides will be available on:

Basic Programming and PC Skills: Basic Programming and PC Skills:

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Outline Basic concepts of Python language

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Math Content by Strand 1

Chapter 15 Functional Programming Languages

Introduction to Python

13 File Output and Input

AP Computer Science Java Mr. Clausen Program 9A, 9B

Introduction to Java

Lecture 4: Writing shell scripts

C++ INTERVIEW QUESTIONS

MBA Jump Start Program

Programmierpraktikum

MATLAB Basics MATLAB numbers and numeric formats

Example of a Java program

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Visual Logic Instructions and Assignments

Programming Languages CIS 443

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

Database Programming with PL/SQL: Learning Objectives

Transcription:

Computational Mathematics with Python Basics Claus Führer, Jan Erik Solem, Olivier Verdier Spring 2010 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 1 / 55

1 Introduction and Motivation Python vs Other Languages Examples and Demo 2 Concepts Basic Types Variables Lists For Loop 3 Practical Information 1 Boolean Arrays Modifying Arrays Comparing Arrays 2 Iteration 3 Objects and References Copying 4 Objects and Types Type Checking Claus Führer, Methods Jan Erik Solem, and OlivierProperties Verdier Computational Mathematics with Python Spring 2010 2 / 55

1 Introduction and Motivation Python vs Other Languages Examples and Demo 2 Concepts Basic Types Variables Lists For Loop 3 Practical Information 1 Boolean Arrays Modifying Arrays Comparing Arrays 2 Iteration 3 Objects and References Copying 4 Objects and Types Type Checking Claus Führer, Methods Jan Erik Solem, and OlivierProperties Verdier Computational Mathematics with Python Spring 2010 3 / 55

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. Used by YouTube, Google, NASA, Los Alamos, NSA among others Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 4 / 55

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. Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 5 / 55

Examples Python may be used in interactive mode: >>> x = 3 >>> y = 5 >>> print x + y 8 Here we solve [ ] 1 2 x = 3 4 >>> M = array ([[1., 2.], [3., 4.]]) >>> V = array ([2., 1.]) >>> x = solve (M, V) >>> print x [-3. 2.5] [ ] 2 1 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 6 / 55

More examples Computing e iπ and 2 100 : >>> print exp ( 1j* pi) # should return -1 : -) (-1+1. 22464679915e - 16j ) >>> print 2** 100 1267650600228229401496703205376L Computing ζ(x) = k=1 1 k x. For x = 2 we know that ζ(2) = π2 6 : # for x = 2: >>> print scipy. special. zeta (2., 1) 1. 64493406685 >>> print pi**2/6 1. 6449340668482264 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 7 / 55

Demo Demo Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 8 / 55

1 Introduction and Motivation Python vs Other Languages Examples and Demo 2 Concepts Basic Types Variables Lists For Loop 3 Practical Information 1 Boolean Arrays Modifying Arrays Comparing Arrays 2 Iteration 3 Objects and References Copying 4 Objects and Types Type Checking Claus Führer, Methods Jan Erik Solem, and OlivierProperties Verdier Computational Mathematics with Python Spring 2010 9 / 55

Numbers A number may be an integer, a real number or a complex number. The usual operations are + and - addition and substraction * and / multiplication and division ** power 2**(2+2) # 16 1j**2 # -1 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 10 / 55

Strings Strings are lists of characters, enclosed by simple or double quotes: valid string " string with double quotes " Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 11 / 55

Strings Strings are lists of characters, enclosed by simple or double quotes: valid string " string with double quotes " You may also use triple quotes for strings including multiple lines: """ This is a long, long string """ Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 11 / 55

Concept: Variable Variables A variable is a reference to an object. An object may have several references. One uses the assignment operator = to assign a value to a variable. Example x = [3, 4] # a list object is created y = x # this object now has two labels : x and y del x # we delete one of the labels del y # both labels are removed : the object is deleted Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 12 / 55

Concept: Lists Lists A python list is an ordered list of objects, enclosed in square brackets. One accesses elements of a list using zero-based indices inside square brackets. Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 13 / 55

List Examples Example L1 = [1, 2] L1[0] # 1 L1[1] # 2 L1[ 2] # raises IndexError L2 = [ a, 1, [3, 4]] L2[0] # a L2[2][0] # 3 L2[-1] # last element : [3,4] L2[- 2] # second to last : 1 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 14 / 55

List Utilities range(n) creates a list with n elements, starting with zero: print range ( 5) [0, 1, 2, 3, 4] Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 15 / 55

List Utilities range(n) creates a list with n elements, starting with zero: print range ( 5) [0, 1, 2, 3, 4] len(l) gives the length of a list: len ([ a, 1, 2, 34]) # returns 4 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 15 / 55

List Utilities range(n) creates a list with n elements, starting with zero: print range ( 5) [0, 1, 2, 3, 4] len(l) gives the length of a list: len ([ a, 1, 2, 34]) # returns 4 Use append to append an element to a list: L = [ a, b, c ] L[-1] # c L. append ( d ) L # L is now [ a, b, c, d ] L[-1] # d Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 15 / 55

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 [< expr > for <x> in < list >] Example L = [2, 3, 10, 1, 5] L2 = [x*2 for x in L] # [4, 6, 20, 2, 10] L3 = [ x* 2 for x in L if 4 < x <= 10] # [ 20, 10] laus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 16 / 55

Comprehensive Lists in Maths Mathematical Notation This is very close to the mathematical notation for sets. Compare: L 2 = {2x; x L} and L2 = [ 2* x for x in L] One big difference though is that lists are ordered while sets aren t. Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 17 / 55

Operations on Lists Adding two lists concatenates (sammanfoga) them: L1 = [1, 2] L2 = [3, 4] L = L1 + L2 # [1, 2, 3, 4] Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 18 / 55

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. } {{ } n times L = [1, 2] 3 * L # [1, 2, 1, 2, 1, 2] Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 18 / 55

Concept: for loop for loop A for loop allows to loop through a list using an index variable. This variable is successively equal to all the elements in the list. Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 19 / 55

Concept: for loop for loop A for loop allows 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: print s * 2, # output : 2 4 20 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 19 / 55

Indentation The part to be repeated in the for loop has to be properly indented: for elt in my_list : do_ something () something_ else () etc print " loop finished " # outside the for block Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 20 / 55

Repeating a Task One typical use of the for loop is to repeat a certain task a fixed number of time: n = 30 for i in range (n): do_ something # this gets executed n times Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 21 / 55

1 Introduction and Motivation Python vs Other Languages Examples and Demo 2 Concepts Basic Types Variables Lists For Loop 3 Practical Information 1 Boolean Arrays Modifying Arrays Comparing Arrays 2 Iteration 3 Objects and References Copying 4 Objects and Types Type Checking Claus Führer, Methods Jan Erik Solem, and OlivierProperties Verdier Computational Mathematics with Python Spring 2010 22 / 55

Python Shell Start a python session by typing scipython in 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, write exit() When you want to run python at home please follow the installation instruction on http://www.maths.lth.se/na/python/install Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 23 / 55

Executing Scripts You often want to execute the contents of a file. We recommand to use Kate 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 named file.py just write execfile( file.py ) in ipython. Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 24 / 55

Getting Help Some tips on how to use ipython: To get help 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 for completion in general Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 25 / 55