NumPy C-API - Data Type API

NumPy C-API - Data Type API

https://docs.scipy.org/doc/numpy/reference/c-api.dtype.html

The standard array can have 24 different data types (and has some support for adding your own types). These data types all have an enumerated type, an enumerated type-character, and a corresponding array scalar Python type object (placed in a hierarchy). There are also standard C typedefs to make it easier to manipulate elements of the given data type. For the numeric types, there are also bit-width equivalent C typedefs and named typenumbers that make it easier to select the precision desired.
标准数组可以有24种不同的数据类型 (并且有一些支持添加自己的类型)。这些数据类型都具有枚举类型,枚举类型字符和相应的数组标量 Python 类型对象 (放置在层次结构中)。还有标准的 C typedef,可以更容易地操作给定数据类型的元素。对于数字类型,还有位宽等效的 C typedef 和命名的 typenumbers,可以更容易地选择所需的精度。

Warning
The names for the types in c code follows c naming conventions more closely. The Python names for these types follow Python conventions. Thus, NPY_FLOAT picks up a 32-bit float in C, but numpy.float_ in Python corresponds to a 64-bit double. The bit-width names can be used in both Python and C for clarity.
c 代码中类型的名称更紧密地遵循 c 命名约定。这些类型的 Python 名称遵循 Python 约定。因此,NPY_FLOAT 在 C 中获取 32 位浮点数,但 Python 中的numpy.float_ 对应于 64 位双精度数。为清晰起见,可以在 Python 和 C 中使用位宽名称。

Enumerated Types

NPY_TYPES
There is a list of enumerated types defined providing the basic 24 data types plus some useful generic names. Whenever the code requires a type number, one of these enumerated types is requested. The types are all called NPY_{NAME}:
列表中列出了枚举类型,提供了基本的 24 种数据类型以及一些有用的通用名称。只要代码需要类型编号,就会请求其中一个枚举类型。这些类型都称为 NPY_ {NAME}

NPY_BOOL
The enumeration value for the boolean type, stored as one byte. It may only be set to the values 0 and 1.

enumeration [ɪ,njuːmə'reɪʃən]:n. 列举,计算,细目
Boolean ['bʊlɪən]:adj. 布尔数学体系的

NPY_BYTE
NPY_INT8
The enumeration value for an 8-bit/1-byte signed integer.

NPY_SHORT
NPY_INT16
The enumeration value for a 16-bit/2-byte signed integer.

NPY_INT
NPY_INT32
The enumeration value for a 32-bit/4-byte signed integer.

NPY_LONG
Equivalent to either NPY_INT or NPY_LONGLONG, depending on the platform.

NPY_LONGLONG
NPY_INT64
The enumeration value for a 64-bit/8-byte signed integer.

NPY_UBYTE
NPY_UINT8
The enumeration value for an 8-bit/1-byte unsigned integer.

NPY_USHORT
NPY_UINT16
The enumeration value for a 16-bit/2-byte unsigned integer.

NPY_UINT
NPY_UINT32
The enumeration value for a 32-bit/4-byte unsigned integer.

NPY_ULONG
Equivalent to either NPY_UINT or NPY_ULONGLONG, depending on the platform.

NPY_ULONGLONG
NPY_UINT64
The enumeration value for a 64-bit/8-byte unsigned integer.

NPY_HALF
NPY_FLOAT16
The enumeration value for a 16-bit/2-byte IEEE 754-2008 compatible floating point type.

NPY_FLOAT
NPY_FLOAT32
The enumeration value for a 32-bit/4-byte IEEE 754 compatible floating point type.

NPY_DOUBLE
NPY_FLOAT64
The enumeration value for a 64-bit/8-byte IEEE 754 compatible floating point type.

NPY_LONGDOUBLE
The enumeration value for a platform-specific floating point type which is at least as large as NPY_DOUBLE, but larger on many platforms.

References

NumPy C-API
https://docs.scipy.org/doc/numpy/reference/c-api.html

Python Types and C-Structures
https://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html

System configuration
https://docs.scipy.org/doc/numpy/reference/c-api.config.html

Data Type API
https://docs.scipy.org/doc/numpy/reference/c-api.dtype.html

Array API
https://docs.scipy.org/doc/numpy/reference/c-api.array.html

Array Iterator API
https://docs.scipy.org/doc/numpy/reference/c-api.iterator.html

UFunc API
https://docs.scipy.org/doc/numpy/reference/c-api.ufunc.html

Generalized Universal Function API
https://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html

NumPy core libraries
https://docs.scipy.org/doc/numpy/reference/c-api.coremath.html

C API Deprecations
https://docs.scipy.org/doc/numpy/reference/c-api.deprecations.html

Interfacing With Other Languages
https://scipy-cookbook.readthedocs.io/items/idx_interfacing_with_other_languages.html

C Extensions for Using NumPy Arrays
https://scipy-cookbook.readthedocs.io/items/C_Extensions_NumPy_arrays.html

C extensions
https://scipy-cookbook.readthedocs.io/items/C_Extensions.html

Ctypes
https://scipy-cookbook.readthedocs.io/items/Ctypes.html

F2py
https://scipy-cookbook.readthedocs.io/items/F2Py.html

Inline Weave With Basic Array Conversion (no Blitz)
https://scipy-cookbook.readthedocs.io/items/Weave.html

SWIG Numpy examples
https://scipy-cookbook.readthedocs.io/items/SWIG_NumPy_examples.html

SWIG and Numpy
https://scipy-cookbook.readthedocs.io/items/SWIG_and_NumPy.html

SWIG memory deallocation
https://scipy-cookbook.readthedocs.io/items/SWIG_Memory_Deallocation.html

f2py and numpy
https://scipy-cookbook.readthedocs.io/items/f2py_and_NumPy.html

Scipy Lecture Notes
https://scipy-lectures.org/index.html

2.8. Interfacing with C
https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

使用NumPy C API读取一个二维的.npy文件,你需要遵循以下步骤: 1. 包含必要的头文件:确保你的C程序中包含了numpy的C API头文件,通常是`numpy/core/src/common/npy_3kcompat.h`。 2. 打开.npy文件:使用标准C库中的`fopen`函数以二进制读取模式打开.npy文件。 3. 读取.npy文件头信息:使用NumPy C API提供的函数读取并解析文件头,这通常涉及到检查魔数(magic number)和版本信息以确保文件格式的兼容性。 4. 读取数据:根据头信息中包含的数据类型和维度信息,使用适当的C API函数读取文件中的数据数组。 5. 关闭文件:完成数据读取后,使用`fclose`函数关闭文件。 下面是一个简单的示例代码片段,展示了如何使用NumPy C API读取二维.npy文件: ```c #include <stdio.h> #include <numpy/npy_common.h> int main() { FILE *fp; npy_intp dims[2]; int type_num; char *data; // 打开.npy文件 fp = fopen("your_array.npy", "rb"); if(!fp) { printf("无法打开文件\n"); return 1; } // 读取头信息并检查 // 这里应该有读取头信息的代码,并填充dims和type_num变量 // 根据数据类型和维度分配内存 // 这里应该有内存分配的代码 // 读取数据 // 这里应该有读取数据的代码 // 关闭文件 fclose(fp); // 使用完毕后,释放内存 // free(data); return 0; } ``` 注意:上面的代码仅为示例,未包含全部必要的细节和错误处理。在实际应用中,你需要完整地实现头信息解析,并根据解析出的数据类型和维度分配内存,然后读取并转换数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值