熔断类型:未声明名称未内置:双倍

问题描述 投票:0回答:1

我想在我的cython代码中使用Fused类型,但是双类型不能编译,如果我从fused类型中删除双类型,编译成功。如果我从熔断类型中删除双类型,编译成功。为什么?

python 3.6.4

cython 0.27.3

import numpy as np
cimport numpy as np
cimport cython
import warnings
warnings.simplefilter("ignore")

ctypedef np.float64_t DTYPE1_t
ctypedef np.int64_t DTYPE2_t

ctypedef fused number_or_arr:
    int
    np.ndarray
    double

def SUM(np.ndarray[DTYPE1_t, ndim=1] npdata,number_or_arr n):
    if n is double:
        print('it is double')
    elif n is int:
        print('it is int')
    elif n is np.ndarray:
        print('it is arr')
    return np.sum(npdata)


[z@localhost strafunc]$ python setuptest.py build_ext --inplace
running build_ext
cythoning ctest.pyx to ctest.c

Error compiling Cython file:
------------------------------------------------------------
...
    int
    np.ndarray
    double

def SUM(np.ndarray[DTYPE1_t, ndim=1] npdata,number_or_arr n):
    if n is double:
           ^
------------------------------------------------------------

ctest.pyx:16:12: undeclared name not builtin: double
building 'ctest' extension
gcc -pthread -B /home/z/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/z/anaconda3/lib/python3.6/site-packages/numpy/core/include -I/home/z/anaconda3/include/python3.6m -c ctest.c -o build/temp.linux-x86_64-3.6/ctest.o
ctest.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
 #error Do not use this file, it is the result of a failed Cython compilation.
  ^
error: command 'gcc' failed with exit status 1
cython
1个回答
0
投票

熔断类型的文档 给这些检查为。

if name_of_fused_type is double

if name_of_variable is double:

在你的情况下,应该是。

if number_or_arr is double:

(然后对其他两个检查进行类似的修改)。你目前的版本的问题是,它是模糊的-----------------------------------------对于 int 的情况下,它可能会要求检查它是否是一个 int 或者它可能是要求检查, n 是一个Python类型的对象,完全等于Python的 int 类型。


作为一个旁观者,我认为像这样加快 "可能是一个数字,可能是一个数组 "的速度是很困难的--你可能最好不对它们进行类型化。Cython和数组在加快索引速度方面的效果最好,但在这里并不适用(因为它可能是一个数字)。

© www.soinside.com 2019 - 2024. All rights reserved.