A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

Similar documents
Uniform and constant electromagnetic fields

2D Plotting with Matlab

Lectures about Python, useful both for beginners and experts, can be found at (

International Journal of Mathematical Archive-5(8), 2014, Available online through ISSN

NumPy 1.5. open source. Beginner's Guide. v g I. performance, Python based free open source NumPy. An action-packed guide for the easy-to-use, high

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Conditioning and Stability

Algorithms for Uncertainty Quantification

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

QuTiP: Quantum Toolbox in Python. Circuit-QED

2 Getting Started with Numerical Computations in Python

MODELS USING MATRICES WITH PYTHON

I. Numerical Computing

Linear Algebra Using MATLAB

Math 307 Learning Goals. March 23, 2010

Matlab Section. November 8, 2005

Computational Methods for Nonlinear Systems

MAS212 Scientific Computing and Simulation

Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations

ECE521 W17 Tutorial 1. Renjie Liao & Min Bai

MATLAB for Engineers

Applied Linear Algebra in Geoscience Using MATLAB

Conjugate-Gradient. Learn about the Conjugate-Gradient Algorithm and its Uses. Descent Algorithms and the Conjugate-Gradient Method. Qx = b.

Reduction to the associated homogeneous system via a particular solution

TOPIC 2 Computer application for manipulating matrix using MATLAB

NUMERICAL METHODS USING MATLAB

Numerical Methods for Engineers. and Scientists. Applications using MATLAB. An Introduction with. Vish- Subramaniam. Third Edition. Amos Gilat.

Vectors and Vector Arithmetic

GES 554 PDE MEMO. Memo: GES554-Project-2. REF: Ext:

MA3232 Summary 5. d y1 dy1. MATLAB has a number of built-in functions for solving stiff systems of ODEs. There are ode15s, ode23s, ode23t, ode23tb.

NUMERICAL COMPUTATION IN SCIENCE AND ENGINEERING

The Shooting Method for Boundary Value Problems

Gradient Descent Methods

Using MATLAB. Linear Algebra

ECE 5615/4615 Computer Project

Companion. Jeffrey E. Jones

Final Examination. CS 205A: Mathematical Methods for Robotics, Vision, and Graphics (Fall 2013), Stanford University

CSC321 Lecture 2: Linear Regression

Computational Methods for Nonlinear Systems

Basic Linear Algebra in MATLAB

Statistical Analysis of fmrl Data

CS 450 Numerical Analysis. Chapter 8: Numerical Integration and Differentiation

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

QR 2: Least Squares and Computing Eigenvalues

1 Overview of Simulink. 2 State-space equations

NONLINEAR EQUATIONS AND TAYLOR S THEOREM

HW5 computer problem: modelling a dye molecule Objectives By the end of this lab you should: Know how to manipulate complex numbers and matrices. Prog

CSCI 250: Intro to Robotics. Spring Term 2017 Prof. Levy. Computer Vision: A Brief Survey

Numerical Methods for CSE. Examination - Solutions

MATH.2720 Introduction to Programming with MATLAB Vector and Matrix Algebra

Table 1 Principle Matlab operators and functions Name Description Page reference

New Mexico Tech Hyd 510

Python & Numpy A tutorial

Iterative Solvers. Lab 6. Iterative Methods

epistasis Documentation

STA141C: Big Data & High Performance Statistical Computing

We wish to solve a system of N simultaneous linear algebraic equations for the N unknowns x 1, x 2,...,x N, that are expressed in the general form

PHYS 7411 Spring 2015 Computational Physics Homework 3

Least squares and Eigenvalues

which arises when we compute the orthogonal projection of a vector y in a subspace with an orthogonal basis. Hence assume that P y = A ij = x j, x i

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

6.003 Homework #1. Problems. Due at the beginning of recitation on September 14, 2011.

NUMERICAL ANALYSIS WEEKLY OVERVIEW

This appendix provides a very basic introduction to linear algebra concepts.

Math 322. Spring 2015 Review Problems for Midterm 2

Multiple Degree of Freedom Systems. The Millennium bridge required many degrees of freedom to model and design with.

COMPRESSED SENSING IN PYTHON

Write a simple 1D DFT code in Python

Eigenvalue problems III: Advanced Numerical Methods

Hani Mehrpouyan, California State University, Bakersfield. Signals and Systems

Chapter 4: Interpolation and Approximation. October 28, 2005

Introduction to Mobile Robotics Compact Course on Linear Algebra. Wolfram Burgard, Bastian Steder

Image Processing in Numpy

Review of Linear Algebra

AM205: Assignment 3 (due 5 PM, October 20)

Notes from Andrew Reeve,U.Maine

Linear least squares problem: Example

CONTROL POLYGONS FOR CUBIC CURVES

USE OF MATLAB TO UNDERSTAND BASIC MATHEMATICS

Lecture 4. Programming

STA141C: Big Data & High Performance Statistical Computing

Linear Systems of Differential Equations

Guide for Ph.D. Area Examination in Applied Mathematics

5 Numerical Integration & Dierentiation

Introduction to Mobile Robotics Compact Course on Linear Algebra. Wolfram Burgard, Cyrill Stachniss, Maren Bennewitz, Diego Tipaldi, Luciano Spinello

Laboratory #3: Linear Algebra. Contents. Grace Yung

Lorenz Equations. Lab 1. The Lorenz System

Numerical Linear Algebra

a11 a A = : a 21 a 22

Matlab Course. Anna Kristine Wåhlin. Department of Geophysics, University of Oslo. January Matlab Course p.1/??

Lecture 5b: Starting Matlab

Numpy. Luis Pedro Coelho. October 22, Programming for Scientists. Luis Pedro Coelho (Programming for Scientists) Numpy October 22, 2012 (1 / 26)

Solutions Preliminary Examination in Numerical Analysis January, 2017

x 3y 2z = 6 1.2) 2x 4y 3z = 8 3x + 6y + 8z = 5 x + 3y 2z + 5t = 4 1.5) 2x + 8y z + 9t = 9 3x + 5y 12z + 17t = 7

