0% found this document useful (0 votes)
29 views6 pages

IC152 Lab Assignment 7 Font Size 14

The document describes an assignment involving 5 problems related to matrices, norms, and probability. The problems involve generating random matrices, performing matrix operations, searching data structures, solving systems of linear equations, calculating vector and matrix norms, and applying the central limit theorem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

IC152 Lab Assignment 7 Font Size 14

The document describes an assignment involving 5 problems related to matrices, norms, and probability. The problems involve generating random matrices, performing matrix operations, searching data structures, solving systems of linear equations, calculating vector and matrix norms, and applying the central limit theorem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IC152: Assignment 7

Matrices, Norms and Probability


5 questions 60 marks

You must keep filenames as mentioned in the assignment since the


assignments will be auto evaluated via a script. There are 5 problems ,
each for 10 marks. and 10 marks are for viva.

Problem 1: Matrices
a. Write a python script to generate two random matrices and create
text files MatA.txt and MatB.txt to save two different matrices.
i. Take the number of rows and number of columns as command
line arguments: r1 c1 r2 c2. r1 and c1 are rows and columns of
the first matrix, r2 and c2 of second.
ii. Each line of the files should have space separated row
elements, and line number should represent row number. So,
the format of the files will be (a(i,j) means value at ith row and
jth column):
rows, columns
a(1,1) a(1,2) a(1,3) . . . a(1,c)
a(2,1) a(2,2) a(2,3) . . . a(2,c)
.
.
.
a(r,1) a(r,2) a(r,3) . . . a(r,c)
For example: a 2 × 3 matrix can be saved as the format
23
149
467

Save the python filename as problem1a.py.

b. Create two files using previous code (try different inputs for this
question). Take two input files via command line arguments,
perform matrix operations. Using numpy, perform addition and
multiplication of the two matrices and save them in files with name
“addOp.txt” and “multOp.txt” in same format as in previous part (a).
Give an appropriate error message if the matrices are not
compatible and handle corner cases.
Save the python filename as problem1b.py.

Problem 2: Search the file ‘datafile.dat’ for the numbers 100831, 24,
43479 and 70125. Estimate the time taken to search through the entire
data using:
a. A Python list
b. A Python dictionary
c. A Numpy array (ndarray)
Extend the script to repeat the search for 10 times and find mean time
taken for each of the processes.
Note: Time can be estimated by importing time library as follows:
import time
start = time.time()
#execution of the function
end = time.time()
total_time = (start-end)

Save the python filename as problem2.py, and save the times in a file
with name problem2Op.txt with mean time using list in line 1, mean time
using dictionary in line 2, and mean time using Numpy array in line 3.

Problem 3: Write a Python script to solve system of Linear Equations


Ax=B, where A is a N X N matrix with N <=3 and B is N X 1, as follows
(use gaussian elimination or numpy.linalg library):

Take input matrices A and B from the user in the form of files, with their
names as two command line arguments similar to inputs in problem 1b.
a. Check for the nature of the solution (No solution, Unique solution,
Infinite solutions). Save the nature of the solution in the first line of
file “problem3aOp.txt”. Save the python filename as problem3a.py.
b. Solve the System of Linear Equations with Numpy. In case of unique
solutions or infinite solutions, save one of the valid solutions in
“problem3bOp.txt” in the same format as of problem 1a. Save the
python filename as problem3b.py.

Problem 4: Norms
a. Vector Norm
If vector x = [x1, x2, … , xn],
Then, nth norm of vector x = ||x||n = (|x1|n + |x2|n + … + |xn|n)(1/n)
Interestingly,
- norm 0 of x represents the number of non-zero values in
x.
- norm 1 of x represents the sum of absolute values of the
elements in x.
- norm 2 of x represents the square root of the sum of
squared values of the elements in x.
- norm infinity represents the largest magnitude among
absolute values of the elements in x.
Take a matrix in the form of a file, with its name as two
command line arguments similar to one of the inputs in
problem 1b. Find the norm 0, norm 1,norm 2, norm 3, and
‘infinity norm’ of each row and save it in file
“problem4aOp.txt” with a line of space separated values. (line
number should represent row number of matrix. Leftmost
value in each line should represent norm 0, and the right most
value should represent the infinity norm). Handle corner cases
(if any).
Save the python filename as problem4a.py.
b. Matrix Norm
- Norm 1 of the matrix is the maximum column sum after
absolute values are taken.
- Norm 2 of a matrix is equal to the norm 2 of a vector
created by concatenating all its rows together.
- Norm infinity of matrix is maximum row sum after
absolute values are taken.
(you do not need to find norm 0 for the matrix)
Take a matrix in the form of a file, with its name as two
command line arguments similar to one of the inputs in
problem 1b. Find the norm 1,norm 2, and infinity norm of the
input matrix and save it in file “problem4bOp.txt” with a line of
space separated values. (leftmost value should represent
norm 0, and right most value should represent infinity norm).
Handle corner cases (if any).
Save the python filename as problem4b.py.

Problem 5: Statistical Regularity and Central Limit Theorem


a. Consider an experiment where you pick up a ball at random out of
four balls having numbers 0, 1, 2, and 3. Simulate the experiment of
50 trials using a random package (random.randint(0,3)) and plot the
relative frequency of each observed value vs number of trials. Then
repeat the experiment 500 times. Then repeat the experiment 5000
times and 50000 times. Save the three plots as problem5a_50.png,
problem5a_500.png, problem5a_5000.png, and
problem5a_50000.png. Share your observations with the lab TAs or
lab Instructor.
Save the python filename as problem5a.py.
b. Central Limit Theorem states that the distribution of sample means
approximate the normal distribution as sample size gets larger. Plot
the histograms of sample means (observed values) of 5 trials in the
previous experiment, by repeating it 50000 times and verify. Save
the plot as problem5b_5_50000.png.

Run the code with 50 trials instead of 5 in each repetition of 50000.


Save the plot as problem5b_50_50000.png.

Share your observations with the lab TAs or lab Instructor.


Save the python filename as problem5b.py.

Create the folder having your python files and inputs/outputs, with name
having your roll number followed by “_assignment7” (don’t use inverted
commas in folder name), compress the folder with .zip extension and
submit it on moodle.

Important: If you copy the assignment or any of its parts from others or
share with others, our plagiarism softwares will catch it and you will be
awarded 0 marks for the whole assignment or F grade for the course.

You might also like