<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def within(self, x, ctrs, kdtree=None):
 """Checks which cubes `x` falls within. Uses a K-D Tree to perform the search if provided."""


        if kdtree is None:
            # If no KDTree is provided, execute a brute-force search
            # over all cubes.
            idxs = np.where(np.max(np.abs(ctrs - x), axis=1) <= self.hside)[0]
        else:
            # If a KDTree is provided, find all points within r (`hside`).
            idxs = kdtree.query_ball_point(x, self.hside, p=np.inf, eps=0)

        return idxs