function [ctrain,ctest] = centerTrTsKernels(trkernel,tskernel)

% [ctrain,ctest] = centerTrainTestKernels(trkernel,tskernel) : centers
% train/test kernels.
% K~ = K - 1_M * K - K * 1_M + 1_M * K * 1_M    1_M = (j * j')/l
% K^test~ = K^test - 1_M' * K - K^test * 1_M + 1_M' * K * 1_M      K^test =
% RBFKernel(test,train) 

l = size(trkernel,1);
j = ones(l,1);
ctrain = trkernel - (j*sum(trkernel,1))/l - (sum(trkernel,2)*j')/l + (j*sum(trkernel,1)*j*j')/(l^2);
ctrain = (ctrain + ctrain')/2;

if( nargin > 1 )
    tk =  (1/l)*sum(trkernel,1); % (1 x l)
    tl = ones(size(tskernel,1),1); % (n x 1)
    ctest = tskernel - ( tl * tk); % ( n x l )  K^test - 1_M' * K
    tk = (1/(size(tskernel,2)))*sum(ctest,2); % ( n x 1 )   
    ctest = ctest - (tk * j'); 
end