含义

A universal function (or ufunc for short) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting and several other standard features.

Broadcast

broadcast规则:

  1. The arrays all have exactly the same shape.
  2. The arrays all have the same number of dimensions and the length of each dimensions is either a common length or 1.
  3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy property 2.

Example

If a.shape is (5,1), b.shape is (1,6), c.shape is (6,) and d.shape is () so that d is a scalar, then a, b, c, and d are all broadcastable to dimension (5,6); and

  • a acts like a (5,6) array where a[:,0] is broadcast to the other columns,
  • b acts like a (5,6) array where b[0,:] is broadcast to the other rows,
  • c acts like a (1,6) array and therefore like a (5,6) array where c[:] is broadcast to every row, and finally,
  • d acts like a (5,6) array where the single value is repeated.

常见的ufunc

  • 数学操作:addsubstractmultiplydivideabsoluteexploglog10sqrtsquare
  • 三角函数:sincostanarcsinarccosarctansinhcoshtanharctanhdeg2radrad2deg
  • 比较函数:greaterless_equalnot_equalequalmaximumminimum
  • 浮点函数:isinfisnanfloor、`ceil