function grad = gradient(m,n,k,r,P,q,B,d,X)

% Computes the gradient

% s mu

B2 = B(:,1);
vecX = X(:);
for i = 1:r
  grad(k*(i-1)+1:k*i,1) = P{i}*vecX - q(i,:)';
end
grad(k*r+1:k*r+n,1) = diag(X) - 1;
grad(k*r+n+1:k*r+n+m,1) = 2*vecX(B2) - d;

grad = -grad;