function g = defnstep(y,m1,m2)
% This function defines a step function on an (m1)st level SG
% graph, constant on each cell of level m2. 
%
% support functions: none
%
% INPUT:
% y - vector of length 3^m2 storing function values on the (m2)nd
% level cells (in TLR ordering)
% m1 - the number of levels (meaning we have 3^(m+1) points) 
% m2 - the level of cell for which to pass function values (must
% have m2 > 0)
% 
% OUTPUT: 
% g - vector of length 3^m1 holding function values on an (m1)st
% level SG graph (in TLR ordering)

% Base case: y is a 3-vector
if(m2 == 1)
  g = [y(1)*ones(1,3^m1) y(2)*ones(1,3^m1) y(3)*ones(1,3^m1)] ;
% Otherwise, we recursively call the function on the three level 1
% subcells, with m1 and m2 decreased by 1 
else
  g = [defnstep(y(1:3^(m2-1)),m1-1,m2-1) ...
       defnstep(y(3^(m2-1)+1:2*3^(m2-1)),m1-1,m2-1) ...
       defnstep(y(2*3^(m2-1)+1:3^m2),m1-1,m2-1) ] ;
end