Python and Cython. a dynamic language. installing Cython factorization again. working with numpy



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

NaviCell Data Visualization Python API

Outline. multiple choice quiz bottom-up design. the modules main program: quiz.py namespaces in Python

Theorist HT Induc0on Course Lesson 1: Se6ng up your new computer (Mac OS X >= 10.6) As of 9/27/2012

MATH 423 Linear Algebra II Lecture 38: Generalized eigenvectors. Jordan canonical form (continued).

MatrixSSL Getting Started

Python Analysis / LATAnalysisScripts / Lightcurves / Fitting Issues

Quickly Creating Professional Looking Application Using wxpython, py2exe and InnoSetup. Miki Tebeka

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

Installing Bacula Client on Mac OS X Server

Python for Series 60 Platform

Python Programming: An Introduction to Computer Science

Main Bullet #1 Main Bullet #2 Main Bullet #3

Mobile Malware and Spyware: Working Through the Bugs. Detective Cindy Murphy

ANSA and μeta as a CAE Software Development Platform

1001ICT Introduction To Programming Lecture Notes

PYTHON Basics

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

Introduction to Python

CS 253: Intro to Systems Programming

Introduction to Python

Continuous Integration/Testing and why you should assume every change breaks your code

Writing standalone Qt & Python applications for Android

Load Balancing. computing a file with grayscales. granularity considerations static work load assignment with MPI

Python Programming: An Introduction to Computer Science

Optimizing and interfacing with Cython. Konrad HINSEN Centre de Biophysique Moléculaire (Orléans) and Synchrotron Soleil (St Aubin)

Using EDA Databases: Milkyway & OpenAccess

CRASH COURSE PYTHON. Het begint met een idee

Oracle Database Cloud Services OGh DBA & Middleware Day

Fully Automated Static Analysis of Fedora Packages

Hosted Connecting Steps Client Installation Instructions

NVIDIA CUDA GETTING STARTED GUIDE FOR MAC OS X

Code Estimation Tools Directions for a Services Engagement

CSE 308. Coding Conventions. Reference

Programming Languages

Shellshock. Oz Elisyan & Maxim Zavodchik

Slides from INF3331 lectures - web programming in Python

Android, Bluetooth and MIAC

Computer Science and Engineering Linux Cisco VPN Client Installation and Setup Guide

Installing (1.8.7) 9/2/ Installing jgrasp

RecoveryVault Express Client User Manual

Supported platforms & compilers Required software Where to download the packages Geant4 toolkit installation (release 9.6)

Apache HTTP Server. Implementation Guide. (Version 5.7) Copyright 2013 Deepnet Security Limited

Orthogonal Diagonalization of Symmetric Matrices

- Python. Get Python from and install on your machine. - py2exe. Get py2exe from

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

IEEEXTREME PROGRAMMING COMPETITION PROBLEM & INSTRUCTION BOOKLET #3

Online Backup Linux Client User Manual

GR A universal framework for visualization applications

Online Backup Client User Manual

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

[1] Diagonal factorization

Android: Setup Hello, World: Android Edition. due by noon ET on Wed 2/22. Ingredients.

GETTING FESTIVAL TO RUN ON WINDOWS XP WITH CYGWIN

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

SVNManager Installation. Documentation. Department of Public Health Erasmus MC University Medical Center

Android Setup Phase 2

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

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Online Backup Client User Manual

Analysis Programs DPDAK and DAWN

Ahsay Offsite Backup Server and Ahsay Replication Server

How do I Install and Configure MS Remote Desktop for the Haas Terminal Server on my Mac?

Bulk Downloader. Call Recording: Bulk Downloader

Inner Product Spaces and Orthogonality

Spotify Homepage Takeover

ALERT installation setup

Application Notes for Packaging and Deploying Avaya Communications Process Manager Sample SDK Web Application on a JBoss Application Server Issue 1.

RTI Quick Start Guide

