IC152 Lab Assignment 7 Font Size 14
IC152 Lab Assignment 7 Font Size 14
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
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.
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.
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.