By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

Dynamic Dimension Array

P: n/a
Hi,

Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.

Mar 8 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Mar 8, 9:02 am, dennis....@gmail.com wrote:
Hi,

Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.
take a look at
http://www.oonumerics.org/blitz/

Regards

Mar 8 '07 #2

P: n/a
On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gmail.comwrote:
On Mar 8, 9:02 am, dennis....@gmail.com wrote:
Hi,
Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.

take a look athttp://www.oonumerics.org/blitz/

Regards
The project engineer is usually not receptive to using a 3rd party lib
so i doubt that it would fly.
Actually, after some thinking i think I can dynamic specify the
dimension and size for the array by allocating a single array of size
a*b*c and mapping a [1][2][3] query into index 1*sizeof(b)*sizeof(c) +
2*sizeof(c) + 3. I guess I have been too relient on stls... I think
this works.

Mar 8 '07 #3

P: n/a
<de********@gmail.comwrote in message
news:11*********************@p10g2000cwp.googlegro ups.com...
: On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gmail.comwrote:
: On Mar 8, 9:02 am, dennis....@gmail.com wrote:
: >
: Hi,
: >
: Is there away to define a multi-dimensional array with respect to
the
: number of dimensions the array has? For example, given a user spec
of
: "a b c d", I want to create a 4 dimensional array with dimensional
: lengths of a, b, c and d. Thanx for any help.
: >
: take a look athttp://www.oonumerics.org/blitz/
: >
: Regards
:
: The project engineer is usually not receptive to using a 3rd party lib
: so i doubt that it would fly.
: Actually, after some thinking i think I can dynamic specify the
: dimension and size for the array by allocating a single array of size
: a*b*c and mapping a [1][2][3] query into index 1*sizeof(b)*sizeof(c) +
: 2*sizeof(c) + 3. I guess I have been too relient on stls... I think
: this works.
Doesn't mean you should not rely on STL, but yes: allocating a single
contiguous storage is probably the preferred solution.
E.g.:
class MultiArray
{
std::vector<unsigneddimensions; // size of each dimension
std::vector<double data; // item count = product of all sizes

double& get(unsigned const* const indices/*array of indices*/)
{
std::size_t offs = 0;
for( unsigned i = 0 ; i!=dimensions.size() ; ++i ) {
RANGE_CHECK( indices[i]<dimensions[i] );
offs = (offs*dimensions[i])+indices[i];
}
return data[offs];
}
};

cheers -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Mar 8 '07 #4

P: n/a
On Wed, 07 Mar 2007 20:52:43 -0800, dennis.sam wrote:
On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gmail.comwrote:
>On Mar 8, 9:02 am, dennis....@gmail.com wrote:
Hi,
Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.

take a look athttp://www.oonumerics.org/blitz/

Regards

The project engineer is usually not receptive to using a 3rd party lib
so i doubt that it would fly.
Does Boost count as "3rd party lib"? If not, you might want to look at:

http://www.boost.org/libs/multi_array/doc/user.html

If it is too 3rd party for your managers, you could always "take some
inspiration" from the Boost code (paying full respect to
http://www.boost.org/LICENSE_1_0.txt, of course).

[...]

--
Lionel B
Mar 8 '07 #5

P: n/a
On Mar 8, 1:25 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
<dennis....@gmail.comwrote in message

news:11*********************@p10g2000cwp.googlegro ups.com...
: On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gmail.comwrote:
: On Mar 8, 9:02 am, dennis....@gmail.com wrote:
: >
: Hi,
: >
: Is there away to define a multi-dimensional array with respect to
the
: number of dimensions the array has? For example, given a user spec
of
: "a b c d", I want to create a 4 dimensional array with dimensional
: lengths of a, b, c and d. Thanx for any help.
: >
: take a look athttp://www.oonumerics.org/blitz/
: >
: Regards
:
: The project engineer is usually not receptive to using a 3rd party lib
: so i doubt that it would fly.
: Actually, after some thinking i think I can dynamic specify the
: dimension and size for the array by allocating a single array of size
: a*b*c and mapping a [1][2][3] query into index 1*sizeof(b)*sizeof(c) +
: 2*sizeof(c) + 3. I guess I have been too relient on stls... I think
: this works.
Doesn't mean you should not rely on STL, but yes: allocating a single
contiguous storage is probably the preferred solution.
E.g.:
class MultiArray
{
std::vector<unsigneddimensions; // size of each dimension
std::vector<double data; // item count = product of all sizes

double& get(unsigned const* const indices/*array of indices*/)
{
std::size_t offs = 0;
for( unsigned i = 0 ; i!=dimensions.size() ; ++i ) {
RANGE_CHECK( indices[i]<dimensions[i] );
offs = (offs*dimensions[i])+indices[i];
}
return data[offs];
}
};

cheers -Ivan
--http://ivan.vecerina.com/contact/?subject=NG_POST<- email contact form
Brainbench MVP for C++ <>http://www.brainbench.com
Well I guess stl is technically a 3rd party lib, I probably should
have said
that I didn't want to use another lib just to implement this array.

Is this
offs = (offs*dimensions[i])+indices[i];
an implementation of Horner's rule?
I see that is O(n) with n being the array dimension and it's much
faster then
a double loop implementation of the indexing with a summation and
product. I am
guess that it should be "+=" instead of "=" right?

thanks for prompt response

Mar 9 '07 #6

P: n/a
de********@gmail.com wrote:
Well I guess stl is technically a 3rd party lib, I probably should
have said
that I didn't want to use another lib just to implement this array.
No, the STL is part of the C++ Standard Library, defined as part of
ISO/IEC 14882:2003. Using it is as much using a third-party lib as
using strcmp would be.

Mar 9 '07 #7

