Skip to content
DeepForgeby svx
All problems
HardLinear Algebrala-181

Kabsch Rotation 2D

Problem

Find the 2D rotation matrix that best aligns point set P to point set Q under the Kabsch algorithm.

Center both sets by their centroids, compute num = sum(px*qy - py*qx) and den = sum(px*qx + py*qy), set theta = atan2(num, den), and return [[cos, -sin], [sin, cos]]. The two sets must have the same number of points.

Hint

Centering removes translation; the optimal angle comes from the cross-covariance terms.

Test cases

case 1
in: [[[1,0],[0,1]],[[0,1],[-1,0]]]
expected: [[0,-1],[1,0]]
case 2
in: [[[0,0],[1,0],[0,1]],[[0,0],[1,0],[0,1]]]
expected: [[1,0],[0,1]]
case 3
in: [[[-1,0],[0,-1]],[[1,0],[0,1]]]
expected: [[-1,0],[0,-1]]
case 4
in: [[[1,0]],[[5,5]]]
expected: [[1,0],[0,1]]
Starter codePython
def kabsch_rotation_2d(P, Q):
    # Your code here
    pass

4 test cases · run your code against them in the editor