Skip to main content

Posts

Ex:2 Familiarization of Scientific Computing

Recent posts

Singular Value Decomposition of a Matrix

Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You will Learn You will learn to the singular value decomposition and implement it using Python. You will understand SVD as a tool for trend analysis and dimensionality reduction. You will use SVD as a primitive image compression scheme. Singular Value Decomposition of a Matrix A matrix $\mathbf{A}$ can be decomposed as \begin{equation*} \tilde{\mathbf{A}}=\sum_{i=0}^{r}\lambda_{i}U_{i}V_{i}^{T} \end{equation*} where $U_{i}$ and $V_{i}$ are the singular vectors and $\lambda_{i}$ are the eigen values with $\lambda_{i} j$. The parameter $r$ is the number of eigen values of the matrix. Now let us consider a matrix of random integers and its SV decomposition using the code below. import numpy as np from pylab import * A = np.mat(np.random.randint(1000, size=(1000, 1000))) [a,b,c]=svd(A) bnorm=b*((max(b))**-1) figure(1) plot(bnorm[0:10],'*') p...

Functions in Python

Functions in Python Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You will Learn You will learn the syntax of Python functions You will learn to import the functions to other codes The Syntax The syntax used the keyword $def$ followed the name of the function again followed by a colon (:) that is then followed by an indented code block of instructions. The function def function_name(arg1.arg2,....argN): statements return results A Simple Python Function Needless to say that the statements and the return command should form an indented block. Consider a simple function that returns the sum of the input values. It is realized as the function below. def summ(x,y): return x+y Unlike MATLAB or IDL, it is possible to execute a function in the Python shell. You may type the above function in a Python shell or an enhanced Python shell such as IPython. Once the ...

Iterations and Conditions in Python

Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You Will Learn You will learn the syntax of for, while loops and if statement in Python You will apply these to solve a mathematical problem. For loop Python identifies code blocks by indentation. The for loop has the syntax shown below. Observe the colon at the end of the first line and the indentation at the start of the second line. Whatever is to happen under the for loop should be given as an indented block. If the indentation is wrong, it raises an exception. for i in range(0,10): print i The above code prints the integers from 0 to 9. One can modify the above code for appending the values to a blank array as shown in the following code. from scipy import * arr=[] for i in range(0,10): arr.append(i) print arr The loops being slow, the above method is not encouraged. Instead, vectorized computation is used as shown below. from scipy impor...

Lists, Arrays and Matrices and their Visualization

What You Will Learn You will create lists, arrays and matrices using scipy You will learn to slice, reverse, append and plot arrays Lists in Python Type in the code at the Ipython console from scipy import * x=[1,2,3,4,5] The first line imports all modules in scipy. The second line creates a list in the variable x. Type x at the console and observe the list Type x[0:2] at the console and observe the first two values in the list Type x[3:5] at the console and observe a slice of the list Type x[::-1] at the console and observe the reversed list Type len(x) at the console and observe the length of the list x Type y=[4,5,6] and then type x+y observe the two lists getting concatenated. Type append(x,y) observe a similar result Type z=['apple','orange','pear'] to create a list of strings Arrays using Scipy scipy and numpy provide arrays that support mathematical operations. Type x=array([1,2,3,4,5]) at the cons...

Overview of Scientific Computing

Introduction Scientific computing broadly means computations carried out for scientific or engineering needs. This mostly includes processing of large arrays or matrices, plotting of functions, solving equations, performing mathematical transforms etc very fast while keeping programming hassles at a minimum. Requirements of Scientific Computing The language for scientific computing should be Interpreted should be modular should have easy routines for array, matrix and vectors should have a publication quality plotting library Python for Scientific Computing Python is an interpreted, modular language that is suitable for scientific computing. Modules such as numpy, scipy etc provide many scientific computing tools. The module matpplotlib alias pylab provides publication quality plotting libraries. Python 2.7 and Python 3 are available in all major Linux distros and can be installed as bundle such as anaconda in Windows. An enhanced Python shell ipython...

Matrix Operations using Python

Dr. Hari V S Department of Electronics and Communication College of Engineering Chengannur What You will Learn You will learn to create matrices You will learn to solve linear equations by computing the inverse of a matrix. You will learn to compute the eigen values of a matrix Matrices using Python A $5\times 3$ matrix is created, printed and displayed as an image using the code below. Its transpose is generated by the $transpose()$ function. from scipy import * from pylab import * x=mat([[1,2,3,4,5],[4,5,6,9,15],[7,8,9,23,25]]) print x print shape(x) print transpose(x) matshow(x) show() Solution of Linear Equations Linear systems are represented by matrix equations. See the electrical network in the figure below. All resistances are in $\Omega$. The network is described by the set of equations. \begin{equation} 9I_{1}-4 I_{2}=20 \end{equation} \begin{equation} -4I_{1}-6 I_{2}=-10 \end{equation} \begin{equation} 8I_{3}-3 I_{4}=-20 \end...