0% found this document useful (0 votes)
20 views

Lab5 Dsa

The document contains 4 questions related to sparse matrix representations in Python. Question 1 converts a matrix to a list and back. Question 2 similarly converts a nested list representing a matrix to a flat list and back. Question 3 takes such a flat list and constructs a sparse matrix from it. Question 4 demonstrates converting a dense matrix to a sparse Compressed Sparse Row representation and back using SciPy routines.

Uploaded by

Sidra Faruqi
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)
20 views

Lab5 Dsa

The document contains 4 questions related to sparse matrix representations in Python. Question 1 converts a matrix to a list and back. Question 2 similarly converts a nested list representing a matrix to a flat list and back. Question 3 takes such a flat list and constructs a sparse matrix from it. Question 4 demonstrates converting a dense matrix to a sparse Compressed Sparse Row representation and back using SciPy routines.

Uploaded by

Sidra Faruqi
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/ 2

Q1

mat =[[4],[3,-5],[1,6,2],[8,0,5,9]]
U=[]

#matrix to list
for j in range(len(mat)):
for k in range(j+1):
U.append(mat[j][k])

print("list: ",U)
#list to matrix
A=[]
for j in range(len(mat)):
r=[]
for k in range(len(mat)):
if k>j:
r.append(0)
else:
r.append(U[int(0.5*j*(j+1)+k)])
A.append(r)

print("matrix: ",A)

OUTPUT :
list: [4, 3, -5, 1, 6, 2, 8, 0, 5, 9]
matrix: [[4, 0, 0, 0], [3, -5, 0, 0], [1, 6, 2, 0], [8, 0, 5, 9]]

Q2

B=[[5,-7],[1,4,3],[9,-3,6],[2,4]]
U=[]
for j in range(len(B)):
if j==0 or j == len(B)-1:
for k in range(2):
U.append(B[j][k])
else:
for k in range(3):
U.append(B[j][k])
print("list: ",U)

OUTPUT :
list: [5, -7, 1, 4, 3, 9, -3, 6, 2, 4]
Q3

U= [5, -7, 1, 4, 3, 9, -3, 6, 2, 4]


B=[]
n=4 #matrix order
p=0
for i in range(n):
r=[]
for j in range(n):
if i-1<=j<=i+1:
r.append(U[p])
p+=1
else:
r.append(0)
B.append(r)

print(B)

OUTPUT
[[5, -7, 0, 0], [1, 4, 3, 0], [0, 9, -3, 6], [0, 0, 2, 4]]
Q4

from scipy.sparse import *


import numpy as np

# sparse matrix
mat = [[11,0,0,0,0,0],[22,33,0,0,88,0],[44,55,66,0,0,99]]

# make dense array of input sparse matrix


dens_arr = np.array(mat)
print("dense array : ",dens_arr)

#convert into csr


csr_mat= csr_matrix(dens_arr) # making a sparse matrix
print("compressed sparsed row : ",csr_mat)

#convert to dense again


dens_arr2 = csr_matrix.todense(csr_mat)
print("dense array : ",dens_arr2)

OUTPUT

C:\python3\python.exe C:/Users/MAHEEN/Desktop/CIS-3-FALL-19-20/ds_lab_codes/csr_alternate.py

dense array : [[11 0 0 0 0 0]


[22 33 0 0 88 0]
[44 55 66 0 0 99]]

compressed sparsed row :


(0, 0) 11
(1, 0) 22
(1, 1) 33
(1, 4) 88
(2, 0) 44
(2, 1) 55
(2, 2) 66
(2, 5) 99

dense array : [[11 0 0 0 0 0]


[22 33 0 0 88 0]
[44 55 66 0 0 99]]

Process finished with exit code 0

You might also like