473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic Dimension Array

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
9 3723
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
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
<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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
2
by: raxitsheth | last post by:
I am using array in my progrm... is there any tool /tips so that i can convert my program to Dynamic Array...so that I can Add more Element to Array.... Code is around 4000 line 'C' (not C++)...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
2
by: Nathan Sokalski | last post by:
I have a multidimensional array declared as the following: Dim guesses(14, 5) As Integer I want to assign all values in a specific dimension to another array declared as follows:
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
1
by: QryS | last post by:
Hi there I have a class within which i assign 2 dimension dynamic array of pointers which points to variable inside these class (variable is of type other class). Now to get into that variable...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: lovecreatesbea... | last post by:
Do the following two functions use dynamic two-dimension array correctly? #include <stdio.h> #include <stdlib.h> #define ROW 3 #define COL 5 int dyn2dary(void);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.