Решение СЛАУ метдом вращения
from matrix_np_api import *
def solve_with_rotation(m):
"""Solve system with rotation method.
:param m: numpy matrix
:return: None
"""
n = m.shape[0]
for i in range(n-1):
for j in range(i + 1, n):
c = m[i, i] / (m[i, i]**2 + m[j, i]**2) ** .5
s = m[j, i] / (m[i, i]**2 + m[j, i]**2) ** .5
tmp1 = m[i, :] * c + m[j, :] * s
tmp2 = m[i, :] * -s + m[j, :] * c
m[i, :] = tmp1
m[j, :] = tmp2
if is_singular(m):
print('The system has infinite number of answers...')
return
x = np.matrix([0.0 for i in range(n)]).T
for k in range(n - 1, -1, -1):
x[k, 0] = (m[k, -1] - m[k, k:n] * x[k:n, 0]) / m[k, k]
display_results(x)
def is_singular(m):
"""Check matrix for nonsingularity.
:param m: matrix (list of lists)
:return: True if system is nonsingular
"""
return np.any(np.diag(m) == 0)
m = read_matrix('rotation_matrix.txt')
solve_with_rotation(m)