ufunclike.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """
  2. Module of functions that are like ufuncs in acting on arrays and optionally
  3. storing results in an output array.
  4. """
  5. from __future__ import division, absolute_import, print_function
  6. __all__ = ['fix', 'isneginf', 'isposinf']
  7. import numpy.core.numeric as nx
  8. from numpy.core.overrides import array_function_dispatch, ENABLE_ARRAY_FUNCTION
  9. import warnings
  10. import functools
  11. def _deprecate_out_named_y(f):
  12. """
  13. Allow the out argument to be passed as the name `y` (deprecated)
  14. In future, this decorator should be removed.
  15. """
  16. @functools.wraps(f)
  17. def func(x, out=None, **kwargs):
  18. if 'y' in kwargs:
  19. if 'out' in kwargs:
  20. raise TypeError(
  21. "{} got multiple values for argument 'out'/'y'"
  22. .format(f.__name__)
  23. )
  24. out = kwargs.pop('y')
  25. # NumPy 1.13.0, 2017-04-26
  26. warnings.warn(
  27. "The name of the out argument to {} has changed from `y` to "
  28. "`out`, to match other ufuncs.".format(f.__name__),
  29. DeprecationWarning, stacklevel=3)
  30. return f(x, out=out, **kwargs)
  31. return func
  32. def _fix_out_named_y(f):
  33. """
  34. Allow the out argument to be passed as the name `y` (deprecated)
  35. This decorator should only be used if _deprecate_out_named_y is used on
  36. a corresponding dispatcher fucntion.
  37. """
  38. @functools.wraps(f)
  39. def func(x, out=None, **kwargs):
  40. if 'y' in kwargs:
  41. # we already did error checking in _deprecate_out_named_y
  42. out = kwargs.pop('y')
  43. return f(x, out=out, **kwargs)
  44. return func
  45. if not ENABLE_ARRAY_FUNCTION:
  46. _fix_out_named_y = _deprecate_out_named_y
  47. @_deprecate_out_named_y
  48. def _dispatcher(x, out=None):
  49. return (x, out)
  50. @array_function_dispatch(_dispatcher, verify=False, module='numpy')
  51. @_fix_out_named_y
  52. def fix(x, out=None):
  53. """
  54. Round to nearest integer towards zero.
  55. Round an array of floats element-wise to nearest integer towards zero.
  56. The rounded values are returned as floats.
  57. Parameters
  58. ----------
  59. x : array_like
  60. An array of floats to be rounded
  61. y : ndarray, optional
  62. Output array
  63. Returns
  64. -------
  65. out : ndarray of floats
  66. The array of rounded numbers
  67. See Also
  68. --------
  69. trunc, floor, ceil
  70. around : Round to given number of decimals
  71. Examples
  72. --------
  73. >>> np.fix(3.14)
  74. 3.0
  75. >>> np.fix(3)
  76. 3.0
  77. >>> np.fix([2.1, 2.9, -2.1, -2.9])
  78. array([ 2., 2., -2., -2.])
  79. """
  80. # promote back to an array if flattened
  81. res = nx.asanyarray(nx.ceil(x, out=out))
  82. res = nx.floor(x, out=res, where=nx.greater_equal(x, 0))
  83. # when no out argument is passed and no subclasses are involved, flatten
  84. # scalars
  85. if out is None and type(res) is nx.ndarray:
  86. res = res[()]
  87. return res
  88. @array_function_dispatch(_dispatcher, verify=False, module='numpy')
  89. @_fix_out_named_y
  90. def isposinf(x, out=None):
  91. """
  92. Test element-wise for positive infinity, return result as bool array.
  93. Parameters
  94. ----------
  95. x : array_like
  96. The input array.
  97. y : array_like, optional
  98. A boolean array with the same shape as `x` to store the result.
  99. Returns
  100. -------
  101. out : ndarray
  102. A boolean array with the same dimensions as the input.
  103. If second argument is not supplied then a boolean array is returned
  104. with values True where the corresponding element of the input is
  105. positive infinity and values False where the element of the input is
  106. not positive infinity.
  107. If a second argument is supplied the result is stored there. If the
  108. type of that array is a numeric type the result is represented as zeros
  109. and ones, if the type is boolean then as False and True.
  110. The return value `out` is then a reference to that array.
  111. See Also
  112. --------
  113. isinf, isneginf, isfinite, isnan
  114. Notes
  115. -----
  116. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  117. (IEEE 754).
  118. Errors result if the second argument is also supplied when x is a scalar
  119. input, if first and second arguments have different shapes, or if the
  120. first argument has complex values
  121. Examples
  122. --------
  123. >>> np.isposinf(np.PINF)
  124. array(True, dtype=bool)
  125. >>> np.isposinf(np.inf)
  126. array(True, dtype=bool)
  127. >>> np.isposinf(np.NINF)
  128. array(False, dtype=bool)
  129. >>> np.isposinf([-np.inf, 0., np.inf])
  130. array([False, False, True])
  131. >>> x = np.array([-np.inf, 0., np.inf])
  132. >>> y = np.array([2, 2, 2])
  133. >>> np.isposinf(x, y)
  134. array([0, 0, 1])
  135. >>> y
  136. array([0, 0, 1])
  137. """
  138. is_inf = nx.isinf(x)
  139. try:
  140. signbit = ~nx.signbit(x)
  141. except TypeError:
  142. raise TypeError('This operation is not supported for complex values '
  143. 'because it would be ambiguous.')
  144. else:
  145. return nx.logical_and(is_inf, signbit, out)
  146. @array_function_dispatch(_dispatcher, verify=False, module='numpy')
  147. @_fix_out_named_y
  148. def isneginf(x, out=None):
  149. """
  150. Test element-wise for negative infinity, return result as bool array.
  151. Parameters
  152. ----------
  153. x : array_like
  154. The input array.
  155. out : array_like, optional
  156. A boolean array with the same shape and type as `x` to store the
  157. result.
  158. Returns
  159. -------
  160. out : ndarray
  161. A boolean array with the same dimensions as the input.
  162. If second argument is not supplied then a numpy boolean array is
  163. returned with values True where the corresponding element of the
  164. input is negative infinity and values False where the element of
  165. the input is not negative infinity.
  166. If a second argument is supplied the result is stored there. If the
  167. type of that array is a numeric type the result is represented as
  168. zeros and ones, if the type is boolean then as False and True. The
  169. return value `out` is then a reference to that array.
  170. See Also
  171. --------
  172. isinf, isposinf, isnan, isfinite
  173. Notes
  174. -----
  175. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  176. (IEEE 754).
  177. Errors result if the second argument is also supplied when x is a scalar
  178. input, if first and second arguments have different shapes, or if the
  179. first argument has complex values.
  180. Examples
  181. --------
  182. >>> np.isneginf(np.NINF)
  183. array(True, dtype=bool)
  184. >>> np.isneginf(np.inf)
  185. array(False, dtype=bool)
  186. >>> np.isneginf(np.PINF)
  187. array(False, dtype=bool)
  188. >>> np.isneginf([-np.inf, 0., np.inf])
  189. array([ True, False, False])
  190. >>> x = np.array([-np.inf, 0., np.inf])
  191. >>> y = np.array([2, 2, 2])
  192. >>> np.isneginf(x, y)
  193. array([1, 0, 0])
  194. >>> y
  195. array([1, 0, 0])
  196. """
  197. is_inf = nx.isinf(x)
  198. try:
  199. signbit = nx.signbit(x)
  200. except TypeError:
  201. raise TypeError('This operation is not supported for complex values '
  202. 'because it would be ambiguous.')
  203. else:
  204. return nx.logical_and(is_inf, signbit, out)