constants.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. # -*- coding: utf-8 -*-
  2. """
  3. =========
  4. Constants
  5. =========
  6. .. currentmodule:: numpy
  7. NumPy includes several constants:
  8. %(constant_list)s
  9. """
  10. #
  11. # Note: the docstring is autogenerated.
  12. #
  13. import re
  14. import textwrap
  15. # Maintain same format as in numpy.add_newdocs
  16. constants = []
  17. def add_newdoc(module, name, doc):
  18. constants.append((name, doc))
  19. add_newdoc('numpy', 'pi',
  20. """
  21. ``pi = 3.1415926535897932384626433...``
  22. References
  23. ----------
  24. https://en.wikipedia.org/wiki/Pi
  25. """)
  26. add_newdoc('numpy', 'e',
  27. """
  28. Euler's constant, base of natural logarithms, Napier's constant.
  29. ``e = 2.71828182845904523536028747135266249775724709369995...``
  30. See Also
  31. --------
  32. exp : Exponential function
  33. log : Natural logarithm
  34. References
  35. ----------
  36. https://en.wikipedia.org/wiki/E_%28mathematical_constant%29
  37. """)
  38. add_newdoc('numpy', 'euler_gamma',
  39. """
  40. ``γ = 0.5772156649015328606065120900824024310421...``
  41. References
  42. ----------
  43. https://en.wikipedia.org/wiki/Euler-Mascheroni_constant
  44. """)
  45. add_newdoc('numpy', 'inf',
  46. """
  47. IEEE 754 floating point representation of (positive) infinity.
  48. Returns
  49. -------
  50. y : float
  51. A floating point representation of positive infinity.
  52. See Also
  53. --------
  54. isinf : Shows which elements are positive or negative infinity
  55. isposinf : Shows which elements are positive infinity
  56. isneginf : Shows which elements are negative infinity
  57. isnan : Shows which elements are Not a Number
  58. isfinite : Shows which elements are finite (not one of Not a Number,
  59. positive infinity and negative infinity)
  60. Notes
  61. -----
  62. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  63. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  64. Also that positive infinity is not equivalent to negative infinity. But
  65. infinity is equivalent to positive infinity.
  66. `Inf`, `Infinity`, `PINF` and `infty` are aliases for `inf`.
  67. Examples
  68. --------
  69. >>> np.inf
  70. inf
  71. >>> np.array([1]) / 0.
  72. array([ Inf])
  73. """)
  74. add_newdoc('numpy', 'nan',
  75. """
  76. IEEE 754 floating point representation of Not a Number (NaN).
  77. Returns
  78. -------
  79. y : A floating point representation of Not a Number.
  80. See Also
  81. --------
  82. isnan : Shows which elements are Not a Number.
  83. isfinite : Shows which elements are finite (not one of
  84. Not a Number, positive infinity and negative infinity)
  85. Notes
  86. -----
  87. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  88. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  89. `NaN` and `NAN` are aliases of `nan`.
  90. Examples
  91. --------
  92. >>> np.nan
  93. nan
  94. >>> np.log(-1)
  95. nan
  96. >>> np.log([-1, 1, 2])
  97. array([ NaN, 0. , 0.69314718])
  98. """)
  99. add_newdoc('numpy', 'newaxis',
  100. """
  101. A convenient alias for None, useful for indexing arrays.
  102. Examples
  103. --------
  104. >>> newaxis is None
  105. True
  106. >>> x = np.arange(3)
  107. >>> x
  108. array([0, 1, 2])
  109. >>> x[:, newaxis]
  110. array([[0],
  111. [1],
  112. [2]])
  113. >>> x[:, newaxis, newaxis]
  114. array([[[0]],
  115. [[1]],
  116. [[2]]])
  117. >>> x[:, newaxis] * x
  118. array([[0, 0, 0],
  119. [0, 1, 2],
  120. [0, 2, 4]])
  121. Outer product, same as ``outer(x, y)``:
  122. >>> y = np.arange(3, 6)
  123. >>> x[:, newaxis] * y
  124. array([[ 0, 0, 0],
  125. [ 3, 4, 5],
  126. [ 6, 8, 10]])
  127. ``x[newaxis, :]`` is equivalent to ``x[newaxis]`` and ``x[None]``:
  128. >>> x[newaxis, :].shape
  129. (1, 3)
  130. >>> x[newaxis].shape
  131. (1, 3)
  132. >>> x[None].shape
  133. (1, 3)
  134. >>> x[:, newaxis].shape
  135. (3, 1)
  136. """)
  137. add_newdoc('numpy', 'NZERO',
  138. """
  139. IEEE 754 floating point representation of negative zero.
  140. Returns
  141. -------
  142. y : float
  143. A floating point representation of negative zero.
  144. See Also
  145. --------
  146. PZERO : Defines positive zero.
  147. isinf : Shows which elements are positive or negative infinity.
  148. isposinf : Shows which elements are positive infinity.
  149. isneginf : Shows which elements are negative infinity.
  150. isnan : Shows which elements are Not a Number.
  151. isfinite : Shows which elements are finite - not one of
  152. Not a Number, positive infinity and negative infinity.
  153. Notes
  154. -----
  155. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  156. (IEEE 754). Negative zero is considered to be a finite number.
  157. Examples
  158. --------
  159. >>> np.NZERO
  160. -0.0
  161. >>> np.PZERO
  162. 0.0
  163. >>> np.isfinite([np.NZERO])
  164. array([ True])
  165. >>> np.isnan([np.NZERO])
  166. array([False])
  167. >>> np.isinf([np.NZERO])
  168. array([False])
  169. """)
  170. add_newdoc('numpy', 'PZERO',
  171. """
  172. IEEE 754 floating point representation of positive zero.
  173. Returns
  174. -------
  175. y : float
  176. A floating point representation of positive zero.
  177. See Also
  178. --------
  179. NZERO : Defines negative zero.
  180. isinf : Shows which elements are positive or negative infinity.
  181. isposinf : Shows which elements are positive infinity.
  182. isneginf : Shows which elements are negative infinity.
  183. isnan : Shows which elements are Not a Number.
  184. isfinite : Shows which elements are finite - not one of
  185. Not a Number, positive infinity and negative infinity.
  186. Notes
  187. -----
  188. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  189. (IEEE 754). Positive zero is considered to be a finite number.
  190. Examples
  191. --------
  192. >>> np.PZERO
  193. 0.0
  194. >>> np.NZERO
  195. -0.0
  196. >>> np.isfinite([np.PZERO])
  197. array([ True])
  198. >>> np.isnan([np.PZERO])
  199. array([False])
  200. >>> np.isinf([np.PZERO])
  201. array([False])
  202. """)
  203. add_newdoc('numpy', 'NAN',
  204. """
  205. IEEE 754 floating point representation of Not a Number (NaN).
  206. `NaN` and `NAN` are equivalent definitions of `nan`. Please use
  207. `nan` instead of `NAN`.
  208. See Also
  209. --------
  210. nan
  211. """)
  212. add_newdoc('numpy', 'NaN',
  213. """
  214. IEEE 754 floating point representation of Not a Number (NaN).
  215. `NaN` and `NAN` are equivalent definitions of `nan`. Please use
  216. `nan` instead of `NaN`.
  217. See Also
  218. --------
  219. nan
  220. """)
  221. add_newdoc('numpy', 'NINF',
  222. """
  223. IEEE 754 floating point representation of negative infinity.
  224. Returns
  225. -------
  226. y : float
  227. A floating point representation of negative infinity.
  228. See Also
  229. --------
  230. isinf : Shows which elements are positive or negative infinity
  231. isposinf : Shows which elements are positive infinity
  232. isneginf : Shows which elements are negative infinity
  233. isnan : Shows which elements are Not a Number
  234. isfinite : Shows which elements are finite (not one of Not a Number,
  235. positive infinity and negative infinity)
  236. Notes
  237. -----
  238. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  239. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  240. Also that positive infinity is not equivalent to negative infinity. But
  241. infinity is equivalent to positive infinity.
  242. Examples
  243. --------
  244. >>> np.NINF
  245. -inf
  246. >>> np.log(0)
  247. -inf
  248. """)
  249. add_newdoc('numpy', 'PINF',
  250. """
  251. IEEE 754 floating point representation of (positive) infinity.
  252. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  253. `inf`. For more details, see `inf`.
  254. See Also
  255. --------
  256. inf
  257. """)
  258. add_newdoc('numpy', 'infty',
  259. """
  260. IEEE 754 floating point representation of (positive) infinity.
  261. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  262. `inf`. For more details, see `inf`.
  263. See Also
  264. --------
  265. inf
  266. """)
  267. add_newdoc('numpy', 'Inf',
  268. """
  269. IEEE 754 floating point representation of (positive) infinity.
  270. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  271. `inf`. For more details, see `inf`.
  272. See Also
  273. --------
  274. inf
  275. """)
  276. add_newdoc('numpy', 'Infinity',
  277. """
  278. IEEE 754 floating point representation of (positive) infinity.
  279. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  280. `inf`. For more details, see `inf`.
  281. See Also
  282. --------
  283. inf
  284. """)
  285. if __doc__:
  286. constants_str = []
  287. constants.sort()
  288. for name, doc in constants:
  289. s = textwrap.dedent(doc).replace("\n", "\n ")
  290. # Replace sections by rubrics
  291. lines = s.split("\n")
  292. new_lines = []
  293. for line in lines:
  294. m = re.match(r'^(\s+)[-=]+\s*$', line)
  295. if m and new_lines:
  296. prev = textwrap.dedent(new_lines.pop())
  297. new_lines.append('%s.. rubric:: %s' % (m.group(1), prev))
  298. new_lines.append('')
  299. else:
  300. new_lines.append(line)
  301. s = "\n".join(new_lines)
  302. # Done.
  303. constants_str.append(""".. data:: %s\n %s""" % (name, s))
  304. constants_str = "\n".join(constants_str)
  305. __doc__ = __doc__ % dict(constant_list=constants_str)
  306. del constants_str, name, doc
  307. del line, lines, new_lines, m, s, prev
  308. del constants, add_newdoc