def solve(a, b): """ :param a: a: matrix, REQUIREMENT: rang(a) should be == n :param b: b: matrix (additional) :return: x: vector of solutions """ n = len(a) if not matrix.is_square_matrix(a): print("It is not possible to find solutions") return False det = matrix.find_det(a) if det == 0: # print(f"Error! det == 0") return False x = [0 for i in range(n)] union_matrix = matrix.find_union_matrix(a) union_matrix = matrix.transpose_matrix(union_matrix) for i in range(n): matrix.mul_row(union_matrix, i, 1 / det) for i in range(n): for j in range(n): x[i] += union_matrix[i][j] * b[j] return x
def solve(a, b, eps=0.01): """ :param a: matrix, REQUIREMENT: rang(a) should be == n :param b: matrix (additional) :param eps: calculation error :return x: vector of solutions """ n = len(a) if not matrix.is_square_matrix(a): print("It is not possible to find solutions") return False x = [0 for i in range(n)] for j in range(n): x[j] = round(b[j] / a[j][j], 3) prev_x = [1 for i in range(n)] is_found = False iterations = 1 while not is_found: for i in range(n): is_found = True if abs(x[i] - prev_x[i]) > eps: is_found = False prev_x[i] = x[i] x[i] = b[i] for j in range(n): if j != i: x[i] -= round(a[i][j] * prev_x[j], 4) x[i] = round(x[i] / a[i][i], 4) iterations += 1 return x, iterations
def solve(a, b): """ :param a: a: matrix, REQUIREMENT: rang(a) should be == n :param b: b: matrix (additional) :return x: x: vector of solutions """ n = len(a) if not matrix.is_square_matrix(a): print("It is not possible to find solutions") return False x = [0 for i in range(n)] for i in range(n): for i2 in range(i + 1, n): count_array(a, b, i, i2) for i in range(n - 1, -1, -1): for i2 in range(i - 1, -1, -1): count_array(a, b, i, i2) for i in range(n): if a[i][i] != 0: x[i] = b[i] / a[i][i] else: # print(f"Error! a[{i}][{i}] == 0") return False return x
def solve(a, b): """ :param a: a: matrix, REQUIREMENT: rang(a) should be == n :param b: b: matrix (additional) :return: x: vector of solutions """ n = len(a) if not matrix.is_square_matrix(a): print("It is not possible to find solutions") return False det = matrix.find_det(a) if det == 0: # print(f"Error! det == 0") return False x = [0 for i in range(n)] new_matrix = [[j for j in range(n)] for i in range(n)] for k in range(n): for i in range(n): for j in range(n): if k == j: new_matrix[i][j] = b[i] else: new_matrix[i][j] = a[i][j] x[k] = matrix.find_det(new_matrix) / det return x
def test_square_11(self): self.assertIs(matrix.is_square_matrix([[1]]), True, "Should be True")
def test_square_1(self): self.assertIs(matrix.is_square_matrix([1]), False, "Should be False")
def test_square_22_tuple(self): self.assertIs(matrix.is_square_matrix(((1, 2), (3, 4))), True, "Should be True")
def test_square_22(self): self.assertIs(matrix.is_square_matrix([[1, 2], [3, 4]]), True, "Should be True")