_ufunclike_impl.py 6.2 KB

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