def random_orthogonal(dim, special=True):
	if dim == 1:
		if 0.5 < np.random.uniform():
			return np.ones((1,1))
		return -np.ones((1,1))
	P = np.random.randn(dim, dim)
	while np.linalg.matrix_rank(P) != dim:
		P = np.random.randn(dim, dim)
	U, S, V = np.linalg.svd(P)
	P = np.dot(U, V)
	if special:
		# Make sure det(P) == 1
		if np.linalg.det(P) < 0:
			P[:, [0, 1]] = P[:, [1, 0]]
	return P

# Code From:
# https://gist.github.com/benanne/2025317
# (Checked for correctness against scipy)

# Log-gamma function for theano
dataset/ETHPy150Open dpkingma/nips14-ssl/anglepy/misc.py/random_orthogonal/swapped operands of `<`