code: def quat2mat(q):
      """Calculate rotation matrix corresponding to quaternion
  
      Parameters
      ----------
      q : 4 element array-like
  
      Returns
      -------
      M : (3,3) array
        Rotation matrix corresponding to input quaternion *q*
  
      Notes
      -----
      Rotation matrix applies to column vectors, and is applied to the
      left of coordinate vectors.  The algorithm here allows non-unit
      quaternions.
  
      References
      ----------
      Algorithm from
      https://en.wikipedia.org/wiki/Rotation_matrix#Quaternion
  
      Examples
      --------
      >>> import numpy as np
      >>> M = quat2mat([1, 0, 0, 0]) # Identity quaternion
      >>> np.allclose(M, np.eye(3))
      True
      >>> M = quat2mat([0, 1, 0, 0]) # 180 degree rotn around axis 0
      >>> np.allclose(M, np.diag([1, -1, -1]))
      True
      """
      w, x, y, z = q
      Nq = w * w + x * x + y * y + z * z
      if Nq < FLOAT_EPS:
          return np.eye(3)
      s = 2.0 / Nq
      X = x * s
      Y = y * s
      Z = z * s
      wX, wY, wZ = w * X, w * Y, w * Z
      xX, xY, xZ = x * X, x * Y, x * Z
      yY, yZ, zZ = y * Y, y * Z, z * Z
      return np.array(
          [
              [1.0 - (yY + zZ), xY - wZ, xZ + wY],
              [xY + wZ, 1.0 - (xX + zZ), yZ - wX],
              [xZ - wY, yZ + wX, 1.0 - (xX + yY)],
          ]
      )
  
file_path: nibabel/nibabel/quaternions.py
imports: import math
  import numpy
  from .casting import sctypes
label_desc: Use image node_id 2 for calling a global function with example usage: quat2mat(q) and returns: np, np