code: def _nearest_sym_pos_def(S, reg=1e-6):
      """Find the nearest SPD matrix.
  
      Parameters
      ----------
      S : ndarray, shape (n, n)
          Square matrix.
      reg : float
          Regularization parameter.
  
      Returns
      -------
      P : ndarray, shape (n, n)
          Nearest SPD matrix.
      """
      A = (S + S.T) / 2
      _, s, V = np.linalg.svd(A)
      H = V.T @ np.diag(s) @ V
      B = (A + H) / 2
      P = (B + B.T) / 2
  
      if is_pos_def(P):
          # Regularize if already PD
          ei, ev = np.linalg.eigh(P)
          if np.min(ei) / np.max(ei) < reg:
              P = ev @ np.diag(ei + reg) @ ev.T
          return P
  
      spacing = np.spacing(np.linalg.norm(A))
      I = np.eye(S.shape[0])  # noqa
      k = 1
      while not is_pos_def(P, fast_mode=False):
          mineig = np.min(np.real(np.linalg.eigvals(P)))
          P += I * (-mineig * k**2 + spacing)
          k += 1
  
      # Regularize
      ei, ev = np.linalg.eigh(P)
      if np.min(ei) / np.max(ei) < reg:
          P = ev @ np.diag(ei + reg) @ ev.T
      return P
  
file_path: pyRiemann/pyriemann/utils/base.py
imports: import numpy
  from numpy.core.numerictypes import typecodes
  from .test import is_pos_def
label_desc: Use image node_id 7 for calling a global function with example usage: _nearest_sym_pos_def(S, reg) and returns: P, P