Ask Your Question
3

list of random non-zero elements

asked 6 years ago

Parker gravatar image

updated 6 years ago

I was wondering if there was a short way to generate lists of random non-zero elements of GF(q) ? What I have so far is:

  L=[];  
  for i in range(10):  
     a=GF(25).random_element();  
     while a==0:  
         a=GF(25).random_element();  
     L.append(a);
Preview: (hide)

Comments

You don't actually need the semicolons at the end of the lines, by the way.

John Palmieri gravatar imageJohn Palmieri ( 6 years ago )

2 Answers

Sort by » oldest newest most voted
2

answered 6 years ago

tmonteil gravatar image

updated 6 years ago

If you really want something short, yo can do something along the lines:

sage: from itertools import ifilter, islice
sage: list(islice(ifilter(lambda x: x !=0 , (GF(25).random_element() for _ in ZZ)), 10))

Explanataion:

  • (GF(25).random_element() for _ in ZZ) creates an infinite iterator of random elements of GF(25)
  • ifilter(lambda x: x !=0 , (GF(25).random_element() for _ in ZZ)) creates an iterator that filters the nonzero elements
  • islice(ifilter(lambda x: x !=0 , (GF(25).random_element() for _ in ZZ)), 10) creates an iterator that produces only the first 10 elements of the previous iterator
  • list(islice(ifilter(lambda x: x !=0 , (GF(25).random_element() for _ in ZZ)), 10)) transforms the previous iterator into a list

As for the first line, itertools is a standard module with very nice tools for creating iterators.

That said, if you do:

sage: import this

You will see that Readability counts, so your code is probably the best since you will be able to understand and modify it later.

EDIIT, if you want to use it when Sage will use Python3, you just have to replace the imported ifilter with the builtin filter:

sage: from itertools import islice
sage: list(islice(filter(lambda x: x !=0 , (GF(25).random_element() for _ in ZZ)), 10))
Preview: (hide)
link

Comments

not compatible with python3...

FrédéricC gravatar imageFrédéricC ( 6 years ago )

@FrédéricC - thanks for watching! This got me started on an alternative solution, see my answer.

slelievre gravatar imageslelievre ( 6 years ago )

I updated to provide a solution that works when Sage will use Python 3.

tmonteil gravatar imagetmonteil ( 6 years ago )

Nice answer, however, I would not recommend the use of underscore as a counter in the loop. You will not be able to use it as the output of the last evaluated expression. Also, instead of ZZ it may be better to use iter(int,1); see https://stackoverflow.com/questions/5...

Amri gravatar imageAmri ( 3 years ago )
1

answered 6 years ago

slelievre gravatar image

updated 6 years ago

Random nonzero elements in a finite field

Idea

Let q=pn for some prime p and some integer n1, and let Fq be the field with q elements, and Fp the field with p elements.

Recall that Fq is a vector space over the prime field Fp, and that for any generator z of Fq as a field extension of Fp, the family (1,z,...,zn1) is a basis of Fq as a vector space over Fp.

One way to build a list of random nonzero elements in Fq is to pick coefficients a0, ..., an1 at random, all between 0 and p1, but not all zero, and to take the element akzk in Fq.

One way to pick such a collection of coefficients is to pick an integer at random between 1 and q1 and to let ak be its k-th digit in base p.

Implementation

Choose p and n, define q=pn, and let F be the finite field with q elements.

sage: p = 5
sage: n = 3
sage: q = p^n
sage: F = GF(q)

Choose m, the number of random elements to pick.

sage: m = 10

Produce a list of length m of random nonzero elements in F.

sage: L = [F(ZZ.random_element(1, q).digits(base=p)) for _ in range(m)]

Explanation

  • r = ZZ.random_element(1, q) picks a random integer between 1 and q1
  • d = r.digits(base=p) gives the list of its digits in base p
  • u = F(d) turns this list into a field element, seen a polynomial in z (where z is the generator of Fq as a field extension of Fp) with coefficients given by the list.

Note on speed

Defining the finite field takes on the order of 0.1 ms.

It is probably worth defining F once and for all, and then picking random elements in F, rather than calling GF(25) inside the loop, which spends time initializing the finite field at each iteration of the loop.

Of course if you're looping only ten times it doesn't matter much.

Preview: (hide)
link

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: 6 years ago

Seen: 2,188 times

Last updated: Sep 14 '18