bundles / numpy 2.4.4 / numpy / ctypeslib / _ctypeslib
module
numpy.ctypeslib._ctypeslib
Members
Additional content
ctypes Utility Functions
See Also
load_libraryLoad a C library. ndpointer : Array restype/argtype with verification. as_ctypes : Create a ctypes array from an ndarray. as_array : Create an ndarray from a ctypes array.
References
[1]
"SciPy Cookbook: ctypes", https://scipy-cookbook.readthedocs.io/items/Ctypes.html
Examples
Load the C library:
>>> _lib = np.ctypeslib.load_library('libmystuff', '.') #doctest: +SKIPOur result type, an ndarray that must be of type double, be 1-dimensional and is C-contiguous in memory:
>>> array_1d_double = np.ctypeslib.ndpointer( ... dtype=np.double, ... ndim=1, flags='CONTIGUOUS') #doctest: +SKIP
Our C-function typically takes an array and updates its values in-place. For example
void foo_func(double* x, int length) { int i; for (i = 0; i < length; i++) { x[i] = i*i; } }
We wrap it using:
>>> _lib.foo_func.restype = None #doctest: +SKIP >>> _lib.foo_func.argtypes = [array_1d_double, c_int] #doctest: +SKIP
Then, we're ready to call foo_func:
>>> out = np.empty(15, dtype=np.double) >>> _lib.foo_func(out, len(out)) #doctest: +SKIP
See also
- as_array
Create an ndarray from a ctypes array.
- as_ctypes
Create a ctypes array from an ndarray.
- load_library
Load a C library.
- ndpointer
Array restype/argtype with verification.
Aliases
-
numpy.ctypeslib._ctypeslib