c - 编写 numpy ufunc 时如何处理复数值?

标签 c numpy complex-numbers numpy-ufunc

基本上我有一个在 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_cdoublenpy_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/

相关文章:

python - 如何修改python复数

java - 如何在 Complex 类中实现 square()、modulus Squared() 和 add() 方法?

c - 如何通过MPI加速这个问题

c - 日志记录队列

python - 如何从随机加权矩阵中选择元素

python - 使用 python 进行多元线性回归

c - C 和汇编中的 goto 语句是否会破坏引用局部性从而降低性能?

使用 C 中的链表创建稀疏矩阵

python - Oja 规则中使用 memmap 导致 RAM 崩溃

algorithm - 如何表达复数的四聚函数