-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Callable | ||
|
||
from book.data_structures import Matrix | ||
from solutions.chapter4.section2.exercise2 import matrix_add | ||
from util import range_of | ||
|
||
|
||
def matrix_multiply_by_squaring(A: Matrix, B: Matrix, C: Matrix, matrix_square: Callable[[Matrix, Matrix, int], None], | ||
n: int) -> None: | ||
"""Multiplies two square matrices and adds the result to the third square matrix, using a function for squaring | ||
matrices. | ||
Args: | ||
A: the first square matrix to multiply | ||
B: the second square matrix to multiply | ||
C: the matrix to add the result of the matrix multiplication | ||
matrix_square: a function for squaring matrices. | ||
The first argument is the square matrix to square, the second argument is the matrix to accumulate the | ||
result, and the third argument is the dimension of the input matrix. | ||
n: the dimension of matrices A and B | ||
""" | ||
D = __create_padded_input_matrix(A, B, n) | ||
E = Matrix(2 * n, 2 * n) | ||
matrix_square(D, E, 2 * n) | ||
matrix_add(C, E.submatrix((n + 1, 2 * n), (1, n)), C, n) | ||
|
||
|
||
def __create_padded_input_matrix(A, B, n): | ||
M = Matrix(2 * n, 2 * n) | ||
for i in range_of(1, to=n): | ||
for j in range_of(1, to=n): | ||
M[i, j] = B[i, j] | ||
M[n + i, j] = A[i, j] | ||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters