function ent = kentanglement(psi,k,varargin)
% kentanglement(psi,k,dim) computes the (k-support norm of entanglement)^2 of the vector psi
% Arguments: vector psi, k (1<=k<=d), (optional) dimensions of the two systems

if nargin > 2
    dim = varargin{1};
else
    dim = sqrt(length(psi));
end

if isscalar(dim)
    schm = svd(reshape(psi,dim,dim).');
else
    schm = svd(reshape(psi,dim(2),dim(1)).');
end

vec = [schm; zeros(min(dim)-length(schm),1)];

ent = ksupport(schm,k);

end