ufunclike.py 6.2 KB

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