Skip to content

Commit 62b8c9f

Browse files
authored
Added matrix permanent operation
1 parent e3773db commit 62b8c9f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: matrix/matrix_operation.py

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ def determinant(matrix: list[list[int]]) -> Any:
138138
)
139139

140140

141+
def permanent(matrix: list[list[int]]) -> Any:
142+
"""
143+
>>> permanent([[1, 2], [3, 4]])
144+
10
145+
>>> permanent([[1.5, 2.5], [3, 4]])
146+
13.5
147+
"""
148+
if len(matrix) == 1:
149+
return matrix[0][0]
150+
151+
return sum(
152+
x * permanent(minor(matrix, 0, i)) for i, x in enumerate(matrix[0])
153+
)
154+
155+
141156
def inverse(matrix: list[list[int]]) -> list[list[float]] | None:
142157
"""
143158
>>> inverse([[1, 2], [3, 4]])
@@ -193,6 +208,7 @@ def main() -> None:
193208
print(f"Identity: {identity(5)}\n")
194209
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
195210
print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")
211+
print(f"Permanent of {matrix_b} = {permanent(matrix_b)} \n")
196212
print(f"Inverse of {matrix_d} = {inverse(matrix_d)}\n")
197213

198214

0 commit comments

Comments
 (0)