function K=gencheb(x,y,n)
% This is the code to generate first kind of Chebyshev kernels...
% However, a similar function can be written to genarete the second kind of
% Chebyshev polynomials too.
%
%
%
%  Usage  K=gencheb(x,y,n)
%  where K is the scalar value as the output of the kernel function
%  x and y are the input "row" vectors, and n is the polynomial order 
%  Example: 
%  x=[0.0461    0.0923    0.1384    0.1845];
%  y=[0.1775    0.1775         0    0.3549];
%  K=gencheb(x,y,6)  % for the 6th order polynomial kernel output
%  the output of the function is K = 1.7560    :) 
%
% This MATLAB code is not optimized and use it as is.
%
% Thank you. 
% Sedat Ozer
%
m=length(x);
if n==0
    K=1;
elseif n==1
    K=x*y';
else
    K=0;
    T1matrix=Tgenerate(x,n);
    T2matrix=Tgenerate(y,n);
    for i=1:n+1
        if mod(i,2)
            K=K+T1matrix(i,1)*T2matrix(i,1);
        else
            K=K+T1matrix(i,:)*T2matrix(i,:)';
        end
    end
end
K=K/sqrt(m-x*y');  % just delete this line if you like to generate Modified Chebyshev kernel and divide the K with the new weighting function (such as Gaussian)
end


function T=Tgenerate(x,ni)
    vectorsize=length(x);
    T_matrix=zeros(ni+1,vectorsize);
    T_matrix(1,:)=1;
    T_matrix(2,:)=x;
    for i=3:ni+1
        if mod(i,2)
            T_1=T_matrix(i-1,:)';
            T_2=T_matrix(i-2,1);
        else
            T_1=T_matrix(i-1,1)';
            T_2=T_matrix(i-2,:);
        end
        T_matrix(i,:)=2*x*T_1-T_2;
    end

T=T_matrix;
end
   