Using Sage math to solve an equation in Finite Field - sage

How can I use Sage to solve an equation in Finite Field?
The following gets error:
sage: L = (q * (q-xk) - nk)
sage: L.parent()
Finite Field in q of size 2^4096
sage: q.parent()
Finite Field in q of size 2^4096
sage: xk.parent()
Finite Field in q of size 2^4096
sage: nk.parent()
Finite Field in q of size 2^4096
sage: L.roots()
...
AttributeError: 'sage.rings.finite_rings.element_ntl_gf2e.FiniteField_ntl_gf2eElement' object has no attribute 'roots'

Define L as a polynomial, not a finite field element:
sage: x = polygen(parent(q))
sage: L = x * (x - xk) - nk
sage: parent(L)
Univariate Polynomial Ring in x over Finite Field in q of size 2^4096
sage: L.roots()
...

Related

How do I use method lagrange_polynomial over a PolynomialRing of Integers?

R.<x> = PolynomialRing(RR)
points = [(1,2), (2,2), (3,6)]
R.lagrange_polynomial(points)
2.00000000000000*x^2 - 6.00000000000000*x + 6.00000000000000
The above works fine but since all the coefficients are integers, I would prefer to do this over integers.
However when I try with
R.<x> = PolynomialRing(ZZ)
R.lagrange_polynomial(points)
I get the error
AttributeError: 'PolynomialRing_integral_domain_with_category' object has no attribute 'lagrange_polynomial'
I know I can use QQ instead of RR & get the coefficients printed as integers, but I am wondering why ZZ is not allowed?
The Lagrange polynomial for three points in ZZ^2 does not
always have coefficients in ZZ, so it makes sense to have
it as a method of QQ['x'] and not of ZZ['x'].
sage: R.<x> = PolynomialRing(QQ)
sage: points = [(0, 1), (1, 0), (2, 2)]
sage: R.lagrange_polynomial(points)
3/2*x^2 - 5/2*x + 1
Since for points = [(1, 2), (2, 2), (3, 6)] the Lagrange
polynomial's coefficients end up being integers, one can do:
sage: R.<x> = PolynomialRing(ZZ)
sage: points = [(1, 2), (2, 2), (3, 6)]
sage: q = R.change_ring(QQ).lagrange_polynomial(points)
sage: q
2*x^2 - 6*x + 6
sage: parent(q)
Univariate Polynomial Ring in x over Rational Field
sage: p = R(q)
sage: p
2*x^2 - 6*x + 6
sage: parent(p)
Univariate Polynomial Ring in x over Integer Ring

Sage for Galois Field

I have a problem in Galois Field in SageMath. I can't convert a binary to polynomial.
If I have a binary number, 1010101 how do I convert this number in polynomial 1010101 = x^6+x^4+x^2+1.
I do not know if there is an inbuilt way (I presume you have already looked also), but you can always do the following:
sage: P.<x> = PolynomialRing(ZZ)
sage: binString = "1010101"
sage: arrayOfTerms = [0]*len(binString)
sage: binString = binString[::-1] #Flip it so that the first digit corresponds to the constant term
sage: for i in xrange(len(binString)):
....: arrayOfTerms[i] = (x**i)*int(binString[i])
....:
sage: poly = sum(arrayOfTerms)
sage: poly
x^6 + x^4 + x^2 + 1

Binary Vector in GF(2) to integer

I am using the CAS SAGE. I have a vector v belonging GF(2). How I will be able to find the integer representation of this vector? Any example please?
aux = random_matrix(GF(2), n,2*n)
for i in range(2*n):
x = ZZ(list(aux[:,i]), base=2)
Assuming I understand you, you have a vector living in a space over GF(2):
sage: V = VectorSpace(GF(2), 5)
sage: V
Vector space of dimension 5 over Finite Field of size 2
sage: v = V.random_element()
sage: v
(0, 1, 0, 1, 1)
There are lots of ways to convert this to an Integer, and lots of equally valid representations. One natural one would be:
sage: i = ZZ(list(v), base=2)
sage: i
26
sage: parent(i)
Integer Ring
sage: i.digits(2)
[0, 1, 0, 1, 1]

Create Graph in Sage

I want define new graph in sage. Let V be vector space over finite field GF(q). The graph's vertices are i-dimensional subspace from V and n-i -dimensional subspace from V and two vertices are adjacent if and only if direct sum of two subspace is V.
I have trouble with define this graph in sage. Any suggestion?
This should get you started:
sage: p = 5
sage: K = GF(p^2, 'a')
sage: V = K^4
sage: len(list(V.subspaces(1)))
16276
sage: len(list(V.subspaces(3)))
16276
So this graph is going to be pretty large: 16276 * 2 = 32552 vertices. Let's do a smaller example. Then you could do something like
sage: p = 3
sage: K = GF(p)
sage: V = K^4
sage: vertices = list(V.subspaces(1)) + list(V.subspaces(3))
sage: for X in vertices:
....: L = []
....: for Y in vertices:
....: if X + Y == V:
....: L.append(Y)
....: d[X] = L
....:
sage: Graph(d)
Graph on 80 vertices

Sage: Finding linear combination of a specified basis giving a specified element?

Given a vector space V, a basis B (list of tuples each of size corresponding to the dimension of V and over the same field) and a vector v - what is the sage command to find the (unique) linear combination of the elements of B giving v?
IIUC, you probably want to do something like this:
sage: basis = [(2,3,4),(1,23/4,3), (9,8/17,11)]
sage: F = QQ
sage: F
Rational Field
sage:
sage: # build the vector space
sage: dim = len(basis[0])
sage: VS = (F**dim).span_of_basis(basis)
sage: VS
Vector space of degree 3 and dimension 3 over Rational Field
User basis matrix:
[ 2 3 4]
[ 1 23/4 3]
[ 9 8/17 11]
and then, having constructed the vector space over the field:
sage: # choose some random vector
sage: v = vector(F, (19/2, 5/13, -4))
sage: v
(19/2, 5/13, -4)
sage:
sage: # get its coordinates
sage: c = VS.coordinate_vector(v)
sage: c
(-470721/19708, 59705/4927, 24718/4927)
sage: parent(c)
Vector space of dimension 3 over Rational Field
sage:
sage: # aside: there's also .coordinates(), but that returns a list instead
sage:
sage: # sanity check:
sage: VS.basis_matrix().transpose() * c
(19/2, 5/13, -4)
sage: VS.basis_matrix().transpose() * c == v
True

Resources