PIXELBANKv9.1.0
Menu

Sparse Matrix Multiplication

Given two sparse matrices A (m x k) and B (k x n), return their product C (m x n). Optimize for sparsity — skip zero elements.

Input: first line = m k n, second = non-zero entries of A as row:col:val comma-separated (or 'none'), third = non-zero entries of B.

Example:

Input:
2 3 2
0:0:1,0:2:-1,1:1:3
0:0:7,1:0:-2,2:1:1
Output:
7 -1
-6 0
Reasoning:
  • The input matrices are A=[10−1030]A = \begin{bmatrix} 1 & 0 & -1 \\ 0 & 3 & 0 \end{bmatrix} and B=[70−2001]B = \begin{bmatrix} 7 & 0 \\ -2 & 0 \\ 0 & 1 \end{bmatrix}, where zeros are implied for missing entries.
  • To find the product C=ABC = AB, we calculate each element CijC_{ij} as the dot product of row ii in AA and column jj in BB: Cij=∑k=13AikBkjC_{ij} = \sum_{k=1}^{3} A_{ik}B_{kj}.
  • For C00C_{00}, this yields C00=(1)(7)+(0)(−2)+(−1)(0)=7C_{00} = (1)(7) + (0)(-2) + (-1)(0) = 7, and for C01C_{01}, C01=(1)(0)+(0)(0)+(−1)(1)=−1C_{01} = (1)(0) + (0)(0) + (-1)(1) = -1.
  • Similarly, for the second row of CC, C10=(0)(7)+(3)(−2)+(0)(0)=−6C_{10} = (0)(7) + (3)(-2) + (0)(0) = -6 and C11=(0)(0)+(3)(0)+(0)(1)=0C_{11} = (0)(0) + (3)(0) + (0)(1) = 0.

Constraints:

  • 1 <= m, k, n <= 100
  • -100 <= values <= 100
solution.py

Test Results

0/0
Run code to see test results.