function sparse_random_tensor(data_dir,sparse_file,I,density)
%SPARSE_RANDOM_TENSOR makes a sparse uniformly distributed random tensor
%
%   Peter Turney
%   October 20, 2007
%
%   Copyright 2007, National Research Council of Canada
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see http://www.gnu.org/licenses/.
%
%   assume a third-order tensor is desired
%
%%  initialize
%
fprintf('SPARSE_RANDOM_TENSOR is running ...\n');
%
%   make sure the directory exists
%
if (isdir(data_dir) == 0)
    mkdir(data_dir);
end
%
file_name = [data_dir, '/', sparse_file];
%
fprintf('   generating tensor of size %d x %d x %d with density %f\n', ...
    I(1), I(2), I(3), density);
%
%%  main loop
%
file_id = fopen(file_name, 'w');
fprintf('   slice: ');
for i1 = 1:I(1)
    fprintf('%d ',i1);       % show progress
    if ((mod(i1,10) == 0) && (i1 ~= I(1)))
        fprintf('\n   ');    % time for new line
    end
    for i2 = 1:I(2)
        for i3 = 1:I(3)
            if (rand < density)
                fprintf(file_id,'%d %d %d %f\n',i1,i2,i3,rand);
            end
        end
    end
end
fprintf('\n');
fclose(file_id);
%
fprintf('SPARSE_RANDOM_TENSOR is done\n');
%