def quaternion_to_rotation_matrix(quaternion: torch.Tensor) -> torch.Tensor:
    'Converts a quaternion to a rotation matrix.\n    The quaternion should be in (x, y, z, w) format.\n\n    Args:\n        quaternion (torch.Tensor): a tensor containing a quaternion to be\n          converted. The tensor can be of shape :math:`(*, 4)`.\n\n    Return:\n        torch.Tensor: the rotation matrix of shape :math:`(*, 3, 3)`.\n\n    Example:\n        >>> quaternion = torch.tensor([0., 0., 1., 0.])\n        >>> quaternion_to_rotation_matrix(quaternion)\n        tensor([[-1.,  0.,  0.],\n                [ 0., -1.,  0.],\n                [ 0.,  0.,  1.]])\n    '
    if (not isinstance(quaternion, torch.Tensor)):
        raise TypeError('Input type is not a torch.Tensor. Got {}'.format(type(quaternion)))
    if (not (quaternion.shape[(- 1)] == 4)):
        raise ValueError('Input must be a tensor of shape (*, 4). Got {}'.format(quaternion.shape))
    quaternion_norm: torch.Tensor = normalize_quaternion(quaternion)
    (x, y, z, w) = torch.chunk(quaternion_norm, chunks=4, dim=(- 1))
    tx: torch.Tensor = (2.0 * x)
    ty: torch.Tensor = (2.0 * y)
    tz: torch.Tensor = (2.0 * z)
    twx: torch.Tensor = (tx * w)
    twy: torch.Tensor = (ty * w)
    twz: torch.Tensor = (tz * w)
    txx: torch.Tensor = (tx * x)
    txy: torch.Tensor = (ty * x)
    txz: torch.Tensor = (tz * x)
    tyy: torch.Tensor = (ty * y)
    tyz: torch.Tensor = (tz * y)
    tzz: torch.Tensor = (tz * z)
    one: torch.Tensor = torch.tensor(1.0)
    matrix: torch.Tensor = torch.stack([(one - (tyy + tzz)), (txy - twz), (txz + twy), (txy + twz), (one - (txx + tzz)), (tyz - twx), (txz - twy), (tyz + twx), (one - (txx + tyy))], dim=(- 1)).view((- 1), 3, 3)
    if (len(quaternion.shape) == 1):
        matrix = torch.squeeze(matrix, dim=0)
    return matrix
Converts a quaternion to a rotation matrix.
The quaternion should be in (x, y, z, w) format.

Args:
    quaternion (torch.Tensor): a tensor containing a quaternion to be
      converted. The tensor can be of shape :math:`(*, 4)`.

Return:
    torch.Tensor: the rotation matrix of shape :math:`(*, 3, 3)`.

Example:
    >>> quaternion = torch.tensor([0., 0., 1., 0.])
    >>> quaternion_to_rotation_matrix(quaternion)
    tensor([[-1.,  0.,  0.],
            [ 0., -1.,  0.],
            [ 0.,  0.,  1.]])
def quaternion_to_rotation_matrix(quaternion: torch.Tensor) -> torch.Tensor:
    'Converts a quaternion to a rotation matrix.\n    The quaternion should be in (x, y, z, w) format.\n\n    Args:\n        quaternion (torch.Tensor): a tensor containing a quaternion to be\n          converted. The tensor can be of shape :math:`(*, 4)`.\n\n    Return:\n        torch.Tensor: the rotation matrix of shape :math:`(*, 3, 3)`.\n\n    Example:\n        >>> quaternion = torch.tensor([0., 0., 1., 0.])\n        >>> quaternion_to_rotation_matrix(quaternion)\n        tensor([[-1.,  0.,  0.],\n                [ 0., -1.,  0.],\n                [ 0.,  0.,  1.]])\n    '
    if (not isinstance(quaternion, torch.Tensor)):
        raise TypeError('Input type is not a torch.Tensor. Got {}'.format(type(quaternion)))
    if (not (quaternion.shape[(- 1)] == 4)):
        raise ValueError('Input must be a tensor of shape (*, 4). Got {}'.format(quaternion.shape))
    quaternion_norm: torch.Tensor = normalize_quaternion(quaternion)
    (x, y, z, w) = torch.chunk(quaternion_norm, chunks=4, dim=(- 1))
    tx: torch.Tensor = (2.0 * x)
    ty: torch.Tensor = (2.0 * y)
    tz: torch.Tensor = (2.0 * z)
    twx: torch.Tensor = (tx * w)
    twy: torch.Tensor = (ty * w)
    twz: torch.Tensor = (tz * w)
    txx: torch.Tensor = (tx * x)
    txy: torch.Tensor = (ty * x)
    txz: torch.Tensor = (tz * x)
    tyy: torch.Tensor = (ty * y)
    tyz: torch.Tensor = (tz * y)
    tzz: torch.Tensor = (tz * z)
    one: torch.Tensor = torch.tensor(1.0)
    matrix: torch.Tensor = torch.stack([(one - (tyy + tzz)), (txy - twz), (txz + twy), (txy + twz), (one - (txx + tzz)), (tyz - twx), (txz - twy), (tyz + twx), (one - (txx + tyy))], dim=(- 1)).view((- 1), 3, 3)
    if (len(quaternion.shape) == 1):
        matrix = torch.squeeze(matrix, dim=0)
    return matrix