'Wrap a numpy function that processes self, returning a Quantity.
Parameters
function : callable
Numpy function to wrap.
args : positional arguments
Any positional arguments to the function beyond the first argument
(which will be set to ``self``).
kwargs : keyword arguments
Keyword arguments to the function.
If present, the following arguments are treated specially:
unit : `~astropy.units.Unit`
Unit of the output result.  If not given, the unit of ``self``.
out : `~astropy.units.Quantity`
A Quantity instance in which to store the output.
Notes
Output should always be assigned via a keyword argument, otherwise
no proper account of the unit is taken.
Returns
out : `~astropy.units.Quantity`
Result of the function call, with the unit set properly.'

def _wrap_function(self, function, *args, **kwargs):

    unit = kwargs.pop(u'unit', self.unit)
    out = kwargs.pop(u'out', None)
    args = ((self.value,) + tuple(((arg.value if isinstance(arg, Quantity) else arg) for arg in args)))
    if (out is not None):
        arrays = tuple((arg for arg in args if isinstance(arg, np.ndarray)))
        kwargs[u'out'] = check_output(out, unit, arrays, function=function)
    result = function(*args, **kwargs)
    return self._result_as_quantity(result, unit, out)
