import torch


def r2_matrix(x, y, device=None):
    """
    Returns a new tensor corresponding to matrix formulation of the given input tensors representing
    R(2) group elements.

    Args:
        x (`torch.FloatTensor`): x attributes of group elements.
        y (`torch.FloatTensor`): y attributes of group elements.
        device (Device, optional): computation device. Defaults to None.

    Returns:
        (`torch.FloatTensor`): matrix representation of the group elements.
    """
    G = torch.zeros((x.nelement(), 3, 3), device=device)
    G[..., 0, 2] = x
    G[..., 1, 2] = y
    G[..., 0, 0] = 1.0
    G[..., 1, 1] = 1.0
    G[..., 2, 2] = 1.0

    return G