Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

n-dimensional Array

I want to create an n-dimensional array of doubles. At compile-time, the number of dimensions n is not known.

I ended up defining the array as a dictionary, with the key being an array of ints corresponding to the different axes (so in a 3-dimensional array, I'd supply [5, 2, 3] to get the double at (5, 2, 3) in the array.

However, I also need to populate the dictionary with doubles from (0, 0, ... 0) to (m1, m2, ... mn), where m1 to mn is the length of each axis.

My initial idea was to create nested for-loops, but as I still don't know how many I'd need (1 for each dimension), I can't do this at compile-time.

I hope I've formulated the question in an understandable manner, but feel free to ask me to elaborate parts.

like image 364
SimonPip Avatar asked Nov 16 '10 11:11

SimonPip


People also ask

What is N-dimensional array in C?

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. The general form of declaring N-dimensional arrays is: data_type array_name[size1][size2].... [sizeN];

How many dimension is n-dimensional array?

Although an array can have as many as 32 dimensions, it is rare to have more than three. When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.

What is the type of n-dimensional array in NumPy?

NumPy N-dimensional Array The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.

What is an ND list?

An NDList represents a sequence of NDArray s with names. Each NDArray in this list can optionally have a name. You can use the name to look up an NDArray in the NDList.

How do you create an N-dimensional array in Java?

dimensions) { int arraySize = 1; multipliers = new int[dimensions. length]; for (int idx = dimensions. length - 1; idx >= 0; idx--) { multipliers[idx] = arraySize; arraySize *= dimensions[idx]; } array = new Object[arraySize]; this. dimensions = dimensions; } ... public Object get(int...


1 Answers

To create a n-dimensional array, you can use the Array.CreateInstance method:

Array array = Array.CreateInstance(typeof(double), 5, 3, 2, 8, 7, 32));

array.SetValue(0.5d, 0, 0, 0, 0, 0, 0);
double val1 = (double)array.GetValue(0, 0, 0, 0, 0, 0);

array.SetValue(1.5d, 1, 2, 1, 6, 0, 30);
double val2 = (double)array.GetValue(1, 2, 1, 6, 0, 30);

To populate the arrays, you can use the Rank property and GetLength method to return the length of the current dimension, using a couple of nested for loops to do a O(n^m) algo (warning - untested):

private bool Increment(Array array, int[] idxs, int dim) {
    if (dim >= array.Rank) return false;

    if (++idxs[idxs.Length-dim-1] == array.GetLength(dim)) {
        idxs[idxs.Length-dim-1] = 0;
        return Increment(array, idxs, dim+1);
    }
    return true;
}

Array array = Array.CreateInstance(typeof(double), ...);
int[] idxs = new int[array.Rank];
while (Increment(array, idxs, 0)) {
    array.SetValue(1d, idxs);
}
like image 67
thecoop Avatar answered Oct 01 '22 04:10

thecoop