You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: SP.m
    path of file: ./repos/hashing-baseline-for-image-retrieval/Method-SP
    
    the code of the file until where you have to start completion: %function [R R2] = SP(X, bit, sparsity, n_iter)
function [R R2] = SP(X, SPparam)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solve:
%   min |XR2'-B|^2 + BETA |XR2'-XR'|^2
%   s.t. R2'R2=I, |R|0<= m
% X: num * dim trianing data matrix. Note in our paper, X is dim * num.
% R and R2: bit * dim projection matrix, R2 is orthogonal and R is sparse
% B: num * bit binary codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bit = SPparam.nbits;
    
function [R R2] = SP(X, bit, sparsity, n_iter)
function [R R2] = SP(X, SPparam)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solve:
%   min |XR2'-B|^2 + BETA |XR2'-XR'|^2
%   s.t. R2'R2=I, |R|0<= m
% X: num * dim trianing data matrix. Note in our paper, X is dim * num.
% R and R2: bit * dim projection matrix, R2 is orthogonal and R is sparse
% B: num * bit binary codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bit = SPparam.nbits;
sparsity = SPparam.sparsity;
n_iter = SPparam.iter;


% initialize with a random rotation
dim = size(X, 2);
R = randn(bit, dim);
B = X*R';
t = (B>0);
B(t) = 1;
B(~t) = -1;

% pre compute pca matrix if bit < dim
if(bit < dim)
    [pc, ~] = eigs(cov(X),bit);
    X_pc = X * pc;   
end