Matrix Multiplication

Advanced

Welcome to the magical world of matrices! In this challenge, you will learn how to multiply two matrices. Imagine you are a wizard who needs to pair up two groups of numbers, and each group is organized in rows and columns like a grid - that's what a matrix is!

Matrix Basics

A matrix is a rectangular array of numbers. In mathematics and computer science, matrix multiplication is an important operation.

To multiply two matrices A and B, the number of columns in A must be equal to the number of rows in B. The resulting matrix, often called C, will have the same number of rows as A and the same number of columns as B.

How to Multiply Matrices

Given:

  • Matrix A (with m x n dimensions)
  • Matrix B (with n x p dimensions)

The resulting matrix, Matrix C, will have dimensions m x p.

Each element C[i][j] in the resulting matrix C is calculated as the sum of products of elements from row i of matrix A and column j of matrix B.

Example:

If you have

A = [ [1, 2, 3], [4, 5, 6] ] B = [ [7, 8], [9, 10], [11, 12] ]

Multiply them to get Matrix C:

C = [ [58, 64], [139, 154] ]

Here's how you compute it:

  • C[0][0] = (17) + (29) + (3*11) = 58
  • C[0][1] = (18) + (210) + (3*12) = 64
  • C[1][0] = (47) + (59) + (6*11) = 139
  • C[1][1] = (48) + (510) + (6*12) = 154

Your Challenge

Implement the function matrix_multiply(A: List[List[int]], B: List[List[int]]) -> List[List[int]] that multiplies two matrices together and returns the resulting matrix.

Function signature:

def matrix_multiply(A: List[List[int]], B: List[List[int]]) -> List[List[int]]: pass

Constraints:

  • The number of columns in A will be equal to the number of rows in B.

Test Cases:

  • Example 1: Multiply matrices as explained above.
  • Example 2: Check with identity matrix.

Have fun multiplying matrices like a math wizard and learn about the magic of numbers! 🎩✨

Geek
Inputs

Arg 1:

[[1,2,3],[4,5,6]]

Arg 2:

[[7,8],[9,10],[11,12]]
Expected Output
[[58, 64], [139, 154]]
Actual Output
Matrix Multiplication [advanced] | Coding for Kids