v9.1.1
Menu

Matrix Multiplication

easyFREEMatrices & TransformationsLinear AlgebraCV: Mathematical Foundations

Implement matrix multiplication for two matrices A and B. This operation is crucial in linear algebra and computer vision, as it represents the composition of linear transformations.

Given matrices AA of size m×nm \times n and BB of size n×pn \times p, the product C=A×BC = A \times B is an m×pm \times p matrix.

  1. Initialize the result matrix CC with zeros.
  2. For each element CijC_{ij}, compute the dot product of row ii from AA and column jj from BB. The resulting matrix CC represents the composition of the transformations represented by AA and BB.
Cij=∑k=1nAik⋅BkjC_{ij} = \sum_{k=1}^{n} A_{ik} \cdot B_{kj}

This technique is widely used in image processing and machine learning applications.

Example

Input
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
Output
[[19, 22], [43, 50]]
Reasoning
  • Inputs: AA and BB are both 2×22 \times 2. The number of columns of AA (2) equals the number of rows of BB (2), so the product exists and is 2×22 \times 2.
  • Entry (row 1, column 1): pair row 1 of AA, (1,2)(1, 2), with column 1 of BB, (5,7)(5, 7): 1⋅5+2⋅7=5+14=191 \cdot 5 + 2 \cdot 7 = 5 + 14 = 19.
  • Entry (row 1, column 2): row (1,2)(1, 2) with column (6,8)(6, 8): 6+16=226 + 16 = 22.
  • Second row: row (3,4)(3, 4) with (5,7)(5, 7) gives 15+28=4315 + 28 = 43, and with (6,8)(6, 8) gives 18+32=5018 + 32 = 50.
  • Output: C=(19224350)C = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}, i.e. [[19, 22], [43, 50]]. Each entry already has fewer than 4 decimal places, so rounding leaves it unchanged.
  • Notice: order matters. Swapping the inputs gives BA=(23343146)BA = \begin{pmatrix} 23 & 34 \\ 31 & 46 \end{pmatrix}, a different matrix, because composing transformations is not commutative.

Constraints

  • Matrix A has dimensions m×n, Matrix B has dimensions n×p
  • 1 ≤ m, n, p ≤ 100
  • Elements are floating-point numbers
  • Round each element of the result to 4 decimal places

Learn this in

More from CV: Mathematical Foundations

solution.py

Test results

Sign in to run your code against the test cases.