Python For Education
Python For Education
Ajith Kumar B.P. Inter University Accelerator Centre New Delhi 110067 www.iuac.res.in
June 2010
Preface Mathematics, rightly viewed, possesses not only truth, but supreme beauty a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the gorgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show, wrote Bertrand Russell about the beauty of mathematics. All of us may not reach such higher planes, probably reserved for Russels and Ramanujans, but we also have beautiful curves and nice geometrical gures with intricate symmetries, like fractals, generated by seemingly dull equations. This book attempts to explore it using a simple tool, the Python programming language. I started using Python for the Phoenix project (www.iuac.res.in). Phoenix was driven in to Python by Pramode CE (pramode.net) and I followed. Writing this document was triggered by some of my friends who are teaching mathematics at Calicut University. In the rst chapter, a general introduction about computers and high level programming languages is given. Basics of Python language, Python modules for array and matrix manipulation, 2D and 3D data visualization, type-setting mathematical equations using latex and numerical methods in Python are covered in the subsequent chapters. Example programs are given for every topic discussed. This document is meant for those who want to try out these examples and modify them for better understanding. Huge amount of material is already available online on the topics covered, and the references to many resources on the Internet are given for the benet of the serious reader. This book comes with a live CD, containing a modied version of Ubuntu GNU/Linux operating system. You can boot any PC from this CD and practice Python. Click on the 'Learn by Coding' icon on the desktop to browse through a collection of Python programs, run any of them with a single click. programs. You can practice Python very easily by modifying and running these example
) This document is prepared using L X, a L T X front-end. It is distributed under the GNU Free E Y Documentation License (www.gnu.org). Feel free to make verbatim copies of this document and
distribute through any media. For the L X source les please contact the author. Y Ajith Kumar IUAC , New Delhi ajith at iuac.res.in
Contents
1 Introduction
1.1 1.2 1.2.1 1.2.2 1.2.3 Hardware Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2.1 1.2.3.1 1.2.3.2 1.3 1.4 1.5 1.6 The Command Terminal . . . . . . . . . . . . . . . . . . . . . . . The File-system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 7 7 7 7 9 9
Software components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10 10 10 11 11
2 Programming in Python
2.1 2.1.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
13 13 14 16 16 17 17 18 18 19 20 20 21 22 23 23 25 26 27 27 28 28 28
Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operators and their Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Strings 2.4.1 Python Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mutable and Immutable Types Input from the Keyboard 2.8.1 2.8.2 Iteration: while and for loops Syntax of 'for loops'
2.9
2.10 Modify loops : break and continue 2.12 Exercises 2.13 Functions
2.14 More on Strings and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.14.1 split and join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.14.2 Manipulating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.14.3 Copying Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29 29 30 30 32 32 33 34 36 38 40 40 42
2.15.1 Dierent ways to import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.15.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16.1 The pickle module 2.18 Exception Handling
2.17 Formatted Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.19 Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.20 Writing GUI Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.21 Object Oriented Programming in Python 2.21.1 Inheritance, reusing code 2.22 Exercises 2.21.2 A graphics example program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The NumPy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Arrays and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . arange(start, stop, step, dtype = None) . . . . . . . . . . . . . . . linspace(start, stop, number of elements) zeros(shape, datatype) random.random(shape) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
44 45 45 45 45 45 46 46 47 47 47 48 48 48 49 49
Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cross product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . dot product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Saving and Restoring Matrix inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Data visualization
4.1 4.1.1 4.1.2 4.1.3 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.3.4 4.4 4.5 4.6 4.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
51 52 53 54 54 54 55 55 56 56 57 58 59 59 60 61 62
Multiple plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Plotting mathematical functions Sine function and friends Trouble with Circle Parametric plots Astroid
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS
4.8 4.9
Meshgrids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.9.1 4.9.2 4.9.3 Surface Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Line Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wire-frame Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63 64 64 64 65 65 66
Document classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dividing the document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Typesetting Equations 5.5.1 Arrays and matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building blocks for typesetting equations
68
68 69 69 70 71 72 73 74 75 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 Numerical methods
6.1 6.1.1 6.2 6.3
Derivative of a function
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78
78 79 80 82 82 83 85 86 88 89 91 92 93 95 95 96 97 97
Ordinary Dierential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Euler method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Runge-Kutta method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function depending on the integral . . . . . . . . . . . . . . . . . . . . . . . Taylor's Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.4
6.5
System of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.6.1 Equation solving using matrix inversion Least Squares Fitting Interpolation 6.8.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Newton's polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
101
Appendix A
102
+D=FJAH
Introduction
Primary objective of this book is to explore the possibilities of using Python language as a tool for learning mathematics and science. The reader is not assumed to be familiar with computer programming. Ability to think logically is enough. Before getting into Python programming, we will briey explain some basic concepts and tools required. Computer is essentially an electronic device like a radio or a television. What makes it dierent from a radio or a TV is its ability to perform dierent kinds of tasks using the same electronic and mechanical components. This is achieved by making the electronic circuits exible enough to work according to a set of instructions. The electronic and mechanical parts of a computer are called the Hardware and the set of instructions is called Software (or computer program). Just by changing the Software, computer can perform vastly dierent kind of tasks. The instructions are stored in binary format using electronic switches.
and an Arithmetic and Logic Unit, ALU. The control unit brings the instructions stored in the main memory one by one and acts according to it. tween memory and input/output units. The ALU can perform arithmetic operations like addition, multiplication and logical operations like comparing two numbers. Memory stores the instructions and data, that is processed by the CPU. All types of information are stored as binary numbers. The smallest unit of memory is called a binary digit or Bit. It can have a value of zero or one. A group of eight bits are called a Byte. A computer has Main and Secondary types of memory. memory. Before processing, data and instructions are moved into the main CPU can select any memory Main memory is organized into words of one byte size.
location by using it's address. Main memory is made of semiconductor switches and is very fast. There are two types of Main Memory. Read Only Memory and Read/Write Memory. Read/Write Memory is also called Random Access Memory. All computers contains some programs in ROM which start running when you switch on the machine. Data and programs to be stored for future use are saved to Secondary memory, mainly devices like Hard disks, oppy disks, CDROM or magnetic tapes.
The cabinet that encloses most of the hardware is called CPU by some, mainly the computer vendors. They are not referring to the actual CPU chip.
CHAPTER 1.
INTRODUCTION
The Input devices are for feeding the input data into the computer.
common input device. Mouse, scanner etc. are other input devices. The processed data is displayed or printed using the output devices. The monitor screen and printer are the most common output devices.
Single user, single process systems like MS DOS. Only one process can run at a time. Such operating systems do not have much control over the application programs. Multi-tasking systems like MS Windows, where more than one processe can run at a time. Multi-user, multi-tasking systems like GNU/Linux, Unix etc. More than one person can use the computer at the same time. Real-time systems, mostly used in control applications, where the response time to any external input is maintained under specied limits.
CHAPTER 1.
INTRODUCTION
start a Terminal from the menu names Applications->Accessories->Terminal. Figure 1.1 shows a Terminal displaying the list of les in a directory (output of the command 'ls -l' , the -l option is for long listing). The command processor oers a host of features to make the interaction more comfortable. It keeps track of the history of commands and we can recall previous commands, modify and reuse them using the cursor keys. There is also a completion feature implemented using the Tab key that reduces typing. Use the tab key to complete command and lenames. To run our test directory, type
python h
hello.py
from
and then press the tab key to complete it. If there are more than
one le starting with 'h', you need to type more characters until the ambiguity is removed. Always use the up-cursor key to recall the previous commands and re-issue it. The commands given at the terminal are processed by a program called the commands are listed below.
shell.
(The version
now popular under GNU/Linux is called bash, the Bourne again shell). Some of the GNU/Linux
top : Shows the CPU and memory usage of all the processes started. cp lename lename : copies a le to another. mv : moves les from one folder to another, or rename a le. rm : deletes les or directories. man : display manual pages for a program. For example 'man bash' will give help on the bash shell. Press 'q' to come out of the help screen. info : A menu driven information system on various topics.
See the manual pages of 'mv', cp, 'rm' etc. to know more about them. Most of these commands are application programs, stored inside the folders /bin or /sbin, that the shell starts for you and displays their output inside the terminal window.
CHAPTER 1.
INTRODUCTION
group
root
who has permission to use all the resources. Ordinary user accounts, with a username and password, are created for everyone who wants to use the computer. In a multi-user operating system, like GNU/Linux, every user will have one directory inside which he can create sub-directories and les. This is called the 'home directory' of that user. Home directory of one user cannot be modied by another user. The operating system les are owned by
root.
fred
/home/fred
(g
1.2) and its contents. That is also called the user's home directory. Every le and directory has three types of permissions : read, write and execute. To view them use the 'ls -l ' command. The rst character of output line tells the type of the le. The next three characters show the
rwx
(read, write, execute) permissions for the owner of that le. Next three for the users belonging to the same group and the next three for other users. A hyphen character (-) means the permission corresponding to that eld is not granted. For example, the gure 1.1 shows a listing of ve les: 1. asecret.dat : read & write for the owner. No one else can even see it. 2. foo.png : rw for owner, but others can view the le. 3. hello.py : rwx for owner, others can view and execute. 4. share.tex : rw for owner and other members of the same group.
CHAPTER 1.
INTRODUCTION
10
5. xdata is a directory. Note that execute permission is required to view contents of a directory. The system of ownerships and permissions also protects the system from virus attacks . The
virus programs damage the system by modifying some application program. On a true multi-user system, for example GNU/Linux, the application program and other system les are owned by the root user and ordinary users have no permission to modify them. When a virus attempts to modify an application, it fails due to this permission and ownership scheme.
'gedit'
A high level programming language is a set of well dened notations which is capable of expressing algorithms.
The notations selected must be suitable for the problems to be solved. In general a high level language should have the following features. 1. Ability to represent dierent data types like characters, integers and real numbers. In addition to this it should also support a collection of similar objects like character strings, arrays etc. 2. Arithmetic and Logical operators that acts on the supported data types. 3. Control ow structures for decision making, branching, looping etc.
Do not expect this from the MS Windows system. Even though it allows to create users, any user ( by running programs or viruses) is allowed to modify the system les. This may be because it grew from a single process system like MSDOS and still keeps that legacy.
CHAPTER 1.
INTRODUCTION
11
4. A set of syntax rules that precisely specify the combination of words and symbols permissible in the language. 5. A set of semantic rules that assigns a single, precise and unambiguous meaning to each syntactically correct statement. Program text written in a high level language is often called the Source Code. It is then translated into the machine language by using translator programs. There are two types of translator programs, the Interpreter and the Compiler.
Interpreter reads the high level language program line by line, translates and executes it. Compilers convert the entire program in to machine language and stores it to a le which can be executed.
High level languages make the programming job easier. We can write programs that are machine independent. For the same program dierent compilers can produce machine language code to run on dierent types of computers and operating systems. BASIC, COBOL, FORTRAN, C, C++, Python etc. are some of the popular high level languages, each of them having advantages in The dierent elds.
Learning how to develop an algorithm is dierent from learning a programming language.Learning a programming language means learning the notations, syntax and semantic rules of that language. Best way to do this
algorithm can be expressed in any suitable high level language. is by writing small programs with very simple algorithms. algorithms.
To write any useful program for solving a problem, one has to develop an algorithm.
notations and rules of the language one can start writing programs to implement more complicated
) GNU/Linux operating system, OpenOce.org oce suite, L T X typesetting system, Apache web E server, GIMP image editor, GNU compiler collection, Python interpreter etc. are some of the
popular examples. For more information refer to www.gnu.org website.
1.6 Exercises
1. What are the basic hardware components of a computer. 2. Name the working directory of a user named 'ramu' under GNU/Linux. 3. What is the command to list the le names inside a directory (folder). 4. What is the command under GNU/Linux to create a new folder. 5. What is the command to change the working directory.
CHAPTER 1.
INTRODUCTION
12
6. Can we install more than one operating systems on a single hard disk. 7. Name two most popular Desktop Environments for GNU/Linux. 8. How to open a command window from the main menu of Ubuntu GNU/Linux. 9. Explain the le ownership and permission scheme of GNU/Linux.
+D=FJAH
Programming in Python
Python is a simple, high level language with a clean syntax. It oers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code. To know more visit the Python website.
Text Editor
and the
Terminal
from the
this method only for viewing the results of single Python statements, for example to use Python as a calculator. It could be confusing when you start writing larger programs, having looping and conditional statements. The preferred way is to enter your source code in a text editor, save it to a le (with .py extension) and execute it from the command terminal using Python. A screen-shot of the Desktop with Text Editor and Terminal is shown in gure 2.1. In this document, we will start writing small programs showing the essential elements of the language without going into the details. The reader is expected to run these example programs and also to modify them in dierent ways.It is like learning to drive a car, you master it by practicing. Let us start with a program to display the words
Hello World
the customary 'hello world' program. There is another version that prints 'Goodbye cruel world', probably invented by those who give up at this point. The Python 'hello world' program is shown below.
http://www.python.org/ http://docs.python.org/tutorial/ This document, example programs and a GUI program to browse through them are at http://www.iuac.res.in/phoenix
13
CHAPTER 2.
PROGRAMMING IN PYTHON
14
Example. hello.py
print 'Hello World'
This should be entered into a text le using any text editor. On a GNU/Linux system you may use the text editor like 'gedit' to create the source le, save it as the $ prompt) type: $ python hello.py
hello.py .
the Python Interpreter to execute the new program. For that, open a command terminal and (at
rst.py
y = f loat(3).
shows how to dene variables of dierent data types. It also shows how
For quick practicing, boot from the CD provided with this book and click on the learn-by-coding icon to browse through the example programs given in this book. The browser allows you to run any of them with a single click, modify and save the modied versions.
CHAPTER 2.
PROGRAMMING IN PYTHON
15
Example: rst.py
' A multi-line comment, within a pair of three single quotes. In a line, anything after a # sign is also a comment ' x = 10 print x, type(x) # print x and its type x = 10.4 print x, type(x) x = 3 + 4j print x, type(x) x = 'I am a String ' print x, type(x)
The output of the program is shown below. Note that the type of the variable the execution of the program, depending on the value assigned to it.
changes during
10 <type 'int'> 10.4 <type 'float'> (3+4j) <type 'complex'> I am a String <type 'str'>
The program treats the variables like humans treat labelled envelopes. We can pick an envelope, write some name on it and keep something inside it for future use. In a similar manner the program creates a variable, gives it a name and keeps some value inside it, to be used in subsequent steps. So far we have used four data types of Python: int, oat, complex and str. To become familiar with them, you may write simple programs performing arithmetic and logical operations using them.
Example: oper.py
x = 2 y = 4 print x + y * 2 s = 'Hello ' print s + s print 3 * s print x == y print y == 2 * x
Running the program
oper.py
==
CHAPTER 2.
PROGRAMMING IN PYTHON
16
Operator or and not x in, not in <, <=, >, >=, !=, == | ^ & < <, > > +,*, /, % +x , -x ~ ** x[index]
Description Boolean OR Boolean AND Boolean NOT Membership tests Comparisons Bitwise OR Bitwise XOR Bitwise AND Bitwise Shifting Add, Subtract Multiply, divide, reminder Positive, Negative Bitwise NOT Exponentiation Subscription
Expression 0 or 4 3 and 0 not 0 3 in [2.2,3,12] 2>3 1|2 1^5 1&3 1 << 3 6-4 5%2 -5*2 ~1 2 ** 3 a='abcd' ; a[1]
2+3*4,
is the
addition done rst or the multiplication? According to elementary arithmetics, the multiplication It means that the multiplication operator has higher precedence than the addition operator. If you want the addition to be done rst, enforce it by using parenthesis like
(2 + 3) 4.
evaluation.
string.py.
Example: string.py
s = 'hello world' print s[0] print s[1] print s[-1] # print first element, h # print e # will print the last character
Example: string2.py
a = 'hello'+'world' print a b = 'ha' * 3
CHAPTER 2.
PROGRAMMING IN PYTHON
17
2.4.1 Slicing
Part of a String can be extracted using the slicing operation. It can be considered as a modied form of indexing a single character. Indexing using
s[a : b]
extracts elements
s[a]
to
s[b 1].
We
can skip one of the indices. If the index on the left side of the colon is skipped, slicing starts from the rst element and if the index on right side is skipped, slicing ends with the last element.
Example: slice.py
a = 'hello world' print a[3:5] print a[6:] print a[:5] The reader can guess the nature of slicing operation from the output of this code, shown below.
slice.py
a[6 : 20]
to demonstrate it.
list1.py
Example: list1.py
a = [2.3, 3.5, 234] # make a list print a[0] a[1] = 'haha' # Change an element print a
The output is shown below
!.
! The oating point number 2.3 showing as 2.2999999999999998 is interesting. This is the very nature of oting point representation of numbers, nothing to do with Python. With the precision we are using, the error in representing 2.3 is around 2.0e-16. This becomes a concern in operations like inversion of big matrices.
CHAPTER 2.
PROGRAMMING IN PYTHON
18
2.3 [2.2999999999999998, 'haha', 234] Lists can be sliced in a manner similar to that if Strings. List addition and multiplication are demonstrated by the following example. We can also have another list as an element of a list.
Example: list2.py
a = [1,2] print a * 2 print a + [3,4] b = [10, 20, a] print b
The output of this program is shown below. [1, 2, 1, 2] [1, 2, 3, 4] [10, 20, [1, 2] ]
We can change the value of an element in a list, add new elements to it and remove any existing element. This is not possible with String type. Uncomment the last line of third.py and run it to
clarify this point.
Example: third.py
s = [3, 3.5, 234] s[2] = 'haha' print s x = 'myname' #x[1] = 2 # make a list # Change an element # String type # uncomment to get ERROR
The List data type is very exible, an element of a list can be another list. We will be using lists extensively in the coming chapters. Tuple is another data type similar to List, except that it is immutable. List is dened inside square brackets, tuple is dened in a similar manner but inside parenthesis, like
raw_input()
input()
for numeric
"
Example: kin1.py
" Functions will be introduced later. For the time being, understand that it is an isolated piece of code, called from the main program with some input arguments and returns some output.
CHAPTER 2.
PROGRAMMING IN PYTHON
19
x = input('Enter an integer ') y = input('Enter one more ') print 'The sum is ', x + y s = raw_input('Enter a String ') print 'You entered ', s
It is also possible to read more than one variable using a single input() statement. data read using raw_input() may be converted into valid characters. characters than
integer
or
oat
String
type
by 2 before printing. Multiplying a String by 2 prints it twice. If the String contains any other
Example: kin2.py
x,y = input('Enter x and y separated by comma ') print 'The sum is ', x + y s = raw_input('Enter a decimal number ') a = float(s) print s * 2 # prints string twice print a * 2 # converted value times 2
We have learned about the basic data types of Python and how to get input data from the keyboard. This is enough to try some simple problems and algorithms to solve them.
Example:
area.py
Example:
badtable.py
1 2 3 4 5
* * * * *
8 8 8 8 8
CHAPTER 2.
PROGRAMMING IN PYTHON
20
Well, we are stopping here and looking for a better way to do this job. The solution is to use the statement) is executed.
while
loop of Python.
while
is evaluated, and if it is True, the body of the while loop (the indented lines below the while The process is repeated until the condition becomes false. We should have some statement inside the body of the loop that will make this condition false after few iterations. Otherwise the program will run in an innite loop and you will have to press Control-C to terminate it. The program loop
Example:
table.py
while
accepted as forms of indentation in Python. Mixing spaces and tabs can create bugs that are hard to nd, since the text editor does not show the dierence. There should not be any extra white
The line before any indented block must end with a colon character. for for
Example:
forloop.py
a = 'my name' for ch in a: # ch is the loop variable print ch b = ['hello', 3.4, 2345, 3+5j] for item in b: print item
For constructing for loops that executes a xed number of times, we can create a list using the range() function and run the for loop over that.
CHAPTER 2.
PROGRAMMING IN PYTHON
21
Example:
forloop2.py
[0, 1, 2, 3, 4].
It is possible to specify
the starting point and increment as arguments in the form range(start, end+1, step). The following
Example:
forloop3.py
Example:
forloop4.py
if
if, elif
and
else keywords, as shown in the next example. elif and else must be kept the same.
Example:
compare.py
Example:
big.py
x = input('Enter a number ') if x > 10: print 'Bigger Number' elif x < 10: print 'Smaller Number' else: print 'Same Number'
CHAPTER 2.
PROGRAMMING IN PYTHON
22
x > 10 and x < 15 can be expressed in a short form, like 10 < x < 15. while and if keywords in the same program. Note the level of indentation when the if statement comes inside the while loop. Remember that, the if statement must be aligned with the corresponding elif and else.
The statement The next example uses
Example:
big2.py
x = 1 while x < 11: if x < 5: print 'Small ', x else: print 'Big ', x x = x + 1 print 'Done'
break
The
continue
statement is used to skip the rest of the block and go to the beginning again. Both are demonstrated
big3.py
Example:
big3.py
x = 1 while x < 10: if x < 3: print 'skipping work', x x = x + 1 continue print x if x == 4: print 'Enough of work' break x = x + 1 print 'Done'
The output of big3.py is listed below. skipping work 1 skipping work 2 3 4 Enough of work Done Now let us write a program to nd out the largest positive number entered by the user. The algorithm works in the following manner. To start with, we assume that the largest number is zero. After reading a number, the program checks whether it is bigger than the current value of the largest number. If so the value of the largest number is replaced with the current number. The program terminates when the user enters zero. also. Modify max.py to work with negative numbers
CHAPTER 2.
PROGRAMMING IN PYTHON
23
Example: max.py
max = 0 while True: # Infinite loop x = input('Enter a number ') if x > max: max = x if x == 0: print max break
longname = 'I am so long and will \ not fit in a single line' print longname
2.12 Exercises
We have now covered the minimum essentials of Python; dening variables, performing arithmetic and logical operations on them and the control ow statements. These are sucient for handling most of the programming tasks. It would be better to get a grip of it before proceeding further, by writing some code. 1. Modify the expression
print 5+3*2
to get a result of 16
print type(4.5)
3. Print all even numbers upto 30, suxed by a * if the number is a multiple of 6. (hint: use % operator) 4. Write Python code to remove the last two characters of 'I am a long string' by slicing, without counting the characters. (hint: use negative indexing) 5. s = '012345' . (a) Slice it to remove last two elements (b) remove rst two element. 6. a = [1,2,3,4,5]. Use Slicing and multiplication to generate [2,3,4,2,3,4] from it.
CHAPTER 2.
PROGRAMMING IN PYTHON
24
7. Compare the results of 5/2, 5.0/2 and 2.0/3. 8. Print the following pattern using a while loop + ++ +++ ++++ 9. Write a program to read inputs like 8A, 10C etc. and print the integer and alphabet parts separately. 10. Write code to print a number in the binary format (for example 5 will be printed as 101) 11. Write code to print all perfect cubes upto 2000. 12. Write a Python program to print the multiplication table of 5. 13. Write a program to nd the volume of a box with sides 3,4 and 5 inches in 2.54 cm) 14. Write a program to nd the percentage of volume occupied by a sphere of diameter in a cube of side
cm3 (
1 inch =
tted
r.
Read
15. Write a Python program to calculate the area of a circle. 16. Write a program to divide an integer by another without using the / operator. (hint: use operator) 17. Count the number of times the character 'a' appears in a String read from the keyboard. Keep on prompting for the string until there is no 'a' in the input. 18. Create an integer division machine that will ask the user for two numbers then divide and give the result. The program should give the result in two parts: the whole number result and the remainder. Example: If a user enters 11 / 4, the computer should give the result 2 and remainder 3. 19. Modify the previous program to avoid division by zero error. 20. Create an adding machine that will keep on asking the user for numbers, add them together and show the total after each step. Terminate when user enters a zero. 21. Modify the adding machine to use raw_input() and check for errors like user entering invalid characters. 22. Create a script that will convert Celsius to Fahrenheit. The program should ask the users to enter the temperature in Celsius and should print out the temperature in Fahrenheit, using f = 9 c + 32. 5 23. Write a program to convert Fahrenheit to Celsius. 24. Create a script that uses a variable and will write 20 times "I will not talk in class." Make each sentence on a separate line. 25. Dene
2 + 5j
and
2 5j
dening the real and imaginary parts separately and using the multiplication formula.
CHAPTER 2.
PROGRAMMING IN PYTHON
25
26. Write the multiplication table of 12 using while loop. 27. Write the multiplication table of a number, from the user, using for loop. 28. Print the powers of 2 up to 1024 using a for loop. (only two lines of code) 29. Dene the list a = [123, 12.4, 'haha', 3.4] a) print all members using a for loop b) print the oat type members ( use type() function) c) insert a member after 12.4 d) append more members 30. Make a list containing 10 members using a for loop. 31. Generate multiplication table of 5 with two lines of Python code. (hint: range function) 32. Write a program to nd the sum of ve numbers read from the keyboard. 33. Write a program to read numbers from the keyboard until their sum exceeds 200. Modify the program to ignore numbers greater than 99. 34. Write a Python function to calculate the GCD of two numbers 35. Write a Python program to nd annual compound interest. Get P,N and R from user
2.13 Functions
Large programs need to be divided into small logical units. A function is generally an isolated unit of code that has a name and does a well dened job. A function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. Python allows you to dene functions using the
def
variables as arguments, which receive their values from the calling program. In the example shown below, function arguments (a and b) get the values 3 and 4 respectively from the caller. One can specify more than one variables in the return statement, separated by commas. The function will return a tuple containing those variables. Some functions may not have any arguments, but while calling them we need to use an empty parenthesis, otherwise the function will not be invoked. If there is no return statement, a None is returned to the caller.
Example func.py
def sum(a,b): # a trivial function return a + b print sum(3, 4)
The function
factorial.py
call. Try to understand the working of this by inserting print statements inside the function.
Example factor.py
CHAPTER 2.
PROGRAMMING IN PYTHON
26
def factorial(n): # a recursive function if n == 0: return 1 else: return n * factorial(n-1) print factorial(10)
Example banocci.py
def fib(n): # print Fibonacci series up to n a, b = 0, 1 while b < n: print b a, b = b, a+b print fib(30)
Runing
banocci.py
will print
a, b = b, a + b
scope.py
demonstrates
Example scope.py
def change(x): counter = x counter = 10 change(5) print counter
The program will print 10 and not 5. The two variables, both named counter, are not related to each other. In some cases, it may be desirable to allow the function to change some external variable. This can be achieved by using the
global
keyword, as shown in
global.py.
Example global.py
def change(x): global counter # use the global variable counter = x counter = 10 change(5) print counter
The program will now print 5. Functions with global variables should be used carefully to avoid inadvertent side eects.
CHAPTER 2.
PROGRAMMING IN PYTHON
27
Example power.py
def power(mant, exp = 2.0): return mant ** exp print power(5., 3) print power(4.) print power() # prints 16 # Gives Error
Example named.py
def power(mant = 10.0, exp = 2.0): return mant ** exp print power(5., 3) print power(4.) print power(exp=3) # prints 16 # mant gets 10.0, prints 1000
Example: stringhelp.py
s = 'hello world' print len(s) print s.upper() help(str) # press q to exit help
Python is an object oriented language and all variables are objects belonging to various classes. The method upper() (a function belonging to a class is called a method) is invoked using the dot operator. All we need to know at this stage is that there are several methods that can be used for manipulating objects and they can be invoked like:
variable_name.method_name().
CHAPTER 2.
PROGRAMMING IN PYTHON
28
Example: split.py
s = 'I am a long string' print s.split() a = 'abc.abc.abc' aa = a.split('.') print aa mm = '+'.join(aa) print mm
The result is shown below ['I', 'am', 'a', 'long', 'string'] ['abc', 'abc', 'abc'] 'abc+abc+abc' The List of strings generated by split is joined using '+' character, resulting in the last line of the output.
list3.py
Example: list3.py
a = [] # make an empty list a.append(3) # Add an element a.insert(0,2.5) # insert 2.5 as first element print a, a[0] print len(a)
The output is shown below. [2.5, 3] 2.5 2
b=a
from list a, it just make a reference to a. The following example will clarify this point. To make a
copy
module.
Example: list_copy.py
CHAPTER 2.
PROGRAMMING IN PYTHON
29
a = [1,2,3,4] print a b = a # b refers to a print a == b # True b[0] = 5 # Modifies a[0] print a import copy c = copy.copy(a) c[1] = 100 print a is c # is False print a, c
The output is shown below. [1, 2, 3, 4] True [5, 2, 3, 4] False [5, 2, 3, 4] [5, 100, 3, 4]
One of the major advantages of Python is the availability of libraries for various applications like The standard library distributed with Python itself has a large number of modules: time, random, pickle, system etc. are some of them. The site http://docs.python.org/library/ has the complete reference. Modules are loaded by using the
import
import
is explained
Example mathsin2.py
import math as m print m.sin(0.5) # Give another name for math # Refer by the new name
We can also import the functions to behave like local (like the ones within our source le) function, as shown below. The character * is a wild card for importing all the functions.
# While giving names to your Python programs, make sure that you are not directly or indirectly importing any Python module having same name. For example, if you create a program named math.py and keep it in your working directory, the import math statement from any other program started from that directory will try to import your le named math.py and give error. If you ever do that by mistake, delete all the les with .pyc extension from your directory.
CHAPTER 2.
PROGRAMMING IN PYTHON
30
Example mathlocal.py
from math import sin # sin is imported as local print sin(0.5)
Example mathlocal2.py
from math import * print sin(0.5) # import everything from math
In the third and fourth cases, we need not type the module name every time. But there could be trouble if two modules imported contains a function with same name. In the program the
conict.py, numpy is capable of handling a list argument. After importing math.py, line 4, the sin function from math module replaces the one from numpy. The error occurs because the sin() from math can accept only a numeric type argument.
sin()
from
Example conict.py
from numpy import * x = [0.1, 0.2, 0.3] print sin(x) from math import * print sin(x)
# numpy's sin can handle lists # sin of math becomes effective # will give ERROR
2.15.2 Packages
Packages are used for organizing multiple modules. The module name A.B designates a submodule named B in a package named A. The concept is demonstrated using the packages Numpy Scipy.
$ and
Example submodule.py
import numpy print numpy.random.normal() import scipy.special print scipy.special.j0(.1)
In this example the package
random is a module inside the package NumPy. Similarly special is a module inside Scipy. We use both of them in the package.module.function() format. But there is
some dierence. In the case of Numpy, the random module is loaded by default, importing scipy does not import the module special by default. This behavior can be dened while writing the Package and it is upto the package author.
Example wle.py
$ NumPy
CHAPTER 2.
PROGRAMMING IN PYTHON
31
Example rle.py
f = open('test.dat' , 'r') print f.read() f.close()
Note that the data written/read are character strings. read() function can also be used to read a xed number of characters, as shown below.
Example rle2.py
f = open('test.dat' , 'r') print f.read(7) # get first seven characters print f.read() # get the remaining ones f.close()
Now we will examine how to read a text data from a le and convert it into numeric type. First we will create a le with a column of numbers.
Example wle2.py
f = open('data.dat' , 'w') for k in range(1,4): s = '%3d\n' %(k) f.write(s) f.close()
The contents of the le created will look like this. 1 2 3 Now we write a program to read this le, line by line, and convert the string type data to integer type, and print the numbers.
Example rle3.py
f = open('data.dat' , 'r') while 1: # infinite loop s = f.readline() if s == : # Empty string means end of file break # terminate the loop m = int(s) # Convert to integer print m * 5 f.close()
% This will give error if there is a blank line in the data le. This can be corrected by changing the comparison statement to if len(s) < 1: , so that the processing stops at a blank line. Modify the code to skip a blank line instead of exiting (hint: use continue ).
CHAPTER 2.
PROGRAMMING IN PYTHON
32
Example pickledump.py
import pickle f = open('test.pck' , 'w') pickle.dump(12.3, f) # write a float type f.close()
Now write another program to read it back from the le and check the data type.
Example pickleload.py
import pickle f = open('test.pck' , 'r') x = pickle.load(f) print x , type(x) # check the type of data read f.close()
Example: format.py
a = 2.0 /3 # 2/3 will print zero print a print 'a = %5.3f' %(a) # upto 3 decimal places
Data can be printed in various formats. etc. The following example shows some of the features available with formatted printing. The conversion types are summarized in the following table. There are several ags that can be used to modify the formatting, like justication, lling
Example: format2.py
a = 'justify as you like' print '%30s'%a # right justified print '%-30s'%a # minus sign for left justification for k in range(1,11): # A good looking table print '5 x %2d = %2d' %(k, k*5)
CHAPTER 2.
PROGRAMMING IN PYTHON
33
Conversion d , i f e x o s 0d
Conversion signed Integer oating point decimal oating point exponential hexadecimal octal string modied 'd'
The output of
format2.py
is given below.
try
and
except
Example: except.py
x = input('Enter a number ') try: print 10.0/x except: print 'Division by zero not allowed'
If any exception occurs while running the code inside the try block, the code inside the except block is executed. The following program implements error checking on input using exceptions.
Example: except2.py
def get_number(): while 1: try: a = raw_input('Enter a number ') x = atof(a)
CHAPTER 2.
PROGRAMMING IN PYTHON
34
Pen
on a two di-
mensional screen to generate graphical patterns. The Pen can be controlled using functions like forward(distance), backward(distance), right(angle), left(angle) etc. . Run the program turtle1.py to understand the functions. This section is included only for those who want to practice programming in a more interesting manner.
&
Example turtle1.py
from turtle import * a = Pen() # Creates a turtle in a window a.forward(50) a.left(45) a.backward(50) a.right(45) a.forward(50) a.circle(10) a.up() a.forward(50) a.down() a.color('red') a.right(90) a.forward(50) raw_input('Press Enter')
Example turtle2.py
from turtle import * a = Pen() for k in range(4): a.forward(50) a.left(90) a.circle(25) raw_input() # Wait for Key press
& http://docs.python.org/library/turtle.html
CHAPTER 2.
PROGRAMMING IN PYTHON
35
Outputs of the program turtle2.py and turtle3.py are shown in gure 2.2. programs like this to generate more complex patterns.
Example turtle3.py
from turtle import * def draw_rectangle(): for k in range(4): a.forward(50) a.left(90) a = Pen() for k in range(36): draw_rectangle() a.left(10) raw_input()
The program turtle3.py creates a pattern by drwaing 36 squares, each drawn tilted by
10
from
the previous one. The program turtle4.py generates the fractal image as shown in gure2.2(c).
Example turtle4.py
from turtle import * def f(length, depth): if depth == 0: forward(length) else: f(length/3, depth-1) right(60) f(length/3, depth-1) left(120) f(length/3, depth-1) right(60) f(length/3, depth-1) f(500, 4) raw_input('Press any Key')
CHAPTER 2.
PROGRAMMING IN PYTHON
36
'
mainloop.
After that
tkmain.py
is the smallest GUI program one can write using Tkinter. The output of tkmain.py is
shown in gure2.3(a).
Example tkmain.py
from Tkinter import * root = Tk() root.mainloop()
Example tklabel.py
from Tkinter import * root = Tk() w = Label(root, text="Hello, world") w.pack() root.mainloop()
The program tklabel.py will generate the output as shown in gure 2.3(b). Terminate the program by clicking on the x displayed at the top right corner. In this example, we used a Label widget to display some text. The next example will show how to use a Button widget.
CHAPTER 2.
PROGRAMMING IN PYTHON
37
A Button widget can have a callback function, hello() in this case, that gets executed when the user clicks on the Button. The program will display a Button on the screen. Every time you click on it, the function
hello
Example tkbutton.py
from Tkinter import * def hello(): print 'hello world' w = Tk() # Creates the main Graphics window b = Button(w, text = 'Click Me', command = hello) b.pack() w.mainloop()
Canvas is another commonly used widget. Canvas is a drawing area on which we can draw elements like line, arc, rectangle, text etc. The program tkcanvas.py creates a Canvas widget and binds the <Button-1> event to the function draw(). When left mouse button is pressed, a small rectangle are drawn at the cursor position. The output of the program is shown in gure 2.4(b).
Example tkcanvas.py
from Tkinter import * def draw(event): c.create_rectangle(event.x, \ event.y, event.x+5, event.y+5) w = Tk() c = Canvas(w, width = 300, height = 200) c.pack() c.bind("<Button-1>", draw) w.mainloop()
The next program is a modication of tkcanvas.py. The right mouse-button is bound to remove(). Every time a rectangle is drawn, its return value is added to a list, a global variable, and this list is used for removing the rectangles when right button is pressed.
CHAPTER 2.
PROGRAMMING IN PYTHON
38
Example tkcanvas2.py
from Tkinter import * recs = [] # List keeping track of the rectangles def remove(event): global recs if len(recs) > 0: c.delete(recs[0]) # delete from Canvas recs.pop(0) # delete first item from list def draw(event): global recs r = c.create_rectangle(event.x, \ event.y, event.x+5, event.y+5) recs.append(r) w = Tk() c = Canvas(w, width = 300, height = 200) c.pack() c.bind("<Button-1>", draw) c.bind("<Button-3>", remove) w.mainloop()
and their interactions to design computer programs. Python is an object oriented language but it does not force you to make all programs object oriented and there is no advantage in making small programs object oriented. In this section, we will discuss some features of OOP. Before going to the new concepts, let us recollect some of the things we have learned. We have seen that the eect of operators on dierent data types is predened. For example
6 and 2 abc
23
results in
results in
abcabc .
by the language designers. One of the key features of OOP is the ability to create user dened data types. The user will specify, how the new data type will behave under the existing operators like add, subtract etc. and also dene methods that will belong to the new data type. We will design a new data type using the class keyword and dene the behavior of it. In the program point.py, we dene a class named Point. The variables xpos and ypos are members of Point. The __init__() function is executed whenever we create an instance of this class, the member variables are initialized by this function. The way in which an object belonging to this class is printed is decided by the __str__ function. We also have dened the behavior of add (+) and subtract (-) operators for this class. The + operator returns a new Point by adding the x and y coordinates of two Points. Subtracting a Point from another gives the distance between the two. The method dist() returns the distance of a Point object from the origin. We have not dened the behavior of Point under copy operation. We can use the copy module of Python to copy objects.
Example point.py
class Point: '
CHAPTER 2.
PROGRAMMING IN PYTHON
39
This is documentation comment. help(Point) will display this. ' def __init__(self, x=0, y=0): self.xpos = x self.ypos = y def __str__(self): # overloads print return 'Point at (%f,%f)'%(self.xpos, self.ypos) def __add__(self, other): #overloads + xpos = self.xpos + other.xpos ypos = self.ypos + other.ypos return Point(xpos,ypos) def __sub__(self, other): #overloads import math dx = self.xpos - other.xpos dy = self.ypos - other.ypos return math.sqrt(dx**2+dy**2) def dist(self): import math return math.sqrt(self.xpos**2 + self.ypos**2)
The program point1.py imports the le point.py to use the class Point dened inside it to demonstrate the properties of the class. A self. is prexed when a method refers to member of the same object. It refers to the variable used for invoking the method.
Example point1.py
from point import * origin = Point() print origin p1 = Point(4,4) p2 = Point(8,7) print p1 print p2 print p1 + p2 print p1 - p2 print p1.dist()
Output of program point1.py is shown below. Point at (0.000000,0.000000) Point at (4.000000,4.000000) Point at (8.000000,7.000000) Point at (12.000000,11.000000) 5.0 5.65685424949 In this section, we have demonstrated the OO concepts like class, object and operator overloading.
CHAPTER 2.
PROGRAMMING IN PYTHON
40
Example cpoint.py
class colPoint(Point): #colPoint inherits Point color = 'black' def __init__(self,x=0,y=0,col='black'): Point.__init__(self,x,y) self.color = col def __str__(self): return '%s colored Point at (%f,%f)'% \ (self.color,self.xpos, self.ypos)
Example point2.py
from cpoint import * p1 = Point(5,5) rp1 = colPoint(2,2,'red') print p1 print rp1 print rp1 + p1 print rp1.dist()
The output of point2.py is listed below. Point at (5.000000,5.000000) red colored Point at (2.000000,2.000000) Point at (7.000000,7.000000) 2.82842712475 For a detailed explanation on the object oriented features of Python, refer to chapters 13, 14 and 15 of the online book http://openbookproject.net//thinkCSpy/
conversion is performed internally. The method named line() accepts a list of xy coordinates. The
tkplot_class.py
Example tkplot_class.py
from Tkinter import * from math import * class disp: def __init__(self, parent, width=400., height=200.):
CHAPTER 2.
PROGRAMMING IN PYTHON
41
self.parent = parent self.SCX = width self.SCY = height self.border = 1 self.canvas = Canvas(parent, width=width, height=height) self.canvas.pack(side = LEFT) self.setWorld(0 , 0, self.SCX, self.SCY) # scale factors def setWorld(self, x1, y1, x2, y2): self.xmin = float(x1) self.ymin = float(y1) self.xmax = float(x2) self.ymax = float(y2) self.xscale = (self.xmax - self.xmin) / (self.SCX) self.yscale = (self.ymax - self.ymin) / (self.SCY) def w2s(self, p): #world-to-screen before plotting ip = [] for xy in p: ix = self.border + int( (xy[0] - self.xmin) / self.xscale) iy = self.border + int( (xy[1] - self.ymin) / self.yscale) iy = self.SCY - iy ip.append((ix,iy)) return ip def line(self, points, col='blue'): ip = self.w2s(points) t = self.canvas.create_line(ip, fill=col)
The program
tkplot.py
imports tkplot_class.py and plots two graphs. The advantage of code reuse
. Output of
tkplot.py
Example tkplot.py
from tkplot_class import * from math import * w = Tk() gw1 = disp(w) xy = []
A more sophisticated version of the available on the CD.
disp
CHAPTER 2.
PROGRAMMING IN PYTHON
42
for k in range(200): x = 2 * pi * k/200 y = sin(x) xy.append((x,y)) gw1.setWorld(0, -1.0, 2*pi, 1.0) gw1.line(xy) gw2 = disp(w) gw2.line([(10,10),(100,100),(350,50)], 'red') w.mainloop()
2.22 Exercises
1. Generate multiplication table of eight and write it to a le. 2. Make a list and write it to a le using the pickle module. 3. Write a Python program to open a le and write 'hello world' to it. 4. Write a Python program to open a text le and read all lines from it. 5. Write a program to generate the multiplication table of a number from the user. The output should be formatted as shown below 1 x 5 = 5 2 x 5 = 10 6. Dene the list [1,2,3,4,5,6] using the range function. Write code to insert a 10 after 2, delete 4, add 0 at the end and sort it in the ascending order. 7. Write Python code to generate the sequence of numbers 25 20 15 10 5 using range function . Delete 15 from the result and sort it. Print it using a for loop. 8. Dene a string
a) print it in reverse order b) split it using space character as sepatator 9. Join the elements of the list ['I', 'am', 'in', 'pieces'] using + character. Do the same using a for loop also. 10. Create a window with ve buttons. Make each button a dierent color. Each button should have some text on it. 11. Create a program that will put words in alphabetical order. The program should allow the user to enter as many words as he wants to. 12. Create a program that will check a sentence to see if it is a palindrome. A palindrome is a sentence that reads the same backwards and forwards ('malayalam'). 13. A text le contains two columns of numbers. Write a program to read them and print the sum of numbers in each row. 14. Read a String from the keyboard. Multiply it by an integer to make its length more than 50. How do you nd out the smallest number that does the job.
CHAPTER 2.
PROGRAMMING IN PYTHON
43
15. Write a program to nd the length of the hypotenuse of a right triangle from the length of other two sides, get the input from the user. 16. Write a program displaying 2 labels and 2 buttons. It should print two dierent messages when clicked on the two buttons. 17. Write a program with a Canvas and a circle drawn on it. 18. Write a program using for loop to reverse a string. 19. Write a Python function to calculate the GCD of two numbers 20. Write a program to print the values of sine function from the mean value of them. 21. Generate N random numbers using random.random() and nd out howmay are below 0.5 . Repeat the same for dierent values of N to draw some conclusions. 22. Use the equation
to
x = (b
b2 4ac)/2a
3x2 + 6x + 12 = 0
23. Write a program to calculate the distance between points (x1,y1) and (x2,y2) in a Cartesian plane. Get the coordinates from the user. 24. Write a program to evaluate
y=
2.3a + a2 + 34.5
for a = 1, 2 and 3.
25. Print Fibanocci numbers upto 100, without using multiple assignment statement. 26. Draw a chess board pattern using turtle graphics. 27. Find the syntax error in the following code and correct it. x=1 while x <= 10: print x * 5
+D=FJAH !
math
module to calculate trigonometric functions. Using the tools introduced so far, let us
generate the data points to plot a sine wave. The program sine.py generates the coordinates to plot a sine wave.
Example sine.py
import math x = 0.0 while x < 2 * math.pi: print x , math.sin(x) x = x + 0.1
The output to the screen can be redirected to a le as shown below, from the command prompt. You can plot the data using some program like xmgrace. $ python sine.py > sine.dat $ xmgrace sine.dat It would be better if we could write such programs without using loops explicitly. scientic computing requires manipulating of large data structures like matrices. The need of special tools is evident even from the simple example shown above. package widely used for scientic computing with Python. The Serious data is a
list
type of Python is very exible but the performance is not acceptable for large scale computing.
NumPy
44
CHAPTER 3.
45
import * ).
In the examples below, we will import numpy functions as local (using the syntax
from numpy
Since it is the only package used there is no possibility of any function name conicts.
Example numpy1.py
from numpy import * x = array( [1, 2, 3] ) print x , type(x) # Make array from list
Example numpy3.py
from numpy import * a = [ [1,2] , [3,4] ] # make a list of lists x = array(a) # and convert to an array print a
Other than than
array(), there are several other functions that can be used for creating dierent
0.0 0.0
CHAPTER 3.
46
3.1.1.5 random.random(shape)
Similar to the functions above, but the matrix is lled with random numbers ranging from 0 to 1, of
oat
array([[ 0.3759652 , 0.58443562, 0.41632997], [ 0.88497654, 0.79518478, 0.60402514], [ 0.65468458, 0.05818105, 0.55621826]])
reshape()
The function
Working of
reshape()
reshape.py
Example reshape.py
from numpy import * a = arange(20) b = reshape(a, [4,5]) print b
The result is shown below.
1, 6, 11, 16,
2, 7, 12, 17,
3, 8, 13, 18,
numpy2.py
Example numpy2.py
from numpy import * a = arange(1.0, 2.0, 0.1) print a b = linspace(1,2,11) print b c = ones(5,'float') print c d = zeros(5, 'int') print d e = random.rand(5) print e
Output of this program will look like; [ 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9] [ 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. ] [ 1. 1. 1. 1. 1.] [ 0. 0. 0. 0. 0.] [ 0.89039193 0.55640332 0.38962117 0.17238343 0.01297415]
CHAPTER 3.
47
3.1.2 Copying
Numpy arrays can be copied using the copy method, as shown below.
Example array_copy.py
from mumpy import * a = zeros(5) print a b = a c = a.copy() c[0] = 10 print a, c b[0] = 10 print a,c
The output of the program is shown below. [ 0. 0. 0.] [ 0. 0. 0.] [ 10. 0. 0.] [ 10. 0. 0.] [ 10. 0. 0.] The statement
b = a
aroper.py
Example aroper.py
from numpy import * a = array([[2,3], [4,5]]) b = array([[1,2], [3,0]]) print a + b print a * b
The output will be as shown below
AB =
i A1 B1
j A2 B2
k A3 B3
(3.1)
CHAPTER 3.
48
It can be evaluated using the function cross((array1, array2). The program 6, -3]
cross.py
prints [-3,
Example cross.py
from numpy import * a = array([1,2,3]) b = array([4,5,6]) c = cross(a,b) print c
cross.py
A.B = A1 B1 + A2 B2 + A3 B3
. If you change
to
c = dot(a, b),
array.tole(lename)
Example leio.py
from numpy import * a = arange(10) a.tofile('myfile.dat') b = fromfile('myfile.dat',dtype = 'int') print b
The function fromle() sets dtype='oat' by default. In this case we have saved an integer array and need to specify that while reading the le. We could have saved it as oat the the statement a.tole('myle.dat', 'oat').
We can verify
the result by multiplying the original matrix with the inverse. elements are having very high values, and it indicates an error.
argument should normally result in an error message. In some cases, you may get a result whose
Example inv.py
from numpy import * a = array([ [4,1,-2], [2,-3,3], [-6,-2,1] ], dtype='float') ainv = linalg.inv(a) print ainv print dot(a,ainv)
Result of this program is printed below.
CHAPTER 3.
49
[[ 0.08333333 0.08333333 -0.08333333] [-0.55555556 -0.22222222 -0.44444444] [-0.61111111 0.05555556 -0.38888889]] [[ 1.00000000e+00 -1.38777878e-17 0.00000000e+00] [-1.11022302e-16 1.00000000e+00 0.00000000e+00] [ 0.00000000e+00 2.08166817e-17 1.00000000e+00]]
Example vfunc.py
from numpy import * a = array([1,10,100,1000]) print log10(a)
The output of the program is [ 0. 1. 2. 3.] , where the log of each element is calculated and returned in an array. This feature simplies the programs a lot. Numpy also provides a function to vectorize functions written by the user.
Example vectorize.py
from numpy import * def spf(x): return 3*x vspf = vectorize(spf) a = array([1,2,3,4]) print vspf(a)
The output will be [ 3 6 9 12] .
3.3 Exercises
1. Write code to make a one dimensional matrix with elements 5,10,15,20 and 25. make another matrix by slicing the rst three elements from it. 2. Create a 3. Create a
32 23
matrix and print the sum of its elements using for loops. matrix and ll it with random numbers.
4. Use linspace to make an array from 0 to 10, with stepsize of 0.1 5. Use arange to make an 100 element array ranging from 0 to 10 6. Make an array a = [2,3,4,5] and copy it to 7. Make a 3x3 matrix and multipy it by 5.
b.
CHAPTER 3.
50
8. Create two 3x3 matrices and add them. 9. Write programs to demonstrate the dot and cross products. 10. Using matrix inversion, solve the system of equations
11. Find the new values of the coordinate (10,10) under a rotation by angle 12. Write a vectorized function to evaluate
/4.
y = x2
+D=FJAH "
Data visualization
A graph or chart is used to present numerical data in visual form. A graph is one of the easiest ways to compare numbers. They should be used to make facts clearer and more understandable. Results of mathematical computations are often presented in graphical format. In this chapter, we will explore the Python modules used for generating two and three dimensional graphs of various types.
pylab, the plotting functions from the submodules pyplot and matrix mlab will be available as local functions. Pylab also
imports Numpy for you. Let us start with some simple plots to become familiar with matplotlib.
Example plot1.py
from pylab import * data = [1,2,5] plot(data) show()
In the above example, the x-axis of the three points is taken from 0 to 2. We can specify both the axes as shown below.
Example plot2.py
from pylab import * x = [1,2,5] y = [4,5,6]
http://matplotlib.sourceforge.net/ http://matplotlib.sourceforge.net/users/pyplot_tutorial.html http://matplotlib.sourceforge.net/examples/index.html http://matplotlib.sourceforge.net/api/axes_api.html
51
CHAPTER 4.
DATA VISUALIZATION
52
plot(x,y) show()
By default, the color is blue and the line style is continuous. This can be changed by an optional argument after the coordinate data, which is the format string that indicates the color and line type of the plot. The default format string is `b-` (blue, continuous line). Let us rewrite the above example to plot using red circles. We will also set the ranges for x and y axes and label them.
Example plot3.py
from pylab import * x = [1,2,5] y = [4,5,6] plot(x,y,'ro') xlabel('x-axis') ylabel('y-axis') axis([0,6,1,7]) show()
The gure 4.1 shows two dierent plots in the same window, using dierent markers and colors.
Example plot4.py
from pylab import * t = arange(0.0, 5.0, 0.2) plot(t, t**2,'x') # t2 plot(t, t**3,'ro') # t3 show()
We have just learned how to draw a simple plot using the pylab interface of matplotlib.
Example subplot1.py
CHAPTER 4.
DATA VISUALIZATION
53
The arguments to subplot function are NR (number of rows) , NC (number of columns) and a
N R N C < 10,
N R N C.
The commas between the arguments are optional You can modify the variable NR and NC
subplot2.py.
to watch the results. Please note that the % character has dierent meanings. In String formatting.
(pn+1)%5,
it
is the reminder operator resulting in a number less than 5. The % character also appears in the
Example subplot2.py
from pylab import * mark = ['x','o','^','+','>'] NR = 2 # number of rows NC = 3 # number of columns pn = 1 for row in range(NR): for col in range(NC): subplot(NR, NC, pn) a = rand(10) * pn plot(a, marker = mark[(pn+1)%5]) xlabel('plot %d X'%pn) ylabel('plot %d Y'%pn) pn = pn + 1 show()
is measured from some agreed starting point. and negative angles clockwise from it.
Use the
x axis
x axis
Matplotlib supports polar plots, using the polar(, r ) function. Let us plot a circle using polar(). For every point on the circle, the value of
radius is the same but the polar angle changes from 0to 2 . Both the coordinate arguments must be arrays of equal size. Since is having 100 points , r also must have the same number. This array can be generated using the ones() function. The axis([min , max , rmin , rmax ) function can be used for setting the scale.
Example polar.py
from pylab import * th = linspace(0,2*pi,100) r = 5 * ones(100) # radius = 5 polar(th,r) show()
CHAPTER 4.
DATA VISUALIZATION
54
Example piechart.py
from pylab import * labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' fracs = [25, 25, 30, 20] pie(fracs, labels=labels) show()
arange, linspace
and
logspace
functions from
numpy
to generate the
input data and also the vectorized versions of the mathematical functions. For arange(), the third argument is the stepsize. The total number of elements is calculated from start, stop and stepsize. In the case of linspace(), we provide start, stop and the total number of points. The step size is calculated from these three parameters. Please note that to create a data set ranging from 0 to 1 (including both) with a stepsize of 0.1, we need to specify linspace(0,1,11) and not linspace(0,1,10).
f (x)
is even if
f (x) = f (x)
f (x) = f (x).
Even functions are functions for which the left half of the plane
looks like the mirror image of the right half of the plane. From the gure 4.2(a) you can see that sin x is odd and sin x2 is even.
Example npsin.py
Why do we need to give the angles in radians and not in degrees. Angle in radian is the length of the arc dened by the given angle, with unit radius. Degree is just an arbitrary unit.
CHAPTER 4.
DATA VISUALIZATION
55
from pylab import * x = linspace(-pi, pi , 200) y = sin(x) y1 = sin(x*x) plot(x,y) plot(x,y1,'r') show()
Exercise: Modify the program
npsin.py
to plot
etc.
x2 + y 2 = a2
of the coordinate system. In order to plot it using Cartesian coordinates, we need to express
in
x,
and is given by
y=
We will create the x-coordinates ranging from (+y and -y). The following program
of y. This will give us only half of the circle, since for each value of x, there are two values of y
circ.py
gure 4.2(b). Any multi-valued function will have this problem while plotting. Such functions can be plotted better using parametric equations or using the polar plot options, as explained in the coming sections.
Example circ.py
from pylab import * a = 10.0 x = linspace(-a, a , 200) yupper = sqrt(a**2 - x**2) ylower = -sqrt(a**2 - x**2) plot(x,yupper) plot(x,ylower) show()
x = a cos
and
y = a sin
Example circpar.py
from pylab import * a = 10.0 th = linspace(0, 2*pi, 200) x = a * cos(th) y = a * sin(th) plot(x,y) show()
CHAPTER 4.
DATA VISUALIZATION
56
to less than
several arcs with dierent radii. 5,10,15 and 20. The range of be
The
for
radians will result in an arc. The following example plots loop will execute four times with the values of radius
also depends on the loop variable. For the next three values it will
, 1.5and2
Example arcs.py
from pylab import * a = 10.0 for a in range(5,21,5): th = linspace(0, pi * a/10, 200) x = a * cos(th) y = a * sin(th) plot(x,y) show()
4.3.1 Astroid
The astroid was rst discussed by Johann Bernoulli in 1691-92. It also appears in Leibniz's correspondence of 1715. It is sometimes called the tetracuspid for the obvious reason that it has four cusps. A circle of radius 1/4 rolls around inside a circle of radius 1 and a point on its circumference traces an astroid. The Cartesian equation is
x3 + y 3 = a3
The parametric equations are
(4.1)
(4.2)
CHAPTER 4.
DATA VISUALIZATION
57
In order to plot the curve in the Cartesian system, we rewrite equation 4.1 as
y = (a 3 x 3 ) 2
The program
The program
astropar.py
uses the parametric equation and plots the complete curve. Both are shown in gure 4.4
Example astro.py
from pylab import * a = 2 x = linspace(0,a,100) y = ( a**(2.0/3) - x**(2.0/3) )**(3.0/2) plot(x,y) show()
Example astropar.py
from pylab import * a = 2 t = linspace(-2*a,2*a,101) x = a * cos(t)**3 y = a * sin(t)**3 plot(x,y) show()
4.3.2 Ellipse
The ellipse was rst studied by Menaechmus [4] . Euclid wrote about the ellipse and it was given its present name by Apollonius. The focus and directrix of an ellipse were considered by Pappus. Kepler, in 1602, said he believed that the orbit of Mars was oval, then he later discovered that it was an ellipse with the sun at one focus. In fact Kepler introduced the word his discovery in 1609. The Cartesian equation is
focus
and published
x2 y2 + 2 =1 a2 b
The parametric equations are
(4.3)
x = a cos(t), y = b sin(t)
The program
(4.4)
ellipse.py
uses the parametric equation to plot the curve. Modifying the para-
metric equations will result in Lissajous gures. The output of lissa.py are shown in gure 4.4(c).
CHAPTER 4.
DATA VISUALIZATION
58
Example ellipse.py
from pylab import * a = 2 b = 3 t = linspace(0, 2 * pi, 100) x = a * sin(t) y = b * cos(t) plot(x,y) show()
Example lissa.py
from pylab import * a = 2 b = 3 t= linspace(0, 2*pi,100) x = a * sin(2*t) y = b * cos(t) plot(x,y) x = a * sin(3*t) y = b * cos(2*t) plot(x,y) show()
The Lissajous curves are closed if the ratio of the arguments for sine and cosine functions is an integer. Otherwise open curves will result, both are shown in gure 4.4(c).
r = a.
r2 = a2 .
Example archi.py
from pylab import * a = 2
CHAPTER 4.
DATA VISUALIZATION
59
Example fermat.py
from pylab import * a = 2 th= linspace(0, 10*pi,200) r = sqrt(a**2 * th) polar(th,r) polar(th, -r) show()
2k
petals and
an even curve is
closed and has nite length. If k is irrational, then it is not closed and has innite length.
Example rose.py
from pylab import * k = 4 th = linspace(0, 10*pi,1000) r = cos(k*th) polar(th,r) show()
There are dozens of other famous curves whose details are available on the Internet. It may be an interesting exercise for the reader. For more details refer to [3, 2, 5]on the Internet.
d2 y +y =0 dx2
which has a series solution of the form
y = a0
n=0
(1)
(4.5)
These are the Maclaurin series for sine and cosine functions. The following code plots several terms of the sine series and their sum.
CHAPTER 4.
DATA VISUALIZATION
60
Example series_sin.py
from pylab import * from scipy import factorial x = linspace(-pi, pi, 50) y = zeros(50) for n in range(5): term = (-1)**(n) * (x**(2*n+1)) / factorial(2*n+1) y = y + term #plot(x,term) #uncomment to see each term plot(x, y, '+b') plot(x, sin(x),'r') # compare with the real one show()
The output of
series_sin.py
sin
library is plotted.
The values calculated by using the series becomes closer to the actual value
with more and more number of terms. The error can be obtained by adding the following lines to
series_sin.py
f (x)
and cosines. The computation and study of Fourier series is known as harmonic analysis and is extremely useful as a way to break up an arbitrary periodic function into a set of simple terms that can be plugged in, solved individually, and then recombined to obtain the solution to the original problem or an approximation to it to whatever accuracy is desired or practical. The examples below shows how to generate a square wave and sawtooth wave using this technique. To make the output better, increase the number of terms by changing the argument of the range() function, used in the for loop. The output of the programs are shown in gure 4.7.
Example fourier_square.py
CHAPTER 4.
DATA VISUALIZATION
61
Figure 4.7: Sawtooth and Square waveforms generated using Fourier series.
from pylab import * N = 100 # number of points x = linspace(0.0, 2 * pi, N) y = zeros(N) for n in range(5): term = sin((2*n+1)*x) / (2*n+1) y = y + term plot(x,y) show()
Example fourier_sawtooth.py
from pylab import * N = 100 # number of points x = linspace(-pi, pi, N) y = zeros(N) for n in range(1,10): term = (-1)**(n+1) * sin(n*x) / n y = y + term plot(x,y) show()
imshow()
50 50
matrix lled
Example imshow1.py
from pylab import * m = random([50,50]) imshow(m) show()
CHAPTER 4.
DATA VISUALIZATION
62
4.7 Fractals
Fractals
! are a part of fractal geometry, which is a branch of mathematics concerned with irregular
patterns made of parts that are in some way similar to the whole (e.g.: twigs and tree branches). A fractal is a design of innite details. It is created using a mathematical formula. No matter how closely you look at a fractal, it never loses its detail. It is innitely detailed, yet it can be contained in a nite space. Fractals are generally self-similar and independent of scale. The theory of fractals was developed from Benoit Mandelbrot's study of complexity and chaos. Complex numbers are required to compute the Mandelbrot and Julia Set fractals and it is assumed that the reader is familiar with the basics of complex numbers. To compute the basic Mandelbrot (or Julia) set one uses the equation is assigned to
f (z) z 2 + c
, where
both z and c are complex numbers. The function is evaluated in an iterative manner, ie. the result
behavior of the values that are put into the function. If the value of the function goes to innity (practically to some xed value, like 1 or 2) after few iterations for a particular value of
, that
point is considered to be outside the Set. A Julia set can be dened as the set of all the complex 2 numbers (z) such that the iteration of f (z) z + c is bounded for a particular value of c. To generate the fractal the number of iterations required to diverge is calculated for a set of points in the selected region in the complex plane. The number of iterations taken for diverging decides the color of each point. The points that did not diverge, belonging to the set, are plotted with the same color. The program
julia.py
The program
creates a 2D array (200 x 200 elements). For our calculations, this array represents a rectangular region on the complex plane centered at the origin whose lower left corner is (-1,-j) and the upper right corner is (1+j). For 200x200 equidistant points in this plane the number of iterations are calculated and that value is given to the corresponding element of the 2D matrix. The plotting is taken care by the imshow function. The output is shown in gure 4.8(b). Change the value of and run the program to generate more patterns. The equation also may be changed.
Example julia.py
' Region of a complex plane ranging and imaginary axes is represented having X x Y elements.For X and Y in the complex plane is 2.0/200 = The nature of the pattern depends ' from pylab import *
! http://en.wikipedia.org/wiki/Fractal
from -1 to +1 in both real using a 2D matrix equal to 200,the stepsize 0.01. much on the value of c.
CHAPTER 4.
DATA VISUALIZATION
63
X = 200 Y = 200 MAXIT = 100 MAXABS = 2.0 c = 0.02 - 0.8j # The constant in equation z**2 + c m = zeros([X,Y]) # A two dimensional array def numit(x,y): # number of iterations to diverge z = complex(x,y) for k in range(MAXIT): if abs(z) <= MAXABS: z = z**2 + c else: return k # diverged after k trials return MAXIT # did not diverge, for x in range(X): for y in range(Y): re = 0.01 * x - 1.0 im = 0.01 * y - 1.0 m[x][y] = numit(re,im) imshow(m) # Colored plot using show()
# complex number for # this (x,y) coordinate # get the color for (x,y) the 2D matrix
4.8 Meshgrids
in order to make contour and 3D plots, we need to understand the meshgrid. Consider a rectangular area on the X-Y plane. Assume there are m divisions in the X direction and n divisions in the Y direction. We now have a
mn
coordinates of each mesh point is held in one matrix and y coordinates are held in another. The NumPy function meshgrid() creates two 2x2 matrices from two 1D arrays, as shown in the example below. This can be used for plotting surfaces and contours, by assigning a Z coordinate to every mesh point.
Example mgrid1.py
from numpy import * x = arange(0, 3, 1) y = arange(0, 3, 1) gx, gy = meshgrid(x, y) print gx print gy
The outputs are as shown below, gx(i,j) contains the x-coordinate and gx(i,j) contains the ycoordinate of the point (i,j). [[0 1 2] [0 1 2] [0 1 2]] [[0 0 0] [1 1 1]
CHAPTER 4.
DATA VISUALIZATION
64
[2 2 2]] We can evaluate a function at all points of the meshgrid by passing the meshgrid as an argument. The program mgrid2.py plots the sum of sines of the x and y coordinates, using imshow to get a result as shown in gure 4.8(c).
Example mgrid2.py
from pylab import * x = arange(-3*pi, 3*pi, 0.1) y = arange(-3*pi, 3*pi, 0.1) xx, yy = meshgrid(x, y) z = sin(xx) + sin(yy) imshow(z) show()
4.9 3D Plots
Matplotlib supports several types of 3D plots, using the Axes3D class. The following three lines of code are required in every program making 3D plots using matplotlib.
Example sufrace3d.py
from pylab import * from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(figure()) x = arange(-3*pi, 3*pi, 0.1) y = arange(-3*pi, 3*pi, 0.1) xx, yy = meshgrid(x, y) z = sin(xx) + sin(yy) ax.plot_surface(xx, yy, z, cmap=cm.jet, cstride=1) show()
Example line3d.py
from pylab import * from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(figure())
CHAPTER 4.
DATA VISUALIZATION
65
phi = linspace(0, 2*pi, 400) x = cos(phi) y = sin(phi) z = 0 ax.plot(x, y, z, label = 'x')# circle z = sin(4*phi) # modulated in z plane ax.plot(x, y, z, label = 'x') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') show()
Modify the code to make x = sin(2*phi) to observe Lissajous gures
from pylab import * from mpl_toolkits.mplot3d import Axes3D ax = Axes3D(figure()) phi = linspace(0, 2 * pi, 100) theta = linspace(0, pi, 100) x = 10 * outer(cos(phi), sin(theta)) y = 10 * outer(sin(phi), sin(theta)) z = 10 * outer(ones(size(phi)), cos(theta)) ax.plot_wireframe(x,y,z, rstride=2, cstride=2) show()
Example ylm20.py
CHAPTER 4.
DATA VISUALIZATION
66
from numpy import * from enthought.mayavi import mlab polar = linspace(0,pi,100) azimuth = linspace(0, 2*pi,100) phi,th = meshgrid(polar, azimuth) r = 0.25 * sqrt(5.0/pi) * (3*cos(phi)**2 - 1) x = r*sin(phi)*cos(th) y = r*cos(phi) z = r*sin(phi)*sin(th) mlab.mesh(x, y, z) mlab.show()
4.11 Exercises
1. Plot a sine wave using markers +, o and x using three dierent colors. 2. Plot
tan
from
from
to
2 ,
3. Plot a circle using the polar() function. 4. Plot the following from the list of Famous curves at reference [3] 2 2 a) r = a cos 2 , Lemniscate of Bernoulli 2 2ex /2 Frequency curve b) y = c) d)
4. Rhodonea curves
y=
7. Write a Python program to calculate sine function using series expansion and plot it. 8. Write a Python program to plot
y = 5x2 + 3x + 2
with axes and title. Use red colored circles to mark the points.
CHAPTER 4.
DATA VISUALIZATION
67
9. Write a Python program to plot a Square wave using Fourier series, number of terms should be a variable. 10. Write a Python program to read the x and y coordinates from a le, in a two column format, and plot them. 11. Plot
x2 + y 2 + z 2 = 25
using mayavi.
12. Make a plot z = sin(x) + sin(y) using imshow() , from 13. Write Python code to plot
4to4
y = x2 ,
) L T X is a powerful typesetting system, used for producing scientic and mathematical documents E
You prepare your document using a plain text editor, and the formatting is
) by L T X, but you need to specify it using some commands. In this chapter, we will discuss some E of these commands mainly to typeset mathematical equations.
) subparagraph etc. are specied by commands and it is the job of L T X to format them properly. E
It does the numbering of sections automatically and can generate a table of contents if requested. Figures and tables are also numbered and placed without the user worrying about it. The latex source document (the .tex le) is compiled by the latex program to generate a device independent (the .dvi le) output. From that you can generate postscript or PDF versions of the document. We will start with a simple example anything after a % sign is taken as a comment.
hello.tex
Example hello.tex
\documentclass{article} \begin{document} Small is beautiful. % I am just a comment \end{document}
Compile, view and make a PDF le using the following commands: $ latex hello.tex $ xdvi hello.dvi $ dvipdf hello.dvi The output will look like : Small is beautiful.
CHAPTER 5.
69
texts.tex
Example texts.tex
\documentclass{article} \begin{document} This is normal text. \newline \textbf{This is bold face text.} \textit{This is italic text.}\\ \tiny{This is tiny text.} \large{This is large text.} \underline{This is underlined text.} \end{document} Compiling
texts.tex, as explained in the previous example, will genearte the following output.
Example sections.tex
\documentclass{article} \begin{document} \tableofcontents \section{Animals} This document denes sections. \subsection{Domestic} This document also denes subsections. \subsubsection{cats and dogs} Cats and dogs are Domestic animals. \end{document} The output of sections.tex is shown in gure 5.1.
To generate the table of contents, you may have to compile the document two times.
CHAPTER 5.
70
5.4 Environments
Environments decide the way in which your text is formatted : numbered lists, tables, equations, quotations, justications, gure, etc. are some of the environments. Environments are dened like : \begin{environment_name} your text \end{environment_name} The example program environ.tex demonstrates some of the environments.
Example environs.tex
\documentclass{article} \begin{document} \begin{ushleft} A bulleted list. \end{ushleft} \begin{itemize} \item dog \item cat \end{itemize} \begin{center} A numbered List. \end{center} \begin{enumerate} \item dog \item cat \end{enumerate} \begin{ushright} This text is right justied. \end{ushright} \begin{quote} Any text inside quote\\ environment will appe-\\ ar as typed.\\ \end{quote} \begin{verbatim} x=1 while x <= 10: print x * 5 x=x+1
CHAPTER 5.
71
\end{verbatim} \end{document} The enumerate and itemize are used for making numbered and non-numbered lists. Flushleft, ushright and center are used for specifying text justication. Quote A and verbatim are used for portions where we do not want L TEX to do the formatting. The output of environs.tex is shown below. A bulleted list. X dog X cat A numbered List. 1. dog 2. cat This text is right justied. Any text inside quote environment will appear as typed.
Example math1.tex
\documentclass{article} \usepackage{amsmath} \begin{document}
CHAPTER 5.
72
The equation $a^2 + b^2 = c^2$ is typeset as inline. The same can be done in a separate line using \begin{equation} a^2 + b^2 = c^2 \end{equation} \end{document} The output of this le is shown below. The equation a2 +b2 = c2 is typeset as inline. The same can be done in a separate line using a2 + b2 = c2 (5.1) The equation number becomes 5.1 because this happens to be the rst equation in chapter 5.
A B
2. Greek letters : $ \alpha \beta \gamma \pi$ 4. Multiple Exponents : $a^b \quad a^{b^c}$ 5. Fractions : $\frac{3}{5}$
3 5
An ab ab
c
Am
n! = 1 2 (n 1) n 0.3 = 1/3
a sin x + arctan y 2 x + y2 z=
x2 +
! \quad is for inserting space, the size of a \quad corresponds to the width of the character `M' of the current font. Use \qquad for larger space.
CHAPTER 5.
73
A=B
AC
2A x2
sinx
)2
(x + 1)(x 1)
f (x)dx
person sex age John male 7 M ary f emale 20 Gopal male 30 The rst column is left justied, second is centered and the third is right justied (decided by the {|l|cr|}). If you insert a | character between c and r, it will add a vertical line between second and third columns. Let us make a matrix using the same command.
$ A = \left( \begin{array}{ccc} x_1 & x_2 & \ldots \\
CHAPTER 5.
74
y_1 & y_2 & \ldots \\ \vdots & \vdots & \ddots \\ \end{array} \right) $ The output is shown below. The \left( and \right) provides the enclosure. All the columns are centered. We have also used horizontal, vertical and diagonal dots in this example. x 1 x2 . . . A = y1 y2 . . . . . .. . . . . .
Learn how to insert pictures with caption inside the gure environment.
20 15 10 5
2.0 1.5
Figure 5.2: Picture of Arc and Sawtooth, inserted with [h] option.
1.0 0.5
0.0
0.5
10
1.0
15
1.5
20
20
15
10
10
15
20
2.0
CHAPTER 5.
75
Example qpaper.tex
\documentclass{article} \usepackage{amsmath} begin{document} \begin{center} \large{\textbf{Sample Question Paper\\for\\ Mathematics using Python}} \end{center} \begin{tabular}{p{8cm}r} \textbf{Duration:3 Hrs} & \textbf{30 weightage} \end{tabular}\\ \section{Answer all Questions. $4\times 1\frac{1}{2}$} \begin{enumerate} A \item What are the main document classes in L TEX. A \item Typeset $\sin^{2}x+\cos^{2}x=1$ using LTEX. \item Plot a circle using the polar() function. \item Write code to print all perfect cubes upto 2000. \end{enumerate} \section{Answer any two Questions. $3\times 5$} \begin{enumerate} A \item Set a sample question paper using LTEX. \item Using Python calculate the GCD of two numbers \item Write a program with a Canvas and a circle. \end{enumerate} \begin{center}\text{End}\end{center} \end{document} The formatted output is shown below.
CHAPTER 5.
76
5.9 Exercises
A 1. What are the main document classes supported by L TEX.
2. How does Latex dier from other word processor programs. 3. Write a .tex le to typeset 'All types of Text Available' in tiny, large, underline and italic. 4. Rewrite the previous example to make the output a list of numbered lines. 5. Generate an article with section and subsections with table of contents. 6. Typeset 'All types of justications' to print it three times; left, right and centered. 7. Write a .tex le that prints 12345 in ve lines (one character per line). 8. Typeset a Python program to generate the multiplication table of 5, using verbatim. 9. Typeset sin2 x + cos2 x = 1
CHAPTER 5.
77
x2 + y 2
(
)2
1 n
= x2 + y 2
A x 0
n=1
1+
)n
=A cos x.dx
b b2 4ac 2a
1 2 3 4
16. Typeset R =
+D=FJAH $
Numerical methods
Solving mathematical equations is an important requirement for various branches of science but many of them evade an analytic solution. The eld of numerical analysis explores the techniques that give approximate but accurate solutions to such problems.1 Even when they have a solution, for all practical purposes we need to evaluate the numeric value of the result, with the desired accuracy. We will focus on developing simple working programs rather than going into the theoretical details. The mathematical equations giving numerical solutions will be explored by changing various parameters and nature of input data.
lim f (x + 2x ) f (x 2x ) (6.1) x 0 x neglecting the higher order terms. The accuracy of the derivative calculated using discrete values depends on the stepsize x. It will also depends on the number of higher order derivatives the function has. We will try to explore these aspects using the program di.py , which evaluates the derivatives of few functions using two dierent stepsizes (0.1 ans 0.01). The input values to function deriv() are the function to be dierentiated, the point at which the derivative is to be found and the stepsize x.
Example di.py
def f1(x): return x**2
Introductory methods of numerical analysis by S.S.Sastry http://ads.harvard.edu/books/1990fnmd.book/
78
CHAPTER 6.
NUMERICAL METHODS
79
x = 0.005
(b) for
x = 1.0
def f2(x): return x**4 def f3(x): return x**10 def deriv(func, x, dx=0.1): df = func(x+dx/2)-func(x-dx/2) return df/dx print deriv(f1, 1.0), deriv(f1, 1.0, 0.01) print deriv(f2, 1.0), deriv(f2, 1.0, 0.01) print deriv(f3, 1.0), deriv(f3, 1.0, 0.01)
The output of the program is shown below. Comparing the two numbers on the same line shows the eect of stepsize. Comparing the rst number on each row shows the eect of the number of higher order derivatives the function has. For the same stepsize x4 gives much less error than x10 . Second derivative of x2 is constant and the result becomes exact, result on the rst line. 2.0 2.0 4.01 4.0001 10.3015768754 10.0030001575 You may explore other functions by modifying the program. It can be seen that the function deriv(), evaluates the function at two points to calculate the derivative. The higher order terms can be calculated by evaluating the function at more points. Techniques used for this will be discussed in section 6.8, on interpolation.
CHAPTER 6.
NUMERICAL METHODS
80
Figure 6.2: Area under the curve is divided it in to a large number of intervals. Area of each of them is calculated by assuming them to be trapezoids.
our deriv() function. The dened function is sine and the derivative is calculated using the vectorized version of deriv(). The actual cosine function also is plotted for comparison. The output of vdi.py is shown in 6.1(a). The value of x is increased to 1.0 by changing one line of code as y = vecderiv(x, 1.0) and the result is shown in 6.1(b). The values calculated using our function is shown using +marker, while the continuous curve is the expected result , ie. the cosine curve.
Example vdi.py
from pylab import * def f(x): return sin(x) def deriv(x,dx=0.005): df = f(x+dx/2)-f(x-dx/2) return df/dx vecderiv = vectorize(deriv) x = linspace(-2*pi, 2*pi, 200) y = vecderiv(x) plot(x,y,'+') plot(x,cos(x)) show()
CHAPTER 6.
NUMERICAL METHODS
81
in to n sub-intervals, each of length h = (b a)/n, and area of a sub-interval is approximated by xn h ydx = (yn1 + yn ) 2 xn1 the integral is given by
h [y0 + 2(y1 + y2 + . . . + yn1 ) + yn ] (6.2) 2 a This is the sum of the areas of the individual trapezoids. The error in using the trapezoid rule is approximately proportional to 1/n2 . If the number of sub-intervals is doubled, the error is reduced by a factor of 4. The program trapez.py does integration of a given function using equation 6.2. We will choose an example where the results can be cross checked easily, the value of is calculated by evaluating the area of a unit circle by integrating the equation of a circle. ydx =
Example trapez.py
from math import * def y(x): # equation of a circle return sqrt(1.0 - x**2) def trapez(f, a, b, n): h = (b-a) / n sum = 0 x = 0.5 * h # f(x) at middle of the slice for i in range (1,n): sum = sum + h * f(x) x = x + h return sum print 4 * trapez(y, 0.0, 1.0,1000) print 4 * trapez(y, 0.0, 1.0,10000) print trapez(sin,0,2,1000) # Why the error ?
The output is shown below. The result gets better by increasing n thus resulting in smaller h. The last line shows, how things can go wrong if the arguments are given in the integer format. Learn how to avoid such pitfalls while writing programs. It is left as an exercise to the reader to modify the function trapez() to accept integer arguments also. 3.14041703178 3.14155546691 0.0
CHAPTER 6.
NUMERICAL METHODS
82
dy = f (x, y); y(x0 ) = y0 (6.3) dx where the derivative of the function f (x, y) is known and the value of the function at some value of x = x0 also is known. The objective is to nd out the value of the function for other values of x. The underlying idea of any routine for solving the initial value problem is to rewrite the dy and dx as nite steps y and x, and multiply the equations by x. This gives algebraic formulas for the change in the value of y(x) when x is changed by one stepsize x . In the limit of making the stepsize very small, a good approximation to the underlying dierential equation is achieved. Implementation of this procedure results in the Euler's method, which is conceptually very important, but not recommended for any practical use. In this section we will discuss Euler's method and the Runge-Kutta method with the help of example programs. For detailed information refer to [10, 11].
lim y(xn + h) y(xn ) h0 h For suciently small values of h , we can write, y (xn , yn ) = y(xn + h) = y(xn , yn ) + hy (xn )
(6.4)
(6.5)
The above equations implies that, if the value of the function y(x) is known to be yn at the point xn , its value at a nearby point xn+1 is given by yn + h y . The program euler.py calculates the value of sine function using its derivative, ie. the cosine function. We start from x = 0 , where sin(x) = 0 and compute the subsequent values using the derivative, cos(x), and compare the result with the actual sine function.
Example euler.py
from pylab import * h = 0.01 # stepsize
CHAPTER 6.
NUMERICAL METHODS
83
x = 0.0 # initial values y = 0.0 ax = [] # Lists to store x and y ay = [] while x < 2*pi: y = y + h * math.cos(x) # Euler equation x = x + h ax.append(x) ay.append(y) plot(ax,ay) show()
The output of
CHAPTER 6.
NUMERICAL METHODS
84
k1 = hf (xn , yn ) k2 = hf (xn + h , yn + k21 ) 2 k3 = hf (xn + h , yn + k22 ) 2 k4 = hf (xn + h, yn + k3 ) 1 (k1 + 2k2 + 2k3 + k4 ) (6.6) 6 The program rk4.py listed below uses the equations shown above to calculate the sine function, by integrating the cosine. The output is shown in gure 6.4(a). yn+1 = yn +
Example rk4.py
from pylab import * def rk4(x, k1 = h k2 = h k3 = h k4 = h return y, fx, h = 0.1): # x, y , f(x), stepsize * fx(x) * fx(x + h/2.0) * fx(x + h/2.0) * fx(x + h) y + ( k1/6 + k2/3 + k3/3 + k4/6 )
h = 0.01 # stepsize x = 0.0 # initial values y = 0.0 ax = [x] ay = [y] while x < math.pi: y = rk4(x,y,math.cos) x = x + h ax.append(x) ay.append(y)
CHAPTER 6.
NUMERICAL METHODS
85
plot(ax,ay) show()
The program compareEuRK4.py calculates the values of Sine by integrating Cosine. The errors in both cases are evaluated at every step, by comparing it with the sine function, and plotted as shown in gure 6.4(b). The accuracy of Runge-Kutta method is far superior to that of Euler's method, for the same step size.
Example compareEuRK4.py
from scipy import * def rk4(x, k1 = h k2 = h k3 = h k4 = h return y, fx, h = 0.1): # x, y , f(x), stepsize * fx(x) * fx(x + h/2.0) * fx(x + h/2.0) * fx(x + h) y + ( k1/6 + k2/3 + k3/3 + k4/6 )
h = 0.1 # stepsize x = 0.0 # initial values ye = 0.0 # for Euler yr = 0.0 # for RK4 ax = [] # Lists to store results euerr = [] rkerr = [] while x < 2*pi: ye = ye + h * math.cos(x) # Euler method yr = rk4(x, yr, cos, h) # RK4 method x = x + h ax.append(x) euerr.append(ye - sin(x)) rkerr.append(yr - sin(x)) plot(ax,euerr,'o') plot(ax, rkerr,'+') show()
CHAPTER 6.
NUMERICAL METHODS
86
Example rk4_proper.py
def f1(x,y): return 1 + y**2 def f2(x,y): return (y-x)/(y+x) def rk4(x, y, fxy, h): # x, y , f(x,y), step k1 = h * fxy(x, y) k2 = h * fxy(x + h/2.0, y+k1/2) k3 = h * fxy(x + h/2.0, y+k2/2) k4 = h * fxy(x + h, y+k3) ny = y + ( k1/6 + k2/3 + k3/3 + k4/6 ) #print x,y,k1,k2,k3,k4, ny return ny h = 0.2 # stepsize x = 0.0 # initial values y = 0.0 print rk4(x,y, f1, h) h = 1 x = 0.0 # initial values y = 1.0 print rk4(x,y,f2,h)
The results are shown below. 0.202707408081 1.5056022409
6.4 Polynomials
A polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coecients. A polynomial in one variable with constant coecients is given by an xn + ... + a2 x2 + a1 x + a0 (6.7) The derivative of 6.7 is,
CHAPTER 6.
NUMERICAL METHODS
87
that any suciently smooth function can locally be approximated by a polynomial. Computers use this property to evaluate trigonometric, logarithmic and exponential functions. One dimensional polynomials can be explored using the poly1d function from Numpy. You can dene a polynomial by supplying the coecient as a list. For example , the statement p = poly1d([3,4,7]) constructs the polynomial 3x2 + 4x + 7. Numpy supports several polynomial operations. The following example demonstrates evaluation at a particular value, multiplication, dierentiation, integration and division of polynomials using poly1d.
Example poly.py
from pylab import * a = poly1d([3,4,5]) b = poly1d([6,7]) c = a * b + 5 d = c/a print a print a(0.5) print b print a * b print a.deriv() print a.integ() print d[0], d[1]
The output of
CHAPTER 6.
NUMERICAL METHODS
88
3x2 + 4x + 5 7.75 6x + 7 18x3 + 45x2 + 58x + 35 6x + 4 1x3 + 2x2 + 5x 6x + 7 5 The last two lines show the result of the polynomial division, quotient and reminder. Note that a polynomial can take an array argument for evaluation to return the results in an array. The program polyplot.py evaluates polynomial 6.8 and its rst derivative. x3 x5 x7 (6.8) x + 6 120 5040 The results are shown in gure 6.5. The equation 6.8 is the rst four terms of series representing sine wave (7! = 5040). The derivative looks like cosine as expected. Try adding more terms and change the limits to see the eects.
Example polyplot.py
from pylab import * x = linspace(-pi, pi, 100) a = poly1d([-1.0/5040,0,1.0/120,0,-1.0/6,0,1,0]) da = a.deriv() y = a(x) y1 = da(x) plot(x,y) plot(x,y1) show()
(x a)2 (x a)n n f (x) = f (a) + (x a)f (a) + f (a) + + f (a) 2! n! For example let us consider the equation f (x) = x3 + x2 + x
We can see that f (0) = 0 and the derivatives are
(6.9)
(6.10)
f (x) = 3x2 + 2x + 1;
f (x) = 6x + 2;
f (x) = 6
CHAPTER 6.
NUMERICAL METHODS
89
Using the values of the derivatives at x = 0and equation 6.9, we evaluate the function at x = .5, using the polynomial expression,
f (0.5) = 0 + 0.5 1 +
The result is same as 0.53 + 0.52 + 0.5 = .875. We have calculated it manually for x = .5 . We can also do this using Numpy as shown in the program taylor.py.
Example taylor.py
from numpy import * p = poly1d([1,1,1,0]) dp = p.deriv() dp2 = dp.deriv() dp3 = dp2.deriv() a = 0 # The known point x = 0 # Evaluate at x while x < .5: tay = p(a) + (x-a)* dp(a) + \ (x-a)**2 * dp2(a) / 2 + (x-a)**3 * dp3(a)/6 print '%5.1f %8.5f\t%8.5f'%(x, p(x), tay) x = x + .1
The result is shown below. 0.0 0.1 0.2 0.3 0.4 0.00000 0.11100 0.24800 0.41700 0.62400 0.00000 0.11100 0.24800 0.41700 0.62400
sin(x) = x
x 3 x5 + + 3! 5!
n=0
(1)n
(6.11)
CHAPTER 6.
NUMERICAL METHODS
90
cos(x) = 1
x2 x 4 + + 2! 4!
n=0
(1)n
x2n (2n)!
(6.12)
The program series_sc.py evaluates the sine and cosine series. The output is shown in gure 6.6. Compare the output of polyplot.py from section 6.4 with this. In both cases, we have evaluated the polynomial of sine function. In the present case, we can easily modify the number of terms and the logic is simpler.
Example series_sc.py
from pylab import * def f(n): # Factorial function if n == 0: return 1 else: return n * f(n-1) NP = 100 x = linspace(-pi, pi, NP) sinx = zeros(NP) cosx = zeros(NP) for n in range(10): sinx += (-1)**(n) * (x**(2*n+1)) / f(2*n+1) cosx += (-1)**(n) * (x**(2*n)) / f(2*n) plot(x, sinx) plot(x, cosx,'r') show()
CHAPTER 6.
NUMERICAL METHODS
91
Example rootsearch.py
def func(x): return x**3-10.0*x*x + 5 def root(f,a,b,dx): x = a while True: f1 = f(x) f2 = f(x+dx) if f1*f2 < 0: return x, x + dx x = x + dx if x >= b: return (None,None) x,y = print x,y = print root(func, 0.0, 1.0,.1) x,y root(math.cos, 0.0, 4,.1) x,y
The outputs are (0.7 , 0.8) and (1.5 , 1.6). The root of cosine,/2, is between 1.5 and 1.6. After the root has been located roughly, we can nd the root with any specied accuracy, using bisection method, Newton-Raphson method etc.
CHAPTER 6.
NUMERICAL METHODS
92
2x2 3x 5
x=4
and
x=4
(b) tan(x).
Example bisection.py
import math def func(x): return x**3 - 10.0* x*x + 5 def bisect(f, x1, x2, epsilon=1.0e-9): f1 = f(x1) f2 = f(x2) if f1*f2 > 0.0: print 'x1 and x2 are on the same side of x-axis' return n = math.ceil(math.log(abs(x2 - x1)/epsilon)/math.log(2.0))
CHAPTER 6.
NUMERICAL METHODS
93
n = int(n) for i in range(n): x3 = 0.5 * (x1 + x2) f3 = f(x3) if f3 == 0.0: return x3 if f2*f3 < 0.0: x1 = x3 f1 = f3 else: x2 = x3 f2 = f3 return (x1 + x2)/2.0 print bisect(func, 0.70, 0.8, 1.0e-4) print bisect(func, 0.70, 0.8, 1.0e-9)
xi+1 = xi
f (xi ) f (xi )
(6.14)
Figure 6.7(a) shows the graph of the quadratic equation 2x2 3x 5 = 0 and its two tangents. It can be seen that the zeros are at x = -1 and x = 2.5, and we use the program newraph.py shown below to nd the roots. The function nr() is called twice, and we get the roots nearer to the corresponding starting values.
Example newraph.py
from pylab import * def f(x): return 2.0 * x**2 - 3*x - 5 def df(x): return 4.0 * x - 3 def nr(x, tol = 1.0e-9):
CHAPTER 6.
NUMERICAL METHODS
94
for i in range(30): dx = -f(x)/df(x) #print x x = x + dx if abs(dx) < tol: return x print nr(4) print nr(0)
The output is shown below. 2.5 -1.0 Uncomment the print statement inside nr() to view how fast this method converges, compared to the bisection method. The program newraph_plot.py, listed below is used for generating the gure 6.7.
Example newraph_plot.py
from pylab import * def f(x): return 2.0 * x**2 - 3*x - 5 def df(x): return 4.0 * x - 3 vf = vectorize(f) x = linspace(-2, 5, 100) y = vf(x) # Tangents at x=3 and 4, using one point slope formula x1 = 4 tg1 = df(x1)*(x-x1) + f(x1) x1 = 3 tg2 = df(x1)*(x-x1) + f(x1) grid(True) plot(x,y) plot(x,tg1) plot(x,tg2) ylim([-20,40]) show()
We have dened the function f (x) = 2x2 3x 5 and vectorized it. The derivative 4x2 3 also is dened by df (x), which is the slope of f (x). The tangents are drawn at x = 4 and x = 3, using the point slope formula for a line y = m(x x1) + y1.
CHAPTER 6.
NUMERICAL METHODS
95
4x + y 2z = 0 2x 3y + 3z = 9 6x 2y + z = 0
can be represented in the matrix form as
4 1 2 x 0 2 3 3 y = 9 6 2 1 z 0
and can be solved by nding the inverse of the coecient matrix.
1
x 4 1 2 3 y = 2 3 z 6 2 1
0 9 0
CHAPTER 6.
NUMERICAL METHODS
96
S(a0 , a1 , . . . , am ) =
n i=0
(6.15)
i = 0, 1, . . . m
(6.16)
f (a, b) = a + bx
Solving the equations
S a
= 0 and
S b
b=
yi (x x) , and a = y xb xi (x x)
n 1 yi n + 1 i=0
x=
n 1 xi , n + 1 i=0
y=
(6.18)
The program lst.py demonstrates the usage of equations 6.17 and 6.18.
Example lst.py
CHAPTER 6.
NUMERICAL METHODS
97
from pylab import * NP = 50 r = 2*ranf([NP]) - 0.5 x = linspace(0,10,NP) data = 3 * x + 2 + r xbar = mean(x) ybar = mean(data) b = sum(data*(x-xbar)) / sum(x*(x-xbar)) a = ybar - xbar * b print a,b y = a + b * x plot(x,y) plot(x,data,'ob') show()
The raw data is made by adding random numbers (between -1 and 1) to the y coordinates generated by y = 3 x + 2 . The Numpy functions mean() and sum() are used. The output is shown in gure 6.8.
6.8 Interpolation
Interpolation is the process of constructing a function f (x) from a set of data points (xi , yi ), in the interval a < x < b that will satisfy yi = f (xi ) for any point in the same interval. The easiest way is to construct a polynomial of degree n that passes through the n + 1 distinct data points.
(6.20)
The coecients ai can be evaluated in the following manner. When x = x0 , all the terms in 6.20 except a0 will vanish due to the presence of (x x0 ) and we get
yo = a0
For x = x1 , only the rst two terms will be non-zero.
(6.21)
CHAPTER 6.
NUMERICAL METHODS
98
y1 = a0 + a1 (x1 x0 ) a1 =
Applying x = x2 , we get
(6.22) (6.23)
(y1 y0 ) (x1 x0 )
(6.24)
y x1 y0 1 x0 (6.25) x2 x0 The other coecients can be found in a similar manner. They can be expressed better using the divided dierence notation as shown below.
[y0 ] = y0 [y0 , y1 ] =
y2 y1 x2 x1
(y1 y0 ) (x1 x0 )
[y0 , y1 , y3 ] =
(6.26)
The divided dierence can be put in the tabular form as shown below. This will be useful while calculating the coecients manually.
x0 x1 x2 x3
x0 y1 y2 y3
0 1 2 3
[y0 ] [y1 ] [y0 , y1 ] [y2 ] [y1 , y2 ] [y0 , y1 , y2 ] [y3 ] [y2 , y3 ] [y1 , y2 , y3 ] [y0 , y1 , y2 , y3 ]
0 3 14 39
30 10 143 21 3914 32
=3 = 11 = 25
113 20 2511 31
=4 =7
74 30
=1
The table given above shows the divided dierence table for the data set x = [0,1,2,3] and y = [0,3,14,39], calculated manually. The program newpoly.py can be used for calculating the coecients, which prints the output [0, 3, 4, 1].
CHAPTER 6.
NUMERICAL METHODS
99
Example newpoly.py
from copy import copy def coef(x,y): a = copy(y) m = len(x) for k in range(1,m): tmp = copy(a) for i in range(k,m): tmp[i] = (a[i] - a[i-1])/(x[i]-x[i-k]) a = copy(tmp) return a x = [0,1,2,3] y = [0,3,14,39] print coef(x,y)
We start by copying the list y to coecient a, the rst element a0 = y0 . While calculating the dierences, we have used two loops and a temporary list. The same can be done in a better way using arrays of Numpy2 , as shown in newpoly2.py.
Example newpoly2.py
from numpy import * def coef(x,y): a = copy(y) m = len(x) for k in range(1,m): a[k:m] = (a[k:m] - a[k-1])/(x[k:m]-x[k-1]) return a x = array([0,1,2,3]) y = array([0,3,14,39]) print coef(x,y)
The next step is to calculate the value of y for any given value of x, using the coecients already calculated. The program newinterpol.py calculates the coecients using the four data points. The function eval() uses the recurrence relation
(6.27)
The program generates 20 new values of x, and calculate corresponding values of y and plots them along with the original data points, as shown in gure 6.9.
This function is from reference [12], some PDF versions of this book are available on the web.
CHAPTER 6.
NUMERICAL METHODS
100
Example newinterpol.py
from pylab import * def eval(a,xpoints,x): n = len(xpoints) - 1 p = a[n] for k in range(1,n+1): p = a[n-k] + (x -xpoints[n-k]) * p return p def coef(x,y): a = copy(y) m = len(x) for k in range(1,m): a[k:m] = (a[k:m] - a[k-1])/(x[k:m]-x[k-1]) return a x = array([0,1,2,3]) y = array([0,3,14,39]) coef = coef(x,y) NP = 20 newx = linspace(0,3, NP) # New x-values newy = zeros(NP) for i in range(NP): # evaluate y-values newy[i] = eval(coef, x, newx[i]) plot(newx, newy,'-x') plot(x, y,'ro') show()
CHAPTER 6.
NUMERICAL METHODS
101
You may explore the results for new points outside the range by changing the second argument of line newx = linspace(0,3,NP) to a higher value. Look for similarities between Taylor's series discussed in section 6.4.1 that and polynomial interpolation process. The derivative of a function represents an innitesimal change in the function with respect to one of its variables. The nite difference is the discrete analog of the derivative. Using the divided dierence method, we are in fact calculating the derivatives in the discrete form.
6.9 Exercises
1. Dierentiate 5x2 + 3x + 5 numerically and evaluate at x = 2 and x = 2. 2. Write code to numerically dierentiate sin(x2 ) and plot it by vectorizing the function. Compare with the analytical result. 3. Integrate ln x, ex from x = 1to 2. 4. Solve 2x + y = 3; x + 4y = 0; 3 + 3y = 1 using matrices. 5. Modify the program julia.py, c = 0.2 0.8j and z = z 6 + c 6. Write Python code, using pylab, to solve the following equations using matrices 4x + y 2z = 0 2x 3y + 3z = 9 6x 2y + z = 0 7. Find the roots of 5x2 + 3x 6 using bisection method. 8. Find the all the roots of sin(x) between 0 and 10, using Newton-Raphson method.
The System
This section will describe how Ubuntu was installed on a system, with MSWindows, having the following partitions: C: (GNU/Linux calls it /dev/sda1) 20 GB D: ( /dev/sda5) 20 GB E: (/dev/sda6) 30 GB We will use the partition E: to install Ubuntu, it will be formatted.
The Procedure
Set the rst boot device CD ROM from the BIOS. Boot the PC from the Ubuntu installation CD, we have used Phoenix Live CD (a modied version on Ubuntu 9.1). After 2 to 3 minutes a desktop as shown below will appear. Click on the Installer icon, the window shown next will pop up. Screens will appear to select the language, time zone and keyboard layout as shown in the gures below.
102
Installing Ubuntu
103
The bar on the top graphically displays the existing partitions. Below that there are three options provided :
Installing Ubuntu
104
1. Install them side by side. 2. Erase and use the entire disk. 3. Specify partitions manually. If you choose the rst option, the Installer will resize and repartition the disk to make some space for the new system. By default this option is marked as selected. The bar at the bottom shows the proposed partition scheme. In the present example, the installer plans to divide the C: drive in to two partitions to put ubuntu on the second. We are going to choose the third option, choose the partition manually. We will use the last partition (drive E: ) for installing Ubuntu. Once you choose that and click forward, a screen will appear where we can add, delete and change partitions. We have selected the third partition and clicked on Change. A pop-up window appeared. Using that we selected the le-system type to ext3, marked the format option, and selected the mount point as / . The screen with the pop-up window is shown below.
If we proceed with this, a warning will appear complaining about the absence of swap partitions. The swap partition is used for supplementing the RAM with some virtual memory. When RAM is full, processes started but not running will be swapped out. One can install a system without swap partition but it is a good idea to have one.
Installing Ubuntu
105
We decide to go back on the warning, to delete the E: drive, create two new partitions in that space and make one of them as swap. This also demonstrates how to make new partitions. The screen after deleting E: , with the pop-up window to make the swap partition is shown below.
We made a 2 GB swap. The remaining space is used for making one more partition, as shown in the gure 6.10. Once disk partitioning is over, you will be presented with a screen to enter a user name and password.3 A warning will be issued if the password is less than 8 characters in length. You will be given an option to import desktop settings from other installations already on the disk, choose this if you like. The next screen will conrm the installation. After the installation is over, mat take 10 to 15 minutes, you will be prompted to reboot the system. On rebooting you will be presented with a menu, to choose the operating system to boot. First item in the menu will be the newly installed Ubuntu.
Installing Ubuntu
106
enormous number of packages, that can be installed very easily. You need to have a reasonably fast Internet connection for this purpose. From the main menu, open System->Administration->Synaptic package manager. After providing the pass word (of the rst user, created during installation), the synaptic window will popup as shown in gure 6.11. Select Settings->Repositories to get a pop-up window as shown below. Tick the four repositories, close the pop-up window and Click on Reload. Synaptic will now try to download the index les from all these repositories. It may take several minute.
Installing Ubuntu
107
Now, you are ready to install any package from the Ubuntu repository. Search
Installing Ubuntu
108
for any package by name, from these repositories, and install it. If you have done the installation from original Ubuntu CD, you may require the following packges: X lyx : A latex front-end. Latex will be installed since lyx dpends on latex. X python-matplotlib : The graphics library X python-visual : 3D graphics X python-imaging-tk : Tkinter, Python Imaging Library etc. will be installed X build-essential : C compiler and related tools.
Installing Ubuntu
109
The storage space of a hard disk drive can be divided into separate data areas, known as partitions. You can create primary partitions and extended partitions. Logical drives (secondary partitions can be created inside the extended partitions). On a disk, you can have up to 4 partitions, where one of them could be an extended partition. You can have many logical drives inside the extended partition. On a MSWindows system, the primary partition is called the C: drive. The logical drives inside the extended partition are named from D: onwards. GNU/Linux uses a dierent naming convention. The individual disks are named as /dev/sda , /dev/sdb etc. and the partitions inside them are named as /dev/sda1, /dev/sda2 etc. The numbering of secondary partitions inside the logical drive starts at /dev/sda5. (1 to 4 are reserved for primary and extended). Hard disk partitioning can be done using the fdisk program. The installation program also does this for you. The process of making a le system on a partition is called formatting. There are many dierent types of le systems. MSWindows use le systems like FAT32, NTFS etc. and GNU/Linux mostly uses le systems like ext3, ext4 etc. The operating system les are kept in directories named boot, sbin, bin, etc etc. The kernel that loads while booting the system is kept in /boot. The conguration les are kept in /etc. /sbin and /bin holds programs that implements many of the shell commands. Most of the application programs are kept in /usr/bin area. The boot loader program is the one provides the selection of OS to boot, when you power on the system. GRUB is the boot loader used by most of the GNU/Linux systems.
Bibliography
[2] http://en.wikipedia.org/wiki/List_of_curves [3] http://www.gap-system.org/~history/Curves/Curves.html [4] http://www.gap-system.org/~history/Curves/Ellipse.html [5] http://mathworld.wolfram.com/ [6] http://www.scipy.org/Numpy_Example_List [7] http://docs.scipy.org/doc/ [8] http://numericalmethods.eng.usf.edu/mws/gen/07int/index.html [9] http://www.angelre.com/art2/fractals/lesson2.htm [10] http://www.zyka.umk.pl/nrbook/bookcpdf.html [11] http://www.mathcs.emory.edu/ccs/ccs315/ccs315/ccs315.html [12] Numerical Methods in Engineering with Python by Jaan Kiusalaas
110