function i = halton_inverse ( r, m )

%*****************************************************************************80
%
%% halton_inverse() inverts an element of the Halton sequence.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 August 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real R(M), the I-th element of the Halton sequence.
%    0 <= R < 1.0
%
%    integer M, the spatial dimension.
%
%  Output:
%
%    integer I, the index of the element of the sequence.
%
  if ( any ( r < 0.0 ) | any ( 1.0 <= r ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'HALTON_INVERSE - Fatal error!\n' );
    fprintf ( 1, '  0 <= R < 1.0 is required.\n' );
    error ( 'HALTON_INVERSE - Fatal error!' );
  end
%
%  Invert using the first component only, because working with base
%  2 is accurate.
%
  i = 0;
  t = r(1);
  p = 1;
  while ( t ~= 0.0 )
    t = t * 2.0;
    d = t - mod ( t, 1.0 );
    i = i + d * p;
    p = p * 2;
    t = mod ( t, 1.0 );
  end
 
  return
end

