code: def multiple_fast_inv(a):
      """Compute the inverse of a set of arrays in-place
  
      Parameters
      ----------
      a: array_like of shape (n_samples, M, M)
          Set of square matrices to be inverted. `a` is changed in place.
  
      Returns
      -------
      a: ndarray shape (n_samples, M, M)
         The input array `a`, overwritten with the inverses of the original 2D
         arrays in ``a[0], a[1], ...``.  Thus ``a[0]`` replaced with
         ``inv(a[0])`` etc.
  
      Raises
      ------
      LinAlgError :
          If `a` is singular.
      ValueError :
          If `a` is not square, or not 2-dimensional.
  
      Notes
      -----
      This function is copied from scipy.linalg.inv, but with some customizations
      for speed-up from operating on multiple arrays.  It also has some
      conditionals to work with different scipy versions.
      """
      # Consider errors for sparse, masked, object arrays, as for
      # _asarray_validated?
      from scipy.linalg.lapack import get_lapack_funcs
  
      S, M, N = a.shape
      if M != N:
          raise ValueError("a must have shape(n_samples, M, M)")
      a = np.asarray_chkfinite(a)
      getrf, getri = get_lapack_funcs(("getrf", "getri"), (a[0],))
      # Calculate lwork on different scipy versions
      try:
          (getri_lwork,) = get_lapack_funcs(("getri_lwork",), (a[0],))
      except (ValueError, AttributeError):  # scipy < 0.15
          # scipy 0.10, 0.11 -> AttributeError
          # scipy 0.12, 0.13, 0.14 -> ValueError
          from scipy.linalg import calc_lwork
  
          lwork = calc_lwork.getri(getri.prefix, M)[1]
      else:  # scipies >= 0.15 have getri_lwork function
          lwork, info = getri_lwork(M)
          if info != 0:
              raise ValueError(
                  "internal getri work space query failed: %d" % (info,)
              )
          lwork = int(lwork.real)
      # XXX: the following line fixes curious SEGFAULT when
      # benchmarking 500x500 matrix inverse. This seems to
      # be a bug in LAPACK ?getri routine because if lwork is
      # minimal (when using lwork[0] instead of lwork[1]) then
      # all tests pass. Further investigation is required if
      # more such SEGFAULTs occur.
      lwork = int(1.01 * lwork)
      for i, ai in enumerate(a):
          lu, piv, info = getrf(ai, overwrite_a=True)
          if info == 0:
              a[i], info = getri(lu, piv, lwork=lwork, overwrite_lu=1)
          if info > 0:
              raise np.linalg.LinAlgError("singular matrix")
          if info < 0:
              raise ValueError(
                  "illegal value in %d-th argument of internal "
                  "getrf|getri" % -info
              )
      return a
  
file_path: nipy/nipy/algorithms/statistics/utils.py
imports: from itertools import combinations
  import numpy
  from scipy.stats import norm
label_desc: Use image node_id 2 for calling a global function with example usage: multiple_fast_inv(a) and returns: a