function xnow = smartrestrict(x,col,j,m1,m2,breakpoints)
% This function performs the restriction of an m1-level SG-circuit
% to an m2-level circuit. 

cellnum = (j-1 - mod(j-1,3)) / 3 + 1 ;
cellnum = (cellnum-1 - mod(cellnum-1,3)) / 3 + 1 ;

n = m1 - m2 ;
xnow = x ;

for i = 1:n
  xprev = xnow ;
  xnow = col(breakpoints(i)+1:breakpoints(i+1),:) ;
  xnow(cellnum,:) = restrictall(xprev(3*(cellnum-1)+1:3*cellnum,:),1) ;
  cellnum = (cellnum-1 - mod(cellnum-1,3)) / 3 + 1 ;
end
