[Numpy-discussion] numpy ndarray questions
Sturla Molden
sturla at molden.no
Tue Jan 27 06:37:45 EST 2009
On 1/27/2009 1:26 AM, Jochen wrote:
> a = fftw3.AlignedArray(1024,complex)
>
> a = a+1
= used this way is not assignment, it is name binding.
It is easy to use function's like fftw_malloc with NumPy:
import ctypes
import numpy
fftw_malloc = ctypes.cdll.fftw.fftw_malloc
fftw_malloc.argtypes = [ctypes.c_ulong,]
fftw_malloc.restype = ctypes.c_ulong
def aligned_array(N, dtype):
d = dtype()
address = fftw_malloc(N * d.nbytes) # restype = ctypes.c_ulong
if (address = 0): raise MemoryError, 'fftw_malloc returned NULL'
class Dummy(object): pass
d = Dummy()
d.__array_interface__ = {
'data' = (address, False),
'typestr' : dtype.str,
'descr' : dtype.descr,
'shape' : shape,
'strides' : strides,
'version' : 3
}
return numpy.asarray(d)
If you have to check for a particular alignment before calling fftw,
that is trivial as well:
def is_aligned(array, boundary):
address = array.__array_interface__['data'][0]
return not(address % boundary)
> there a way that I get a different object type? Or even better is there
> a way to prevent operations like a=a+1 or make them automatically
> in-place operations?
a = a + 1 # rebinds the name 'a' to another array
a[:] = a + 1 # fills a with the result of a + 1
This has to do with Python syntax, not NumPy per se. You cannot overload
the behaviour of Python's name binding operator (=).
Sturla Molden
More information about the NumPy-Discussion
mailing list