什么原因导致“ Poly对象没有属性'expand'”错误?

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

我有一个4x4多项式的sympy矩阵:

S = Matrix([[Poly(alpha**4 + alpha**3, alpha, modulus=2), Poly(alpha**4 + alpha**3 + alpha + 1, alpha, modulus=2),Poly(alpha**3 + alpha**2 + alpha, alpha, modulus=2), Poly(alpha**4 + alpha**3 + alpha**2 + alpha, alpha, modulus=2)],
       [Poly(alpha**4 + alpha**3 + alpha + 1, alpha, modulus=2), Poly(alpha**3 + alpha**2 + alpha, alpha, modulus=2), Poly(alpha**4 + alpha**3 + alpha**2 + alpha, alpha, modulus=2),Poly(alpha**3 + alpha**2, alpha, modulus=2)],
       [Poly(alpha**3 + alpha**2 + alpha, alpha, modulus=2), Poly(alpha**4 + alpha**3 + alpha**2 + alpha, alpha, modulus=2), Poly(alpha**3 + alpha**2, alpha, modulus=2), Poly(alpha**4 + alpha + 1, alpha, modulus=2)],
       [Poly(alpha**4 + alpha**3 + alpha**2 + alpha, alpha, modulus=2), Poly(alpha**3 + alpha**2, alpha, modulus=2), Poly(alpha**4 + alpha + 1, alpha, modulus=2),Poly(alpha**2 + alpha + 1, alpha, modulus=2)]])

目标是获取S.det()并设置域:

S_det = (S.det()).set_domain(GF(self.q))

调试器输出:

文件“ / home / gamergod77 / work /楼梯代码/产品代码/bchcoder.py”,第52行,在解码中S_det =(S.det())。set_domain(GF(self.q))

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/matrices.py”,行123,在det中return _det(self,method = method,iszerofunc = iszerofunc)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py”,第596行,在_det中返回M._eval_det_bareiss(iszerofunc = iszerofunc)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/matrices.py”,第99行,在_eval_det_bareiss中return _det_bareiss(self,iszerofunc = iszerofunc)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py”,行669,在_det_bareiss中返回光亮(M)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py”,第637行,在裸机中ivot_pos,pivot_val,_,_ = _find_reasonable_pivot(mat [:, 0],iszerofunc = iszerofunc)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py”,第56行,在_find_reasonable_pivot中is_zero = iszerofunc(x)

文件“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/utilities.py”,第84行,位于_is_zero_after_expand_mul中返回expand_mul(x)== 0

File“ /home/gamergod77/.local/lib/python3.8/site-packages/sympy/core/function.py”,第2799行,在expand_mul中返回sympify(expr).expand(deep = deep,mul = True,power_exp = False,

[AttributeError:'Poly'对象没有属性'expand'

注意类似

S[:3, :3].det().set_domain(GF(2))

具有相同的S可以很好地通过

python matrix sympy attributeerror poly
1个回答
0
投票

您应该在矩阵中使用Expr,或者应该使用PolyMatrix。看来使用Expr最适合您的示例:

In [5]: S = S.applyfunc(lambda p: p.as_expr())                                                                                    

In [6]: S                                                                                                                         
Out[6]: 
⎡     4    3        4    3              3    2          4    3    2    ⎤
⎢    α  + α        α  + α  + α + 1     α  + α  + α     α  + α  + α  + α⎥
⎢                                                                      ⎥
⎢ 4    3              3    2          4    3    2           3    2     ⎥
⎢α  + α  + α + 1     α  + α  + α     α  + α  + α  + α      α  + α      ⎥
⎢                                                                      ⎥
⎢   3    2          4    3    2           3    2           4           ⎥
⎢  α  + α  + α     α  + α  + α  + α      α  + α           α  + α + 1   ⎥
⎢                                                                      ⎥
⎢ 4    3    2           3    2           4                 2           ⎥
⎣α  + α  + α  + α      α  + α           α  + α + 1        α  + α + 1   ⎦

In [7]: S.det()                                                                                                                   
Out[7]: 
 15      14      13      12      11      10      9      8    7      5      4    3      2          
α   + 3⋅α   + 4⋅α   + 4⋅α   - 2⋅α   - 3⋅α   + 6⋅α  + 9⋅α  + α  + 5⋅α  + 2⋅α  + α  + 5⋅α  + 4⋅α + 1

In [8]: d = S.det()                                                                                                               

In [9]: d                                                                                                                         
Out[9]: 
 15      14      13      12      11      10      9      8    7      5      4    3      2          
α   + 3⋅α   + 4⋅α   + 4⋅α   - 2⋅α   - 3⋅α   + 6⋅α  + 9⋅α  + α  + 5⋅α  + 2⋅α  + α  + 5⋅α  + 4⋅α + 1

In [10]: Poly(d, alpha).set_domain(GF(2))                                                                                         
Out[10]: Poly(alpha**15 + alpha**14 + alpha**10 + alpha**8 + alpha**7 + alpha**5 + alpha**3 + alpha**2 + 1, alpha, modulus=2)

推荐问答
热门问答
最新问答