mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-04 16:57:32 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -39,8 +39,10 @@ def multiply(matrix_a, matrix_b):
|
||||
rows, cols = _verify_matrix_sizes(matrix_a, matrix_b)
|
||||
|
||||
if cols[0] != rows[1]:
|
||||
raise ValueError(f'Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) '
|
||||
f'and ({rows[1]},{cols[1]})')
|
||||
raise ValueError(
|
||||
f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) "
|
||||
f"and ({rows[1]},{cols[1]})"
|
||||
)
|
||||
for i in range(rows[0]):
|
||||
list_1 = []
|
||||
for j in range(cols[1]):
|
||||
@ -59,7 +61,7 @@ def identity(n):
|
||||
:return: Identity matrix of shape [n, n]
|
||||
"""
|
||||
n = int(n)
|
||||
return [[int(row == column) for column in range(n)] for row in range(n)]
|
||||
return [[int(row == column) for column in range(n)] for row in range(n)]
|
||||
|
||||
|
||||
def transpose(matrix, return_map=True):
|
||||
@ -75,15 +77,15 @@ def transpose(matrix, return_map=True):
|
||||
|
||||
|
||||
def minor(matrix, row, column):
|
||||
minor = matrix[:row] + matrix[row + 1:]
|
||||
minor = [row[:column] + row[column + 1:] for row in minor]
|
||||
minor = matrix[:row] + matrix[row + 1 :]
|
||||
minor = [row[:column] + row[column + 1 :] for row in minor]
|
||||
return minor
|
||||
|
||||
|
||||
def determinant(matrix):
|
||||
if len(matrix) == 1:
|
||||
return matrix[0][0]
|
||||
|
||||
|
||||
res = 0
|
||||
for x in range(len(matrix)):
|
||||
res += matrix[0][x] * determinant(minor(matrix, 0, x)) * (-1) ** x
|
||||
@ -99,10 +101,13 @@ def inverse(matrix):
|
||||
for i in range(len(matrix)):
|
||||
for j in range(len(matrix)):
|
||||
matrix_minor[i].append(determinant(minor(matrix, i, j)))
|
||||
|
||||
cofactors = [[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] for row in range(len(matrix))]
|
||||
|
||||
cofactors = [
|
||||
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
|
||||
for row in range(len(matrix))
|
||||
]
|
||||
adjugate = transpose(cofactors)
|
||||
return scalar_multiply(adjugate, 1/det)
|
||||
return scalar_multiply(adjugate, 1 / det)
|
||||
|
||||
|
||||
def _check_not_integer(matrix):
|
||||
@ -122,8 +127,10 @@ def _verify_matrix_sizes(matrix_a, matrix_b):
|
||||
shape = _shape(matrix_a)
|
||||
shape += _shape(matrix_b)
|
||||
if shape[0] != shape[2] or shape[1] != shape[3]:
|
||||
raise ValueError(f"operands could not be broadcast together with shape "
|
||||
f"({shape[0], shape[1]}), ({shape[2], shape[3]})")
|
||||
raise ValueError(
|
||||
f"operands could not be broadcast together with shape "
|
||||
f"({shape[0], shape[1]}), ({shape[2], shape[3]})"
|
||||
)
|
||||
return [shape[0], shape[2]], [shape[1], shape[3]]
|
||||
|
||||
|
||||
@ -132,13 +139,19 @@ def main():
|
||||
matrix_b = [[3, 4], [7, 4]]
|
||||
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
|
||||
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
|
||||
print('Add Operation, %s + %s = %s \n' %(matrix_a, matrix_b, (add(matrix_a, matrix_b))))
|
||||
print('Multiply Operation, %s * %s = %s \n' %(matrix_a, matrix_b, multiply(matrix_a, matrix_b)))
|
||||
print('Identity: %s \n' %identity(5))
|
||||
print('Minor of %s = %s \n' %(matrix_c, minor(matrix_c, 1, 2)))
|
||||
print('Determinant of %s = %s \n' %(matrix_b, determinant(matrix_b)))
|
||||
print('Inverse of %s = %s\n'%(matrix_d, inverse(matrix_d)))
|
||||
print(
|
||||
"Add Operation, %s + %s = %s \n"
|
||||
% (matrix_a, matrix_b, (add(matrix_a, matrix_b)))
|
||||
)
|
||||
print(
|
||||
"Multiply Operation, %s * %s = %s \n"
|
||||
% (matrix_a, matrix_b, multiply(matrix_a, matrix_b))
|
||||
)
|
||||
print("Identity: %s \n" % identity(5))
|
||||
print("Minor of %s = %s \n" % (matrix_c, minor(matrix_c, 1, 2)))
|
||||
print("Determinant of %s = %s \n" % (matrix_b, determinant(matrix_b)))
|
||||
print("Inverse of %s = %s\n" % (matrix_d, inverse(matrix_d)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Reference in New Issue
Block a user