Ask Your Question

Revision history [back]

Matrix to polynomial

Hi,

I want to make a python function to use in sagemath that converts from polynomial to matrix using a normal basis generator in a GF. I am now running

F.<a> = GF(2^4)
a = F.gen()

R.<x> = PolynomialRing(F)
poly = x^3

b = a^3

M = polynomial_to_matrix(F, poly, b)
poly_rec = matrix_to_polynomial(F, M, basis)

Then expecting a matrix like [ 0, a^6, 1, a^3 ] [ a^6, 0, a^12, 1 ] [ 1, a^12, 0, a^9 ] [ a^3, 1, a^9, 0 ]

Rather I get [ 0 a^3 + a^2 + a a^2 + a + 1 a^3 + a^2 + 1] [a^3 + a^2 + a 0 a^3 + a + 1 a^2 + a] [ a^2 + a + 1 a^3 + a + 1 0 a^3 + 1] [a^3 + a^2 + 1 a^2 + a a^3 + 1 0

What concept do I not get or have misinterpreted? All help is greatly appreciated

The code

def polynomial_to_matrix(F, poly, basis_gen):
n = F.degree()

basis = [basis_gen**(2**i) for i in range(n)]
M = [[poly(bi) + poly(bj) + poly(bi + bj) + poly(0) for bj in basis] for bi in basis]

return Matrix(F, M)


def matrix_to_polynomial(F, M, basis):
n = F.degree()    
R = PolynomialRing(F, 'x')
x = R.gen()

M_alpha = Matrix(F, [[basis[i]**(2**j) for j in range(n)] for i in range(n)])
M_alpha_inv = M_alpha.inverse()
CF = M_alpha_inv.transpose() * M * M_alpha_inv

poly = 0
for i in range(n):
    for j in range(i):
        c = CF[i,j]
        if c != 0:
            poly += c * x**(2**i + 2**j)

return poly
click to hide/show revision 2
None

Matrix to polynomial

Hi,

I want to make a python function to use in sagemath that converts from polynomial to matrix using a normal basis generator in a GF. I am now running

F.<a> = GF(2^4)
a = F.gen()

R.<x> = PolynomialRing(F)
poly = x^3

b = a^3

M = polynomial_to_matrix(F, poly, b)
poly_rec = matrix_to_polynomial(F, M, basis)

Then expecting a matrix like like

[ 0, a^6, 1, a^3 ]
[ a^6, 0, a^12, 1 ]
[ 1, a^12, 0, a^9 ]
[ a^3, 1, a^9, 0 ]

]

Rather I get get

[            0 a^3 + a^2 + a   a^2 + a + 1 a^3 + a^2 + 1]
[a^3 + a^2 + a             0   a^3 + a + 1       a^2 + a]
[  a^2 + a + 1   a^3 + a + 1             0       a^3 + 1]
[a^3 + a^2 + 1       a^2 + a       a^3 + 1             0

0

What concept do I not get or have misinterpreted? All help is greatly appreciated

The code

def polynomial_to_matrix(F, poly, basis_gen):
 n = F.degree()

 basis = [basis_gen**(2**i) for i in range(n)]
 M = [[poly(bi) + poly(bj) + poly(bi + bj) + poly(0) for bj in basis] for bi in basis]

 return Matrix(F, M)


 def matrix_to_polynomial(F, M, basis):
 n = F.degree()    
 R = PolynomialRing(F, 'x')
 x = R.gen()

 M_alpha = Matrix(F, [[basis[i]**(2**j) for j in range(n)] for i in range(n)])
 M_alpha_inv = M_alpha.inverse()
 CF = M_alpha_inv.transpose() * M * M_alpha_inv

 poly = 0
 for i in range(n):
     for j in range(i):
         c = CF[i,j]
         if c != 0:
             poly += c * x**(2**i + 2**j)

 return poly
click to hide/show revision 3
None

Matrix to polynomial

Hi,

I want to make a python function to use in sagemath that converts from polynomial to matrix using a normal basis generator in a GF. I am now running

F.<a> = GF(2^4)
a = F.gen()

R.<x> = PolynomialRing(F)
poly = x^3

b = a^3

M = polynomial_to_matrix(F, poly, b)
poly_rec = matrix_to_polynomial(F, M, basis)

Then expecting a matrix like

[ 0, a^6, 1, a^3 ]
[ a^6, 0, a^12, 1 ]
[ 1, a^12, 0, a^9 ]
[ a^3, 1, a^9, 0 ]

Rather I get

[            0 a^3 + a^2 + a   a^2 + a + 1 a^3 + a^2 + 1]
[a^3 + a^2 + a             0   a^3 + a + 1       a^2 + a]
[  a^2 + a + 1   a^3 + a + 1             0       a^3 + 1]
[a^3 + a^2 + 1       a^2 + a       a^3 + 1             0

What concept do I not get or have misinterpreted? All help is greatly appreciated

The code

def polynomial_to_matrix(F, poly, basis_gen):
    n = F.degree()

    basis = [basis_gen**(2**i) for i in range(n)]
    M = [[poly(bi) + poly(bj) + poly(bi + bj) + poly(0) for bj in basis] for bi in basis]

    return Matrix(F, M)


 def matrix_to_polynomial(F, M, basis):
    n = F.degree()    
    R = PolynomialRing(F, 'x')
    x = R.gen()

    M_alpha = Matrix(F, [[basis[i]**(2**j) for j in range(n)] for i in range(n)])
    M_alpha_inv = M_alpha.inverse()
    CF = M_alpha_inv.transpose() * M * M_alpha_inv

    poly = 0
    for i in range(n):
        for j in range(i):
            c = CF[i,j]
            if c != 0:
                poly += c * x**(2**i + 2**j)

    return poly