constants.py 9.1 KB

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