【问题标题】:How to treat complex values when writing a numpy ufunc?编写numpy ufunc时如何处理复杂值?
【发布时间】:2019-09-08 12:26:43
【问题描述】:

基本上我有一个在npy_cdoublenpy_cfloat 数组上运行的numpy ufunc。例如:

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 &lt;anonymous&gt;’}。如何正确对待npy_cdouble

【问题讨论】:

    标签: c numpy complex-numbers numpy-ufunc


    【解决方案1】:

    结构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;
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2023-02-04
      • 2020-07-29
      • 2012-01-20
      相关资源
      最近更新 更多