CONVERSION OF COORDINATES BETWEEN FRAMES

Linear Algebra (Review) Volker Tresp 2018

A = 3 B = A 1 1 matrix is the same as a number or scalar, 3 = [3].

Vector Fields and Solutions to Ordinary Differential Equations using Octave

The Gram-Schmidt Process 1

SIGNALS AND LINEAR SYSTEMS LABORATORY EELE Experiment (2) Introduction to MATLAB - Part (2) Prepared by:

Transcription:

A Glimpse at Scipy FOSSEE June 010 Abstract This document shows a glimpse of the features of Scipy that will be explored during this course. 1 Introduction SciPy is open-source software for mathematics, science, and engineering. SciPy (pronounced Sigh Pie ) is a collection of mathematical algorithms and convenience functions built on the Numpy extension for Python. It adds significant power to the interactive Python session by exposing the user to highlevel commands and classes for the manipulation and visualization of data. With SciPy, an interactive Python session becomes a data-processing and systemprototyping environment rivaling sytems such as Matlab, IDL, Octave, R-Lab, and Scilab. [1] 1.1 Sub-packages of Scipy SciPy is organized into subpackages covering different scientific computing domains. These are summarized in the table 1. 1. Use of Scipy in this course Following is a partial list of tasks we shall perform using Scipy, in this course. 1. Plotting 1. Matrix Operations Inverse Determinant 3. Solving Equations System of Linear equations 1 using pylab - see Appendix A 1

Subpackage cluster constants fftpack integrate interpolate io linalg maxentropy ndimage odr optimize signal sparse spatial special stats weave Table 1: Sub-packages available in Scipy Description Clustering algorithms Physical and mathematical constants Fast Fourier Transform routines Integration and ordinary differential equation solvers Interpolation and smoothing splines Input and Output Linear algebra Maximum entropy methods N-dimensional image processing Orthogonal distance regression Optimization and root-nding routines Signal processing Sparse matrices and associated routines Spatial data structures and algorithms Special functions Statistical distributions and functions C/C++ integration Polynomials Non-linear equations 4. Integration Quadrature ODEs A Glimpse of Scipy functions This section gives a brief overview of the tasks that are going to be performed using Scipy, in future classes of this course..1 Matrix Operations Let A be the matrix 1 3 5 5 1 3 8 To input A matrix into python, we do the following in ipython In []: A = array ([[1,3,5],[,5,1],[,3,8]]) ipython must be started with -pylab flag

.1.1 Inverse The inverse of a matrix A is the matrix B such that AB = I where I is the identity matrix consisting of ones down the main diagonal. Usually B is denoted B = A 1. In SciPy, the matrix inverse of matrix A is obtained using inv(a). In []: inv (A) Out []: array ([[ -1.48, 0.36, 0.88], [ 0.56, 0.08, -0.36], [ 0.16, -0.1, 0.04]]).1. Determinant The determinant of a square matrix A is denoted A. Suppose a ij are the elements of the matrix A and let M ij = A ij be the determinant of the matrix left by removing the i th row and j th column from A. Then for any row i A = j ( 1) i+j a ij M ij This is a recursive way to define the determinant where the base case is defined by accepting that the determinant of a 1 1 matrix is the only matrix element. In SciPy the determinant can be calculated with det. For example, the determinant of 1 3 5 A = 5 1 3 8 is A = 1 5 1 3 8 3 1 8 + 5 5 3 = 1(5 8 3 1) 3( 8 1) + 5( 3 5) = 5 In SciPy, this is computed as shown below In []: A = array ([[1, 3, 5], [, 5, 1], [, 3, 8]]) In []: det (A) Out []: -5.0. Solving Equations..1 Linear Equations Solving linear systems of equations is straightforward using the scipy command solve. This command expects an input matrix and a right-hand-side vector. 3

