i,D+2,i);

  dsi = -bsxfun(@times,inp2,2.*inp2);                 <span class="comment">% d(sum(inp2.*inp2,2))/dX</span>
  dslb = zeros(1,D);

  <span class="keyword">for</span> d = 1:D
    sqdi = K(:,:,i).*bsxfun(@minus,ii(:,d),ii(:,d)');
    sqdiBi = sqdi*beta(:,i);
    tlbdi(:,d) = sqdi*liK.*beta(:,i) + sqdiBi.*liK;
    tlbdi2 = -tliK*(-bsxfun(@times,sqdi,beta(:,i))'-diag(sqdiBi));
    dVdi(:,i,:,d) = c*(iR(:,d)*lb' - bsxfun(@times,t,tlb(:,d))' + tlbdi2);
    dsqdX = bsxfun(@plus,dsi(:,d),dsi(:,d)') + 4.*