function [Xlearn_new, Xtest_new]=HDF_Rotation(Xlearn,Ylearn,Xtest,Ytest,rho,k)
%%%% X is n-by-p; Y is n-by-1 with values 0 or 1.
%%%% Xlearn is the learning data; Ylearn is the corresponding label
%%%% Xtest is the testing data; Ytest is the corresponding label
%%%% rho is a parameter that can be learned through cross-validation
%%%% k is the number of eigenvectors, i.e. number of columns of U which is
%%%% used to rotate Xlearn and Xtest.

if exist('rho')==0
    rho=0.25;
end
if exist('k')==0
    k=min(size(Xlearn));
end
Xl0=Xlearn(Ylearn==0,:);Xl1=Xlearn(Ylearn==1,:);
mu=mean(Xl0);
Xlearn_new=Xlearn-repmat(mu,length(Ylearn),1);Delta=mean(Xl0)-mean(Xl1);
COV1=cov(Xl0);COV2=cov(Xl1); A=0.5*(COV1+COV2);
[U, ~]=eigs(A+rho*Delta'*Delta,k);
Xlearn_new=(U'*Xlearn_new')';
Xtest_new=(U'*(Xtest-repmat(mu,length(Ytest),1))')';