Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

sparse IVlatrx - A matrix is called a Sparse Matrix if majority of its elements are zero. - Storing a sparse matrix as list-of-lists is

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
sparse IVlatrx - A matrix is called a Sparse Matrix if majority of its elements are zero. - Storing a sparse matrix as list-of-lists is not very efficient. - Alternatively, one can store only the nonzero elements, by using a dictionary in Python. - keys: indices (i,j) of nonzero elements - values: non-zero values to be stored. - In order to keep the size of the matrix, add a special key 'size', which contains a tuple (n,m) containing the matrix dimensions. 0000000011000000010000000000010000000110100010000010000010101000001000000000000000010010100111000000 Sparse Matrix functions Uvport Labe9a tiport random def generate_randon_sparse_natrix (n,n, spar=0.9): Download the file Lab09b.py The file contains two functions that are already implemented. It also imports Lab09a.py as a module. So this file has to be in the same folder. This function generates a randon natrix as a list of lists with a given sparsity. a=[[6 for t in range(m)] for J in range(n)] mHow many elesents should be nonzero nr =tnt((1, spar )nm) pos a random, sample(range( nn), nr) for tnd in pos: n tind = int ( tnd ) n.tnd = Lnd-n_tad*m. return a and ][nind ]=1 der dense_to_sparse( A) : This function converts a list*of-llsts type matrix to the sparse representation stored in a dicttanary 5p=[} n=ten(A) =len(A[0]) for t in range(n): for 1 th range(n): Ifsp[stze]=A[t][j]:sp[(t,j)]=A[t][j]n,m return sp Sparse Matrix functions Your task is to implement def matrix_add_sparse (A,B) : Given two dictionaries, which contain the sparse matrix representations of matrices A and B, this function calculates the Hint: You should not write a nested for-loop structure to iterate over the full size matrix. This would defeat the purpose !!! Sparse Matrix functions In [31]: HLet's generate a sparse matrix of 5iz0 4-by-4 ....: "thonly half the elements are non-zero :A= (generate_randon_sparse_matrix (4,4,.5) ) ...: matrix_display(A) [[1100] In [32]: \#Convert list-of-lists to sparse representation .:A5p= dense to 5sparse(A) {(0,0):1,(0,1):1,(1,1):1,(2,0):1,(2,2):1,(3,1):1,(3,2):1,(3,3):1, 'size': (4,4)} In [33]: ACreate a second matrix .B= (generate_randon_sparse_natr (x(4,4,.5)) : matrix_display (B) : \#Convert list-of-Lists to sparse representation :Bsp= dense_to_sparse(B) [;prtint(Bsp) [[10] {(0,6):1,(1,2):1,(1,3):1,(2,0):1,(2,1):1,(2,2):1,(3,2):1,(3,3):1,5{2e:(4,4)} In [34]: aHere is the sum of matrices A+B (c,) print (natrix_add_sparse (Asp,B sp sp)) {(,):2,(,1):1,(1,1):1,(2,):2,(2,2):2,(3,1):1,(3,2):2,(3,3):2,5{2e:(4,4),(1,2) (1,3):1,(2,1):1)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Discuss the various types of policies ?

Answered: 1 week ago

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago

Question

Explain the contribution of Peter F. Drucker to Management .

Answered: 1 week ago