matrix.matrix_multiplication_recursion

Perform matrix multiplication using a recursive algorithm. https://en.wikipedia.org/wiki/Matrix_multiplication

Attributes

Matrix

matrices

matrix_1_to_4

matrix_5_to_8

matrix_5_to_9_high

matrix_5_to_9_wide

matrix_a

matrix_count_up

matrix_unordered

Functions

is_square(→ bool)

matrix_multiply(→ Matrix)

matrix_multiply_recursive(→ Matrix)

Module Contents

matrix.matrix_multiplication_recursion.is_square(matrix: Matrix) bool
>>> is_square([])
True
>>> is_square(matrix_1_to_4)
True
>>> is_square(matrix_5_to_9_high)
False
matrix.matrix_multiplication_recursion.matrix_multiply(matrix_a: Matrix, matrix_b: Matrix) Matrix
>>> matrix_multiply(matrix_1_to_4, matrix_5_to_8)
[[19, 22], [43, 50]]
matrix.matrix_multiplication_recursion.matrix_multiply_recursive(matrix_a: Matrix, matrix_b: Matrix) Matrix
Parameters:
  • matrix_a – A square Matrix.

  • matrix_b – Another square Matrix with the same dimensions as matrix_a.

Returns:

Result of matrix_a * matrix_b.

Raises:

ValueError – If the matrices cannot be multiplied.

>>> matrix_multiply_recursive([], [])
[]
>>> matrix_multiply_recursive(matrix_1_to_4, matrix_5_to_8)
[[19, 22], [43, 50]]
>>> matrix_multiply_recursive(matrix_count_up, matrix_unordered)
[[37, 61, 74, 61], [105, 165, 166, 129], [173, 269, 258, 197], [241, 373, 350, 265]]
>>> matrix_multiply_recursive(matrix_1_to_4, matrix_5_to_9_wide)
Traceback (most recent call last):
    ...
ValueError: Invalid matrix dimensions
>>> matrix_multiply_recursive(matrix_1_to_4, matrix_5_to_9_high)
Traceback (most recent call last):
    ...
ValueError: Invalid matrix dimensions
>>> matrix_multiply_recursive(matrix_1_to_4, matrix_count_up)
Traceback (most recent call last):
    ...
ValueError: Invalid matrix dimensions
matrix.matrix_multiplication_recursion.Matrix
matrix.matrix_multiplication_recursion.matrices
matrix.matrix_multiplication_recursion.matrix_1_to_4 = [[1, 2], [3, 4]]
matrix.matrix_multiplication_recursion.matrix_5_to_8 = [[5, 6], [7, 8]]
matrix.matrix_multiplication_recursion.matrix_5_to_9_high = [[5, 6], [7, 8], [9]]
matrix.matrix_multiplication_recursion.matrix_5_to_9_wide = [[5, 6], [7, 8, 9]]
matrix.matrix_multiplication_recursion.matrix_a = [[1, 2], [3, 4]]
matrix.matrix_multiplication_recursion.matrix_count_up = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
matrix.matrix_multiplication_recursion.matrix_unordered = [[5, 8, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1], [2, 6, 10, 14]]