function kernelDB(d,type,iso,prec,minmax,ranges)
global KDB
if nargin<2; type='log'; end
if nargin<3; iso=[false,true]; end
if nargin<4; prec=0.05; end
if nargin<5; minmax=[0.25,20]; end
if nargin<6; ranges=[3,4]; end
if d==1; iso(:)=true; end
if numel(iso)==1; iso=[iso,iso]; end
KDB=struct(); KDB.type=type; KDB.minmax=minmax;
if strcmp(type,'linear')
    hr=minmax(1):prec:minmax(2);
    KDB.sz=length(hr);
    KDB.prec=prec;
elseif strcmp(type,'log')
    KDB.sz=ceil(log(minmax(2)/minmax(1))/log(1+prec))+1;
    hr=logspace(log10(minmax(1)),log10(minmax(2)),KDB.sz);
    KDB.prec=log(hr(2)/hr(1));
end
celldim=ones(1,3); celldim(1:d)=KDB.sz;
KDB.dens=cell(celldim); KDB.curv=KDB.dens;
skip=false(1,2);
for i=1:celldim(1)
    for j=1:min(celldim(2),i)
        for k=1:celldim(3)
            I=[i,j,k]; skip(:)=false;
            if (d>1)&&any(iso); if ~all(I(1:d)==i); skip(iso)=true; end; end
            if ~skip(1); KDB.dens{i,j,k}=kernelmat(hr(I),ranges(1)); end
            if ~skip(2); KDB.curv{i,j,k}=kernelmat_curv(repmat(hr(I),[d,1]),ranges(2)); end
        end
    end
end
    function W=kernelmat(h_div_l,range)
        if numel(h_div_l)==1; h_div_l=h_div_l*ones(1,d); end
        if nargin<3
            range=3;
        end
        z=cell(d,1);
        for ii=1:d
            z{ii}=(0:1:(range*h_div_l(ii)))';
        end
        s=zeros(1,d); s((d+1):3)=1;
        for ii=1:d
            s(ii)=length(z{ii});
        end
        if d==2; [z{1},z{2}]=ndgrid(z{1},z{2});
        elseif d==3; [z{1},z{2},z{3}]=ndgrid(z{1},z{2},z{3}); end
        ii=1; W=(1/(2^d))*(erf((z{ii}+1/2)/(h_div_l(ii)*sqrt(2)))-erf((z{ii}-1/2)/(h_div_l(ii)*sqrt(2))));
        for ii=2:d; W=W.*(erf((z{ii}+1/2)/(h_div_l(ii)*sqrt(2)))-erf((z{ii}-1/2)/(h_div_l(ii)*sqrt(2)))); end
        W=[flip(W,1);W(2:end,:,:)];
        if d>1; W=[flip(W,2),W(:,2:end,:)]; end
        if d>2; W=cat(3,flip(W,3),W(:,:,2:end)); end
        W=W/sum(W(:));
    end
    function V=kernelmat_curv(g_div_l,range)
        if numel(g_div_l)==1; g_div_l=g_div_l*ones(d); end
        if nargin<3
            range=4;
        end
        z=cell(d,1);
        for ii=1:d
            z{ii}=(0:1:(range*max(g_div_l(:,ii))))';
        end
        s=zeros(1,d); s((d+1):3)=1;
        for ii=1:d
            s(ii)=length(z{ii});
        end
        if d==2; [z{1},z{2}]=ndgrid(z{1},z{2});
        elseif d==3; [z{1},z{2},z{3}]=ndgrid(z{1},z{2},z{3}); end
        V=repmat(zeros(s),[1,1,1,d]);
        for ii=1:d
            V(:,:,:,ii)=-(1/((2^(d-1/2))*sqrt(pi)*(g_div_l(ii,ii)^3)))*((z{ii}+1/2).*exp(-((z{ii}+1/2).^2)/(2*(g_div_l(ii,ii)^2)))-(z{ii}-1/2).*exp(-((z{ii}-1/2).^2)/(2*(g_div_l(ii,ii)^2))));
            oth=1:d; oth(ii)=[]; for jj=oth; V(:,:,:,ii)=V(:,:,:,ii).*(erf((z{jj}+1/2)/(g_div_l(ii,jj)*sqrt(2)))-erf((z{jj}-1/2)/(g_div_l(ii,jj)*sqrt(2)))); end
        end
        V=[flip(V,1);V(2:end,:,:,:)];
        if d>1; V=[flip(V,2),V(:,2:end,:,:)]; end
        if d>2; V=cat(3,flip(V,3),V(:,:,2:end,:)); end
        for ii=1:d
            Vi=V(:,:,:,ii);
            Vi(Vi>0)=-(sum(Vi(Vi<0))/sum(Vi(Vi>0)))*Vi(Vi>0);
            Vi=Vi*sqrt(3/((2^(d+2))*(pi^(d/2))*(g_div_l(ii,ii)^5)*sum(sum(sum(Vi.^2,1),2),3)));
            oth=1:d; oth(ii)=[]; for jj=oth; Vi=Vi/sqrt(g_div_l(ii,jj)); end
            V(:,:,:,ii)=Vi;
        end
    end
end

