function phi = degexpand(X,deg)
  % Expands input vectors to contain  powers of input features,
  [n,d] = size(X) ;

  phi = [];
  for i=1:n,
    pol = X(i,:); 
    feavec = pol;
    for j=1:deg-1,
      pol = reshape(pol'*X(i,:),1,[]);
      feavec = [feavec,pol];
    end;
    phi = [phi;feavec];
  end;
