views:

211

answers:

1

Hi to all,

first i would like to say i am Newbie in C++.

As part of my master thesis i am writing a program in C++ which also get as parameters the variables m and d (both integers). Were d is the power of 2 (this means 2^d elements). Parameter m define the number of possible interactions between one element and the total group (2^d elements).

The number of possible interactions is computed as following:

\kappa = \sum_{i=0}^m\binom{d}{i}

(at present i generate vector of vectors for 2^d x \kappa, but my Prof. would like me to create different statistics to different m's. My first though was to to generate a dynamic array of m arrays of different sizes... Then i though of defining a 3-dim array with the biggest needed 2d array, but also program speed is important (e.g d = 20).

i would like to ask for your advice how to define such kind of dynamic array that will also be fast.

Regards

A: 

Use boost mutlidimensional arrays

http://www.boost.org/doc/libs/1%5F41%5F0/libs/multi%5Farray/doc/index.html

for example look at this code. This is very easy

#include "boost/multi_array.hpp"
#include <cassert>

int main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}
Davit Siradeghyan
Does Boost support jagged arrays? I think that is what Eagle wants.
Doc Brown
I looked before in boost project and saw this code as well, but using boost multi-array type, you need to define the size at compilation time and i know the size of dimension only at run-time (executing). Assuming that in my case m = 3, but my two other dimension will not have the same size for all m's.
Eagle
So, what's wrong with a vector<vector<vector<double> > > ?
Doc Brown
lets forget the discussion about accues speed, using a vector<vector<vector<double> > > will generate me a [m][n][p] array, which is bigger then what i need. I need for m = 0 an array of [n_0][p_0], for m = 1 i need an array of [n_1][p_1] and etc. were n_0 \neq n_1 and etc. I could also define n_max = max(n_0,n_1,...) and p_max = max(p_0,p_1,...) and generate [m}[n_max][p_max], but i am fear that such kind of code implementation would decrease speed preformence...
Eagle
You can call .resize() for every single element in a vector<vector<double> >, and the same goes for vector<vector<vector<double> > >
Doc Brown
Doc Brown, could you please write an example how you add an element to a vector<vector<double> >? it is also possible to use push_back(), which is also kind of resize.
Eagle