The solution vector is then computed. An option for entering a symmetrix matrix is offered which can speed up the processing when applicable. As an example, suppose it is desired to solve the following simultaneous equations: x + 3y + 5z = 10 (1) x + 5y + z = 8 () x + 3y + 8z = 3 (3) We could find the solution vector using a matrix inverse: x y z = 1 3 5 5 1 3 8 1 10 8 3 = 1 5 3 19 19 = 9.8 5.16 0.76 However, it is better to use the solve command which can be faster and more numerically stable. In this case it however gives the same answer. In []: A = array ([[1, 3, 5], [, 5, 1], [, 3, 8]]) In []: b = array ([[10], [8], [3]]) In []: dot ( inv (A), b) Out []: array ([[ -9.8], [ 5.16], [ 0.76]]) In []: solve (A,b) Out []: array ([[ -9.8], [ 5.16], [ 0.76]]).. Polynomials Solving a polynomial is straightforward in scipy using the roots command. It expects the coefficients of the polynomial in their decreasing order. For example, let s find the roots of x 3 x 1 x + 1 are, and. This is easy to see. x 3 x 1 x + 1 = 0 x (x ) 1 (x ) = 0 (x )(x 1 ) = 0 (x )(x 1 )(x + 1 ) = 0 4

We do it in scipy as shown below: In []: coeff = array ([1, -, -, 4]) In []: roots ( coeff )..3 Non-linear Equations To find a root of a set of non-linear equations, the command fsolve is needed. For example, the following example finds the roots of the single-variable transcendental equation and the set of non-linear equations x + cos (x) = 0, x 0 cos (x 1 ) = 4, (4) x 0 x 1 x 1 = 5 (5) The results are x = 1.099 and x 0 = 6.5041, x 1 = 0.9084. In []: def func (x):...: return x + * cos (x) In []: def func (x):...: out = [x [0]* cos (x [1]) - 4]...: out. append (x [1]* x [0] - x [1] - 5)...: return out In []: from scipy. optimize import fsolve In []: x0 = fsolve ( func, 0. 3) In []: print x0-1. 09866593 In []: x0 = fsolve ( func, [1, 1]) In []: print x0 [ 6. 50409711 0. 9084141].3 Integration.3.1 Quadrature The function quad is provided to integrate a function of one variable between two points. The points can be ± (± inf) to indicate infinite limits. For example, suppose you wish to integrate the expression e sin(x) in the interval [0, π], i.e. π 0 e sin(x) dx, it could be computed using 5

In []: def func (x):...: return exp ( sin (x)) In []: from scipy. integrate import quad In []: result = quad (func, 0, * pi) In []: print result ( 7. 9549651018457, 4. 05187416451979 e - 10).3. ODE We wish to solve an (a system of) Ordinary Differential Equation. For this purpose, we shall use odeint. As an illustration, let us solve the ODE dy = ky(l y) (6) dt L = 5000, k = 0.00003, y(0) = 50 We solve it in scipy as shown below. In []: from scipy. integrate import odeint In []: def f(y, t):...: k, L = 0.00003, 5000...: return k*y*(l-y)...: In []: t = linspace (0, 1, 60) In []: y0 = 50 In []: y = odeint (f, y0, t) Note: To solve a system of ODEs, we need to change the function to return the right hand side of all the equations and the system and the pass the required number of initial conditions to the odeint function. A Plotting using Pylab The following piece of code, produces the plot in Figure 1 using pylab[] in ipython 3 [3] In []: x = linspace (0, * pi, 50) In []: plot (x, sin (x)) In []: title ( Sine Curve between 0 and $\ pi$ ) In []: legend ([ sin (x) ]) 3 start ipython with -pylab flag 6

Figure 1: Sine curve References [1] Eric Jones and Travis Oliphant and Pearu Peterson and others, SciPy: Open source scientific tools for Python, 001, http://www.scipy.org/ [] John D. Hunter, Matplotlib: A D Graphics Environment, Computing in Science and Engineering, vol. 9, no. 3, pp. 90-95, May/June 007, doi:10.1109/mcse.007.55 [3] Fernando Perez, Brian E. Granger, IPython: A System for Interactive Scientific Computing, Computing in Science and Engineering, vol. 9, no. 3, pp. 1-9, May/June 007, doi:10.1109/mcse.007.53. 7