I've found a quite good sparse matrix implementation for c# over http://www.blackbeltcoder.com/Articles/algorithms/creating-a-sparse-matrix-in-net.

But as i work in 3d coordinate-system, i need a sparse-matrix implementation that i can use to map the 3d-coordinate system.

Details: I'm storing large amounts of primitive shapes data in memory like cubes. I do have large amounts of them (around 30 million) and i've lots of null (zero) entries around. Given that my each entry costs 1-bytes of entry, i'd like to implement a sparse-matrix so that i can fairly save memory space.

Note: Fast access to matrix cells is a fairly important factor for me, so i'd be trading speed over memory consumption.

A very simple solution which I just made is this:

```
public class Sparse3DMatrix<T>
{
Dictionary<Tuple<int,int,int>, T> values = new Dictionary<Tuple<int, int, int>, T>();
public T this[int x, int y, int z]
{
get { return values[new Tuple<int, int, int>(x, y, z)]; }
set { values[new Tuple<int, int, int>(x, y, z)] = value; }
}
public bool ContainsKey(int x, int y, int z)
{
return values.ContainsKey(new Tuple<int, int, int>(x, y, z));
}
}
```

usage:

```
var test = new Sparse3DMatrix<float>();
test[1, 1, 1] = 1f;
Console.WriteLine(test[1, 1, 1]);
```

It could be extended with methods like those his version have, and with checks for `x, y, z`

values etc.

I'm sure someone have something to say about its performance. It will be a decent implementation unless you really need something it for high-performance. It depends on the hash-code implementation of `Tuple`

and your specific usage. If we assume the hashes are good, we will have `O(1)`

lookup time. If you know you will have a lot of elements, you could use `new Dictionary<...>(initial capacity)`

to avoid unnecessary resizing when added items.

*Unlike his*, this only have a single `Dictionary`

with all the items. His version have dictionaries of dictionaries. The benefit of his, is if you have to scan over an entire row, you can just iterate the second-level dictionary (this will not help you is you want to scan over columns) which is faster than individual lookup of the items. But having a single dictionary means smaller memory usage - especially when you have few items per row.

The fact that you're working in a 3D coordinate system doesn't change whether or not you can use this data structure. A matrix for a 3D space can be contained using a sparse matrix the same as a 2D matrix; it's just the entries that change.

You'd use a sparse matrix for large matricies with lots of zero entries. This is typical in discrete representations of problems in physics that come from finite difference and finite element methods. They have bands of non-zero entries clustered around the diagonal; entries outside the diagonal band are usually zero. A sparse matrix won't store these; decompositions like LU and QR have to be written to know how to deal with the sparsity.

These matricies can describe problems in either 2D or 3D spaces.

I believe you're incorrect if you think you need another data structure.

Lasse Espeholt's solution is practical but it can be improved by removing elements when they are "zeroed" or nulled. If you don't do this matrix or array can lose sparsity. Here is an alternative solution that assumes if an element of some type has not been inserted that it is the default of that type. For example, for `double`

that means 0.0 and for `string`

that means `null`

.

```
public class Sparse3DArray<T>
{
private Dictionary<Tuple<int, int, int>, T> data = new Dictionary<Tuple<int, int, int>, T>();
public int Nnz { get { return data.Count; } }
public T this[int x, int y, int z]
{
get
{
var key = new Tuple<int, int, int>(x, y, z);
T value;
data.TryGetValue(key, out value);
return value;
}
set
{
var key = new Tuple<int, int, int>(x, y, z);
if (null == value)
data.Remove(key);
else if (value.Equals(default(T)))
data.Remove(key);
else
data[key] = value;
}
}
}
```

Similar Questions

I've been wondering about this question for quite a while but cannot find a reference: How does Matlab transpose a sparse matrix so fast, given that it is stored in CSC (compressed sparse column) form

Are there any storage optimized Sparse Matrix implementations in C#?

I see 2 implementations of sparse matrix in this package. OpenMapRealMatrix SparseFieldMatrix Both are documented as Sparse matrix implementation based on an open addressed map. Do you know what a

I'm trying to use hcluster library in python. I have no enough python knowledges to use sparse matrix in hcluster. Please help me anybody. So, that what I'm doing: import os.path import numpy import

