anchor.diff:  
           ! Create arrays and data for index mapping needed for UEG.
   
  -        use basis, only: basis_fns, nbasis
  +        use basis, only: basis_fns, nbasis, bit_lookup, basis_length
           use system, only: box_length, ueg_ecutoff, ndim
   
           use checking, only: check_allocate
  -
  -        integer :: ierr, i, N_kx, k_min(ndim)
  +        use utils, only: tri_ind
  +
  +        integer :: ierr, i, j, a, ind, N_kx, k_min(ndim), bit_pos, bit_el, k(ndim)
   
           ueg_basis_max = ceiling(sqrt(2*ueg_ecutoff))
   
  
positive.code: 
          ! Now fill in the values for the alpha orbitals which are in the basis.
          forall (i=1:nbasis:2) ueg_basis_lookup(dot_product(basis_fns(i)%l, ueg_basis_dim) + ueg_basis_origin) = i
  
      end subroutine init_ueg_indexing
  
  
negative.code: ! Note that N_x = 2*kmax+1
  integer :: ueg_basis_max
  
  ! When creating an arbitrary excitation, k_i,k_j->k_a,k_b, we must conserve
  ! crystal momentum, k_i+k_j-k_a-k_b=0.  Hence once we've chosen k_i, k_j and
  ! k_a, k_b is uniquely defined.  Further, once we've chosen k_i and k_j and if
  ! we require k_b to exist in the basis, then only certain values of k_a are
  ! permitted.  ueg_ternary_conserve(0,i,j) gives how many k_a are permitted for
  ! a given k_i and k_j and ueg_ternary_conserve(1,i,j) gives a bit string with
  ! only bytes set corresponding to permitted k_a values.  Note only basis
  ! functions corresponding to *alpha* orbitals are set.  Finally, we use
  ! tri_ind((j+1)/2,(i+1)/2), j>=i, to store only the lower triangular section of
  ! the array and only store it for one set of spin functions.
  ! (j+1)/2 gives the same value for the indices of both alpha and beta basis
  ! functions of the same wavevector.
  ! TODO: reduce this to minimum memory required (several k_i+k_j will have the
  ! same value!).
  integer(i0), allocatable :: ueg_ternary_conserve(:,:) ! (0:basis_length,nbasis/2*(nbasis/2+1)/2)
  
  abstract interface
  
      ! UEG-specific integral procedure pointers.
  