Last active
June 8, 2016 13:29
-
-
Save Zaharid/db11a391dccd62f1e384c2883721ce25 to your computer and use it in GitHub Desktop.
Random rotation of dimension n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import numpy.linalg as la | |
def rand_unit(n): | |
r = np.random.randn(n) | |
return r/la.norm(r) | |
def householder(unit_v): | |
return np.eye(len(unit_v)) - 2*np.outer(unit_v,unit_v) | |
def random_rotation_matrix(n): | |
arr = np.eye(n) | |
for dim in range(1,n): | |
ref = householder(rand_unit(dim+1)) | |
arr[:dim+1,:dim+1] = ref@arr[:dim+1,:dim+1] | |
#Positive determinant | |
if not n%2: | |
arr[0] *= -1 | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment