Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
ufuncs.cpython-313.pyc139 linesDownload Raw Back to __pycache__
1�

2z�j����Srg)a3===================4Universal Functions5===================6 7Ufuncs are, generally speaking, mathematical functions or operations that are8applied element-by-element to the contents of an array. That is, the result9in each output array element only depends on the value in the corresponding10input array (or arrays) and on no other array elements. NumPy comes with a11large suite of ufuncs, and scipy extends that suite substantially. The simplest12example is the addition operator: ::13 14 >>> np.array([0,2,3,4]) + np.array([1,1,-1,2])15 array([1, 3, 2, 6])16 17The ufunc module lists all the available ufuncs in numpy. Documentation on18the specific ufuncs may be found in those modules. This documentation is19intended to address the more general aspects of ufuncs common to most of20them. All of the ufuncs that make use of Python operators (e.g., +, -, etc.)21have equivalent functions defined (e.g. add() for +)22 23Type coercion24=============25 26What happens when a binary operator (e.g., +,-,\*,/, etc) deals with arrays of27two different types? What is the type of the result? Typically, the result is28the higher of the two types. For example: ::29 30 float32 + float64 -> float6431 int8 + int32 -> int3232 int16 + float32 -> float3233 float32 + complex64 -> complex6434 35There are some less obvious cases generally involving mixes of types36(e.g. uints, ints and floats) where equal bit sizes for each are not37capable of saving all the information in a different type of equivalent38bit size. Some examples are int32 vs float32 or uint32 vs int32.39Generally, the result is the higher type of larger size than both40(if available). So: ::41 42 int32 + float32 -> float6443 uint32 + int32 -> int6444 45Finally, the type coercion behavior when expressions involve Python46scalars is different than that seen for arrays. Since Python has a47limited number of types, combining a Python int with a dtype=np.int848array does not coerce to the higher type but instead, the type of the49array prevails. So the rules for Python scalars combined with arrays is50that the result will be that of the array equivalent the Python scalar51if the Python scalar is of a higher 'kind' than the array (e.g., float52vs. int), otherwise the resultant type will be that of the array.53For example: ::54 55  Python int + int8 -> int856  Python float + int8 -> float6457 58ufunc methods59=============60 61Binary ufuncs support 4 methods.62 63**.reduce(arr)** applies the binary operator to elements of the array in64  sequence. For example: ::65 66 >>> np.add.reduce(np.arange(10))  # adds all elements of array67 4568 69For multidimensional arrays, the first dimension is reduced by default: ::70 71 >>> np.add.reduce(np.arange(10).reshape(2,5))72     array([ 5,  7,  9, 11, 13])73 74The axis keyword can be used to specify different axes to reduce: ::75 76 >>> np.add.reduce(np.arange(10).reshape(2,5),axis=1)77 array([10, 35])78 79**.accumulate(arr)** applies the binary operator and generates an80equivalently shaped array that includes the accumulated amount for each81element of the array. A couple examples: ::82 83 >>> np.add.accumulate(np.arange(10))84 array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45])85 >>> np.multiply.accumulate(np.arange(1,9))86 array([    1,     2,     6,    24,   120,   720,  5040, 40320])87 88The behavior for multidimensional arrays is the same as for .reduce(),89as is the use of the axis keyword).90 91**.reduceat(arr,indices)** allows one to apply reduce to selected parts92  of an array. It is a difficult method to understand. See the documentation93  at:94 95**.outer(arr1,arr2)** generates an outer operation on the two arrays arr1 and96  arr2. It will work on multidimensional arrays (the shape of the result is97  the concatenation of the two input shapes.: ::98 99 >>> np.multiply.outer(np.arange(3),np.arange(4))100 array([[0, 0, 0, 0],101        [0, 1, 2, 3],102        [0, 2, 4, 6]])103 104Output arguments105================106 107All ufuncs accept an optional output array. The array must be of the expected108output shape. Beware that if the type of the output array is of a different109(and lower) type than the output result, the results may be silently truncated110or otherwise corrupted in the downcast to the lower type. This usage is useful111when one wants to avoid creating large temporary arrays and instead allows one112to reuse the same array memory repeatedly (at the expense of not being able to113use more convenient operator notation in expressions). Note that when the114output argument is used, the ufunc still returns a reference to the result.115 116 >>> x = np.arange(2)117 >>> np.add(np.arange(2, dtype=float), np.arange(2, dtype=float), x,118 ...        casting='unsafe')119 array([0, 2])120 >>> x121 array([0, 2])122 123and & or as ufuncs124==================125 126Invariably people try to use the python 'and' and 'or' as logical operators127(and quite understandably). But these operators do not behave as normal128operators since Python treats these quite differently. They cannot be129overloaded with array equivalents. Thus using 'and' or 'or' with an array130results in an error. There are two alternatives:131 132 1) use the ufunc functions logical_and() and logical_or().133 2) use the bitwise operators & and \|. The drawback of these is that if134    the arguments to these operators are not boolean arrays, the result is135    likely incorrect. On the other hand, most usages of logical_and and136    logical_or are with boolean arrays. As long as one is careful, this is137    a convenient way to apply these operators.138 139N)�__doc__���VD:\code\apps\devtools\python\user_packages\Python313\site-packages\numpy/doc/ufuncs.py�<module>rs��Ir
codekingpro/portable-devtools · Team Ai