def get_hypercube_interpolation_fn(coefficients):
  """Returns function which does hypercube interpolation.

  This is only for 2^d lattice aka hypercube.

  Args:
    coefficients: coefficients of hypercube ordered according to index of
      corresponding vertex.

  Returns:
    Function which takes d-dimension point and performs hypercube interpolation
    with given coefficients.
  """

  def hypercube_interpolation_fn(x):
    """Does hypercube interpolation."""
    if 2**len(x) != len(coefficients):
      raise ValueError("Number of coefficients(%d) does not correspond to "
                       "dimension 'x'(%s)" % (len(coefficients), x))
    result = 0.0
    for coefficient_index in range(len(coefficients)):
      weight = 1.0
      for input_dimension in range(len(x)):
        if coefficient_index & (1 << input_dimension):
          # If statement checks whether 'input_dimension' bit of
          # 'coefficient_index' is set to 1.
          weight *= x[input_dimension]
        else:
          weight *= (1.0 - x[input_dimension])
      result += coefficients[coefficient_index] * weight
    return result

  return hypercube_interpolation_fn