Implement sparse-matrix operations with explicit error handling.
A matrix A has dimensions r x c and is sparse, meaning most entries are zero.
You may choose a representation (e.g., hashmap/dictionary keyed by (row, col) for non-zero values, or compressed rows), but your implementation should be efficient for sparse inputs.
Implement:
add(A, B)
→ returns
A + B
mul(A, B)
→ returns
A * B
A
and
B
must have the same shape.
A
is
r x k
,
B
must be
k x c
.
r*c
cells.
10^5
non-zero entries per matrix.
10^9
), so dense storage is not allowed.