Search code examples
functional-programmingalgebrasage

sage writing and reading from files; spesific example


As I am new in programming with SAGE, I wonder whether anyone can help me in this topic which I think is a matter of writing results to a file first, and then taking them from there.

In particular, I have a command like this;

n = 11
K = GF(4,'a')
R = PolynomialRing(GF(4,'a'),"x")
x = R.gen()
a = K.gen()

v = [1,0,0,0,1,1,1,0,0,0,1]
R(v)
f = x^n-R(v)
S = R.quotient(f, 'y')
y = S.gen()

In the later steps I am using this v as a list of coefficients of a polynomial. And I do some algebra on them. But I want this v to run over all possible 11-length vectors over the finite field K as I defined. And I want to get the results for each v separately.

How can I write a program that will do this for me?

Thanks in advance.


Solution

  • You could do this:

    n = 11
    K = GF(4,'a')
    for v in VectorSpace(K, n):
        do stuff with v
    

    On my computer, it took 14.1 seconds to construct the list of all vectors in VectorSpace(K, 10), 55 seconds for VectorSpace(K, 11). When you run the loop, it doesn't construct the list all at once, so there is no long pause at the beginning and it shouldn't fill up a lot of memory, it iterates through the elements of the vector space pretty quickly.

    If you really want to write to a file, you should investigate file input and output in Python, for example in the Python docs.