Methods

All ufuncs have 4 methods. However, these methods only make sense on ufuncs that take two input arguments and return one output argument. Attempting to call these methods on other ufuncs will cause a ValueError. The reduce-like methods all take an axis keyword and a dtype keyword, and the arrays must all have dimension >= 1. The axis keyword specifies which axis of the array the reduction will take place over and may be negative, but must be an integer. The dtype keyword allows you to manage a very common problem that arises when naively using <op>.reduce. Sometimes you may have an array of a certain data type and wish to add up all of its elements, but the result does not fit into the data type of the array. This commonly happens if you have an array of single-byte integers. The dtype keyword allows you to alter the data type that the reduction takes place over (and therefore the type of the output). Thus, you can ensure that the output is a data type with

X

?

b

h

i

l

q

p

B

H

I

L

Q

P

f

d

g

F

D

G

S

U

V

O

?

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

b

0

1

1

1

1

1

1

0

0

0

0

0

0

1

1

1

1

1

1

1

1

1

1

h

0

0

1

1

1

1

1

0

0

0

0

0

0

1

1

1

1

1

1

1

1

1

1

i

0

0

0

1

1

1

1

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

l

0

0

0

1

1

1

1

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

q

0

0

0

0

0

1

0

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

p

0

0

0

1

1

1

1

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

B

0

0

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

H

0

0

0

1

1

1

1

0

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

i

0

0

0

0

0

1

0

0

0

1

1

1

1

0

1

1

0

1

1

1

1

1

1

L

0

0

0

0

0

1

0

0

0

1

1

1

1

0

1

1

0

1

1

1

1

1

1

Q

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

1

0

1

1

1

1

1

1

P

0

0

0

0

0

1

0

0

0

1

1

1

1

0

1

1

0

1

1

1

1

1

1

f

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

1

1

1

1

1

1

1

d

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

0

1

1

1

1

1

1

g

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

1

1

1

1

F

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

1

1

1

1

D

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

1

1

1

G

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

1

1

S

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

1

U

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

1

V

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

O

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

1

Figure 9.1: Code segment showing the can cast safely table for a 32-bit system.

large-enough precision to handle your output. The responsibility of altering the reduce type is mostly up to you. There is one exception: if no dtype is given for a reduction on the "add" or "multiply" operations, then if the input type is an integer (or boolean) data-type and smaller than the size of the int_ data type, it will be internally upcast to the int_ (or uint) data type.

0 0

Post a comment