Sparse Matrix Multiplication
Given two sparse matrices A and B, return the result of AB.
You may assume that A's column number is equal to B's row number. A matrix is sparse if many of its elements are zero. The matrix product of two matrices involves summing up products of corresponding elements. Optimally compute the multiplication so as to take advantage of the sparsity of the matrices.
[ [ [ 1, 0, 0 ], [ -1, 0, 3 ] ], [ [ 7, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 1 ] ] ]
Explanation. The product of the 2x3 matrix `A` and the 3x3 matrix `B` results in a 2x3 matrix. Calculations are carried out considering non-zero entries to optimize operations.
[ [ [ 0, 1 ], [ 1, 1 ] ], [ [ 1, 1 ], [ 1, 1 ] ] ]
Explanation. Each non-zero element from `A` and `B` multiply and sum up to the correct positions in the result matrix.
[ [ [ 0, 2, 0 ], [ 0, 0, 4 ] ], [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ]
Explanation. The sparse nature allows skipping multiplications with zero. The non-zero multiplications provide the results for the matrix.
- The number of columns in `A` is equal to the number of rows in `B`. - Elements of the matrices `A` and `B` are integers. - Both matrices `A` and `B` are not necessarily square matrices.
- Views
- 4