0

I am working on redesigning of a code in which 6D array are created . This is required for Cplex linear calculation.

Now, there are API's which can support 1D or int variable in cplex also so its not important to have 6 dimension array. Only the mapping is important with the variable which will be required when retrieing it from another method.

Is there any way of replacing 6D array. This will also help in simplying the complexity of the code.

-> can i replace somehow the 6D array and store the variable in 1D array or int variable.. -> I tried HashMap with key as list and value as Integer but eventually it is taking more time because HashMap internally uses Array.

Sample code is:

int [][][][][][] testVariable = new int [2][2][3][5][2][2];
        HashMap<List<Integer>, Integer> mapping = new HashMap<List<Integer>, Integer>();
        int[] arrayAdd = new int[11111];

    for (int a =0; a <= 2; a++) {       
        for (int l = 0; l < 2; l++) {
                for (int y = 0; y < 3; y++) {
                    for (int r = 0; r < 5; r++) {
                        for(int oc=0; oc< 2; oc++){
                            for(int c=0; c< 2; c++){

                                // usual array of 6D

                                testVariable[a][l][y][r][oc][c]=count;

                                // FIRST METHOD ( Takes more time then Array) 

                                LinkedList<Integer> listAdd = new LinkedList<Integer>();
                                listAdd.add(a);
                                listAdd.add(l);
                                listAdd.add(y);
                                listAdd.add(r);
                                listAdd.add(oc);
                                listAdd.add(c);
                                mapping.put(listAdd, count);

                                // second method .. this i am still testing but it doesnt make much sense)

                                arrayAdd[a + l + y + r + oc + c] = count;


                            //  Arrays.fill(arrayAdd, count);
                                //System.out.println("Value of list is  " + listAdd);
                                count ++;

                        }
                    }
                }
            }
       }
  }
5
  • 1
    how did you API change? Are you sure that the new API is backwards compatible with the old one?
    – Timofey
    Sep 9, 2014 at 21:56
  • so, the api is compatible. Eventually it would be requiring an integer or a range only .. Not an issue with api...
    – Rahul
    Sep 9, 2014 at 22:06
  • int a =0; a <= 2; a++ is going to cause an index out of bounds exception. Use a < 2 like the other loops.
    – Brian S
    Sep 9, 2014 at 22:14
  • testVariable stores 240 total integers; why is arrayAdd 11111 elements long?
    – Brian S
    Sep 9, 2014 at 22:16
  • Brian, that is just dummy code... i am testing something.. Actually real scenario the array is of size 2 million or more
    – Rahul
    Sep 9, 2014 at 22:18

3 Answers 3

3

If you know that the each of the dimensions' lengths won't change, then you can simulate a 6D array with a 1D array. The trick is to multiply the indexes for all but the last index c by a high enough number so that there won't be any collisions between distinct sets of 6D indexes mapping to the same 1D index.

int[] oneDArray = new int[2*2*3*5*2*2];

And for the calculation of the 1D index, using the 6 6D indexes. Multiplying by the product of all the dimensions' lengths after each particular 6D index will avoid the collisions.

int oneDIndex = 2*3*5*2*2 * a  // last 5 dimensions' lengths
              +   3*5*2*2 * l  // last 4 dimensions' lengths; variable letter l
              +     5*2*2 * y  // last 3 dimensions' lengths
              +       2*2 * r  // last 2 dimensions' lengths
              +         2 * oc // last dimension's length only
              +             c; // no multiplier necessary for last index

It will be better to use constants for each of the dimensions' lengths here, to reduce hard-coding.

3
  • 1
    I suggest wrapping all of that in an object, to encapsulate all those constants in one place.
    – Karol S
    Sep 9, 2014 at 22:04
  • ok, but how will i setup the array with value and unique indexes. I also need to retrieve the value from array in different method with 6 nested for loop ... .so the indexe should be exactly same.. can you help me with 3-4 line of code...
    – Rahul
    Sep 9, 2014 at 22:09
  • @Rahul, using rgettman's code, in your innermost loop, you'd use oneDArray[oneDIndex] instead of testVariable[a][l][y][r][oc][c], for example.
    – Brian S
    Sep 9, 2014 at 22:18
1

