multi dimensional array

G

Gernot Frisch

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.
 
J

John Harrison

Gernot said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.).
 
F

Florian Kaufmann

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
 
B

BobR

Gernot Frisch said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top