Skip to content

Commit 895d125

Browse files
Jay2219Jay Prajapatipre-commit-ci[bot]cclauss
authored
[Add] Kronecker Product (#12023)
* [Add] Kronecker Product * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: failed tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md --------- Co-authored-by: Jay Prajapati <jay.prajapati@upsquare.in> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 0dd623a commit 895d125

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@
970970
* [Count Paths](matrix/count_paths.py)
971971
* [Cramers Rule 2X2](matrix/cramers_rule_2x2.py)
972972
* [Inverse Of Matrix](matrix/inverse_of_matrix.py)
973+
* [Kronecker Product](matrix/kronecker_product.py)
973974
* [Largest Square Area In Matrix](matrix/largest_square_area_in_matrix.py)
974975
* [Matrix Based Game](matrix/matrix_based_game.py)
975976
* [Matrix Class](matrix/matrix_class.py)
@@ -1510,6 +1511,7 @@
15101511
* [Autocomplete Using Trie](strings/autocomplete_using_trie.py)
15111512
* [Barcode Validator](strings/barcode_validator.py)
15121513
* [Bitap String Match](strings/bitap_string_match.py)
1514+
* [Booths Algorithm](strings/booths_algorithm.py)
15131515
* [Boyer Moore Horspool](strings/boyer_moore_horspool.py)
15141516
* [Boyer Moore Search](strings/boyer_moore_search.py)
15151517
* [Camel Case To Snake Case](strings/camel_case_to_snake_case.py)
@@ -1536,6 +1538,7 @@
15361538
* [Jaro Winkler](strings/jaro_winkler.py)
15371539
* [Join](strings/join.py)
15381540
* [Knuth Morris Pratt](strings/knuth_morris_pratt.py)
1541+
* [Largest Smallest Words](strings/largest_smallest_words.py)
15391542
* [Levenshtein Distance](strings/levenshtein_distance.py)
15401543
* [Lower](strings/lower.py)
15411544
* [Manacher](strings/manacher.py)

matrix/kronecker_product.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# @Author : jay2219
2+
# @File : kronecker_product.py
3+
# @Date : 13/10/2024
4+
5+
"""
6+
Perform Kronecker product of two matrices.
7+
https://en.wikipedia.org/wiki/Kronecker_product
8+
"""
9+
10+
11+
def is_2d(matrix: list[list[int]]) -> bool:
12+
"""
13+
>>> is_2d([])
14+
True
15+
>>> is_2d([1, 2])
16+
False
17+
>>> is_2d([[1, 2], [3, 4]])
18+
True
19+
"""
20+
21+
return all(isinstance(matrix, list) and (isinstance(i, list) for i in matrix))
22+
23+
24+
def kronecker_product(
25+
matrix_a: list[list[int]], matrix_b: list[list[int]]
26+
) -> list[list[int]]:
27+
"""
28+
:param matrix_a: A 2-D Matrix with dimension m x n
29+
:param matrix_b: Another 2-D Matrix with dimension p x q
30+
:return: Result of matrix_a ⊗ matrix_b
31+
:raises ValueError: If the matrices are not 2-D.
32+
33+
>>> kronecker_product([[1, 2]], [[5, 6], [7, 8]])
34+
[[5, 6, 10, 12], [7, 8, 14, 16]]
35+
36+
>>> kronecker_product([[1, 2], [4, 5]], [[5, 6], [7, 8]])
37+
[[5, 6, 10, 12], [7, 8, 14, 16], [20, 24, 25, 30], [28, 32, 35, 40]]
38+
39+
>>> kronecker_product([1, 2], [[5, 6], [7, 8]])
40+
Traceback (most recent call last):
41+
...
42+
ValueError: Input matrices must be 2-D.
43+
"""
44+
45+
# Check if the input matrices are valid
46+
if not all((is_2d(matrix_a), is_2d(matrix_b))):
47+
raise ValueError("Input matrices must be 2-D.")
48+
49+
if not matrix_a or not matrix_b:
50+
return []
51+
52+
rows_matrix_a, cols_matrix_a = len(matrix_a), len(matrix_a[0])
53+
rows_matrix_b, cols_matrix_b = len(matrix_b), len(matrix_b[0])
54+
55+
# Resultant matrix dimensions
56+
result = [
57+
[0] * (cols_matrix_a * cols_matrix_b)
58+
for _ in range(rows_matrix_a * rows_matrix_b)
59+
]
60+
61+
for r_index_a in range(rows_matrix_a):
62+
for c_index_a in range(cols_matrix_a):
63+
for r_index_b in range(rows_matrix_b):
64+
for c_index_b in range(cols_matrix_b):
65+
result[r_index_a * rows_matrix_b + r_index_b][
66+
c_index_a * cols_matrix_b + c_index_b
67+
] = matrix_a[r_index_a][c_index_a] * matrix_b[r_index_b][c_index_b]
68+
69+
return result
70+
71+
72+
if __name__ == "__main__":
73+
import doctest
74+
75+
doctest.testmod()

0 commit comments

Comments
 (0)