Dynamic Dimension Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dennis.sam@gmail.com

    #1

    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.

  • Amol

    #2
    Re: Dynamic Dimension Array

    On Mar 8, 9:02 am, dennis....@gmai l.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


    Regards

    Comment

    • dennis.sam@gmail.com

      #3
      Re: Dynamic Dimension Array

      On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gma il.comwrote:
      On Mar 8, 9:02 am, dennis....@gmai l.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)*siz eof(c) +
      2*sizeof(c) + 3. I guess I have been too relient on stls... I think
      this works.

      Comment

      • Ivan Vecerina

        #4
        Re: Dynamic Dimension Array

        <dennis.sam@gma il.comwrote in message
        news:1173329563 .395586.66000@p 10g2000cwp.goog legroups.com...
        : On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gma il.comwrote:
        : On Mar 8, 9:02 am, dennis....@gmai l.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)*siz eof(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<uns igneddimensions ; // size of each dimension
        std::vector<dou ble 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.s ize() ; ++i ) {
        RANGE_CHECK( indices[i]<dimensions[i] );
        offs = (offs*dimension s[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



        Comment

        • Lionel B

          #5
          Re: Dynamic Dimension Array

          On Wed, 07 Mar 2007 20:52:43 -0800, dennis.sam wrote:
          On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gma il.comwrote:
          >On Mar 8, 9:02 am, dennis....@gmai l.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:



          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

          Comment

          • dennis.sam@gmail.com

            #6
            Re: Dynamic Dimension Array

            On Mar 8, 1:25 am, "Ivan Vecerina"
            <_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
            <dennis....@gma il.comwrote in message
            >
            news:1173329563 .395586.66000@p 10g2000cwp.goog legroups.com...
            : On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gma il.comwrote:
            : On Mar 8, 9:02 am, dennis....@gmai l.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)*siz eof(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<uns igneddimensions ; // size of each dimension
            std::vector<dou ble 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.s ize() ; ++i ) {
            RANGE_CHECK( indices[i]<dimensions[i] );
            offs = (offs*dimension s[i])+indices[i];
            }
            return data[offs];
            }
            };
            >
            cheers -Ivan
            --http://ivan.vecerina.c om/contact/?subject=NG_POS T<- 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*dimension s[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

            Comment

            • red floyd

              #7
              Re: Dynamic Dimension Array

              dennis.sam@gmai l.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.

              Comment

              • Lionel B

                #8
                Re: Dynamic Dimension Array

                On Thu, 08 Mar 2007 16:39:22 -0800, dennis.sam wrote:
                On Mar 8, 1:25 am, "Ivan Vecerina"
                <_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
                ><dennis....@gm ail.comwrote in message
                >>
                >news:117332956 3.395586.66000@ p10g2000cwp.goo glegroups.com.. .
                >: On Mar 7, 8:32 pm, "Amol" <Amol.Ga...@gma il.comwrote:
                >: On Mar 8, 9:02 am, dennis....@gmai l.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)*siz eof(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<uns igneddimensions ; // size of each dimension
                > std::vector<dou ble 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.s ize() ; ++i ) {
                > RANGE_CHECK( indices[i]<dimensions[i] );
                > offs = (offs*dimension s[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*dimension s[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

                Comment

                • Ivan Vecerina

                  #9
                  Re: Dynamic Dimension Array

                  "Lionel B" <me@privacy.net wrote in message
                  news:esr9vs$68m $2@south.jnrs.j a.net...
                  : On Thu, 08 Mar 2007 16:39:22 -0800, dennis.sam wrote:
                  : > offs = (offs*dimension s[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)*siz e_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

                  Comment

                  • Lionel B

                    #10
                    Re: Dynamic Dimension Array

                    On Fri, 09 Mar 2007 11:01:07 +0100, Ivan Vecerina wrote:
                    "Lionel B" <me@privacy.net wrote in message
                    news:esr9vs$68m $2@south.jnrs.j a.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_ar ray 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

                    Comment

                    Working...