The question is: Is it possible to create a sparse matrix using the following sparse list implementation? In special, using a class template with a class template (SparseList*>)? I've created a cla

I'm currently working with sparse matrices, and I have to compare the computation time of sparse matrix-matrix multiplication with full matrix-matrix multiplication. The issue is that sparse matrix co

Hello I know there are a lot of questions on sparse matrix multiplication, but many of the answers say to just use libraries. I want to do it without using library functions. So far I've done the easy

I just started to learn to program in Python and I am trying to construct a sparse matrix using Scipy package. I found that there are different types of sparse matrices, but all of them require to sto

Question one: Are there specialized databases to store dense and sparse matrices ? I googled but didn't find any... The matrix in question is huge (10^5 by 10^5) but it's sparse, which means that most

I need to create spare matrices with variable elements. Unfortunately, sparse matrix index operations are very slow. Is there any way to speed up the process? Maybe there are some tricks that I don't

I have a csv file with headers like: Given this test.csv file contains sparse matrix: A,B,C,D,E,F,timestamp 611.88243,0,0,0,0,0,0 0,9089.5601,0,864.07514,0,0,0 0,0,5133.0,0,0,0,0 I simp

I am working on a KNN algorithm for a university assignment and at the moment I'm working on finding the Euclidean distance between each of the training vectors stored as a Scipy lil_matrix (due to th

I've created a TermDocumentMatrix from the tm library in R. It looks something like this: > inspect(freq.terms) A document-term matrix (19 documents, 214 terms) Non-/sparse entries: 256/3810 Sparsi

