Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.

Slides:



Advertisements
Similar presentations
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Advertisements

Introduction to MATLAB The language of Technical Computing.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Refresher: Vector and Matrix Algebra Mike Kirkpatrick Department of Chemical Engineering FAMU-FSU College of Engineering.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
By. What advantages has it? The Reasons for Choosing Python  Python is free  It is object-oriented  It is interpreted  It is operating-system independent.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Chapter 10 Review: Matrix Algebra
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
ECON 1150 Matrix Operations Special Matrices
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Matlab tutorial course Lesson 2: Arrays and data types
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Multi-Dimensional Arrays
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Review of Matrices Or A Fast Introduction.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
418512: Computer Programming Languages Lecture 7 Pramook Khungurn TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A AAAA.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Eng Ship Structures 1 Introduction to Matlab.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Intro to Matlab 1.Using scalar variables 2.Vectors, matrices, and arithmetic 3.Plotting 4.Solving Systems of Equations Can be found at:
© 2011 Autodesk Freely licensed for use by educational institutions. Reuse and changes require a note indicating that content has been modified from the.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Python Crash Course Numpy 3 rd year Bachelors V1.0 dd Hour 5.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Linear Algebra Libraries: BLAS, LAPACK, ScaLAPACK, PLASMA, MAGMA
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Linear Algebra Libraries: BLAS, LAPACK, ScaLAPACK, PLASMA, MAGMA Shirley Moore CPS5401 Fall 2013 svmoore.pbworks.com November 12, 2012.
NumPy, SciPy, Mpi4Py Shepelenko Olha. History of NumPy Originally, Python was not developed as a language for numerical computing. However, due to its.
Python Scripting for Computational Science CPS 5401 Fall 2014 Shirley Moore, Instructor October 6,
PH2150 Scientific Computing Skills
Lecture: MATLAB Chapter 1 Introduction
Other Kinds of Arrays Chapter 11
INTRODUCTION TO BASIC MATLAB
Introduction to MATLAB
CSE Social Media & Text Analytics
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Communication and Coding Theory Lab(CS491)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Simulation And Modeling
Dr. Sampath Jayarathna Cal Poly Pomona
Introduction to Matlab
Matlab Training Session 2: Matrix Operations and Relational Operators
Dr. Sampath Jayarathna Old Dominion University
1.8 Matrices.
1.8 Matrices.
The Elements of Linear Algebra
Presentation transcript:

Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s online (see below) NumPy page at the SciPy site (Better—but not up at the current time) The fundamental package for scientific computing with Python. Contains:  a powerful N-dimensional array object  sophisticated functions that accommodate disparities in array dimensions  tools for integrating C/C++ and Fortran code  linear algebra, Fourier transform, and random number capabilities

Any algorithm expressed primarily as operations on arrays and matrices can run almost as fast in NumPy as the equivalent in C Seen as a good free alternative to MATLAB  MATLAB has many additional toolboxes (e.g., Simulink packages)  NumPy’s advantages include Python is a more modern and complete programming language than MATLAB’s programming language Open source and free Internally, both rely on LAPACK for efficient linear algebra computations.  LAPACK (Linear Algebra PACKage) is a library for numerical computing written in Fortran 77

Installing Go to Click on folder Click on numpy win32-superpack-python2.7.exe to download and run Accept the defaults  Will find C:\Python27\ in the registry  Actually installed in C:\Python27\Lib\site-packages\numpy To test, type in the Python command line from numpy import *

Documentation NumPy User Guide (work in progress)  Last part documents the C API  Like a language reference, not really a tutorial Tentative NumPy Tutorial (unfinished)  Good NumPy Reference Guide (detailed and terse)

Ivan Idris, NumPy Beginner’s Guide (2 nd Ed.), 2013, Packt Publishing, 310 pages  Amazon $40, 4.5 on 11 reviews Hans Petter Langtangen, Python Scripting for Computational Science, Springer, 2010, 756 pages  Amazon $46, 4.5 on 11 reviews Numpy Example List  Examples of all 215 NumPy functions and methods  Indexed by function name

Numpy Example List With Doc  Auto-generated version of Numpy Example List with added documentation from doc strings and arguments specification for methods and functions  Not all entries have a doc string  Not as regularly updated as Numpy Example List The SciPy Additional Documentation page  Has a section for NumPy with numerous links

SciPy Installation and Documentation An Open Source library of scientific tools for Python  Depends on the NumPy library Gathers a variety of high level science and engineering modules into one package Provides modules for  statistics  optimization  numerical integration  linear algebra  Fourier transforms  signal processing  image processing  genetic algorithms  ODE solvers  special functions  and more

Download Go to Click on folder Download and execute scipy win32-superpack-python2.6.exe Creates folder C:\Python27\Lib\site-packages\scipy To test, try out example under “Basic matrix operations” (the 1 st example) in the Tutorial (see below)

