multi dimensional array

Discussion in 'C++' started by Gernot Frisch, Jun 26, 2007.

  1. Hi,

    I need a class, that has a 4 dimensional array (can be 3 dimensional,
    too)

    with such an operator:
    T operator()(int x1, int x2=0, int x3=0, int x4=0);


    that can be used as:
    myarray(12);
    or
    myarray(12,14);
    depending on the array's dimension.
    So far I have stored factors for each 'x' dimension, and put the data
    in a 1-dimensional array. But it's quite ineffective, I guess.

    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
    Gernot Frisch, Jun 26, 2007
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    > Hi,
    >
    > I need a class, that has a 4 dimensional array (can be 3 dimensional,
    > too)
    >
    > with such an operator:
    > T operator()(int x1, int x2=0, int x3=0, int x4=0);
    >
    >
    > that can be used as:
    > myarray(12);
    > or
    > myarray(12,14);
    > depending on the array's dimension.
    > So far I have stored factors for each 'x' dimension, and put the data
    > in a 1-dimensional array. But it's quite ineffective, I guess.
    >


    Sounds fine to me.

    john
    John Harrison, Jun 26, 2007
    #2
    1. Advertising

  3. On 2007-06-26 20:40, Gernot Frisch wrote:
    > Hi,
    >
    > I need a class, that has a 4 dimensional array (can be 3 dimensional,
    > too)
    >
    > with such an operator:
    > T operator()(int x1, int x2=0, int x3=0, int x4=0);
    >
    >
    > that can be used as:
    > myarray(12);
    > or
    > myarray(12,14);
    > depending on the array's dimension.
    > So far I have stored factors for each 'x' dimension, and put the data
    > in a 1-dimensional array. But it's quite ineffective, I guess.
    >


    For a two dimensional structure I would use an array as the container
    and use it something like this:

    template<typename T>
    class matrix {
    size_t rows, cols;
    T* data;
    public:
    matrix(size_t r, size_t c) : data(new T[x*Y]), rows(r), cols(c) {}
    T operator(size_t r, size_t c) { return data[r * cols + c]; }
    };

    It's fairly easy to extend this to more dimensions (though take care to
    count right when doing the indexing etc.). As for efficiency it's about
    as good as it gets unless you have more data about the usages, ie.
    currently it's stored so that all rows are contiguous, you could store
    is so that the columns were, and if a lot of the values will have some
    specific value you could use a storage scheme which does not store those
    values (Compressed Row Storage etc.).

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 26, 2007
    #3
  4. > currently it's stored so that all rows are contiguous, you could store
    > is so that the columns were, and if a lot of the values will have some
    > specific value you could use a storage scheme which does not store those
    > values (Compressed Row Storage etc.).


    I only can redirect you to further information that might help. I
    remember that some math program use special representations for
    special matrices. There are sparse matrices and skyline matrices.

    http://en.wikipedia.org/wiki/Matrix_representation

    Flo
    Florian Kaufmann, Jun 26, 2007
    #4
  5. Gernot Frisch

    BobR Guest

    Gernot Frisch <> wrote in message...
    > Hi,
    > I need a class, that has a 4 dimensional array (can be 3 dimensional,
    > too)


    If this is homework, I assume you will not be allowed to use 'vectors'.

    #include <vector>
    #include <iostream>

    { // 2x2x2x2 QuadArray
    std::vector< std::vector< std::vector<
    std::vector< int > > > > QuadArray( 2, // dim' 1
    std::vector< std::vector<
    std::vector< int > > >( 2, // dim' 2
    std::vector< std::vector< int > >( 2, 2 ) ) ); // dim'
    3 & 4

    QuadArray.at( 1 ).at( 1 ).at( 1 ).at( 1 ) = 42; // set last element
    std::cout<< QuadArray.at( 1 ).at( 1 ).at( 1 ).at( 1 ) << std::endl;
    }
    // out: 42

    I'll let you stuff it into a class, and move the initialization into the
    'init list' of the class. ( ask if you need help with that.)

    >
    > with such an operator:
    > T operator()(int x1, int x2=0, int x3=0, int x4=0);


    Using the above 'QuadArray', don't forget to check the dimensions:
    if( x1 >= QuadArray.size() ){ /* kill it */ }
    if( x2 >= QuadArray.at( x1 ).size() ){ /* kill it */ }
    if( x3 >= QuadArray.at( x1 ).at( x2 ).size() ){ /* kill it */ }
    if( x4 >= QuadArray.at( x1 ).at( x2 ).at( x3 ).size() ){ /* kill it */ }
    return T( QuadArray.at( x1 ).at( x2 ).at( x3 ).at( x4 ) );
    /* kill it */ should be your own return T
    The .at() will throw 'std::eek:ut_of_range()' if index is outside range.
    You may want to change x1..x4 to type 'std::size_t' to stop warning

    > that can be used as:
    > myarray(12);
    > or
    > myarray(12,14);
    > depending on the array's dimension.
    > So far I have stored factors for each 'x' dimension, and put the data
    > in a 1-dimensional array. But it's quite ineffective, I guess.


    Don't know, I can't see it from here. <G>

    --
    Bob R
    POVrookie
    BobR, Jun 27, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,150
  2. Replies:
    7
    Views:
    833
    Ryan Stewart
    Feb 20, 2005
  3. John Harrison
    Replies:
    2
    Views:
    704
    John Harrison
    Jul 14, 2003
  4. Venkat
    Replies:
    4
    Views:
    961
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page