function sset = collatz_inverse ( tset )

%*****************************************************************************80
%
%% collatz_inverse() computes the inverse image of a set of values.
%
%  Discussion:
%
%    The Collatz sequence is defined recursively as follows:
%
%      Let T be the current entry of the sequence, and U the next:
%
%      If T = 1, the sequence terminates (or U = 1, your choice);
%      Else if T is even, U = T / 2;
%      Else (if T is odd, and greater than 1) U = 3 * T + 1.
%
%    Given a set TSET with typical point T, the inverse image under 
%    the Collatz map is the set SSET of points S, including
%      T*2
%    always, plus potentially
%      ( T - 1 ) / 3 
%    if mod ( T, 3 ) = 1
%
%    As long as T is a proper set (no value occurs twice), then S is
%    also guaranteed to be a proper set.
%      
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer TSET(*): the initial set of points.
%
%  Output:
%
%    integer SSET(*): the preimage of the points.
%
  sset = zeros ( 0, 1 );
  n = 0;

  for t = tset

    n = n + 1;
    sset(n) = 2 * t;

    if ( mod ( t, 6 ) == 4 & t ~= 4 )
      n = n + 1;
      sset(n) = ( t - 1 ) / 3;
    end
    
  end

  return
end

