Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python: Define a subclass of Matrix called SparseMatrix. In a sparse matrix,

ID: 3805644 • Letter: I

Question

In python:

Define a subclass of Matrix called SparseMatrix. In a sparse matrix, we keep track of the nonzero elements in a dictionary. The keys are tuples (i,j) giving the location of the element and the value assigned to this key gives the value of the nonzero element. If a tuple (i,j) does not appear in the dictionary, the matrix element at that location is assumed to be zero. When creating or modifying a sparse matrix, we don’t want to store zero entries. In the class SparseMatrix override some (but as few as possible) of the parent class Matrix to make sparse matrices work anywhere you can put a Matrix

Explanation / Answer

Dictionary Of Keys based sparse matrix.

This is an efficient structure for constructing sparse matrices incrementally.

This can be instantiated in several ways:

dok_matrix(D)

with a dense matrix, D

dok_matrix(S)

with a sparse matrix, S

dok_matrix((M,N), [dtype])

create the matrix with initial shape (M,N) dtype is optional, defaulting to dtype=’d’

Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.

Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.

Examples

>>>

Attributes

Methods

dtype (dtype) Data type of the matrix shape (2-tuple) Shape of the matrix ndim (int) Number of dimensions (this is always 2) nnz Number of nonzero elements
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote