1

READ FIRST: I have rewritten this question with the help of a friend to be hopefully more specific in what is required. It can be found here

I'm not very clear on n-cubes, but I believe they are what I am referring to as the square family.

New Question Wording:
Perhaps I wasn't clear enough. What I'm asking, is how to set a 1D array to hold data for a cloud of a number of evenly-spaced points that form the most complete representation of the space occupied by an n-cube of n dimensions.

In 1D this would simply fill the array with a series of 1D co-ordinates creating a line segment. A 1-cube.

In 2D however this would fill every first co-ordinate to the x value and the every second to the y, generating the most complete square possible for that spacing and number of particles. The most complete possible 2-cube.

In 3D, this would fill ever first with x, every second with y and every third with z, generating the most complete possible cube for that spacing and number of particles. The most complete possible 3-cube.

I wish to be able to do this for any reasonable combination of number of particles, spacing and dimensions. Ideally I could do at least up to a 4-cube using a generic fill algorithm for all n-cubes initialised to double * parts_

Yet another definition of what kind of object I'm trying to represent:
In 1D its a line. Sweep it through the second dimension it becomes a square. Sweep that square through the third, it becomes a cube. I presume this behaviour extends past three dimensions and wish to store a cloud of points representing the space taken up by one of these objects of any reasonable dimension, spacing and number of points in a 1D array.

The original wording of the question:
I'm struggling to find a good way to put this question but here goes. I'm making a system that uses a 1D array implemented as double * parts_ = new double[some_variable];. I want to use this to hold co-ordinates for a particle system that can run in various dimensions.

What I want to be able to do is write a generic fill algorithm for filling this in n-dimensions with a common increment in all direction to a variable size. Examples will serve best I think.

Consider the case where the number of particles stored by the array is 4

In 1D this produces 4 elements in the array because each particle only has one co-ordinate.
1D:
{0, 25, 50, 75};
In 2D this produces 8 elements in the array because each particle has two co-ordinates..
2D:
{0, 0, 0, 25, 25, 0, 25, 25}
In 3D this produces 12 elements in the array because each particle now has three co-ordinates
{0, 0, 0, 0, 0, 25, 0, 0, 50, ... }

These examples are still not quite accurate, but they hopefully will suffice.

The way I would do this normally for two dimensions:

int i = 0; 
for(int x = 0; x < parts_size_ / dims_ / dims_ * 25; x += 25) {  
    for(int y = 0; y < parts_size_ / dims_ / dims_ * 25; y += 25) { 
        parts_[i] = x;
        parts_[i+1] = y;
        i+=2;
    }
}

How can I implement this for n-dimensions where 25 can be any number?

The straight line part is because it seems to me logical that a line is a somewhat regular shape in 1D, as is a square in 2D, and a cube in 3D. It seems to me that it would follow that there would be similar shapes in this family that could be implemented for 4D and higher dimensions via a similar fill pattern. This is the shape I wish to set my array to represent.

EDIT: Apparently I'm trying to fill this array to represent the n-cube with the fewest missing elements for the given n, spacing and number of elements. If that makes my goal any clearer.

Community
  • 1
  • 1
Ben
  • 107
  • 1
  • 12
  • 1
    Please format your code using the `101010` button on the edit page. – sbi Jun 09 '10 at 06:21
  • Cheers. That's a lot easier than what I was trying to do with block quotes/escapes. – Ben Jun 09 '10 at 06:29
  • Why do you want to store a multi dimension array in a single dimension array, and not use an array with the appropriate dimension count? – Rudi Jun 09 '10 at 06:39
  • Because I want a generic data member (and filling method, which is what the question is about) for all the possible particle sets that I stated, not a single example. – Ben Jun 09 '10 at 06:40
  • Please use `std::vector` instead of raw arrays. – avakar Jun 09 '10 at 06:48
  • That's a pretty useless comment avakar. It doesn't address my problem or provide justification for your recommendation. Is it possible to have something like what I'd call variable depth recursion. The for loops look identical, but for three dimensions it would be `parts_size_ / dims_ / dims_ / dims_` I think. Can this be implemented? – Ben Jun 09 '10 at 06:59
  • @Ben: When you're answering in comments, you need to properly @address whom you answer to, so that it shows up in their list of responses. Otherwise they'll never find out you have answered. – sbi Jun 11 '10 at 10:47
  • @sbi Cheers for that. I'm still new here so it's useful to find these things out. :) – Ben Jun 11 '10 at 15:45

1 Answers1

0

As I understand it, you aren't sure how to process every element in multi-dimensional array (stored as 1D array), where N is arbitrary number of dimensions.

Processing of multidimensional array with arbitrary number of dimensions goes like this:

#include <stdio.h>
#include <vector>
using std::vector;

int main(int argc, char** argv){
    int index = 0;
    const int numDimensions = 10;
    vector<int> counters;
    vector<int> dimensionSizes;
    counters.resize(numDimensions);
    dimensionSizes.resize(numDimensions);

    for (int i = 0; i < numDimensions; i++){
        counters[i] = 0;
        dimensionSizes[i] = 13;
    }

    long long arraySize = 1;
    for (int i = 0; i < numDimensions; i++)
        arraySize *= dimensionSizes[i];


    printf("%d\n", arraySize);
    for (int elementIndex = 0; elementIndex < arraySize; elementIndex++){
        fprintf(stderr, "element %08d: ", elementIndex);
        for (int i = 0; i < numDimensions; i++)    
            fprintf(stderr, "%04d ", counters[i]);
        fprintf(stderr, "\n");
    //at this point you have 1D element index 
    //AND all n-dimensional coordinates stored in counters array. 
    //Just use them to for your data
    //"counters" is N-dimensional coord. XYZW etc.

        for (int i = 0; i < numDimensions; i++){
            counters[i] = counters[i] + 1;                
            if (counters[i] < dimensionSizes[i])
                break;
            else
                counters[i] = 0;
        }
    }


    return 0;
}

Just make an array of structs you need to access in N dimensions, and access them using calculated index somewhere after comment. It is better to use array of structs representing the data you want to be stored in N dimensionals. If you don't want to do that, you'll have to multiply elementIndex by number of doubles per element.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • Not really the question I asked. Thanks for your solution however. I rewrote my question in hopes of being clear. It's just hard when you can grasp a concept but do not know the correct name for it. I think n-cubes is right though. – Ben Jun 09 '10 at 06:55