algorithm analysis modifying an element the setitem method for the sparsematrix class is bit more involved than that for the matrix class the value of an element cannot be directly set as was done when using the - array insteadthere are four possible conditions the element is in the list (and thus non-zeroand the new value is non-zero the element is in the listbut the new value is zeroturning the element into zero element the element is not currently in the list and the new value is non-zero the element is not currently in the listand the new value is zero the step in implementing the setitem methodas shown in lines - of listing is to determine if the element is in the list using the findposition(helper method if the entry is in the listwe either change the corresponding element to the new value if it is non-zero or we remove the entry from the list when the new value is zero on the other handif there is no entry for the given elementthen new matrixelement object must be created and appended to the list of coursethis is only done if the new value is non-zero matrix scaling scaling matrix requires multiplying each element of the matrix by given scale factor since the zero elements of the matrix are not affected by the scale factorthe implementation of this operation for the sparse matrix is as simple as traversing the list of matrixelement objects and scaling the corresponding value matrix addition in the add(method of the matrix class implemented in we iterated over the - array and added the valueselement by elementand stored the results in the corresponding element of the new matrix we could use the same loop structure shown here for the sparsematrix classadd the corresponding elements in the two matrices for in rangeself numrows(for in rangeself numcols(newmatrixrc selfrc rhsmatrixrc return newmatrix given matrix of size nthis implementation of the add operation requires ( time if the sparse matrix contains significant number of zero elementsthis can be inefficient insteadonly the non-zero elements contained in the two sparse matrices must be considered when adding to matrices the nested loops can be replaced with two separate loops to reduce the number of required iterations the new solution for sparse matrix addition requires four steps