function A = reg2D( M, g )
%Spatial regularisation of a 2D image using a langrange multiplier
%M = the image. For example uniform random noise. Must be square.
%g = the value of the langrange multiplier
	
x=size( M );	
n=x( 1 );

	% check matrix is square
	if x( 1 ) ~= x( 2 )
		error 'Input matrix must be square'
	end

	%generate an appropriately sized constraint
	BTB = genConstraintMatrix( n ) ; %minimizes first differences.
    
	%flatten the image out
	a = reshape( M, n*n, 1 );

	%get identity matrix
	I=eye( n*n );	

	%regularise
	A = inv( I + g * BTB) * a ;
	    
	%make square again
	A = reshape( A,n,n);
    
    size(A)
    