function v = soc_proj(v)

% Computes projection onto 2nd-order cone, w\in Re, y\in Re^n
w = v(1);
y = v(2:end);

n = size(y,1);
normy = norm(y);

if norm(y) == 0
    
    coef1 = max(w, 0);
    coef2 = coef1;
    e     = [1; zeros(n-1,1)];
    u1    = 0.5 * [1; -e];
    u2    = 0.5 * [1; e];
    
else
    
    coef1 = max(w - normy, 0);
    coef2 = max(w + normy, 0);
    e     = y / normy;
    u1    = 0.5 * [1; -e];
    u2    = 0.5 * [1; e];
    
end

v = coef1 * u1 + coef2 * u2;
  