Sparse 3D matrix
I need to create a Sparse 3D matrix in C++ of size (256,256,256) I tried doing this on Boost uBlas but I don't think it supports 3D matrices. How do I do this?
-Tim
[178 byte] By [
timchanzee] at [2007-11-11 8:35:25]

# 1 Re: Sparse 3D matrix
Certainly one way to do this would be a linked-list implementation, with the first two lists being lists of references to lists which, in the third dimension, contains the values which are populating its dimension.
nspils at 2007-11-11 21:01:13 >

# 3 Re: Sparse 3D matrix
You said this is sparse. You will have nodes in the list only for those points/coordinates which actually have values. You don't need to have nodes for points that don't have values. You don't have to reserve space of empty locations, the way you would for an array.
If there is no entry at all in column 2 of the second dimension, the lists for that column will all be non-existent or null.
Another thought would be a series of bitsets for each of the first two dimensions, and a list for the third dimension.
One more thought: you said you treid creating a 3D array and you ran out of space at a 256x256x256 array. Were you creating it on the heap or on the stack?
nspils at 2007-11-11 21:03:17 >

# 4 Re: Sparse 3D matrix
Hi,
or save your matrix in a map or a tree! Because your map is sparse you could map the values of the 3D matrix to strings that "describe the indices"
map<string,double> myMatrix;
myMatrix.insert(pair<string,double>("10,102,56",1234.0897));
myMatrix.insert(pair<string,double>("31,52,222",-4613.572));
If you want to do it very clever, then create a struct with 3 integers x,y,z and implement the comparison iterators ("<", "==", ... ) and instead of using a string-double map use your index struct in the map
Cheers,
D