### Matrix constructor runs out of memory

I am trying to construct a matrix with entries in $\mathbb{Q}(q)$, from a list of Sage vectors. I do this using

```
A = matrix(QQ['q'].fraction_field(), relations)
```

where the vectors in the list `relations`

are Sage vectors over `QQ['q'].fraction_field()`

.

The list `relations`

is generated by a subroutine involving some randomness, and in many cases the above works fine but sometimes my script uses too much memory and is killed by the operating system. Tracing the memory allocation it is clear that the problem occurs at the given line where I construct the matrix from the list of vectors.

The sorts of matrices for which the script fails are tend to have about 100 columns and 50-100 rows. They are sparse (each row has <= 4 nonzero entries) and the nonzero entries are of the form `-2/q^x + 2`

where `x`

is in the range `10000 - 100000`

.

It seems clear that when some combination of the order of magnitude of the exponents `x`

and the matrix size is reached, then the matrix constructor uses too much memory. I wondered if there is a solution anyone can suggest, or even just explain a bit more about why this constructor seems to max out on memory?

(P.S. I plan to re-implement things using sparse matrices, and maybe this will help, though I'd still like to understand better why the dense version fails.)