On 9/3/11 2:33 AM, dkrumm wrote:
Is there a data structure in Python that would allow me to do the
following:
I have a list of positive integer pairs, say [[1,2], [1,3], [2,5]].
For each pair [i,j] in my list I need to store in memory a number
c[i,j] which is a floating point number with some large precision. I'd
like to create some sort of object M indexed by the elements of my
list so that I can refer to M[i,j] and obtain the number c[i,j]. One
way (in the example above) would be to create a 5x5 matrix M and only
use its entries with indices [1,2], [1,3], and [2,5] to store my
numbers. That way I can refer to M[2,5], M[1,2], M[1,3] and get my
numbers back. However, this seems like a very inefficient solution (my
list is going to be very long, so this matrix M would be huge, and
only a few of its entries used).
Another way to do it is to use a sparse matrix, which does not store the
nonzero entries. Just create the matrix and pass the parameter sparse=True.
Or, as Simon suggested, use a dictionary. Storage-wise, there's not a
huge amount of difference. In fact, IIRC, sparse matrices are
implemented using dictionaries in Sage.
Thanks,
Jason
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org