ufuncs.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """
  2. ===================
  3. Universal Functions
  4. ===================
  5. Ufuncs are, generally speaking, mathematical functions or operations that are
  6. applied element-by-element to the contents of an array. That is, the result
  7. in each output array element only depends on the value in the corresponding
  8. input array (or arrays) and on no other array elements. NumPy comes with a
  9. large suite of ufuncs, and scipy extends that suite substantially. The simplest
  10. example is the addition operator: ::
  11. >>> np.array([0,2,3,4]) + np.array([1,1,-1,2])
  12. array([1, 3, 2, 6])
  13. The unfunc module lists all the available ufuncs in numpy. Documentation on
  14. the specific ufuncs may be found in those modules. This documentation is
  15. intended to address the more general aspects of unfuncs common to most of
  16. them. All of the ufuncs that make use of Python operators (e.g., +, -, etc.)
  17. have equivalent functions defined (e.g. add() for +)
  18. Type coercion
  19. =============
  20. What happens when a binary operator (e.g., +,-,\\*,/, etc) deals with arrays of
  21. two different types? What is the type of the result? Typically, the result is
  22. the higher of the two types. For example: ::
  23. float32 + float64 -> float64
  24. int8 + int32 -> int32
  25. int16 + float32 -> float32
  26. float32 + complex64 -> complex64
  27. There are some less obvious cases generally involving mixes of types
  28. (e.g. uints, ints and floats) where equal bit sizes for each are not
  29. capable of saving all the information in a different type of equivalent
  30. bit size. Some examples are int32 vs float32 or uint32 vs int32.
  31. Generally, the result is the higher type of larger size than both
  32. (if available). So: ::
  33. int32 + float32 -> float64
  34. uint32 + int32 -> int64
  35. Finally, the type coercion behavior when expressions involve Python
  36. scalars is different than that seen for arrays. Since Python has a
  37. limited number of types, combining a Python int with a dtype=np.int8
  38. array does not coerce to the higher type but instead, the type of the
  39. array prevails. So the rules for Python scalars combined with arrays is
  40. that the result will be that of the array equivalent the Python scalar
  41. if the Python scalar is of a higher 'kind' than the array (e.g., float
  42. vs. int), otherwise the resultant type will be that of the array.
  43. For example: ::
  44. Python int + int8 -> int8
  45. Python float + int8 -> float64
  46. ufunc methods
  47. =============
  48. Binary ufuncs support 4 methods.
  49. **.reduce(arr)** applies the binary operator to elements of the array in
  50. sequence. For example: ::
  51. >>> np.add.reduce(np.arange(10)) # adds all elements of array
  52. 45
  53. For multidimensional arrays, the first dimension is reduced by default: ::
  54. >>> np.add.reduce(np.arange(10).reshape(2,5))
  55. array([ 5, 7, 9, 11, 13])
  56. The axis keyword can be used to specify different axes to reduce: ::
  57. >>> np.add.reduce(np.arange(10).reshape(2,5),axis=1)
  58. array([10, 35])
  59. **.accumulate(arr)** applies the binary operator and generates an an
  60. equivalently shaped array that includes the accumulated amount for each
  61. element of the array. A couple examples: ::
  62. >>> np.add.accumulate(np.arange(10))
  63. array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
  64. >>> np.multiply.accumulate(np.arange(1,9))
  65. array([ 1, 2, 6, 24, 120, 720, 5040, 40320])
  66. The behavior for multidimensional arrays is the same as for .reduce(),
  67. as is the use of the axis keyword).
  68. **.reduceat(arr,indices)** allows one to apply reduce to selected parts
  69. of an array. It is a difficult method to understand. See the documentation
  70. at:
  71. **.outer(arr1,arr2)** generates an outer operation on the two arrays arr1 and
  72. arr2. It will work on multidimensional arrays (the shape of the result is
  73. the concatenation of the two input shapes.: ::
  74. >>> np.multiply.outer(np.arange(3),np.arange(4))
  75. array([[0, 0, 0, 0],
  76. [0, 1, 2, 3],
  77. [0, 2, 4, 6]])
  78. Output arguments
  79. ================
  80. All ufuncs accept an optional output array. The array must be of the expected
  81. output shape. Beware that if the type of the output array is of a different
  82. (and lower) type than the output result, the results may be silently truncated
  83. or otherwise corrupted in the downcast to the lower type. This usage is useful
  84. when one wants to avoid creating large temporary arrays and instead allows one
  85. to reuse the same array memory repeatedly (at the expense of not being able to
  86. use more convenient operator notation in expressions). Note that when the
  87. output argument is used, the ufunc still returns a reference to the result.
  88. >>> x = np.arange(2)
  89. >>> np.add(np.arange(2),np.arange(2.),x)
  90. array([0, 2])
  91. >>> x
  92. array([0, 2])
  93. and & or as ufuncs
  94. ==================
  95. Invariably people try to use the python 'and' and 'or' as logical operators
  96. (and quite understandably). But these operators do not behave as normal
  97. operators since Python treats these quite differently. They cannot be
  98. overloaded with array equivalents. Thus using 'and' or 'or' with an array
  99. results in an error. There are two alternatives:
  100. 1) use the ufunc functions logical_and() and logical_or().
  101. 2) use the bitwise operators & and \\|. The drawback of these is that if
  102. the arguments to these operators are not boolean arrays, the result is
  103. likely incorrect. On the other hand, most usages of logical_and and
  104. logical_or are with boolean arrays. As long as one is careful, this is
  105. a convenient way to apply these operators.
  106. """
  107. from __future__ import division, absolute_import, print_function