This question evaluates understanding of sparse matrix representations, algorithmic efficiency for linear algebra operations, and the ability to manipulate sparse data structures and aggregate non-zero entries.
Design a way to store a sparse matrix (most entries are zero) and implement efficient operations.
You are given matrices using their non-zero entries:
A
has shape
(m, n)
and is represented as a list of triples
(row, col, value)
containing only entries where
value != 0
.
B
has shape
(n, p)
(for multiplication) and/or shape
(m, n)
(for addition), represented the same way.
Implement the following:
A + B
(only when shapes match), returning the result in the same sparse triple-list form (do not output explicit zeros).
A × B
, returning the result in sparse triple-list form.
Constraints
1e5
in each direction), but the number of non-zero entries
k
is much smaller than
m*n
.
Output requirements