0

I have a 4x4 sympy matrix of polynomials:

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)]])

The goal is to get S.det() and set the domain:

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

Debugger output:

 File "/home/gamergod77/work/Staircase codes/Product Codes/bchcoder.py", line 52, in decode
    S_det = (S.det()).set_domain(GF(self.q))

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/matrices.py", line 123, in det
    return _det(self, method=method, iszerofunc=iszerofunc)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py", line 596, in _det
    return M._eval_det_bareiss(iszerofunc=iszerofunc)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/matrices.py", line 99, in _eval_det_bareiss
    return _det_bareiss(self, iszerofunc=iszerofunc)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py", line 669, in _det_bareiss
    return bareiss(M)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py", line 637, in bareiss
    pivot_pos, pivot_val, _, _ = _find_reasonable_pivot(mat[:, 0], iszerofunc=iszerofunc)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/determinant.py", line 56, in _find_reasonable_pivot
    is_zero = iszerofunc(x)

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/matrices/utilities.py", line 84, in _is_zero_after_expand_mul
    return expand_mul(x) == 0

  File "/home/gamergod77/.local/lib/python3.8/site-packages/sympy/core/function.py", line 2799, in expand_mul
    return sympify(expr).expand(deep=deep, mul=True, power_exp=False,

AttributeError: 'Poly' object has no attribute 'expand'

Noteble that something like

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

with the same S passes perfectly fine

Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
  • You should use the `PolyMatrix` class: https://github.com/sympy/sympy/issues/19429 – Oscar Benjamin Jun 01 '20 at 15:25
  • `@oscarbenjamin` Can you clarrify how do i get a det from PolyMatrix? `S.det().set_domain(GF(2))` where S is the same as before only PolyMatrix ends up in the same error – Ivan Yablochkin Jun 01 '20 at 15:51

1 Answers1

0

You should either use Expr in the matrix or you should use PolyMatrix instead. It looks like using Expr will be best for your example:

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)

Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14