% kernel function: fills kernel for laplace or helm eqn
%               also gives nodes and weights
% inputs k : wavenumber
%        m : number of nodes
%
% outputs ker: kernel matrix
%         [z dzdx, w, n] : parameterization


function [ker, z, dzdx, w, n]=hlker(m,k)

%first, set up nodes and weights

[z,dzdx,n,kap]=param(m); %parameterizes equation

w=zeros(1,numel(dzdx))+2*pi/m; %periodic trapezoid weights


%now to set up kernel matrix
ker=zeros(m,m); %initalizes kernel matrix

for i=1:m; %does diagonal entries- same for laplace or helm eqns
    ker(i,i)=-kap(i)/(4*pi)*dzdx(i)*w(i);
end

%now to fill rest of matrix (non-diagonal)
if k==0 %laplace-type equation

%function for evaluating dphi/dny
dphi=@(x,y,n) -1/(2*pi)*(n'*(y-x))/((y-x)'*(y-x));

for i=1:m
    for j=1:m
       if ~(i==j)
        ker(i,j)= dphi(z(:,i),z(:,j),n(:,j))*dzdx(j)*w(j);
       end
    end
end

    
else %helmholtz-type equation

%function for evaluating dphi/dny
dphi=@(x,y,n,k) 1i*k/4*n'*(x-y)/norm(x-y)*besselh(1,k*(norm(x-y)));

for i=1:m
    for j=1:m
       if ~(i==j)
        ker(i,j)= dphi(z(:,i),z(:,j),n(:,j),k)*dzdx(j)*w(j);
       end
    end
end

end
    