function [fval, X] = objective(m,n,r,vecG,P,q,B,d,mu,s,nG)

% Computes the objective value for lssdp_gp

mu2 = repmat(-mu(n+1:end)',2,1);
y = zeros(n^2,1);
B2 = B';
y(B2(:)) = mu2(:);
X = vecG + reshape(diag(-mu(1:n)),n^2,1) + y;
const = 0;
for i = 1:r
  X = X + P{i}'*(-s(:,i));
  const = const + q(i,:)*s(:,i);
end

X = reshape(X,n,n);
X = (X + X')/2;
[U,D] = eig(X);
X = U*max(D,0)/U;

X = (X + X')/2;

fval = norm(X,'fro')^2/2 - nG + sum(mu(1:n)) + mu(n+1:end)'*d + const;