original_docstring: r"""Utility function mapping vectors from the cube to its inscribed sphere.
  
      The mapping is supported only for dimensions of blocks up to three.
  
      Parameters
      ----------
      w : torch.Tensor
          The vectors within the hypercubes to be mapped to the hyperspheres.
          This has shape ``(batch_size, n_features)``.
      blocks : int or List[int]
          The size of the blocks. If an integer, ``w`` is divided into
          blocks of equal size. Otherwise, it is divided into ``len(blocks)``
          blocks, with the i-th block having size ``blocks[i]``.
      shorten_last_block : bool, optional
          If ``True`` and ``blocks`` is an integer that is not a divisor of
          the number of features, the last block is shortened automatically.
          Otherwise, an exception is raised if ``blocks`` is an integer
          that does not divide the number of features.
  
      Returns
      -------
      mapped_w : torch.Tensor
          The mapped vectors of the same shape of ``w``.
  
      
docstring: r"""Utility function mapping vectors from the cube to its inscribed sphere.
  The mapping is supported only for dimensions of blocks up to three.
  
  Parameters
  
  w : torch.Tensor
  The vectors within the hypercubes to be mapped to the hyperspheres.
  
  Returns
  
  mapped_w : torch.Tensor
  The mapped vectors of the same shape of ``w``.
docstring_tokens: r " " " Utility function mapping vectors from the cube to its inscribed sphere . The mapping is supported only for dimensions of blocks up to three . Parameters w : torch . Tensor The vectors within the hypercubes to be mapped to the hyperspheres . Returns mapped_w : torch . Tensor The mapped vectors of the same shape of ` ` w ` ` .
code: def unit_cube_to_inscribed_sphere(w, blocks, shorten_last_block=False):
      r"""Utility function mapping vectors from the cube to its inscribed sphere.
  
      The mapping is supported only for dimensions of blocks up to three.
  
      Parameters
      ----------
      w : torch.Tensor
          The vectors within the hypercubes to be mapped to the hyperspheres.
          This has shape ``(batch_size, n_features)``.
      blocks : int or List[int]
          The size of the blocks. If an integer, ``w`` is divided into
          blocks of equal size. Otherwise, it is divided into ``len(blocks)``
          blocks, with the i-th block having size ``blocks[i]``.
      shorten_last_block : bool, optional
          If ``True`` and ``blocks`` is an integer that is not a divisor of
          the number of features, the last block is shortened automatically.
          Otherwise, an exception is raised if ``blocks`` is an integer
          that does not divide the number of features.
  
      Returns
      -------
      mapped_w : torch.Tensor
          The mapped vectors of the same shape of ``w``.
  
      """
      batch_size, n_features = w.shape
  
      # Eventually convert a constant block size to a list of block sizes.
      blocks = generate_block_sizes(n_features, blocks, shorten_last_block)
  
      # Initialized the returned value.
      mapped_w = torch.empty_like(w)
  
      # The pointer to the index where the current block starts.
      block_pointer = 0
  
      for block_size in blocks:
          w_block = w[:, block_pointer:block_pointer+block_size]
  
          if block_size == 3:
              squared = w_block**2
              squared_norms = squared.sum(dim=1, keepdim=True)
              yxx = torch.index_select(squared, dim=1, index=torch.tensor([1, 0, 0]))
              zzy = torch.index_select(squared, dim=1, index=torch.tensor([2, 2, 1]))
              mapped_w_block = w_block * torch.sqrt(1 - (squared_norms - squared) / 2 + yxx * zzy / 3)
          elif block_size == 2:
              swapped = torch.index_select(w_block, dim=1, index=torch.tensor([1, 0]))
              mapped_w_block = w_block * torch.sqrt(1 - swapped**2/2)
          elif block_size == 1:
              mapped_w_block = w_block
          else:
              raise NotImplementedError('Hypercube to hypersphere mapping is not implemented')
  
          mapped_w[:, block_pointer:block_pointer+block_size] = mapped_w_block
  
          block_pointer += block_size
  
      return mapped_w
code_tokens: def unit_cube_to_inscribed_sphere ( w , blocks , shorten_last_block = False ) : batch_size , n_features = w . shape blocks = generate_block_sizes ( n_features , blocks , shorten_last_block ) mapped_w = torch . empty_like ( w ) block_pointer = 0 for block_size in blocks : w_block = w [ : , block_pointer : block_pointer + block_size ] if block_size == 3 : squared = w_block ** 2 squared_norms = squared . sum ( dim = 1 , keepdim = True ) yxx = torch . index_select ( squared , dim = 1 , index = torch . tensor ( [ 1 , 0 , 0 ] ) ) zzy = torch . index_select ( squared , dim = 1 , index = torch . tensor ( [ 2 , 2 , 1 ] ) ) mapped_w_block = w_block * torch . sqrt ( 1 - ( squared_norms - squared ) / 2 + yxx * zzy / 3 ) elif block_size == 2 : swapped = torch . index_select ( w_block , dim = 1 , index = torch . tensor ( [ 1 , 0 ] ) ) mapped_w_block = w_block * torch . sqrt ( 1 - swapped ** 2 / 2 ) elif block_size == 1 : mapped_w_block = w_block else : raise NotImplementedError ( 'Hypercube to hypersphere mapping is not implemented' ) mapped_w [ : , block_pointer : block_pointer + block_size ] = mapped_w_block block_pointer += block_size return mapped_w
short_docstring: r"""Utility function mapping vectors from the cube to its inscribed sphere.
short_docstring_tokens: r " " " Utility function mapping vectors from the cube to its inscribed sphere .
comment: r"""Utility function mapping vectors from the cube to its inscribed sphere.
  
      The mapping is supported only for dimensions of blocks up to three.
  
      Parameters
      ----------
      w : torch.Tensor
          The vectors within the hypercubes to be mapped to the hyperspheres.
          This has shape ``(batch_size, n_features)``.
      blocks : int or List[int]
          The size of the blocks. If an integer, ``w`` is divided into
          blocks of equal size. Otherwise, it is divided into ``len(blocks)``
          blocks, with the i-th block having size ``blocks[i]``.
      shorten_last_block : bool, optional
          If ``True`` and ``blocks`` is an integer that is not a divisor of
          the number of features, the last block is shortened automatically.
          Otherwise, an exception is raised if ``blocks`` is an integer
          that does not divide the number of features.
  
      Returns
      -------
      mapped_w : torch.Tensor
          The mapped vectors of the same shape of ``w``.
  
      """$SEP$# Eventually convert a constant block size to a list of block sizes.$SEP$# Initialized the returned value.$SEP$# The pointer to the index where the current block starts.