%% function [cvx_optval,X,Y,W] = NormCPTNI(J,dim)
% computes the CPTNI norm of the Hermiticity-preserving map J
% with input/output dimensions optionally provided in dim=[dimA,dimB]

function [cvx_optval,X,Y,W] = NormCPTNI(J,dim)
    
J = (J+J')/2; % make sure that J is Hermitian, avoid numerical issues

if nargin<2
    d = max(size(J));
    dim = [1,1]*sqrt(d);
else
    d = dim(1)*dim(2);
end
 
cvx_begin sdp quiet

   variable X(d,d) hermitian semidefinite
   variable Y(d,d) hermitian semidefinite
   variable mu1 nonnegative
   variable mu2 nonnegative
   dual variable W
   
   PartialTrace(X,2,dim) <= mu1*eye(dim(1));
   PartialTrace(Y,2,dim) <= mu2*eye(dim(1));
   W: X-Y == J;
   
   minimize mu1+mu2
   
cvx_end
   
end