Questions tagged [sparse-array]

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

The occurrence of zero elements in a large array is inefficient for both computation and storage. An array in which there is a large number of zero elements is referred to as being sparse.

In the case of sparse arrays, one can ask for a value from an "empty" array position. If one does this, then for an array of numbers, a value of zero should be returned, and for an array of objects, a value of null should be returned.

A naive implementation of an array may allocate space for the entire array, but in the case where there are few non-default values, this implementation is inefficient. Typically the algorithm used instead of an ordinary array is determined by other known features (or statistical features) of the array. For instance, if the sparsity is known in advance or if the elements are arranged according to some function (e.g., the elements occur in blocks). A heap memory allocator in a program might choose to store regions of blank space in a linked list rather than storing all of the allocated regions in, say a bit array.

Wikipedia: http://en.wikipedia.org/wiki/Sparse_array

94 questions
5
votes
2 answers

How to represent a sparse array in JSON?

I've got a sparse array that I want to represent in JSON. For example: -10 => 100 -1 => 102 3 => 44 12 => -87 12345 => 0 How can I do this? Can I do this?
Vilx-
  • 97,629
  • 82
  • 259
  • 398
4
votes
1 answer

Data structure for representing sparse tensor?

What is an appropriate data structure to represent a sparse tesnor in C++? The first option that comes to mind is a boost::unordered_map since it allows operations like fast setting and retrieval of an an element like below: A(i,j,k,l) = 5 However,…
D R
  • 19,435
  • 28
  • 102
  • 146
4
votes
5 answers

Anyone Know a Great Sparse One Dimensional Array Library in Python?

I am working on an algorithm in Python that uses arrays of int64s heavily. The arrays are typically sparse and are read from and written to constantly. I am currently using relatively large native arrays and the performance is good but the memory…
TheJacobTaylor
  • 4,011
  • 1
  • 17
  • 23
4
votes
2 answers

Why is SparseIntArray.equals(Object) not working?

I am using a SparseIntArray and I am puzzled by this behavior: public static SparseIntArray getArray() { SparseIntArray result = new SparseIntArray(); result.append(0, 99); result.append(1, 988); result.append(2, 636); return…
Bamaco
  • 522
  • 6
  • 23
4
votes
2 answers

JSON implementations that handle sparse arrays

I need to know if any JSON implementations can handle sparse arrays to my satisfaction. I've seen the question: How to represent a sparse array in JSON? but using an object rather than an array is not an option for me; I need an array. My minimum…
Dexygen
  • 11,681
  • 11
  • 73
  • 144
4
votes
1 answer

how sparse array differs from a hashmap in java

In my Android application I have used Hash maps in many situations. And I was asked to use sparse arrays by concerning the performance of the application. So, how sparse array differs from a hashmap in java and what are the advantages of sparse…
Vijitha
  • 295
  • 4
  • 7
4
votes
2 answers

Android SparseArray> initialization

I have to implement the classification of somehting like Hashmap with two keys and a value, let's say Hashmap, where the two keys are integers and the value is a generic MyObject defined by me. I read this, this, and this post, and I also…
The Good Giant
  • 1,560
  • 1
  • 18
  • 31
4
votes
1 answer

How to quickly compact a sparse array with CUDA C?

Summary Array [A - B - - - C] in device memory but want [A B C] - what's the quickest way with CUDA C? Context I have an array A of integers on device (GPU) memory. At each iteration, I randomly choose a few elements that are larger than 0 and…
mchen
  • 8,622
  • 10
  • 59
  • 113
4
votes
2 answers

Are JavaScript/ECMAScript arrays "sparse" in Node.js?

My question is the same as Are Javascript arrays sparse? with one difference... Are JavaScript arrays sparse, as implemented in Node.js (and/or V8)? I had assumed the were, but then I did the following test: var testArray = []; testArray[10000] =…
Brad
  • 146,404
  • 44
  • 300
  • 476
3
votes
1 answer

The fastest way to set up sparse matrix in matlab

I am working with iterative methods, and thus with large sparse matrices. For instance, I want to set up a matrix like this: 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 …
Mikhail Genkin
  • 2,721
  • 3
  • 20
  • 42
3
votes
1 answer

List with sparse data consumes less memory then the same data as numpy array

I am working with very high dimensional vectors for machine learning and was thinking about using numpy to reduce the amount of memory used. I run a quick test to see how much memory I could save using numpy (1)(3): Standard list import…
zeebonk
  • 4,274
  • 3
  • 19
  • 28
3
votes
1 answer

Wrong size of array when checking in a function

I'm trying to get all checked items from a list view in a Sparse boolean array and passing that array to a function which shows the user a dialog and on clicking yes does some stuff in the db for the selected items in the Sparse array. The problem…
trpride
  • 639
  • 5
  • 16
3
votes
2 answers

how to implement a sparse_vector class

I am implementing a templated sparse_vector class. It's like a vector, but it only stores elements that are different from their default constructed value. So, sparse_vector would store the lazily-sorted index-value pairs for all indices whose…
Neil G
  • 28,787
  • 31
  • 143
  • 234
3
votes
0 answers

Pandas: Quickly joining on a MultiIndex or reindexing

I have some very sparse, multidimensional time series, with activity at certain times and zero activity at other times. I'm representing the data in Pandas as a SparseDataframe with a MultiIndex. The work flow is to perform calculations on the small…
jeffalstott
  • 2,483
  • 3
  • 22
  • 31
3
votes
1 answer

Performing lookups on a sparse Javascript arrays

I have an interesting Javascript task (performed in Node.js, FWIW): I need to take the "weighted median" of a dataset for which I have values (income, in this case) and a weight for each one. For example: income #people 0 5 16000 3 20000 …
Chris Wilson
  • 6,371
  • 8
  • 31
  • 64