Ask Your Question

Revision history [back]

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.binary()
sage: b
'1110'

For your purpose, it has to be read from right to left since the coefficient in a^0 is the last letter. You can reverse this as follows (and get a iterator of letters):

sage: list(reversed(b))
['0', '1', '1', '1']

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(reversed(b)))
[(0, '0'), (1, '1'), (2, '1'), (3, '1')]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(reversed(n.binary())) if j == '1')
a^3 + a^2 + a

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.binary()
sage: b
'1110'

For your purpose, it has to be read from right to left since the coefficient in a^0 is the last letter. You can reverse this as follows (and get a iterator of letters):

sage: list(reversed(b))
['0', '1', '1', '1']

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(reversed(b)))
[(0, '0'), (1, '1'), (2, '1'), (3, '1')]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(reversed(n.binary())) if j == '1')
a^3 + a^2 + a

Now, regarding polynomials, you can first construct the integer polynomial to represent them:

sage: D.<X> = PolynomialRing(ZZ)

Then, you can construct your example polynomial as follows:

sage: P = sum(i*X^i for i in range(1,11))
sage: P
10*X^10 + 9*X^9 + 8*X^8 + 7*X^7 + 6*X^6 + 5*X^5 + 4*X^4 + 3*X^3 + 2*X^2 + X

A nice way to access coefficients of a polynomial is by using a dictionary that maps powers to coefficients, so that at the end, you get:

sage: sum([sum(a^i for i,j in enumerate(reversed(ZZ(power).binary())) if j == '1') * x^power for power,coeff in P.dict().items()])
(a^3 + a)*x^10 + (a^3 + 1)*x^9 + a^3*x^8 + (a^2 + a + 1)*x^7 + (a^2 + a)*x^6 + (a^2 + 1)*x^5 + a^2*x^4 + (a + 1)*x^3 + a*x^2 + x

All that said, note that Reed-Solomon codes ar already implemented in Sage https://doc.sagemath.org/html/en/reference/coding/sage/coding/grs_code.html

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.binary()
sage: b
'1110'

For your purpose, it has to be read from right to left since the coefficient in a^0 is the last letter. You can reverse this as follows (and get a iterator of letters):

sage: list(reversed(b))
['0', '1', '1', '1']

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(reversed(b)))
[(0, '0'), (1, '1'), (2, '1'), (3, '1')]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(reversed(n.binary())) if j == '1')
a^3 + a^2 + a

Note that you can go the other way easily:

sage: c = a^3 + a^2 + a
sage: c.integer_representation()
14

Now, regarding polynomials, you can first construct the integer polynomial to represent them:

sage: D.<X> = PolynomialRing(ZZ)

Then, you can construct your example polynomial as follows:

sage: P = sum(i*X^i for i in range(1,11))
sage: P
10*X^10 + 9*X^9 + 8*X^8 + 7*X^7 + 6*X^6 + 5*X^5 + 4*X^4 + 3*X^3 + 2*X^2 + X

A nice way to access coefficients of a polynomial is by using a dictionary that maps powers to coefficients, so that at the end, you get:

sage: sum([sum(a^i for i,j in enumerate(reversed(ZZ(power).binary())) if j == '1') * x^power for power,coeff in P.dict().items()])
(a^3 + a)*x^10 + (a^3 + 1)*x^9 + a^3*x^8 + (a^2 + a + 1)*x^7 + (a^2 + a)*x^6 + (a^2 + 1)*x^5 + a^2*x^4 + (a + 1)*x^3 + a*x^2 + x

All that said, note that Reed-Solomon codes ar already implemented in Sage https://doc.sagemath.org/html/en/reference/coding/sage/coding/grs_code.html

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.binary()
sage: b
'1110'

For your purpose, it has to be read from right to left since the coefficient in a^0 is the last letter. You can reverse this as follows (and get a iterator of letters):

sage: list(reversed(b))
['0', '1', '1', '1']

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(reversed(b)))
[(0, '0'), (1, '1'), (2, '1'), (3, '1')]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(reversed(n.binary())) if j == '1')
a^3 + a^2 + a

Note that you can go the other way easily:

sage: c = a^3 + a^2 + a
sage: c.integer_representation()
14

Regarding the index, since F^* is multiplicatively cyclic with a as a generator, you can define and use the following dictionary:

sage: index = {a^i:i for i in range(15)}
sage: index[a^3 + a^2 + a]
11

Now, regarding polynomials, you can first construct the integer polynomial to represent them:

sage: D.<X> = PolynomialRing(ZZ)

Then, you can construct your example polynomial as follows:

sage: P = sum(i*X^i for i in range(1,11))
sage: P
10*X^10 + 9*X^9 + 8*X^8 + 7*X^7 + 6*X^6 + 5*X^5 + 4*X^4 + 3*X^3 + 2*X^2 + X

A nice way to access coefficients of a polynomial is by using a dictionary that maps powers to coefficients, so that at the end, you get:

sage: sum([sum(a^i for i,j in enumerate(reversed(ZZ(power).binary())) if j == '1') * x^power for power,coeff in P.dict().items()])
(a^3 + a)*x^10 + (a^3 + 1)*x^9 + a^3*x^8 + (a^2 + a + 1)*x^7 + (a^2 + a)*x^6 + (a^2 + 1)*x^5 + a^2*x^4 + (a + 1)*x^3 + a*x^2 + x

