Ask Your Question
1

Polynomial from coefficient vector mod 2

asked 2022-07-24 08:58:40 +0200

mathwizard2000 gravatar image

updated 2022-07-24 09:15:55 +0200

slelievre gravatar image

I want to extract the polynomial over GF(2), whose coefficients correspond to a certain vector over GF(2). However, I get an error:

TypeError: unable to convert (0, 0, 1, 1, 1) to a rational

when doing so. Here's the relevant section of code:

R.<x> = PolynomialRing(GF(2))
print(A)
v = vector(A.basis()[i]);
print(v.category())
print(v)                     
print(R(v))

I get the following:

Vector space of degree 5 and dimension 2 over Finite Field of size 2
Basis matrix:
[1 0 0 0 0]
[0 0 1 1 1]

 raise TypeError("error coercing to finite field")

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/rings/finite_rings/integer_mod.pyx in sage.rings.finite_rings.integer_mod.IntegerMod (build/cythonized/sage/rings/finite_rings/integer_mod.c:4878)()
    200             return a
    201     t = modulus.element_class()
--> 202     return t(parent, value)
    203 
    204 

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/rings/finite_rings/integer_mod.pyx in sage.rings.finite_rings.integer_mod.IntegerMod_abstract.__init__ (build/cythonized/sage/rings/finite_rings/integer_mod.c:6408)()
    387                     value = py_scalar_to_element(value)
    388                 if isinstance(value, Element) and value.parent().is_exact():
--> 389                     value = sage.rings.rational_field.QQ(value)
    390                     z = value % self.__modulus.sageInteger
    391                 else:

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9450)()
    895         if mor is not None:
    896             if no_extra_args:
--> 897                 return mor._call_(x)
    898             else:
    899                 return mor._call_with_args(x, args, kwds)

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4734)()
    159                 print(type(C), C)
    160                 print(type(C._element_constructor), C._element_constructor)
--> 161             raise
    162 
    163     cpdef Element _call_with_args(self, x, args=(), kwds={}):

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4626)()
    154         cdef Parent C = self._codomain
    155         try:
--> 156             return C._element_constructor(x)
    157         except Exception:
    158             if print_warnings:

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/rings/rational.pyx in sage.rings.rational.Rational.__init__ (build/cythonized/sage/rings/rational.cpp:6531)()
    536         """
    537         if x is not None:
--> 538             self.__set_value(x, base)
    539 
    540     def __reduce__(self):

/home/sc_serv/sage/local/var/lib/sage/venv-python3.10.3/lib/python3.10/site-packages/sage/rings/rational.pyx in sage.rings.rational.Rational.__set_value (build/cythonized/sage/rings/rational.cpp:8675)()
    689 
    690         else:
--> 691             raise TypeError("unable to convert {!r} to a rational".format(x))
    692 
    693     cdef void set_from_mpq(Rational self, mpq_t value):

TypeError: unable to convert (0, 0, 1, 1, 1) to a rational
edit retag flag offensive close merge delete

Comments

1

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 2022-07-24 09:20:32 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-24 09:20:06 +0200

slelievre gravatar image

Convert the vector to a tuple.

Here is an example.

Define the polynomial ring:

sage: R.<x> = PolynomialRing(GF(2))

Using a tuple works:

sage: u = (0, 0, 1, 1, 1)
sage: R(u)
x^4 + x^3 + x^2

Using a vector does not work:

sage: v = vector((0, 0, 1, 1, 1))
sage: R(v)
Traceback (most recent call last):
...
TypeError: unable to convert (0, 0, 1, 1, 1) to a rational

Turning the vector into a tuple does the trick:

sage: R(tuple(v))
x^4 + x^3 + x^2
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2022-07-24 08:58:40 +0200

Seen: 98 times

Last updated: Jul 24 '22