Experiment No 1 : Familiarization of the Computing Tool

Objective

  • To Familiarize with scientific computing tool

Learning outcomes

  • Needs and requirements in scientific computing

  • Familiarization of Python programming language and Google Colab for scientific computing experiments

  • Familiarization of data types in Python language used.

  • Familiarization of the syntax of while, for, if statements.

  • Basic syntax and execution of small scripts.

Language used

Python


Theory

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 Matplotlib alias pylab provides publication quality plotting libraries.

Unlike Matlab, or R, Python does not come with a pre-bundled set of modules for scientific computing. Below are the basic building blocks that can be combined to obtain a scientific computing environment:

Python, a generic and modern computing language

  • The language: flow control, data types (string, int), data collections (lists, dictionaries), etc.

  • Modules of the standard library: string processing, file management, simple network protocols.

  • A large number of specialized modules or applications written in Python: web framework, etc. … and scientific computing.

  • Development tools (automatic testing, documentation generation)


Core numeric libraries

Numpy: numerical computing with powerful numerical arrays objects, and routines to manipulate them.

Scipy : high-level numerical routines. Optimization, regression, interpolation, etc.

Matplotlib : 2-D visualization, “publication-ready” plots http://matplotlib.org/


Google Colab – Introduction

Google is quite aggressive in AI research. Over many years, Google developed an AI framework called TensorFlow and a development tool called Colaboratory. Today TensorFlow is open-sourced and since 2017, Google made Colaboratory free for public use. Colaboratory is now known as Google Colab or simply Colab.

Another attractive feature that Google offers to the developers is the use of GPU. Colab supports GPU and it is totally free. The reasons for making it free for the public could be to make its software a standard in the academics for teaching machine learning and data science. It may also have a long term perspective of building a customer base for Google Cloud APIs which are sold per-use basis.

What Colab Offers

As a programmer, you can perform the following using Google Colab.

  • Write and execute code in Python

  • Document your code that supports mathematical equations

  • Create/Upload/Share notebooks

  • Import/Save notebooks from/to Google Drive

  • Import/Publish notebooks from GitHub

  • Import external datasets e.g. from Kaggle

  • Integrate PyTorch, TensorFlow, Keras, OpenCV

  • Free Cloud service with free GPU

Your First Colab Notebook

Follow the steps that have been given wherever needed.

Note: As Colab implicitly uses Google Drive for storing your notebooks, ensure that you are logged in to your Google Drive account before proceeding further.

Step 1: Open the following URL in your browser: https://colab.research.google.com

Your browser would display the following screen (assuming that you are logged into your Google Drive):

Step 2: Click on the NEW NOTEBOOK link at the bottom of the screen. A new notebook would open up as shown in the screen below.

By default, the notebook uses the naming convention UntitledXX.ipynb. To rename the notebook, click on this name and type in the desired name in the edit box as shown below :

Entering Code

You will now enter a trivial Python code in the code window and execute it. Enter the following two Python statements in the code window:

Executing Code

To execute the code, click on the arrow on the left side of the code window. After a while, you will see the output underneath the code window

Adding Code Cells

To add more code to your notebook, select the following menu options: insert → code cell

Or alternatively click on + code on top

A new code cell will be added underneath the current cell. Add the following two statements in the newly created code window:

To run the entire code in your notebook without an interruption, execute the following menu options: Runtime → Run all

Data types in Python 

Python is an object oriented language and everything in Python is an object.In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: 

Text Type : str

Numeric Types : int, float, complex

Sequence Types : list, tuple, range

Mapping Type : dict

Set Types : set, frozenset

Boolean Type : bool

Binary Types : bytes, bytearray, memoryview



Python Conditions 

If statements

Python supports the usual logical conditions from mathematics:

Equals : a == b

Not Equals : a != b

Less than : a < b

Less than or equal to : a <= b

Greater than : a > b

Greater than or equal to : a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword. If statement, without indentation (will raise an error):

Syntax

The syntax of the if...else statement is −

if expression:

   statement(s)

else:

   statement(s)

Example 1

a = 33

b = 200

if b > a:

  print("b is greater than a")


Output

b is greater than a


Elif

The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".

Example 2

a = 33

b = 33

if b > a:

  print("b is greater than a")

elif a == b:

    print("a and b are equal")

Output

a and b are equal

Else

The else keyword catches anything which isn't caught by the preceding conditions.

syntax

if expression1:

   statement(s)

elif expression2:

   statement(s)

elif expression3:

   statement(s)

else:

   statement(s)

Example 3

a=6

b=11

if a>b:

  print("a is greater")

elif b>a:

  print("b is greater")

else:

  print("a and b are equal")

Output

b is greater

Python Loops

Python has two primitive loop commands:

  • while loops

  • for loops

The while Loop

A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

Syntax

The syntax of a while loop in Python programming language is −

while expression:

   statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

Example 1

i = 1

while i < 7:

  print(i)

  i += 1

Output

1

2

3

4

5

6

Example 2

count = 0

while count < 5:

   print(count, " is  less than 5")

   count = count + 1

else:

   print(count, " is not less than 5")

Output

0  is  less than 5

1  is  less than 5

2  is  less than 5

3  is  less than 5

4  is  less than 5

5  is not less than 5


For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Syntax

for iterating_var in sequence:

statements(s)

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.



Example 1

fruits = ["apple", "banana", "cherry"]

for x in fruits:

  print(x)

Output

apple

banana

cherry

Example 2

A = range(1,10,2)

for i in A:

  print(i)

Results

1

3

5

7

9

Inference

Familiarized and understood Python based scientific computing tool and its basics 


No comments:

Post a Comment

Signals & System Lect 15 | Stable & Unstable Systems | Solved examples

  ECT 204 SIGNALS AND SYSTEMS Topics covered  00:00 - Introduction to Stable & unstable Systems 01:08 - BIBO criteria 03:38 Problem No 1...