code: def kabsch(P, Q):
      
  
      # Computation of the covariance matrix
      C = np.dot(np.transpose(P), Q)
  
      # Computation of the optimal rotation matrix
      # This can be done using singular value decomposition (SVD)
      # Getting the sign of the det(V)*(W) to decide
      # whether we need to correct our rotation matrix to ensure a
      # right-handed coordinate system.
      # And finally calculating the optimal rotation matrix U
      # see http://en.wikipedia.org/wiki/Kabsch_algorithm
      V, S, W = np.linalg.svd(C)
      d = (np.linalg.det(V) * np.linalg.det(W)) < 0.0
  
      if d:
          S[-1] = -S[-1]
          V[:, -1] = -V[:, -1]
  
      # Create Rotation matrix U
      U = np.dot(V, W)
  
      return U
docs: Using the Kabsch algorithm with two sets of paired point P and Q, centered
      around the centroid. Each vector set is represented as an NxD
      matrix, where D is the the dimension of the space.
  
      The algorithm works in three steps:
      - a centroid translation of P and Q (assumed done before this function
        call)
      - the computation of a covariance matrix C
      - computation of the optimal rotation matrix U
  
      For more info see http://en.wikipedia.org/wiki/Kabsch_algorithm
  
      Parameters
      ----------
      P : array
          (N,D) matrix, where N is points and D is dimension.
      Q : array
          (N,D) matrix, where N is points and D is dimension.
  
      Returns
      -------
      U : matrix
          Rotation matrix (D,D)