Extending the answer of rgettman and proving more general way to convert from 6D->1D and vice versa.

  1. Convert 6D array to 1D array:

    int[] sixDArrayToOneDArray(final int[][][][][][] sixDArray) {
    
        int dim1Length = sixDArray.length;
        int dim2Length = sixDArray[0].length;
        int dim3Length = sixDArray[0][0].length;
        int dim4Length = sixDArray[0][0][0].length;
        int dim5Length = sixDArray[0][0][0][0].length;
        int dim6Length = sixDArray[0][0][0][0][0].length;
    
        int[] result = new int[dim1Length * dim2Length * dim3Length * dim4Length * dim5Length * dim6Length];
    
        for (int i1 = 0; i1 < dim1Length; i1++)
            for (int i2 = 0; i2 < dim2Length; i2++)
                for (int i3 = 0; i3 < dim3Length; i3++)
                    for (int i4 = 0; i4 < dim4Length; i4++)
                        for (int i5 = 0; i5 < dim5Length; i5++)
                            for (int i6 = 0; i6 < dim6Length; i6++) {
                                int oneDIndex = i1 * dim2Length * dim3Length * dim4Length * dim5Length * dim6Length
                                              + i2 * dim3Length * dim4Length * dim5Length * dim6Length                                        + i3 * dim4Length * dim5Length * dim6Length
                                              + i4 * dim5Length * dim6Length
                                              + i5 * dim6Length
                                              + i6;
                                result[oneDIndex] = sixDArray[i1][i2][i3][i4][i5][i6];
                            }
    
        return result;
    }
    
  2. Convert 1D array to 6D array:

    int[][][][][][] oneDArrayToSixDArray(int[] oneDArray, int dim1Length, int dim2Length, int dim3Length, int dim4Length, int dim5Length, int dim6Length) {
    
        if (oneDArray.length != dim1Length * dim2Length * dim3Length * dim4Length * dim5Length * dim6Length) {
            throw new IllegalArgumentException(String.format("One dimension array length [%s] does not match to six dimensions array lengths [%s, %s, %s, %s, %s, %s]", dim1Length, dim2Length, dim3Length, dim4Length, dim5Length, dim6Length));
        }
    
        int[][][][][][] result = new int[dim1Length][dim2Length][dim3Length][dim4Length][dim5Length][dim6Length];
        for (int i = 0; i < oneDArray.length; i++) {
            int i1 =  i / (dim2Length * dim3Length * dim4Length * dim5Length * dim6Length);
            int i2 = (i % (dim2Length * dim3Length * dim4Length * dim5Length * dim6Length)) / (dim3Length * dim4Length * dim5Length * dim6Length);
            int i3 = (i % (dim3Length * dim4Length * dim5Length * dim6Length)) / (dim4Length * dim5Length * dim6Length);
            int i4 = (i % (dim4Length * dim5Length * dim6Length)) / (dim5Length * dim6Length);
            int i5 = (i % (dim5Length * dim6Length)) / dim6Length;
            int i6 =  i % dim6Length;
            result[i1][i2][i3][i4][i5][i6] = oneDArray[i];
        }
    
        return result;
    }
    
2
  • This is great. But i do need to retrive the value of result[oneDIndex] based on 6 nested loops...
    – Rahul
    Sep 10, 2014 at 17:55
  • @Rahul could you describe which exactly is the use case of retrieving the value? Use case: get a single value from the resulting one dimension array based on 6 indexes. Is that what you mean?
    – Timofey
    Sep 10, 2014 at 18:31
0

Just my opinion, but assuming I wanted a non-sparse, hyper-rectangular, I would use a 1D Java array, and do the index arithmetic myself.

E.g., for a 2D array with 3 rows and 5 columns:

final int num_rows = 3;
final int num_cols = 5;

final T[] storage = new T[num_rows * num_cols];

T get(int row, int col) {
    assert(row >= 0);
    assert(row < num_rows);
    assert(col >= 0);
    assert(col < num_cols);
    return storage[row*num_cols + col];
}

You can imagine how this would be generalized to more than two dimensions.

Hint: I would replace num_rows and num_cols with a 1D, effectively immutable array named 'dimension'. The length of the dimension array would be the number of dimensions presented to the client. The length of the storage array would be the product of all of the members of the dimension array, and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.