def _maybe_real(A, B, tol=None):
<EOL>if np.isrealobj(A) and np.iscomplexobj(B):<EOL><INDENT>if tol is None:<EOL><INDENT>tol = {<NUM_LIT:0>:feps*<NUM_LIT>, <NUM_LIT:1>:eps*<NUM_LIT>}[_array_precision[B.dtype.char]]<EOL><DEDENT>if np.allclose(B.imag, <NUM_LIT:0.0>, atol=tol):<EOL><INDENT>B = B.real<EOL><DEDENT><DEDENT>return B<EOL>
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.