Simple 3x3 matrix inverse code (C++)

后端 未结 13 2085
花落未央
花落未央 2020-12-04 19:35

What\'s the easiest way to compute a 3x3 matrix inverse?

I\'m just looking for a short code snippet that\'ll do the trick for non-singular matrices, possibly using C

13条回答
  •  死守一世寂寞
    2020-12-04 19:57

    I went ahead and wrote it in python since I think it's a lot more readable than in c++ for a problem like this. The function order is in order of operations for solving this by hand via this video. Just import this and call "print_invert" on your matrix.

    def print_invert (matrix):
      i_matrix = invert_matrix (matrix)
      for line in i_matrix:
        print (line)
      return
    
    def invert_matrix (matrix):
      determinant = str (determinant_of_3x3 (matrix))
      cofactor = make_cofactor (matrix)
      trans_matrix = transpose_cofactor (cofactor)
    
      trans_matrix[:] = [[str (element) +'/'+ determinant for element in row] for row in trans_matrix]
    
      return trans_matrix
    
    def determinant_of_3x3 (matrix):
      multiplication = 1
      neg_multiplication = 1
      total = 0
      for start_column in range (3):
        for row in range (3):
          multiplication *= matrix[row][(start_column+row)%3]
          neg_multiplication *= matrix[row][(start_column-row)%3]
        total += multiplication - neg_multiplication
        multiplication = neg_multiplication = 1
      if total == 0:
        total = 1
      return total
    
    def make_cofactor (matrix):
      cofactor = [[0,0,0],[0,0,0],[0,0,0]]
      matrix_2x2 = [[0,0],[0,0]]
      # For each element in matrix...
      for row in range (3):
        for column in range (3):
    
          # ...make the 2x2 matrix in this inner loop
          matrix_2x2 = make_2x2_from_spot_in_3x3 (row, column, matrix)
          cofactor[row][column] = determinant_of_2x2 (matrix_2x2)
    
      return flip_signs (cofactor)
    
    def make_2x2_from_spot_in_3x3 (row, column, matrix):
      c_count = 0
      r_count = 0
      matrix_2x2 = [[0,0],[0,0]]
      # ...make the 2x2 matrix in this inner loop
      for inner_row in range (3):
        for inner_column in range (3):
          if row is not inner_row and inner_column is not column:
            matrix_2x2[r_count % 2][c_count % 2] = matrix[inner_row][inner_column]
            c_count += 1
        if row is not inner_row:
          r_count += 1
      return matrix_2x2
    
    def determinant_of_2x2 (matrix):
      total = matrix[0][0] * matrix [1][1]
      return total - (matrix [1][0] * matrix [0][1])
    
    def flip_signs (cofactor):
      sign_pos = True 
      # For each element in matrix...
      for row in range (3):
        for column in range (3):
          if sign_pos:
            sign_pos = False
          else:
            cofactor[row][column] *= -1
            sign_pos = True
      return cofactor
    
    def transpose_cofactor (cofactor):
      new_cofactor = [[0,0,0],[0,0,0],[0,0,0]]
      for row in range (3):
        for column in range (3):
          new_cofactor[column][row] = cofactor[row][column]
      return new_cofactor
    

提交回复
热议问题