P: n/a
On Thu, 08 Mar 2007 16:39:22 -0800, dennis.sam wrote:
On Mar 8, 1:25 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
><dennis....@gmail.comwrote in message

news:11*********************@p10g2000cwp.googlegr oups.com...
: On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gmail.comwrote:
: On Mar 8, 9:02 am, dennis....@gmail.com wrote:
: >
: Hi,
: >
: Is there away to define a multi-dimensional array with respect to
the
: number of dimensions the array has? For example, given a user spec
of
: "a b c d", I want to create a 4 dimensional array with dimensional
: lengths of a, b, c and d. Thanx for any help.
: >
: take a look athttp://www.oonumerics.org/blitz/
: >
: Regards
:
: The project engineer is usually not receptive to using a 3rd party lib
: so i doubt that it would fly.
: Actually, after some thinking i think I can dynamic specify the
: dimension and size for the array by allocating a single array of size
: a*b*c and mapping a [1][2][3] query into index 1*sizeof(b)*sizeof(c) +
: 2*sizeof(c) + 3. I guess I have been too relient on stls
You can't be too reliant on the STL ;)
>Doesn't mean you should not rely on STL, but yes: allocating a single
contiguous storage is probably the preferred solution.
E.g.:
class MultiArray
{
std::vector<unsigneddimensions; // size of each dimension
std::vector<double data; // item count = product of all sizes

double& get(unsigned const* const indices/*array of indices*/)
{
std::size_t offs = 0;
for( unsigned i = 0 ; i!=dimensions.size() ; ++i ) {
RANGE_CHECK( indices[i]<dimensions[i] );
offs = (offs*dimensions[i])+indices[i];
}
return data[offs];
}
};

Well I guess stl is technically a 3rd party lib,
No, STL is part of the language standard.
I probably should have said that I didn't want to use another lib just
to implement this array.
Why not? the STL in particular is no different from using other standard
lib stuff like maths functions, or printf, or cout, or whatever.
Is this
> offs = (offs*dimensions[i])+indices[i];
an implementation of Horner's rule?
I see that is O(n) with n being the array dimension and it's much faster
then a double loop implementation of the indexing with a summation and
product. I am guess that it should be "+=" instead of "=" right?
"=" looks ok to me...

But I can't help feeling that you're re-inventing wheels here. Libraries
like Blitz++ or Boost have been around for some time (many contributions
from the latter have actually become part of the standard, with more to
follow), are mature, thoroughly debugged, in daily use in production code
and - from your point of view - written by dedicated experts in their
fields who will probably have implemented them better and more efficiently
than you can. So why ever not use them?

They are also, of course, open source, so if your managers have any
concerns you can always demonstrate to them (and to yourself) the
soundness of the code :)

--
Lionel B
Mar 9 '07 #8

P: n/a
"Lionel B" <me@privacy.netwrote in message
news:es**********@south.jnrs.ja.net...
: On Thu, 08 Mar 2007 16:39:22 -0800, dennis.sam wrote:
: > offs = (offs*dimensions[i])+indices[i];
: an implementation of Horner's rule?
Not quite (Horner is about polynomial reduction), but similar.
When you coupute a 3-dimensional index, it is common to compute an index
with something like:
offs = ((index_x*size_y)+index_y)*size_z+index_z;
This is what the above loop does for N dimensions.
For me this is easier to read and represent mentally than:
offs = index_x*size_y*size_z + index_y*size_z + index_z;

: I see that is O(n) with n being the array dimension and it's much
faster
: then a double loop implementation of the indexing with a summation
and
: product. I am guess that it should be "+=" instead of "=" right?
:
: "=" looks ok to me...
Right.

: But I can't help feeling that you're re-inventing wheels here.
Libraries
: like Blitz++ or Boost have been around for some time (many
contributions
: from the latter have actually become part of the standard, with more
to
: follow), are mature, thoroughly debugged, in daily use in production
code
: and - from your point of view - written by dedicated experts in their
: fields who will probably have implemented them better and more
efficiently
: than you can. So why ever not use them?
Agreed.
By reading the OP, it appeared to me that a particular requirement
was that the **number of dimensions** was not known at compile-time,
but was dependant on user input at run-time.
Is this supported by the libraries you refer to ?

: They are also, of course, open source, so if your managers have any
: concerns you can always demonstrate to them (and to yourself) the
: soundness of the code :)
And explain that the code does not use a threatening viral license ;)

Cheers, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Mar 9 '07 #9

P: n/a
On Fri, 09 Mar 2007 11:01:07 +0100, Ivan Vecerina wrote:
"Lionel B" <me@privacy.netwrote in message
news:es**********@south.jnrs.ja.net...
: On Thu, 08 Mar 2007 16:39:22 -0800, dennis.sam wrote:
[...]
But I can't help feeling that you're re-inventing wheels here.
Libraries like Blitz++ or Boost have been around for some time (many
contributions from the latter have actually become part of the
standard, with more to follow), are mature, thoroughly debugged, in
daily use in production code and - from your point of view - written
by dedicated experts in their fields who will probably have
implemented them better and more efficiently than you can. So why ever
not use them?

Agreed. By reading the OP, it appeared to me that a particular
requirement was that the **number of dimensions** was not known at
compile-time, but was dependant on user input at run-time. Is this
supported by the libraries you refer to ?
Hm, re-reading his posts it's still not clear to me... no, in both Blitz++
and Boost::multi_array the number of dimensions is specified as a template
parameter.
They are also, of course, open source, so if your managers have any
concerns you can always demonstrate to them (and to yourself) the
soundness of the code :)

And explain that the code does not use a threatening viral license ;)
Indeed :)

--
Lionel B
Mar 9 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.