0

I am currently building a program that will calculate a self-avoiding walk in n-dimensions. The program has to have a list of coordinates that it has previously visited. For a known maximum number of dimensions, I would simply make a vector of a position struct as such:

struct Position
{
    long int x;
    long int y;
    long int z;
    long int w;
    etc...
}

std::vector<Position> history;
Position currentSite;

But when programming for an n-dimensional position, I am not sure how to do that without making an array of n*walk_length in size.

Is there a more "correct" way to do it?

Note: I'm programming in C++.

NictraSavios
  • 502
  • 2
  • 9
  • 29

2 Answers2

3

If n is known at compile time, you could use std::array<long,n> to represent a position.

If n is not known until runtime, std::vector<long> would be a good choice.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • n-dimensions, not n-variables. It would be n^walk_length variables. – NictraSavios Nov 13 '13 at 15:44
  • @NictraSavios: What I described would be used to represent one position. To represent a sequence of positions you'd use a vector of those just as you're doing already. – NPE Nov 13 '13 at 15:56
2

I'd use vector for coordinates, and set for positions:

typedef std:vector<long> Position;
typedef std::set<Position> VisitedPositions;

Then you'll be able to choose n dynamically and search for positions more quickly.

HEKTO
  • 3,876
  • 2
  • 24
  • 45
  • How would I be able to choose n dynamically if I don't have variables in my struct for it? I would need a struct that creates new objects. – NictraSavios Nov 13 '13 at 15:43
  • @NictraSavios - as far as I understood you needed a program which works with any n without changes, right? In this case it can request the n value from you interactively and keep all the coordinates in the vector. This case I call "dynamic". If you know the n in advance and it's not too big you can hardcode the structure with all the coordinates - it will be "static" case. – HEKTO Nov 13 '13 at 15:51
  • Yes, I'd like a dynamic case, so how could I create a dynamically dimensional vector? Would it be something like vector Which would produce a "table" of axis by value, step and value, like 3,3,-10 , which would mean w-axis (4d), 3rd step in the walk and at w=-10. – NictraSavios Nov 13 '13 at 17:13
  • @NictraSavios - no, I meant you save all your coordinates into the vector one by one using the _push_back_ function. So, the pos[0] will contain x-coordinate, pos[1] - y-coordinate and so on. The length of the vector will be equal to your dimensionality n. It's the same approach the NPE was talking about. – HEKTO Nov 13 '13 at 21:06