在numpy的c源代码中如何使用at符号(@)?

2024-03-28 23:31:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在阅读numpy的一些源代码,我注意到很多c源代码都使用@variablename@结构。例如,在文件“npy_math_complex.c.src”(位于here)中:

/*==========================================================
* Constants
*=========================================================*/
static const @ctype@ c_1@c@ = {1.0@C@, 0.0};
static const @ctype@ c_half@c@ = {0.5@C@, 0.0};
static const @ctype@ c_i@c@ = {0.0, 1.0@C@};
static const @ctype@ c_ihalf@c@ = {0.0, 0.5@C@};

@ctype@和{}是什么意思?这些是宏吗?我猜它们不是普通的C宏,因为我查看了文件中列出的相关头文件,它们似乎没有定义任何使用“@”的宏。在

在将c代码编译成python模块时,@name@distutils使用的某种宏吗?在

我以前从未在c代码中见过@符号,所以我有点困惑。。。在


Tags: 文件代码srcnumpyhere源代码staticmath
1条回答
网友
1楼 · 发布于 2024-03-28 23:31:13

因为这些文件是模板。如果我没记错的话,NumPy使用了几个模板引擎(感谢@user2357112帮我找到了合适的一个):

第二个程序实际上负责将这些文件转换为“常规”C文件—在这些文件被编译之前。在

基本上,这些函数将被克隆很多次,对于每个函数,在%之间插入一个特殊的占位符。在

例如in this case it begins with

/**begin repeat
 * #type = npy_float, npy_double, npy_longdouble#
 * #ctype = npy_cfloat,npy_cdouble,npy_clongdouble#
 * #c = f, , l#
 * #C = F, , L#
 * ....
 */

因此在第一次迭代中,@ctype@将被npy_cfloat替换,而{}将被{}和{}替换为{}:

^{pr2}$

在下一个迭代中是@ctype@npy_cdouble。。。在

static const npy_cdouble c_1 = {1.0, 0.0};
static const npy_cdouble c_half = {0.5, 0.0};
static const npy_cdouble c_i = {0.0, 1.0};
static const npy_cdouble c_ihalf = {0.0, 0.5};

在第三次迭代中:

static const npy_clongdouble c_1l = {1.0L, 0.0};
static const npy_clongdouble c_halfl = {0.5L, 0.0};
static const npy_clongdouble c_il = {0.0, 1.0L};
static const npy_clongdouble c_ihalfl = {0.0, 0.5L};

然后将这些文件编译为普通的C文件。在

相关问题 更多 >