Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def test_determinant_11(self):
     self.assertEqual(matrix.find_det([[1]]), 1, "Should be 1")
Ejemplo n.º 4
0
 def test_determinant_1(self):
     self.assertIs(matrix.find_det([1]), False, "Should be False")
Ejemplo n.º 5
0
 def test_determinant_44(self):
     self.assertEqual(
         matrix.find_det([[1, 2, 3, 4], [0, 1, 2, 3], [0, 0, 1, 2],
                          [0, 0, 0, 2]]), 2, "Should be 2")
Ejemplo n.º 6
0
 def test_determinant_22_tuple(self):
     self.assertEqual(matrix.find_det(((1, 2), (0, 2))), 2, "Should be 2")