基本上我有一个在 npy_cdouble
上运行的 numpy ufunc和npy_cfloat
数组。例如:
static void
ufunc_H( char ** args
, npy_intp * dimensions
, npy_intp * steps
, void * data)
{
npy_cdouble * qm_in = (npy_cdouble *) (args[0]);
npy_cdouble * qm_out = (npy_cdouble *) (args[1]);
npy_intp i;
for(i = 0; i < ndim; i++)
{
qm_out[i] = (qm_in[i] - qm_in[i ^ ipow(2, argument.act)]) * M_SQRT1_2;
}
}
但这不起作用,编译器说 qm_in
类型为 ‘npy_cdouble’ {aka ‘struct <anonymous>’}
。我该如何治疗npy_cdouble
正确吗?
最佳答案
结构体npy_cdouble
和npy_cfloat
有成员.real
和.imag
。这些可用于执行操作:
static void
ufunc_H( char ** args
, npy_intp * dimensions
, npy_intp * steps
, void * data)
{
npy_cdouble * qm_in = (npy_cdouble *) (args[0]);
npy_cdouble * qm_out = (npy_cdouble *) (args[1]);
npy_intp i;
for(i = 0; i < ndim; i++)
{
qm_out[i].real = (qm_in[i].real - qm_in[i ^ ipow(2, argument.act)].real) * M_SQRT1_2;
qm_out[i].imag = (qm_in[i].imag - qm_in[i ^ ipow(2, argument.act)].imag) * M_SQRT1_2;
}
}
关于c - 编写 numpy ufunc 时如何处理复数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841958/