Search code examples
matrixvectorparipari-gp

How can I save multi variable results in a data structure using Pari?


I have a function that loops over its input and produces zero or more results, with each result consisting of three numbers. I want to keep those results in a data structure (e.g., a matrix or a vector of vectors) but I don't know how many entries there will be until the loop terminates. I need to be able to extract a column of the results (e.g.the first variable of each entry) easily.


Solution

  • First off, please look at the PARI/GP reference for vectors/matrices stuff: https://pari.math.u-bordeaux.fr/dochtml/html-stable/Vectors__matrices__linear_algebra_and_sets.html.

    You can use the matrix in your loop as follows:

    entries = Mat();
    
    for(i = 1, 1000, {
        your_entry = [i, i+1, i+2];
        entries = matconcat([entries; Mat(your_entry)]);
    });
    
    print(matsize(entries))
    gp> [1000, 3]
    
    print(entries[,1])  \\ Fetch the 1st column
    

    Hope, it helps.