function visual_basis( A, dim_r, mag, cols )
% visual - display a basis for image patches
%
% A        the basis, with patches as column vectors
% mag      magnification factor
% cols     number of columns (x-dimension of map)
%

% Get maximum absolute value (it represents white or black; zero is gray)
maxi=max(max(abs(A)));
mini=-maxi;

% This is the side of the window
% dim = sqrt(size(A,1));
dim_c = size(A,1)/dim_r;
if dim_c - floor(dim_c)~=0 error('Patch size not interger!'); end;
% Helpful quantities
dimm = dim_r-1;
dimp = dim_r+1;

dimm_c = dim_c-1;
dimp_c = dim_c+1;
rows = size(A,2)/cols;
if rows-floor(rows)~=0, error('Fractional number of rows!'); end

% Initialization of the image
I = maxi*ones(dim_r*rows+rows-1,dim_c*cols+cols-1); 

for i=0:rows-1
  for j=0:cols-1
    
    % This sets the patch
    I(i*dimp+1:i*dimp+dim_r,j*dimp_c+1:j*dimp_c+dim_c) = ...
			reshape(A(:,i*cols+j+1),[dim_c dim_r])';
  end
end

I = imresize(I,mag);

figure;
colormap(gray(256));
iptsetpref('ImshowBorder','tight'); 
subplot('position',[0,0,1,1]);
imshow(I,[mini maxi]);
truesize;  

