def projection(X, Y):
rankX = rank(X)
rankY = rank(Y)
# rank, or dimension, or the original space
rankO = rankX + rankY
# check if two subspaces have the same shapes
if X.shape != Y.shape:
raise Exception('The two subspaces do not have the same shapes')
# check if O is singular
if la.det(np.hstack((X, Y))) == 0:
raise Exception('X + Y is not the direct sum of the original space')
# check whether each subspace is of full column/row rank
if rankX < min(X.shape):
raise Exception('subspace X is not of full rank')
elif rankY < min(Y.shape):
raise Exception('subspace Y is not of full rank')
# X and Y are of full column rank
elif rankX == X.shape[1] & rankY == Y.shape[1]:
return np.hstack((X, np.zeros((X.shape[0], rankO - rankX)))).dot(la.inv(np.hstack((X, Y))))
# X and Y are of full row rank
elif rankX == X.shape[0] & rankY == Y.shape[0]:
return np.vstack((X, np.zeros((rankO - rankX, X.shape[1])))).dot(la.inv(np.vstack(X, Y)))
# orthogonal projection matrix
评论列表
文章目录