我目前有以下功能:
def add_bch(compressed):
k = 16 #input blocks of 16 bits.
prim_poly = Poly([1, 0, 1, 0, 0, 1]) # x^5+x^2+1
prim_poly_3 = Poly([1, 0, 1, 1, 1, 1]) #prim_poly^3 -> x^5+x^4+x^3+x^2+1
prim_poly_5 = Poly([1, 1, 1, 0, 1, 1]) #prim_poly^5 -> x^5+x^4+x^2+x+1
gen_poly = npp.polymul(npp.polymul(prim_poly, prim_poly_3), prim_poly_5)[0]
compressed = add_padding(compressed, k)
compressed_with_ecc = []
for i in range(len(compressed)/k):
block = compressed[(k*i):(k*(i+1))]
block_poly = bitstr_to_poly(block)
quotient, remainder = npp.polydiv(block_poly, gen_poly)
compressed_with_ecc.append(poly_to_bitstr(block. remainder))
return Bits().join(compressed_with_ecc)
相关的进口声明是:
from bitstring import BitArray, Bits
import numpy.polynomial.polynomial as npp
from numpy.polynomial import Polynomial as Poly
当我运行代码时,我收到错误:
bash-4.2$ python send_mess < pg2600.txt
Traceback (most recent call last):
File "send_mess", line 113, in <module>
encoded = add_bch(compressed)
File "send_mess", line 105, in add_bch
quotient, remainder = npp.polydiv(block_poly, gen_poly)
File "/usr/lib64/python2.7/site-packages/numpy/polynomial/polynomial.py", line 408, in polydiv
return c1/c2[-1], c1[:1]*0
File "<string>", line 373, in __rtruediv__
UnboundLocalError: local variable 'quo' referenced before assignment
我知道numpy导入可能会导致名称空间问题,但请尽可能地解决问题所在。我遇到的一件事是两个numpy import语句可能会发生冲突,但在我看来它们应该是相互分离的,因为它应该是:
numpy
...
polynomial
...
Polynomial
polynomial
...
polymul
polydiv
...
...
...
但是我确实尝试过:
from bitstring import BitArray, Bits
import numpy.polynomial as npp
随着所有相关名称的更改,无济于事。我也尝试直接导入所有内容,所以:
from bitstring import BitArray, Bits
from numpy.polynomial.polynomial import polymul, polydiv
from numpy.polynomial import Polynomial as Poly
哪个也行不通。在功能(或我的程序一般)中没有任何一点,我是指一个变量&#34; quo&#34;,而且我不知道我的任何代码可以踩到numpy&#39; s脚趾。
答案 0 :(得分:4)
这是numpy中的一个错误。在https://github.com/numpy/numpy/blob/master/numpy/polynomial/_polybase.py#L421的__rtruediv__
实施中,您可以看到如果len(self.coef)
不是1,则声明
return self.__class__(quo, self.domain, self.window)
已执行,但未定义quo
。
这是一个生成错误的简单示例:
In [1]: import numpy.polynomial.polynomial as npp
In [2]: p1 = npp.Polynomial([1,2])
In [3]: p2 = npp.Polynomial([3,4])
In [4]: npp.polydiv(p1, p2)
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-4-00c25f56fb20> in <module>()
----> 1 npp.polydiv(p1, p2)
/Users/warren/anaconda/lib/python2.7/site-packages/numpy/polynomial/polynomial.pyc in polydiv(c1, c2)
406 len2 = len(c2)
407 if len2 == 1 :
--> 408 return c1/c2[-1], c1[:1]*0
409 elif len1 < len2 :
410 return c1[:1]*0, c1
/Users/warren/anaconda/lib/python2.7/site-packages/numpy/polynomial/polynomial.pyc in __rtruediv__(self, other)
UnboundLocalError: local variable 'quo' referenced before assignment
我在这里报告了这个问题:https://github.com/numpy/numpy/issues/4631。
答案 1 :(得分:0)
这里只是一个注释。您不应该调用polydiv
,而是使用Python divmod(block_poly, gen_poly)
运算符执行divmod
。给定代码的错误确实在__rtruediv__
,但这是一个意外。函数调用将除法视为具有多项式系数的两个0次多项式,并且因为polynomial.py
文件的顶部有from __future__ import division
,所以尝试进行真正的除法。获得错误的更直接方法是
operator.truediv(block_poly, gen_poly)
。