_ufunclike_impl.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. return nx.trunc(x, out=out)
  47. @array_function_dispatch(_dispatcher, verify=False, module='numpy')
  48. def isposinf(x, out=None):
  49. """
  50. Test element-wise for positive infinity, return result as bool array.
  51. Parameters
  52. ----------
  53. x : array_like
  54. The input array.
  55. out : array_like, optional
  56. A location into which the result is stored. If provided, it must have a
  57. shape that the input broadcasts to. If not provided or None, a
  58. freshly-allocated boolean array is returned.
  59. Returns
  60. -------
  61. out : ndarray
  62. A boolean array with the same dimensions as the input.
  63. If second argument is not supplied then a boolean array is returned
  64. with values True where the corresponding element of the input is
  65. positive infinity and values False where the element of the input is
  66. not positive infinity.
  67. If a second argument is supplied the result is stored there. If the
  68. type of that array is a numeric type the result is represented as zeros
  69. and ones, if the type is boolean then as False and True.
  70. The return value `out` is then a reference to that array.
  71. See Also
  72. --------
  73. isinf, isneginf, isfinite, isnan
  74. Notes
  75. -----
  76. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  77. (IEEE 754).
  78. Errors result if the second argument is also supplied when x is a scalar
  79. input, if first and second arguments have different shapes, or if the
  80. first argument has complex values
  81. Examples
  82. --------
  83. >>> import numpy as np
  84. >>> np.isposinf(np.inf)
  85. True
  86. >>> np.isposinf(-np.inf)
  87. False
  88. >>> np.isposinf([-np.inf, 0., np.inf])
  89. array([False, False, True])
  90. >>> x = np.array([-np.inf, 0., np.inf])
  91. >>> y = np.array([2, 2, 2])
  92. >>> np.isposinf(x, y)
  93. array([0, 0, 1])
  94. >>> y
  95. array([0, 0, 1])
  96. """
  97. is_inf = nx.isinf(x)
  98. try:
  99. signbit = ~nx.signbit(x)
  100. except TypeError as e:
  101. dtype = nx.asanyarray(x).dtype
  102. raise TypeError(f'This operation is not supported for {dtype} values '
  103. 'because it would be ambiguous.') from e
  104. else:
  105. return nx.logical_and(is_inf, signbit, out)
  106. @array_function_dispatch(_dispatcher, verify=False, module='numpy')
  107. def isneginf(x, out=None):
  108. """
  109. Test element-wise for negative infinity, return result as bool array.
  110. Parameters
  111. ----------
  112. x : array_like
  113. The input array.
  114. out : array_like, optional
  115. A location into which the result is stored. If provided, it must have a
  116. shape that the input broadcasts to. If not provided or None, a
  117. freshly-allocated boolean array is returned.
  118. Returns
  119. -------
  120. out : ndarray
  121. A boolean array with the same dimensions as the input.
  122. If second argument is not supplied then a numpy boolean array is
  123. returned with values True where the corresponding element of the
  124. input is negative infinity and values False where the element of
  125. the input is not negative infinity.
  126. If a second argument is supplied the result is stored there. If the
  127. type of that array is a numeric type the result is represented as
  128. zeros and ones, if the type is boolean then as False and True. The
  129. return value `out` is then a reference to that array.
  130. See Also
  131. --------
  132. isinf, isposinf, isnan, isfinite
  133. Notes
  134. -----
  135. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  136. (IEEE 754).
  137. Errors result if the second argument is also supplied when x is a scalar
  138. input, if first and second arguments have different shapes, or if the
  139. first argument has complex values.
  140. Examples
  141. --------
  142. >>> import numpy as np
  143. >>> np.isneginf(-np.inf)
  144. True
  145. >>> np.isneginf(np.inf)
  146. False
  147. >>> np.isneginf([-np.inf, 0., np.inf])
  148. array([ True, False, False])
  149. >>> x = np.array([-np.inf, 0., np.inf])
  150. >>> y = np.array([2, 2, 2])
  151. >>> np.isneginf(x, y)
  152. array([1, 0, 0])
  153. >>> y
  154. array([1, 0, 0])
  155. """
  156. is_inf = nx.isinf(x)
  157. try:
  158. signbit = nx.signbit(x)
  159. except TypeError as e:
  160. dtype = nx.asanyarray(x).dtype
  161. raise TypeError(f'This operation is not supported for {dtype} values '
  162. 'because it would be ambiguous.') from e
  163. else:
  164. return nx.logical_and(is_inf, signbit, out)