code: def _maybe_real(A, B, tol=None):
      """
      Return either B or the real part of B, depending on properties of A and B.
  
      The motivation is that B has been computed as a complicated function of A,
      and B may be perturbed by negligible imaginary components.
      If A is real and B is complex with small imaginary components,
      then return a real copy of B.  The assumption in that case would be that
      the imaginary components of B are numerical artifacts.
  
      Parameters
      ----------
      A : ndarray
          Input array whose type is to be checked as real vs. complex.
      B : ndarray
          Array to be returned, possibly without its imaginary part.
      tol : float
          Absolute tolerance.
  
      Returns
      -------
      out : real or complex array
          Either the input array B or only the real part of the input array B.
  
      """
      # Note that booleans and integers compare as real.
      if np.isrealobj(A) and np.iscomplexobj(B):
          if tol is None:
              tol = {0: feps * 1e3, 1: eps * 1e6}[
                  _array_precision[B.dtype.char]
              ]
          if np.allclose(B.imag, 0.0, atol=tol):
              B = B.real
      return B
  
imports: from itertools import product
  import numpy
  from numpy import dot, diag, prod, logical_not, ravel, transpose, conjugate, absolute, amax, sign, isfinite, triu
  from numpy.lib.scimath import sqrt
  from scipy.linalg import LinAlgError, bandwidth
  from ._misc import norm
  from ._basic import solve, inv
  from ._decomp_svd import svd
  from ._decomp_schur import schur, rsf2csf
  from ._expm_frechet import expm_frechet, expm_cond
  from ._matfuncs_sqrtm import sqrtm
  from ._matfuncs_expm import pick_pade_structure, pade_UV_calc
  from numpy import single
label_desc: Use image node_id 2 for calling a global function with example usage: _maybe_real(A, B, tol) and returns: B