Documentation SciPy Tutorial Travis E. Oliphant, SciPy Tutorial (the “old tutorial”, 42 pp.),  The example files: Dave Kuhlman, SciPy Course Outline (45 pp.),  This is much more than a course outline. Francisco J. Blanco-Silva, Learning SciPy for Numerical and Scientific Computing, Packt Publishing, 2013, 150 pages  Amazon $26, 4 on 7 reviews

The SciPy Community, SciPy Reference Guide (Release , pages),  Complete, very much a reference

A Brief Introduction to NumPy The main data type is an array  A set of elements, all of the same type Arrays can be created in different ways Constructor array() takes a list and returns an array >>> import numpy as np >>> a = np.array([2, 4, 6]) >>> a array([2, 4, 6])  np.array(2,4,6) is wrong: need a list Constructor arange() is like range() but returns an array >>> b = np.arange(6, 18, 3) >>> b array([ 6, 9, 12, 15])

Using arange() with floating point arguments, can’t easily predict the number of elements produced Use linspace() : like arange(), but last argument is number of elements (not the step) >>> x = np.linspace(0, 2*pi, 10) >>> x array([ 0., , , , , , , , , ]) Normally scalar functions apply element-wise to arrays >>> f = np.sin(x) >>> f array([ e+00, e-01, e-01, e-01, e-01, e-01, e-01, e-01, e-01, e-16])

Binary arithmetic and logical operations on arrays are performed element-wise >>> a = np.arange(4) >>> b = np.arange(1, 5) >>> c = a + b >>> c array([1, 3, 5, 7]) >>> d = -a + 2*b >>> d array([2, 3, 4, 5])

NumPy's main object is the homogeneous multidimensional array  A table of elements (usually numbers) all of the same type indexed by a tuple of positive integers  E.g., vectors, matrices, images and spreadsheets ‘Multidimensional’ means arrays can have several dimensions or axes  Because dimension is ambiguous, use axis  Number of axes is the array’s rank  E.g., [1, 2, 1] has rank 1: it has 1 axis (with length 3) The multidimensional array class is called ndarray  Not the same as the Standard Python Library class array (a 1D array)

ones() takes a tuple of axis lengths and returns an array of 1’s of the indicated shape  zeros() is analogous The value of array property shape is the shape-describing tuple of the array >>> y = np.zeros( (2, 3) ) >>> y array([[ 0., 0., 0.], [ 0., 0., 0.]]) >>> type(y) >>> y.shape (2, 3)

Change an array’s shape by assigning to its shape property >>> y.shape = 3, 2 >>> y array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])

Operate on arrays with different shapes as long as they “fit well”: broadcasting >>> a1 = np.arange(3) >>> a1 array([0, 1, 2]) >>> a2 = np.arange(6) >>> a2.shape = 2, 3 >>> a2 array([[0, 1, 2], [3, 4, 5]]) >>> a1 + a2 # add a1 to both rows of a2 array([[0, 2, 4], [3, 5, 7]])

Arrays can be indexed, sliced, iterated over (like Python lists) >>> a array([0, 1, 2]) >>> a[0] 0 >>> a1[0:2] = 4, 3 >>> for i in a1:... print i,

Indexing more than 1 dimension, indices are separated by commas >>> a2[0,1] 1 >>> a2[1] = a1 # copy a1 into a2’s 2 nd row >>> a2 array([[0, 1, 2], [4, 3, 2]]) >>> a2[1,:] # a2’s 2 nd row array([4, 3, 2])

Copies and Views When working with arrays, their data is sometimes copied into a new array and sometimes not  There are three cases No Copy Arrays are objects (instances of ndarry )  Variables are actually bound to references to arrays, not to arrays themselves  Assignment involving such variables copies the reference and not the array

>>> a = np.array([1,2,3]) >>> b = a >>> b is a True >>> b[0] = 5 >>> print a [5 2 3] Similarly, Python passes mutable objects as references  So function calls make no copies of arrays

View or Shallow Copy Different array objects can share the same data Method view() creates a new array object that looks at the same data >>> a = np.arange(4) >>> c = a.view() >>> c is a False >>> print c [ ] Changing the shape of a view doesn’t change the shape of its base >>> c.shape = 2,2 >>> a.shape (4,)

Can change the base via the view even when they have different shapes >>> c[0,0] = 7 >>> print a [ ] The type of the view is ndarry, like all NumPy arrays >>> type(c) What distinguishes a view is that it doesn’t own its own memory Value of the base attribute for an array that doesn’t own its own memory is the array whose memory the view references  For an array that owns its own memory, the value is None >>> c.base array([7, 1, 2, 3]) >>> print a.base None

A slice is a view  Its base is the array it’s derived from >>> a = np.arange(8) >>> s = a[2:6] >>> type(s) >>> print s [ ] >>> s.base is a True Again, we can update the base via the view (slice) >>> s[:] = 9 >>> print a [ ]

