import torch


def v_interactions(W, scaled, cheb, tree, scheme):
    """Calculates the v-:ref:`interactions <eval>` used in a solution. 

    :param W: the :func:`weights` for the solution.
    :param scaled: a :func:`scale`'d problem.
    :param cheb: a :class:`~pybbfmm.chebyshev.Chebyshev` object.
    :param tree: a :ref:`tree <presolve>`.
    :param scheme: the :ref:`scheme <presolve>` for the solution.
    :return: a (n_boxes, n_cheb_coeffs :sup:`dim`)-tensor of interactions.
    """
    ixns = torch.zeros_like(W) 
    for v in scheme.v:
        scale = 1/2**v.depth.float()
        boxes = scale*cheb.nodes
        friends = scale*(2*v.offset + cheb.nodes)
        k = scaled.kernel(boxes[None, :], friends[:, None])
        ixns[v.boxes] += W[v.friends] @ k
    
    return ixns