code: def quaternion_to_matrix(quaternions: torch.Tensor) -> torch.Tensor:
      """
      Convert rotations given as quaternions to rotation matrices.
  
      Args:
          quaternions: quaternions with real part first,
              as tensor of shape (..., 4).
  
      Returns:
          Rotation matrices as tensor of shape (..., 3, 3).
      """
      r, i, j, k = torch.unbind(quaternions, -1)
      # pyre-fixme[58]: `/` is not supported for operand types `float` and `Tensor`.
      two_s = 2.0 / (quaternions * quaternions).sum(-1)
  
      o = torch.stack(
          (
              1 - two_s * (j * j + k * k),
              two_s * (i * j - k * r),
              two_s * (i * k + j * r),
              two_s * (i * j + k * r),
              1 - two_s * (i * i + k * k),
              two_s * (j * k - i * r),
              two_s * (i * k - j * r),
              two_s * (j * k + i * r),
              1 - two_s * (i * i + j * j),
          ),
          -1,
      )
      return o.reshape(quaternions.shape[:-1] + (3, 3))
  
imports: from typing import Optional
  import torch
  import torch.nn.functional
  from ..common.datatypes import Device
label_desc: Use image node_id 1 for calling a global function with example usage: quaternion_to_matrix(quaternions) and returns: o