I have a 3D matrix (MxNxK) and want to resize it to (M'xN'xK') (like imresize in matlab). I am using image pyramid, but its result is not very accurate and need a better one. Any solution?

I have a sparse matrix from the sklearn bag-of-words vectorizer. It's a csr_matrix and its elements represent word frequency in a document. But now what I need is the 0/1 matrix where 1 represents the

I am trying to calculate 3D shapes out of a 3 dimensional matrix of samples. My idea is that I would have a 3 dimensional matrix of data points, with each corresponding location in (X, Y, Z) space, an

I would like to multiply each element(say [i,j]) of a MxN 2D matrix(say A) to to all elements in the 3D row of a 3D matrix(say B), so B[i,j,:]. The following doesn't help because it gives me a (2,3,3)

I initialize an empty sparse matrix using S = scipy.sparse.lil_matrix((n,n),dtype=int) As expected print S doesn't show anything, since nothing has been assigned. Yet if I test: print S[0,0]==0 I r

I am using the cusp library with CUDA to use sparse matrix. Can't I use it in a struct in C like: #include <cusp/coo_matrix.h> #include <cusp/multiply.h> #include <cusp/print.h> #in

I am working on a scilab program to average a 3d matrix in cubes.This is mostly done, but I want to set the size to a given sum,(the voxels all add up to a set amount in each cube)and I keep breaking

I have two large sparse matrices: In [3]: trainX Out[3]: <6034195x755258 sparse matrix of type '<type 'numpy.float64'>' with 286674296 stored elements in Compressed Sparse Row format> In [

How do you save/load a scipy sparse csr_matrix in a portable format? The scipy sparse matrix is created on Python 3 (Windows 64-bit) to run on Python 2 (Linux 64-bit). Initially, I used pickle (with p

How can you take the log base 10 of every element in a sparse matrix (COO)? >>print type(X) <class 'scipy.sparse.coo.coo_matrix'> I've tried this but it doesn't work: import math X.data =

Suppose I have a matrix of elements like so: A = reshape(1:25, 5, 5) A = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 I would like to efficiently compute a 3D matrix of outer pro

I'm trying to use large 10^5x10^5 sparse matrices but seem to be running up against scipy: n = 10 ** 5 x = scipy.sparse.rand(n, n, .001) gets ValueError: Trying to generate a random sparse matrix su

To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right: A = numpy.sparse.csr_sparse(something) B = numpy.matrix(something) C = A.dot(B) # C = A*

I have a 3d mxnxt matrix , I want to be able to extract t 2d nxm matrices. In my case I have a 1024x1024x10 matrix and I want to have 10 images showing it to me. This is not reshaping, I want just par

If I have a 3D matrix, X that is 4 x 10 x 50. The matrix consists of positions and velocities in the first dimension, different particles (or boats or whatever) indexes in the second and lastly the d

I can define a sparse Matrix using a vector for i, j, and x: i <- c(1,3:8) j <- c(2,9,6:10) x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) I want to extract the i, j, and x elements from

I am building a simple model of the Milky Way and one of the things I need to store is a 3D grid of mass densities. The problem is that if I put a rectangular box around the galaxy, most of the grid c

In Matlab, if I were to have a 3D Matrix as follows:- >> T = rand(4,4,3) T(:,:,1) = 0.3214 0.0986 0.4552 0.4033 0.2283 0.8989 0.7460 0.8627 0.9535 0.5170 0.6831 0.6013 0.1657 0.7017 0.9876 0.944

I have a very big and sparse matrix, represented as a CSV file (67 GB). Is it possible to load and work with this matrix in Matlab? I can use a 64bit version on a MAC OS computer, 8GB RAM. I have read

I was trying to iterate over the non zero elements of a row major sparse matrix, such as shown below: Eigen::SparseMatrix<double,Eigen::RowMajor> Test(2, 3); Test.insert(0, 1) = 34; Test.insert

I'm using Python, Numpy and Scipy packages to do matrix computations. I am attempting to perform the calculation X.transpose() * W * X where X is a 2x3 dense matrix and W is a sparse diagonal matrix.

So I have a large 3D matrix (Matrix1=round(rand(100,100,3)*100);) and I need to use the Find option to pick out all the values <16 and replace them with 0. I know it's easier to use other ways, but

I have the following sparse matrix that contains O(N) elements boost::numeric::ublas::compressed_matrix<int> adjacency (N, N); I could write a brute force double loop to go over all the entries

I have a large 500x53380 sparse matrix and trying to dichotomize it. I have tried using event2dichot under sna package but no success because it requires an adjacency matrix or network object. I al

In a scipy program I'm creating a dia_matrix (sparse matrix type) with 5 diagonals. The centre diagonal the +1 & -1 diagonals and the +4 & -4 diagonals (usually >> 4, but the principle i

In MATLAB I have calculated the Fundamental matrix (of two images) using the normalized Eight point algorithm. From that I need to triangulate the corresponding image points in 3D space. From what I u

I need to create a matrix with values from a numpy array. The values should be distributed over the matrix lines according to an array of indices. Like this: >>> values array([ 0.73620381, 0.

We have a matlab program in which we want to calculate the following expression: sum( (M*x) .* x) Here, M is a small dense matrix (say 100 by 100) and x is a sparse fat matrix (say of size 100 by 1

I have a 3D large Matrix, the first index (x) represents frequency and the second and third indexes (y and z) are the indexes of the data. I want to print the data for each index for all the frequenci

Using the Eigen library in C++, given a sparse matrix A, what is the most efficient way (row-wise operations? how to?) to compute a sparse matrix B such that B(i, j) = A(i, j) / A(i, i) ? That is, div

I have a sparse matrix A, generated as an output of glmnet function. When I print the matrix A, it shows all the entries and at the top it reads - 1897 x 100 sparse Matrix of class dgCMatrix [[ su

i am now having a single date vector A (362 rows) and i have a 3D matrix B (dimensions 360*180*3620) > str(ssta_date) POSIXlt[1:362], format: 1981-11-15 1981-12-15 1982-01-15 1982-02-15 19

I want to use sparse matrices for BOW feature representation. I have experimented with coo_matrix from scipy, but it doesn't seem to support what I want to do: I would like to initialize a matrix of a

I'm writing a program that will convert a sparse matrix to Blocked Compressed Row Storage BCRS. I know how to acquire Rowptr, Colind(although not in the code) and A_f. Code: p = 0; for (i = 0; i <

I don`t know how to solve this problem in Fundamentals of data structure in C ed.2nd ch2.5 On a computer with w bits per word, how much storage is needed to represent a sparse matrix, A, with t nonzer

I need to smooth a 3D matrix M. The output of smoothing is S. The matlab code can be like this: S = smooth3(M, 'box', 3); The problem is only some parts in the matrix M should be considered during th

I have a 3d array like this datamonth <- array(0, dim = c(length(LONG),length(LATG),length(YEAR))) >dim(datamonth) [1] 361 181 30 where the first two dimensions are Longitude and Latitude (I ha