Jupyter 笔记本语法错误:libc.math cimport sqrt 的语法无效

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

我在jupyter笔记本上尝试过这个

%load_ext Cython

我能够成功安装 cython,但无法使用 cimport

%%cython

from libc.math cimport sqrt
import numpy as np

def filt_conv3(double complex[:,:] filterInput, Py_ssize_t sample_per_symbol, double[:] h, int flag, Py_ssize_t num_of_taps):
    cdef Py_ssize_t input_batch_size = filterInput.shape[0]
    cdef Py_ssize_t input_length = filterInput.shape[1]

    cdef Py_ssize_t num_of_output = num_of_taps + (input_length-1) * sample_per_symbol  
    cdef double complex[:,:] output = np.zeros((input_batch_size, num_of_output), dtype=np.complex)  
    cdef double complex[:,:] FiltSig = np.zeros((input_batch_size, num_of_output), dtype=np.complex)  

    

但它返回给我以下错误:

Cell In[12], line 4
    from libc.math cimport sqrt
                   ^
SyntaxError: invalid syntax

这个语法错误的原因是什么? 我该怎么办?

我用 import 替换了 cimport

%%cython

from libc.math import sqrt
import numpy as np

我又遇到一个错误

def filt_conv3(double complex[:,:] filterInput, Py_ssize_t sample_per_symbol, double[:] h, int flag, Py_ssize_t num_of_taps):
                          ^
SyntaxError: invalid syntax`your text`

而且这段代码也有错误

import numpy as np
cimport cython
from libc.math cimport sqrt

cimport cython
        ^
SyntaxError: invalid syntax
python math jupyter-notebook cython cimport
1个回答
0
投票

嗯,我在 jupyter 中运行了你的原始代码,没有出现错误。但是如果我在

#test
行之前放置注释行
%%cython
,我会得到同样的错误。您可以尝试确保
%%cython
行位于代码块的最顶部。

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