code: def apply_affine(aff, pts, inplace=False):
      """Apply affine matrix `aff` to points `pts`
  
      Returns result of application of `aff` to the *right* of `pts`.  The
      coordinate dimension of `pts` should be the last.
  
      For the 3D case, `aff` will be shape (4,4) and `pts` will have final axis
      length 3 - maybe it will just be N by 3. The return value is the
      transformed points, in this case::
  
          res = np.dot(aff[:3,:3], pts.T) + aff[:3,3:4]
          transformed_pts = res.T
  
      This routine is more general than 3D, in that `aff` can have any shape
      (N,N), and `pts` can have any shape, as long as the last dimension is for
      the coordinates, and is therefore length N-1.
  
      Parameters
      ----------
      aff : (N, N) array-like
          Homogeneous affine, for 3D points, will be 4 by 4. Contrary to first
          appearance, the affine will be applied on the left of `pts`.
      pts : (..., N-1) array-like
          Points, where the last dimension contains the coordinates of each
          point.  For 3D, the last dimension will be length 3.
      inplace : bool, optional
          If True, attempt to apply the affine directly to ``pts``.
          If False, or in-place application fails, a freshly allocated
          array will be returned.
  
      Returns
      -------
      transformed_pts : (..., N-1) array
          transformed points
  
      Examples
      --------
      >>> aff = np.array([[0,2,0,10],[3,0,0,11],[0,0,4,12],[0,0,0,1]])
      >>> pts = np.array([[1,2,3],[2,3,4],[4,5,6],[6,7,8]])
      >>> apply_affine(aff, pts) #doctest: +ELLIPSIS
      array([[14, 14, 24],
             [16, 17, 28],
             [20, 23, 36],
             [24, 29, 44]]...)
  
      Just to show that in the simple 3D case, it is equivalent to:
  
      >>> (np.dot(aff[:3,:3], pts.T) + aff[:3,3:4]).T #doctest: +ELLIPSIS
      array([[14, 14, 24],
             [16, 17, 28],
             [20, 23, 36],
             [24, 29, 44]]...)
  
      But `pts` can be a more complicated shape:
  
      >>> pts = pts.reshape((2,2,3))
      >>> apply_affine(aff, pts) #doctest: +ELLIPSIS
      array([[[14, 14, 24],
              [16, 17, 28]],
      <BLANKLINE>
             [[20, 23, 36],
              [24, 29, 44]]]...)
      """
      aff = np.asarray(aff)
      pts = np.asarray(pts)
      shape = pts.shape
      pts = pts.reshape((-1, shape[-1]))
      # rzs == rotations, zooms, shears
      rzs = aff[:-1, :-1]
      trans = aff[:-1, -1]
  
      if inplace:
          try:
              np.dot(pts, rzs.T, out=pts)
          except ValueError:
              inplace = False
          else:
              pts += trans[None, :]
      if not inplace:
          pts = pts @ rzs.T + trans[None, :]
  
      return pts.reshape(shape)
  
file_path: nibabel/nibabel/affines.py
imports: from functools import reduce
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: apply_affine(aff, pts, inplace) and returns: pts