self _listofrows[rownewnode else prednode next newnode scales the matrix by the given scalar def scalebyselfscalar )for row in rangeself numrows(curnode self _listofrows[rowwhile curnode is not none curnode value *scalar curnode curnode next creates and returns new matrix that is the transpose of this matrix def transposeself )matrix additionnewmatrix self rhsmatrix def __add__selfrhsmartrix make sure the two matrices have the correct size assert rhsmatrix numrows(=self numrows(and rhsmatrix numcols(=self numcols()"matrix sizes not compatable for adding create new sparse matrix of the same size newmatrix sparsematrixself numrows()self numcols(add the elements of this matrix to the new matrix for row in rangeself numrows(curnode self _listofrows[rowwhile curnode is not none newmatrix[rowcurnode colcurnode value curnode curnode next add the elements of the rhsmatrix to the new matrix for row in rangerhsmatrix numrows(curnode rhsmatrix _listofrows[rowwhile curnode is not none value newmatrix[rowcurnode colvalue +curnode value newmatrix[rowcurnode colvalue curnode curnode next return the new matrix return newmatrix -matrix subtraction and multiplication --def __sub__selfrhsmatrix def __mul__selfrhsmatrix storage class for creating matrix element nodes class _matrixelementnode def __init__selfcolvalue self col col self value value self next none