-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.py
More file actions
45 lines (29 loc) · 958 Bytes
/
task1.py
File metadata and controls
45 lines (29 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
def input_matrix(n):
matrix = []
for el in range(n):
matrix.append(list(map(float, input().split())))
return matrix
def input_vector(n):
return list(map(float, input().split()))
def mofidy_output_matrix(matrix):
answer = []
for el in matrix:
answer.append(' '.join(map(str, el)))
return '\n'.join(answer)
def sherman_morrison():
n, i = list(map(int, input().split()))
matrix_a = np.array(input_matrix(n))
matrix_b = np.array(input_matrix(n))
vector_x = np.array(input_vector(n))
vector_z = matrix_b.dot(vector_x)
if vector_z[i - 1] == 0:
return 'NO'
vector_l = vector_z.copy()
vector_l[i - 1] = -1
vector_l_cover = -1 / vector_z[i - 1] * vector_l
matrix_m = np.eye(n)
matrix_m[:, i - 1] = vector_l_cover
return f'YES\n{mofidy_output_matrix(matrix_m.dot(matrix_b))}'
if __name__ == '__main__':
print(sherman_morrison())