u = [ 2 4 5] has one row with three components (a 3 v = [2 4 5] has three rows separated by semicolons (a 3 w = 2:5 generates the row vector w = [ 2 3

Computational Mathematics with Python

Mobile Labs Plugin for IBM Urban Code Deploy

CS197U: A Hands on Introduction to Unix

Cascading Pattern - How to quickly migrate Predictive Models (PMML) from SAS, R, Micro Strategies etc., onto Hadoop and deploy them at scale

Real-time Debugging using GDB Tracepoints and other Eclipse features

Tutorial on OpenCV for Android Setup

1. Product Information

Calling R Externally : from command line, Python, Java, and C++

Mobile Testing, Agile Methodologies & You!

CS 1133, LAB 2: FUNCTIONS AND TESTING

How to Install Multicraft on a VPS or Dedicated Server (Ubuntu bit)

How do I Install and Configure MS Remote Desktop for the Haas Terminal Server on my Mac?

Online Backup Client User Manual

Hello World. by Elliot Khazon

Online Backup Client User Manual Linux

CS 40 Computing for the Web

Chapter 13: Program Development and Programming Languages

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

Intel IoT Gateways: Publishing Data to an MQTT Broker Using Python

Magento Search Extension TECHNICAL DOCUMENTATION

B&K Precision 1785B, 1786B, 1787B, 1788 Power supply Python Library

AMS526: Numerical Analysis I (Numerical Linear Algebra)

SQMB '11 Automated Model Quality Rating of Embedded Systems

Linear Algebra Review. Vectors

SALEM COMMUNITY COLLEGE Carneys Point, New Jersey COURSE SYLLABUS COVER SHEET. Action Taken (Please Check One) New Course Initiated

RSA Authentication Manager 8.1 Virtual Appliance Getting Started

Transcription:

1 2 Python and 3 MCS 275 Lecture 39 Programming Tools and File Management Jan Verschelde, 19 April 2010

Python and 1 2 3

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

The Development Cycle compilation versus interpretation Traditional build cycle: 1 run the application 2 test the behavior of the code 3 stop the application 4 edit the program code 5 recompile the code 6 relink executable 7 goto step 1 This is the static language build cycle. Python eliminates steps 4 and 5. Recompilation and relinking is not a trivial task for systems with over 100,000 lines of code.

Python is a dynamic Language some advertisements Python may be a scripting language, but it scales well. Python is executable pseudocode." very readable, complexities involving memory addresses are hidden Python is OOP done right." compared to C++, the class mechanism in Python is much simpler The statements are quotes taken from Mark Lutz: Programming Python book. 2nd edition, O Reilly 2001.

Python is a dynamic Language some advertisements Python may be a scripting language, but it scales well. Python is executable pseudocode." very readable, complexities involving memory addresses are hidden Python is OOP done right." compared to C++, the class mechanism in Python is much simpler The statements are quotes taken from Mark Lutz: Programming Python book. 2nd edition, O Reilly 2001.

Python is a dynamic Language some advertisements Python may be a scripting language, but it scales well. Python is executable pseudocode." very readable, complexities involving memory addresses are hidden Python is OOP done right." compared to C++, the class mechanism in Python is much simpler The statements are quotes taken from Mark Lutz: Programming Python book. 2nd edition, O Reilly 2001.

Python and 1 2 3

prototyping: hybrid: delivery: Consider the slider: Rapid Application Development an answer to the software crisis all Python mixture and C/C++ all C/C++ prototyping hybrid delivery all Python mixture all C/C++ Identifying the bottlenecks in a working prototype leads to a gradual of modules in C/C++. Python complements languages like C/C++ and Java.

prototyping: hybrid: delivery: Consider the slider: Rapid Application Development an answer to the software crisis all Python mixture and C/C++ all C/C++ prototyping hybrid delivery all Python mixture all C/C++ Identifying the bottlenecks in a working prototype leads to a gradual of modules in C/C++. Python complements languages like C/C++ and Java.

prototyping: hybrid: delivery: Consider the slider: Rapid Application Development an answer to the software crisis all Python mixture and C/C++ all C/C++ prototyping hybrid delivery all Python mixture all C/C++ Identifying the bottlenecks in a working prototype leads to a gradual of modules in C/C++. Python complements languages like C/C++ and Java.

Sinking the Titanic the iceberg that sank so many large software projects... The Titanic refers to the iceberg. What we see is the exposed interface (the domain ), what is hidden are the system internals (the domain of C/C++). "Python provides a simple but powerful rapid language, along with the integration tools to apply it in realistic environments." Mark Lutz, page 1195 The idea of integration is not new, but the innovation is in providing the tools. Python is open source and cross platform.

Sinking the Titanic the iceberg that sank so many large software projects... The Titanic refers to the iceberg. What we see is the exposed interface (the domain ), what is hidden are the system internals (the domain of C/C++). "Python provides a simple but powerful rapid language, along with the integration tools to apply it in realistic environments." Mark Lutz, page 1195 The idea of integration is not new, but the innovation is in providing the tools. Python is open source and cross platform.

Sinking the Titanic the iceberg that sank so many large software projects... The Titanic refers to the iceberg. What we see is the exposed interface (the domain ), what is hidden are the system internals (the domain of C/C++). "Python provides a simple but powerful rapid language, along with the integration tools to apply it in realistic environments." Mark Lutz, page 1195 The idea of integration is not new, but the innovation is in providing the tools. Python is open source and cross platform.

Python and 1 2 3

Installing to install (on Mac OS X): download from http://cython.org and unpack tar ball or zip file in the -0.12 directory, at command prompt, type python setup.py install at the command prompt. Note that the gcc compiler must be installed. Trying the example on http://docs.cython.org/src/quickstart/build.html making the file hello.pyx making the file setup.py

Installing to install (on Mac OS X): download from http://cython.org and unpack tar ball or zip file in the -0.12 directory, at command prompt, type python setup.py install at the command prompt. Note that the gcc compiler must be installed. Trying the example on http://docs.cython.org/src/quickstart/build.html making the file hello.pyx making the file setup.py

Content of the file hello.pyx: def say_hello_to(name): print("hello %s!" % name) Content of setup.py: a hello world example from distutils.core import setup from distutils.extension import Extension from.distutils import build_ext ext_modules = [Extension("hello", ["hello.pyx"])] setup( name = Hello world app, cmdclass = { build_ext : build_ext}, ext_modules = ext_modules )

Content of the file hello.pyx: def say_hello_to(name): print("hello %s!" % name) Content of setup.py: a hello world example from distutils.core import setup from distutils.extension import Extension from.distutils import build_ext ext_modules = [Extension("hello", ["hello.pyx"])] setup( name = Hello world app, cmdclass = { build_ext : build_ext}, ext_modules = ext_modules )

At the command prompt, type Building and Using It $ python setup.py build_ext --inplace This creates a C program, a shared object file, and a directory build. Ready for use in a Python session: $ python Python 2.6.4 (r264:75821m, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" fo >>> from hello import say_hello_to >>> say_hello_to("me") hello me! >>>

At the command prompt, type Building and Using It $ python setup.py build_ext --inplace This creates a C program, a shared object file, and a directory build. Ready for use in a Python session: $ python Python 2.6.4 (r264:75821m, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" fo >>> from hello import say_hello_to >>> say_hello_to("me") hello me! >>>

At the command prompt, type Building and Using It $ python setup.py build_ext --inplace This creates a C program, a shared object file, and a directory build. Ready for use in a Python session: $ python Python 2.6.4 (r264:75821m, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" fo >>> from hello import say_hello_to >>> say_hello_to("me") hello me! >>>

Python and 1 2 3

In the file factors_of_number.pyx we have: def factors_of_number(n): """ Prints the factorization of the number n. """ d = 2; s = %d = % n while(d < n): (q,r) = divmod(n,d) if(r == 0): s = s + %d % d n = q; d = 2 else: d = d + 1 s = s + %d % n print s

a modified setup.py from distutils.core import setup from distutils.extension import Extension from.distutils import build_ext ext_modules = [Extension("factors_of_number", ["factors_of_number.pyx"])] setup( name = factors of number app, cmdclass = { build_ext : build_ext}, ext_modules = ext_modules )

Building and Using It To build, we again type: $ python setup build\_ext --inplace Then we can test: $ python Python 2.6.4 (r264:75821m, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" fo >>> from factors_of_number import factors_of_number >>> factors_of_number(1230) 1230 = 2 3 5 41 >>>

Python and 1 2 3

Let A be any n-by-n matrix. The vector v 0 is an eigenvector if Av = λv, for some λ, lambda is then an eigenvalue. A simple, yet powerful recipe to compute the eigenvector corresponding to the largest eigenvalue: y = x for i in range(0,m): y = A*x y = y/norm(y)

Let A be any n-by-n matrix. The vector v 0 is an eigenvector if Av = λv, for some λ, lambda is then an eigenvalue. A simple, yet powerful recipe to compute the eigenvector corresponding to the largest eigenvalue: y = x for i in range(0,m): y = A*x y = y/norm(y)

Python and 1 2 3

matrix-vector multiplication from numpy import * def MatrixVectorMultiply(A,x): """ Returns the product of A with x. """ n = A.shape[0] y = zeros((n,1),double) for i in range(0,a.shape[0]): for j in range(0,a.shape[1]): y[i] = y[i] + A[i,j]*x[j] return y

matrix-vector multiplication from numpy import * def MatrixVectorMultiply(A,x): """ Returns the product of A with x. """ n = A.shape[0] y = zeros((n,1),double) for i in range(0,a.shape[0]): for j in range(0,a.shape[1]): y[i] = y[i] + A[i,j]*x[j] return y

normalizing def Norm(x): """ Returns the 2-norm of the vector x. """ z = 0 for i in range(0,x.shape[0]): z = z + x[i]*x[i] return sqrt(z) def NormalizedVector(x): """ Returns x/norm(x) """ n = Norm(x) y = x for i in range(0,x.shape[0]): y[i] = y[i]/n return y

normalizing def Norm(x): """ Returns the 2-norm of the vector x. """ z = 0 for i in range(0,x.shape[0]): z = z + x[i]*x[i] return sqrt(z) def NormalizedVector(x): """ Returns x/norm(x) """ n = Norm(x) y = x for i in range(0,x.shape[0]): y[i] = y[i]/n return y

the power method def Power(A,x,n): """ Given a square numpy matrix in A, multiplies x n times with A. """ y = x for i in range(0,n): y = MatrixVectorMultiply(A,y) y = NormalizedVector(y) return y

the main program def main(): """ Test on the power method. """ n = input( give the dimension : ) a = random.normal(0,1,(n,n)) x = random.normal(0,1,(n,1)) m = input( give number of iterations : ) y = Power(a,x,m) print y ans = raw_input( See all eigenvectors? (y/n if ans == y : [L,V] = linalg.eig(a) print V

Summary + Assignments We returned to lecture 2.