Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.ma.MaskedArray.ctypes

MaskedArray.ctypes

An object to simplify the interaction of the array with the ctypesmodule.

This attribute creates an object that makes it easier to use arrayswhen calling shared libraries with the ctypes module. The returnedobject has, among others, data, shape, and strides attributes (seeNotes below) which themselves return ctypes objects that can be usedas arguments to a shared library.

Parameters:

None

Returns:

c : Python object

Possessing attributes data, shape, strides, etc.

See also

numpy.ctypeslib

Notes

Below are the public attributes of this object which were documentedin “Guide to NumPy” (we have omitted undocumented public attributes,as well as documented private attributes):

  • data: A pointer to the memory area of the array as a Python integer.This memory area may contain data that is not aligned, or not in correctbyte-order. The memory area may not even be writeable. The arrayflags and data-type of this array should be respected when passing thisattribute to arbitrary C-code to avoid trouble that can include Pythoncrashing. User Beware! The value of this attribute is exactly the sameas self._array_interface_[‘data’][0].
  • shape (c_intp*self.ndim): A ctypes array of length self.ndim wherethe basetype is the C-integer corresponding to dtype(‘p’) on thisplatform. This base-type could be c_int, c_long, or c_longlongdepending on the platform. The c_intp type is defined accordingly innumpy.ctypeslib. The ctypes array contains the shape of the underlyingarray.
  • strides (c_intp*self.ndim): A ctypes array of length self.ndim wherethe basetype is the same as for the shape attribute. This ctypes arraycontains the strides information from the underlying array. This stridesinformation is important for showing how many bytes must be jumped toget to the next element in the array.
  • data_as(obj): Return the data pointer cast to a particular c-types object.For example, calling self._as_parameter_ is equivalent toself.data_as(ctypes.c_void_p). Perhaps you want to use the data as apointer to a ctypes array of floating-point data:self.data_as(ctypes.POINTER(ctypes.c_double)).
  • shape_as(obj): Return the shape tuple as an array of some other c-typestype. For example: self.shape_as(ctypes.c_short).
  • strides_as(obj): Return the strides tuple as an array of some otherc-types type. For example: self.strides_as(ctypes.c_longlong).

Be careful using the ctypes attribute - especially on temporaryarrays or arrays constructed on the fly. For example, calling(a+b).ctypes.data_as(ctypes.c_void_p) returns a pointer to memorythat is invalid because the array created as (a+b) is deallocatedbefore the next Python statement. You can avoid this problem usingeitherc=a+b orct=(a+b).ctypes. In the latter case, ct willhold a reference to the array until ct is deleted or re-assigned.

If the ctypes module is not available, then the ctypes attributeof array objects still returns something useful, but ctypes objectsare not returned and errors may be raised instead. In particular,the object will still have the as parameter attribute which willreturn an integer equal to the data attribute.

Examples

>>>importctypes>>>xarray([[0, 1],       [2, 3]])>>>x.ctypes.data30439712>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_long))<ctypes.LP_c_long object at 0x01F01300>>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)).contentsc_long(0)>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_longlong)).contentsc_longlong(4294967296L)>>>x.ctypes.shape<numpy.core._internal.c_long_Array_2 object at 0x01FFD580>>>>x.ctypes.shape_as(ctypes.c_long)<numpy.core._internal.c_long_Array_2 object at 0x01FCE620>>>>x.ctypes.strides<numpy.core._internal.c_long_Array_2 object at 0x01FCE620>>>>x.ctypes.strides_as(ctypes.c_longlong)<numpy.core._internal.c_longlong_Array_2 object at 0x01F01300>

Previous topic

numpy.ma.MaskedArray.base

Next topic

numpy.ma.MaskedArray.dtype

  • © Copyright 2008-2009, The Scipy community.
  • Last updated on Jun 10, 2017.
  • Created usingSphinx 1.5.3.

[8]ページ先頭

©2009-2025 Movatter.jp