All that said, note that Reed-Solomon codes ar already implemented in Sage https://doc.sagemath.org/html/en/reference/coding/sage/coding/grs_code.html

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.binary()
n.bits()
sage: b
'1110'

For your purpose, it has to be read from right to left since the coefficient in a^0 is the last letter. You can reverse this as follows (and get a iterator of letters):

sage: list(reversed(b))
['0', '1', '1', '1']
[0, 1, 1, 1]

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(reversed(b)))
list(enumerate(b))
[(0, '0'), 0), (1, '1'), 1), (2, '1'), 1), (3, '1')]
1)]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(reversed(n.binary())) enumerate(n.bits()) if j == '1')
j)
a^3 + a^2 + a

Note that you can go the other way easily:

sage: c = a^3 + a^2 + a
sage: c.integer_representation()
14

Regarding the index, since F^* is multiplicatively cyclic with a as a generator, you can define and use the following dictionary:

sage: index = {a^i:i for i in range(15)}
sage: index[a^3 + a^2 + a]
11

Now, regarding polynomials, you can first construct the integer polynomial to represent them:

sage: D.<X> = PolynomialRing(ZZ)

Then, you can construct your example polynomial as follows:

sage: P = sum(i*X^i for i in range(1,11))
sage: P
10*X^10 + 9*X^9 + 8*X^8 + 7*X^7 + 6*X^6 + 5*X^5 + 4*X^4 + 3*X^3 + 2*X^2 + X

A nice way to access coefficients of a polynomial is by using a dictionary that maps powers to coefficients, so that at the end, you get:

sage: sum([sum(a^i for i,j in enumerate(reversed(ZZ(power).binary())) enumerate(ZZ(power).bits()) if j == '1') j) * x^power for power,coeff in P.dict().items()])
(a^3 + a)*x^10 + (a^3 + 1)*x^9 + a^3*x^8 + (a^2 + a + 1)*x^7 + (a^2 + a)*x^6 + (a^2 + 1)*x^5 + a^2*x^4 + (a + 1)*x^3 + a*x^2 + x

All that said, note that Reed-Solomon codes ar already implemented in Sage https://doc.sagemath.org/html/en/reference/coding/sage/coding/grs_code.html

First, you can transform a decimal number into a string representing its binary expansion as follows:

sage: n = 14
sage: b = n.bits()
sage: b
[0, 1, 1, 1]

Then you can get the indices of the letters by enumerating this iterator:

sage: list(enumerate(b))
[(0, 0), (1, 1), (2, 1), (3, 1)]

Putting everything together, you can construct your element of F:

sage: sum(a^i for i,j in enumerate(n.bits()) if j)
a^3 + a^2 + a

Note that you can go the other way easily:

sage: c = a^3 + a^2 + a
sage: c.integer_representation()
14

Regarding the index, since F^* is multiplicatively cyclic with a as a generator, you can define and use the following dictionary:

sage: index = {a^i:i for i in range(15)}
sage: index[a^3 + a^2 + a]
11

Now, regarding polynomials, you can first construct the integer polynomial to represent them:

sage: D.<X> = PolynomialRing(ZZ)

Then, you can construct your example polynomial as follows:

sage: P = sum(i*X^i sum((11-i)*X^i for i in range(1,11))
range(11))
sage: P
10*X^10 + 9*X^9 + 8*X^8 + 7*X^7 + 6*X^6 + 5*X^5 + 4*X^4 + 3*X^3 + 2*X^2 + X
X^10 + 2*X^9 + 3*X^8 + 4*X^7 + 5*X^6 + 6*X^5 + 7*X^4 + 8*X^3 + 9*X^2 + 10*X + 11

A nice way to access coefficients of a polynomial is by using a dictionary that maps powers to coefficients, so that at the end, you get:

sage: sum([sum(a^i for i,j in enumerate(ZZ(power).bits()) enumerate(ZZ(coeff).bits()) if j) * x^power for power,coeff in P.dict().items()])
x^10 + a*x^9 + (a + 1)*x^8 + a^2*x^7 + (a^2 + 1)*x^6 + (a^2 + a)*x^5 + (a^2 + a + 1)*x^4 + a^3*x^3 + (a^3 + a)*x^10 1)*x^2 + (a^3 + 1)*x^9 + a^3*x^8 + (a^2 + a + 1)*x^7 + (a^2 + a)*x^6 + (a^2 + 1)*x^5 + a^2*x^4 + (a + 1)*x^3 + a*x^2 + x
a)*x + a^3 + a + 1

If you want the index-ed form, as an integer polynomial:

sage: sum(index[sum(a^i for i,j in enumerate(ZZ(coeff).bits()) if j)] * X^power for power,coeff in P.dict().items())
X^9 + 4*X^8 + 2*X^7 + 8*X^6 + 5*X^5 + 10*X^4 + 3*X^3 + 14*X^2 + 9*X + 7

All that said, note that Reed-Solomon codes ar already implemented in Sage https://doc.sagemath.org/html/en/reference/coding/sage/coding/grs_code.html