Re: [Numpy-discussion] Mapping of dtype to C types
Den 09.05.2011 15:58, skrev Keith Goodman: On Mon, May 9, 2011 at 1:46 AM, Pauli Virtanenp...@iki.fi wrote: Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? Long can be 32-bit or 64-bit, depending on the platform. The types available in Numpy are listed here, including the information which of them are compatible with which C types: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html The C long seems not to be listed -- but it's the same as Python int, i.e., np.int_ will work. IIRC, the Numpy type codes, dtype.kind, map directly to C types. Does this mapping look right? No. C int is at least 16 bits, C long is at least 32 bits. The size of long and size of int depend on compiler and platform. I'd write a small Cython function and ask the C compiler for an authorative answer. Alternatively you could use ctypes. This is 64-bit Python on Windows: import ctypes ctypes.sizeof(ctypes.c_long) 4 ctypes.sizeof(ctypes.c_int) 4 Sturla ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
2011/5/11 Sturla Molden stu...@molden.no Den 09.05.2011 15:58, skrev Keith Goodman: On Mon, May 9, 2011 at 1:46 AM, Pauli Virtanenp...@iki.fi wrote: Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? Long can be 32-bit or 64-bit, depending on the platform. The types available in Numpy are listed here, including the information which of them are compatible with which C types: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html The C long seems not to be listed -- but it's the same as Python int, i.e., np.int_ will work. IIRC, the Numpy type codes, dtype.kind, map directly to C types. Does this mapping look right? No. C int is at least 16 bits, C long is at least 32 bits. The size of long and size of int depend on compiler and platform. I'd write a small Cython function and ask the C compiler for an authorative answer. Alternatively you could use ctypes. This is 64-bit Python on Windows: import ctypes ctypes.sizeof(ctypes.c_long) 4 ctypes.sizeof(ctypes.c_int) 4 I think Keith's approach should work, as long as there is one C type listed on the url you mention that corresponds to your four dtypes. Something like (not tested at all): map = {} for dtype in ('int32', 'int64', 'float32', 'float64'): for ctype (N.byte, N.short, N.intc, N.long.long, ...): # List all compatible C types here if N.dtype(ctype) == dtype: map{dtype} = ctype break assert len(map) == 4 -=- Olivier ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
On Wed, May 11, 2011 at 2:07 PM, Olivier Delalleau sh...@keba.be wrote: 2011/5/11 Sturla Molden stu...@molden.no Den 09.05.2011 15:58, skrev Keith Goodman: On Mon, May 9, 2011 at 1:46 AM, Pauli Virtanenp...@iki.fi wrote: Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? Long can be 32-bit or 64-bit, depending on the platform. The types available in Numpy are listed here, including the information which of them are compatible with which C types: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html The C long seems not to be listed -- but it's the same as Python int, i.e., np.int_ will work. IIRC, the Numpy type codes, dtype.kind, map directly to C types. Does this mapping look right? No. C int is at least 16 bits, C long is at least 32 bits. The size of long and size of int depend on compiler and platform. I'd write a small Cython function and ask the C compiler for an authorative answer. Alternatively you could use ctypes. This is 64-bit Python on Windows: import ctypes ctypes.sizeof(ctypes.c_long) 4 ctypes.sizeof(ctypes.c_int) 4 I think Keith's approach should work, as long as there is one C type listed on the url you mention that corresponds to your four dtypes. Something like (not tested at all): map = {} for dtype in ('int32', 'int64', 'float32', 'float64'): for ctype (N.byte, N.short, N.intc, N.long.long, ...): # List all compatible C types here if N.dtype(ctype) == dtype: map{dtype} = ctype break assert len(map) == 4 -=- Olivier ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion But that result is just due to how Microsoft implemented it's 64-bit support so it will not work for 64-bit Linux and other similar OSes. I thought that you would just use things like npy_intp (or intp) and npy_float as used by mtrandom: numpy/random/mtrand/numpy.pxi These are defined in numpy/core/include/numpy/ndarraytypes.h Bruce ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? Long can be 32-bit or 64-bit, depending on the platform. The types available in Numpy are listed here, including the information which of them are compatible with which C types: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html The C long seems not to be listed -- but it's the same as Python int, i.e., np.int_ will work. IIRC, the Numpy type codes, dtype.kind, map directly to C types. Pauli ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
On Mon, May 9, 2011 at 1:46 AM, Pauli Virtanen p...@iki.fi wrote: Sun, 08 May 2011 14:45:45 -0700, Keith Goodman wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? Long can be 32-bit or 64-bit, depending on the platform. The types available in Numpy are listed here, including the information which of them are compatible with which C types: http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html The C long seems not to be listed -- but it's the same as Python int, i.e., np.int_ will work. IIRC, the Numpy type codes, dtype.kind, map directly to C types. Does this mapping look right? np.float32 -- float np.float64 -- double if np.int_ == np.int32: # np.int32 -- long # np.int64 -- long long elif np.int_ == np.int64: # np.int32 -- int # np.int64 -- long ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Mapping of dtype to C types
I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
I want to add a question: is there a function that returns the name of the corresponding C type given the numpy type? On Sun, May 8, 2011 at 2:45 PM, Keith Goodman kwgood...@gmail.com wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Mapping of dtype to C types
On Sun, May 8, 2011 at 2:45 PM, Keith Goodman kwgood...@gmail.com wrote: I'm writing a function that accepts four possible dtypes: int32, int64, float32, float64. The function will call a C extension (wrapped in Cython). What are the equivalent C types? int, long, float, double, respectively? Will that work on all systems? I think you're fine on float and double, but on many systems 'long' is 32 bits. #include stdint.h int32_t this_is_32_bits; int64_t this_is_64_bits; (There may still be compilers out there that don't come with stdint.h by default, but if you need to support them then you'll be able to find appropriate autoconf goo to create it, since it's a standard interface.) -- Nathaniel ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion