{ } Raw JSON

bundles / numpy 2.4.4 / docs

Doc

ufunc API

docs/reference:c-api:ufunc

Constants

UFUNC_{THING}_{ERR}

Deprecated, use NPY_{THING}_{ERR} instead

PyUFunc_{VALUE}

Macros

Types

Functions

Create a new broadcasting universal function from required variables. Each ufunc builds around the notion of an element-by-element operation. Each ufunc object contains pointers to 1-d loops implementing the basic functionality for each supported type.

paramfunc

Must point to an array containing ntypes PyUFuncGenericFunction elements.

paramdata

Should be NULL or a pointer to an array of size ntypes. This array may contain arbitrary extra-data to be passed to the corresponding loop function in the func array, including NULL.

paramtypes

Length (nin + nout) * ntypes array of char encoding the numpy.dtype.num (built-in only) that the corresponding function in the func array accepts. For instance, for a comparison ufunc with three ntypes, two nin and one nout, where the first function accepts numpy.int32 and the second numpy.int64, with both returning numpy.bool_, types would be (char[]) {5, 5, 0, 7, 7, 0} since NPY_INT32 is 5, NPY_INT64 is 7, and NPY_BOOL is 0.

The bit-width names can also be used (e.g. NPY_INT32, NPY_COMPLEX128 ) if desired.

ufuncs.casting will be used at runtime to find the first func callable by the input/output provided.

paramntypes

How many different data-type-specific functions the ufunc has implemented.

paramnin

The number of inputs to this operation.

paramnout

The number of outputs

paramidentity

Either PyUFunc_One, PyUFunc_Zero, PyUFunc_MinusOne, or PyUFunc_None. This specifies what should be returned when an empty array is passed to the reduce method of the ufunc. The special value PyUFunc_IdentityValue may only be used with the PyUFunc_FromFuncAndDataAndSignatureAndIdentity method, to allow an arbitrary python object to be used as the identity.

paramname

The name for the ufunc as a NULL terminated string. Specifying a name of 'add' or 'multiply' enables a special behavior for integer-typed reductions when no dtype is given. If the input type is an integer (or boolean) data type smaller than the size of the numpy.int_ data type, it will be internally upcast to the numpy.int_ (or numpy.uint) data type.

paramdoc

Allows passing in a documentation string to be stored with the ufunc. The documentation string should not contain the name of the function or the calling signature as that will be dynamically determined from the object and available when accessing the __doc__ attribute of the ufunc.

paramunused

Unused and present for backwards compatibility of the C-API.

This function is very similar to PyUFunc_FromFuncAndData above, but has an extra signature argument, to define a generalized universal functions. Similarly to how ufuncs are built around an element-by-element operation, gufuncs are around subarray-by-subarray operations, the signature defining the subarrays to operate on.

paramsignature

The signature for the new gufunc. Setting it to NULL is equivalent to calling PyUFunc_FromFuncAndData. A copy of the string is made, so the passed in buffer can be freed.

This function is very similar to PyUFunc_FromFuncAndDataAndSignature above, but has an extra identity_value argument, to define an arbitrary identity for the ufunc when identity is passed as PyUFunc_IdentityValue.

paramidentity_value

The identity for the new gufunc. Must be passed as NULL unless the identity argument is PyUFunc_IdentityValue. Setting it to NULL is equivalent to calling PyUFunc_FromFuncAndDataAndSignature.

This function allows the user to register a 1-d loop with an already- created ufunc to be used whenever the ufunc is called with any of its input arguments as the user-defined data-type. This is needed in order to make ufuncs work with built-in data-types. The data-type must have been previously registered with the numpy system. The loop is passed in as function. This loop can take arbitrary data which should be passed in as data. The data-types the loop requires are passed in as arg_types which must be a pointer to memory at least as large as ufunc->nargs.

This function behaves like PyUFunc_RegisterLoopForType above, except that it allows the user to register a 1-d loop using PyArray_Descr objects instead of dtype type num values. This allows a 1-d loop to be registered for structured array data-dtypes and custom data-types instead of scalar data-types.

Replace a 1-d loop matching the given signature in the already-created ufunc with the new 1-d loop newfunc. Return the old 1-d loop function in oldfunc. Return 0 on success and -1 on failure. This function works only with built-in types (use PyUFunc_RegisterLoopForType for user-defined types). A signature is an array of data-type numbers indicating the inputs followed by the outputs assumed by the 1-d loop.

Generic functions

At the core of every ufunc is a collection of type-specific functions that defines the basic functionality for each of the supported types. These functions must evaluate the underlying function times. Extra-data may be passed in that may be used during the calculation. This feature allows some general functions to be used as these basic looping functions. The general function has all the code needed to point variables to the right place and set up a function call. The general function assumes that the actual function to call is passed in as the extra data and calls it with the correct values. All of these functions are suitable for placing directly in the array of functions stored in the functions member of the PyUFuncObject structure.

Type specific, core 1-d functions for ufuncs where each calculation is obtained by calling a function taking one input argument and returning one output. This function is passed in func. The letters correspond to dtypechar's of the supported data types ( e - half, f - float, d - double, g - long double, F - cfloat, D - cdouble, G - clongdouble). The argument func must support the same signature. The _As_X_X variants assume ndarray's of one data type but cast the values to use an underlying function that takes a different data type. Thus, PyUFunc_f_f_As_d_d uses ndarrays of data type NPY_FLOAT but calls out to a C-function that takes double and returns double.

Type specific, core 1-d functions for ufuncs where each calculation is obtained by calling a function taking two input arguments and returning one output. The underlying function to call is passed in as func. The letters correspond to dtypechar's of the specific data type supported by the general-purpose function. The argument func must support the corresponding signature. The _As_XX_X variants assume ndarrays of one data type but cast the values at each iteration of the loop to use the underlying function that takes a different data type.

One-input, one-output, and two-input, one-output core 1-d functions for the NPY_OBJECT data type. These functions handle reference count issues and return early on error. The actual function to call is func and it must accept calls with the signature (PyObject*) (PyObject*) for PyUFunc_O_O or (PyObject*)(PyObject *, PyObject *) for PyUFunc_OO_O.

This general purpose 1-d core function assumes that func is a string representing a method of the input object. For each iteration of the loop, the Python object is extracted from the array and its func method is called returning the result to the output array.

This general purpose 1-d core function assumes that func is a string representing a method of the input object that takes one argument. The first argument in args is the method whose function is called, the second argument in args is the argument passed to the function. The output of the function is stored in the third entry of args.

This is the 1-d core function used by the dynamic ufuncs created by umath.frompyfunc(function, nin, nout). In this case func is a pointer to a PyUFunc_PyFuncData structure which has definition

At each iteration of the loop, the nin input objects are extracted from their object arrays and placed into an argument tuple, the Python callable is called with the input arguments, and the nout outputs are placed into their object arrays.

Importing the API