Introduction To Python Programming For Numerical Computation
Introduction To Python Programming For Numerical Computation
Introduction To Python Programming For Numerical Computation
CHAPTER-1.1 NOTES
Getting Started:
Installation:
Visit the official Python website to download and install Python.
Consider using package managers like Anaconda that come bundled with popular numerical
computing libraries.
Why Python?
Portability
• Scripting language hence easily portable • Python interpreter is supported on most modern
OS’s
Python Interpreter
The system component of Python is the interpreter.
• The interpreter is independent of your code and is required to execute your code.
Two major versions of interpreter are currently available:
• Python 2.7.X (broader support, legacy libraries)
• Python 3.6.X (newer features, better future support)
What is an Object?
Numbers
• Can be integers, decimals (fixed precision), floating points (variable precision), complex
numbers etc.
• Simple assignment creates an object of number type such as:
• a = 3 • b = 4.56 • Supports simple to complex arithmetic operators. • Assignment via numeric
operator also creates a number object:
• c = a / b • a, b and c are numeric objects.
• Try dir(a) and dir(b) . This command lists the functions available for these objects.
Strings
• A string object is a ‘sequence’, i.e., it’s a list of items where each item has a defined position.
• Each character in the string can be referred, retrieved and modified by using its position.
• This order id called the ‘index’ and always starts with 0.
Strings
2
Lists
• List is a more general sequence object that allows the individual items to be of different
types.
• Equivalent to arrays in other languages.
• Lists have no fixed size and can be expanded or contracted as needed.
• Items in list can be retrieved using the index.
• Lists can be nested just like arrays, i.e., you can have a list of lists.
Simple list:
Nested list:
Dictionaries:
Files
3
• File objects are built for interacting with files on the system. • Same object used for any file
type. • User has to interpret file content and maintain integrity
• Numbers, strings and tuples are immutable i.,e cannot be directly changed.
• Lists, dictionaries and sets can be changed in place.
Tuples
• Tuples are immutable lists. • Maintain integrity of data during program execution.
Sets
• Special data type introduced since Python 2.4 onwards to support mathematical set theory
operations.
• Unordered collection of unique items.
• Set itself is mutable, BUT every item in the set has to be an immutable type.
• So, sets can have numbers, strings and tuples as items but cannot have lists or dictionaries as
items.
Python is known for its simplicity and readability, making it an excellent choice for beginners
and experienced developers alike.
1. Comments:
4
Comments start with the # symbol and are ignored by the Python interpreter.
3. Print Statement:
4. Indentation:
Python uses indentation to indicate blocks of code. It's crucial for readability and structure.
5. Control Flow:
5
6. Functions:
7. Lists:
8. Dictionaries:
9. Tuples:
6
Similar to lists, but immutable.
10. Strings:
13. Classes:
7
14. Importing Modules:
Control Structures
The programs we have studied so far have all been sequential, with each line corresponding to
one instruction: this is definitely not optimal.
For example, we have introduced in the previous chapter the concept of lists and arrays, to
avoid having to use many scalar variables to store data (remember that if we were to store the
whole human genome, we would need either 30,000 scalar variables, one for each gene, or a
single array, whose items are the individual genes); if we wanted to perform the same operation
on each of these genes, we would still have to write one line for each gene.
In addition, the programs we have written so far would attempt to perform all their instructions,
once given the input. Again, this is not always desired: we may want to perform some
instructions only if a certain condition is satisfied. Again, Python has thought about these
issues, and offers solutions in the form of control structures: the if structure that allows to
control if a block of instruction need to be executed, and the for structure (and equivalent), that
repeats a set of instructions for a preset number of times. In this chapter, we will look in details
on the syntax and usage of these two structures.
8
Figure : The three main types of flow in a computer program: sequential, in which instructions
are executed successively, conditional, in which the blocks “instructions 1” and “instructions
2” are executed if the Condition is True or False, respectively, and repeating, in which
instructions are repeated over a whole list.
Logical operators Most of the control structure we will see in this chapter test if a condition is
true or false. For programmers, “truth” is easier to define in terms of what is not truth! In
Python, there is a short, specific list of false values:
We can test whether a number is bigger, smaller, or the same as another. Similarly, we can test
if a string comes before or after another string, based on the alphabetical order. All the results
of these tests are TRUE or FALSE. Table 3.1 lists the common comparison operators available
in Python.
Notice that the numeric operators look a little different from what we have learned in Math: this is because Py
9
These comparisons apply both to numeric value and to strings. Note that you can compare
numbers to strings, but the result can be arbitrary: I would strongly advise to make sure that the
types of the variables that are compared are the same.
Conditional structures
If
The most fundamental control structure is the if structure. It is used to protect a block of code
that only needs to be executed if a prior condition is met (i.e. is TRUE). The generate format of
an if statement is:
Else
When making a choice, sometimes you have two different things you want to do, depending
upon the outcome of the conditional. This is done using an if …else structure that has the
following format:
10
Loops
One of the most obvious things to do with an array is to apply a code block to every item in the
array: loops allow you to do that. Every loop has three main parts:
Note the syntax similar to the syntax of an if statement: the : at the end of the condition, and
the indentation of the code block. A for loop is simple: it loops over all possible values of
variable as found in the list listA, executing each time the code block.
For example, the Python program:
While loop
Sometimes, we face a situation where neither Python nor we know in advance how many times
a loop will need to execute. This is the case for example when reading a file: we do not know
in advance how many lines it has. Python has a structure for that: the while loop:
11
The while structure executes the code block as long as the TEST expression evaluates as
TRUE. For example, here is a program that prints the number between 0 and N, where N is
input:
Note that it is important to make sure that the code block includes a modification of the test:
if we had forgotten the line i+=1 in the example above, the while loop would have become an
infinite loop. Note that any for loop can be written as a while loop.
In practice however, it is better to use a for loop, as Python executes them faster
Python provides two functions that can be used to control loops from inside its code block:
break allows you to exit the loop, while continue skips the following step in the loop.
while TEST: code block; >>> N=int(raw_input(“Enter N --> “)) >>> print “Counting numbers
from 0 to “,N,”\n” i=0 while i < N+1: print i,”\n” i+=1 >>> N=int(raw_input(“Enter the last
integer considered --> “)) >>> Sum=0 >>> for i in range(0,N+1,1): Sum+=i**2 >>> print “The
sum of the squares between 0 and “,N,” is “,Sum 33 Here is an example of a program that
counts and prints number from 1 to N (given as input), skipping numbers that are multiples of 5
and stopping the count if the number reached squared is equal to N:
Linear algebra is a branch of mathematics that deals with linear equations and their
representations using vectors and matrices. It’s a fundamental subject in several
12
areas of engineering, and it’s a prerequisite to a deeper understanding of machine
learning.
To work with linear algebra in Python, you can count on SciPy, which is an open-
source Python library used for scientific computing, including several modules for
common tasks in science and engineering.
Of course, SciPy includes modules for linear algebra, but that’s not all. It also
offers optimization, integration, interpolation, and signal processing capabilities. It’s
part of the SciPy stack, which includes several other packages for scientific
computing, such as NumPy, Matplotlib, SymPy, IPython, and pandas.
scipy.linalg includes several tools for working with linear algebra problems, including
functions for performing matrix calculations, such
as determinants, inverses, eigenvalues, eigenvectors, and the singular value
decomposition.
We’re going a step further, using scipy.linalg to study linear systems and build linear
models for real-world problems.
In order to use scipy.linalg, you have to install and set up the SciPy library. Besides
that, you’re going to use Jupyter Notebook to run the code in an interactive
environment. SciPy and Jupyter Notebook are third-party packages that you need to
install. For installation, you can use the conda or pip package manager.
Revisit Working With Linear Systems in Python With scipy.linalg for installation
details.
Note: In Python, NumPy is the most used library for working with matrices and vectors. It uses
a special type called ndarray to represent them. As an example, imagine that you need to create
the following matrix:
With NumPy, you can use np.array() to create it, providing a nested list containing the elements
of each row of the matrix:
Python
Here you have two equations involving two variables. In order to have a linear system, the
values that multiply the variables x₁ and x₂ must be constants, like the ones in this example.
It’s common to write linear systems using matrices and vectors. For example, you can write the
previous system as the following matrix product:
Comparing the matrix product form with the original system, you can notice the elements of
matrix A correspond to the coefficients that multiply x₁ and x₂. Besides that, the values in the
right-hand side of the original equations now make up vector b.
In Working With Linear Systems in Python With scipy.linalg, you’ve seen how to solve linear
systems using scipy.linalg.solve(). Now you’re going to learn how to use determinants to study
the possible solutions and how to solve problems using the concept of matrix inverses.
Solving Problems Using Matrix Inverses and Determinants
Matrix inverses and determinants are tools that allow you to get some information about the
linear system and also to solve it. Before going through the details on how to calculate matrix
inverses and determinants using scipy.linalg, take some time to remember how to use these
structures.
As you may recall from your math classes, not every linear system can be solved. You may
have a combination of equations that’s inconsistent and has no solution. For example, a system
with two equations given by x₁ + x₂ = 2 and x₁ + x₂ = 3 is inconsistent and has no solution.
This happens because no two numbers x₁ and x₂ can add up to both 2 and 3 at the same time.
Besides that, some systems can be solved but have more than one solution. For example, if you
have a system with two equivalent equations, such as x₁ + x₂ = 2 and 2x₁ + 2x₂ = 4, then you
can find an infinite number of solutions, such as (x₁=1, x₂=1), (x₁=0, x₂=2), (x₁=2, x₂=0), and
so on.
A determinant is a number, calculated using the matrix of coefficients, that tells you if there’s
a solution for the system. Because you’ll be using scipy.linalg to calculate it, you don’t need to
care much about the details on how to make the calculation. However, keep the following in
mind:
If the determinant of a coefficients matrix of a linear system is different from zero, then you
can say the system has a unique solution.
If the determinant of a coefficients matrix of a linear system is equal to zero, then the system
may have either zero solutions or an infinite number of solutions.
Now that you have this in mind, you’ll learn how to solve linear systems using matrices.
Using Matrix Inverses to Solve Linear Systems
To understand the idea behind the inverse of a matrix, start by recalling the concept of
the multiplicative inverse of a number. When you multiply a number by its inverse, you get 1
14
as the result. Take 3 as an example. The inverse of 3 is 1/3, and when you multiply these
numbers, you get 3 × 1/3 = 1.
With square matrices, you can think of a similar idea. However, instead of 1, you’ll get
an identity matrix as the result. An identity matrix has ones in its diagonal and zeros in the
elements outside of the diagonal, like the following examples:
The identity matrix has an interesting property: when multiplied by another matrix A of the
same dimensions, the obtained result is A. Recall that this is also true for the number 1, when
you consider the multiplication of numbers.
This allows you to solve a linear system by following the same steps used to solve an equation.
As an example, consider the following linear system, written as a matrix product:
By calling A⁻¹ the inverse of matrix A, you could multiply both sides of the equation by A⁻¹,
which would give you the following result:
This way, by using the inverse, A⁻¹, you can obtain the solution x for the system by
calculating A⁻¹b.
It’s worth noting that while non-zero numbers always have an inverse, not all matrices have an
inverse. When the system has no solution or when it has multiple solutions, the determinant
of A will be zero, and the inverse, A⁻¹, won’t exist.
Now you’ll see how to use Python with scipy.linalg to make these calculations.
Calculating Inverses and Determinants With scipy.linalg
You can calculate matrix inverses and determinants using scipy.linalg.inv() and scipy.linalg.det().
Recall that the linear system for this problem could be written as a matrix product:
Previously, you used scipy.linalg.solve() to obtain the solution 10, 10, 20, 20, 10 for the
variables x₁ to x₅, respectively. But as you’ve just learned, it’s also possible to use the inverse
of the coefficients matrix to obtain vector x, which contains the solutions for the problem. You
have to calculate x = A⁻¹b, which you can do with the following program:
Python
1In [1]: import numpy as np
2 ...: from scipy import linalg
3
4In [2]: A = np.array(
5 ...: [
6 ...: [1, 9, 2, 1, 1],
15
7 ...: [10, 1, 2, 1, 1],
8 ...: [1, 0, 5, 1, 1],
9 ...: [2, 1, 1, 2, 9],
10 ...: [2, 1, 2, 13, 2],
11 ...: ]
12 ...: )
13
14In [3]: b = np.array([170, 180, 140, 180, 350]).reshape((5, 1))
15
16In [4]: A_inv = linalg.inv(A)
17
18In [5]: x = A_inv @ b
19 ...: x
20Out[5]:
21array([[10.],
22 [10.],
23 [20.],
24 [20.],
25 [10.]])
Lines 1 and 2 import NumPy as np, along with linalg from scipy. These imports allow you to
use linalg.inv().
Lines 4 to 12 create the coefficients matrix as a NumPy array called A.
Line 14 creates the independent terms vector as a NumPy array called b. To make it a column
vector with five elements, you use .reshape((5, 1)).
Line 16 uses linalg.inv() to obtain the inverse of matrix A.
Lines 18 and 19 use the @ operator to perform the matrix product in order to solve the linear
system characterized by A and b. You store the result in x, which is printed.
You get exactly the same solution as the one provided by scipy.linalg.solve(). Because this
system has a unique solution, the determinant of matrix A must be different from zero. You can
confirm that it is by calculating it using det() from scipy.linalg:
Python
In [6]: linalg.det(A)
Out[6]:
45102.0
As expected, the determinant isn’t zero. This indicates that the inverse of A, denoted as A⁻¹
and calculated with inv(A), exists, so the system has a unique solution. A⁻¹ is a square matrix
with the same dimensions as A, so the product of A⁻¹ and A results in an identity matrix. In
this example, it’s given by the following:
Python
In [7]: A_inv
Out[7]:
Now that you know the basics of using matrix inverses and determinants, you’ll see how to use
these tools to find the coefficients of polynomials.
17