Deep Copy Method copy() makes a complete copy of the array and its data >>> a = np.arange(4) >>> d = a.copy() >>> d is a False >>> print d.base None >>> d[0] = 9 >>> print a [ ]

Linear Algebra in NumPy numpy.dot(A, B) is matrix multiplication of 2D arrays A and B numpy.linalg.solve(A, b), with 2D array A and 1D array b, returns 1D array x as solution to Ax = b >>> import numpy as np >>> A = np.array([[1,-2,1], [0,2,-8], [-4,5,9]]) >>> b = np.array([0,8,-9]) >>> import numpy.linalg >>> x = numpy.linalg.solve(A, b) >>> print x [ ] >>> np.dot(A[0], x) 0.0

numpy.linalg.inv(A) returns the matrix inverse of 2D array A >>> A = np.array([[1,-2,1], [0,2,-8], [-4,5,9]]) >>> Ainv = numpy.linalg.inv(A) >>> print Ainv [[ ] [ ] [ ]] >>> print numpy.dot(A, Ainv) [[ ] [ ] [ ]]

numpy.linalg.det(A) returns the determinant of A >>> A = np.array([[2,0,0], [1,3,0], [2,1,4]]) >>> print numpy.linalg.det(A) 24.0

Eigenvalues and Eigenvectors in NumPy The following functions are in package numpy.linalg eigvals(A ) Return an array of all solutions ( ) to the equation A x = x eig(A ) Return all solutions (, x) to the equation A x = x 1 st element of the return tuple is an array of all the eigenvalues 2 nd element is an array of the corresponding eigenvectors in the columns  x[:,i] is the i th eigenvector, corresponding to [i]  Eigenvectors are only defined up to a constant scale factor The scaling factor here is chosen so that

>>> import numpy as np >>> import numpy.linalg >>> A = np.array([[0.95, 0.03], [0.05, 0.97]]) Find the eigenvalues and eigenvectors of A >>> [lamb, x] = numpy.linalg.eig(A) >>> print lamb [ ] >>> print x [[ ] [ ]] Note how the eigenvectors are scaled >>> print sqrt( x[0,0]**2 + x[1,0]**2 ) 1.0 >>> print sqrt( x[0,1]**2 + x[1,1]**2 ) 1.0

Confirm that A x[:,i] = [i] x[:,i], i = 0, 1 >>> print np.dot(A, x[:,0]) [ ] >>> print lamb[0] * x[:,0] [ ] >>> print np.dot(A, x[:,1]) [ ] >>> print lamb[1] * x[:,1] [ ]

Matrices Matrices (subclass matrix ) are 2D objects that inherit from ndarray matrix() is like array() but produces a matrix >>> import numpy as np >>> import numpy.linalg >>> A = np.matrix( [[1,2,3],[11,12,13],[21,22,23]]) >>> print A [[ 1 2 3] [ ] [ ]]

Make a column vector, 4x1 (not just 4) >>> x = np.matrix( [[1],[2],[3]] ) >>> print x [[1] [2] [3]] Make a row vector, 1x4 >>> y = np.matrix( [[1,2,3]] ) >>> print y [[1 2 3]] * is now matrix (not element-wise) multiplication >>> A * x matrix([[ 14], [ 74], [134]])

Find the transpose >>> A.T matrix([[ 1, 11, 21], [ 2, 12, 22], [ 3, 13, 23]]) Solve a system of equations  Result is in the order value for x[0], for x[1], for x[2] >>> numpy.linalg.solve(A, x) matrix([[ ], [ ], [ ]]) Find the determinant >>> B = np.matrix([[1, 2], [3, 1]]) >>> numpy.linalg.det(B) -5.0

Find the inverse (a matrix) >>> Binv = numpy.linalg.inv(B) >>> print Binv [[ ] [ ]] >>> type(Binv) >>> B * Binv matrix([[ 1., 0.], [ 0., 1.]])

matrix() also takes an array as argument  Converts it to a (2D) matrix even if its 1D >>> np.matrix(arange(4)) matrix([[0, 1, 2, 3]]) >>> np.matrix(arange(4)).T matrix([[0], [1], [2], [3]]) Can reshape the array before converting >>> np.matrix(arange(4).reshape(2,2)) matrix([[0, 1], [2, 3]])

Or reshape the matrix >>> D = np.matrix(arange(4)) >>> D.reshape(2,2) matrix([[0, 1], [2, 3]]) The A attribute of a matrix is the underlying 1D array >>> D.A array([[0, 1, 2, 3]])

Can index, slice, and iterate over matrices much as with arrays The linear algebra package works with both arrays and matrices  It’s generally advisable to use arrays  But you can mix them E.g., use arrays for the bulk of the code Switch to matrices when doing lots of multiplication