CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2016
    Posts
    24

    raw major order calculation in c++

    i have an array/map or 13 dimension, where i want to convert the 13 dimension index value into its raw major/offset value and than simply save the 13 dimension array as 1 dimension array or map with one value. I saw that implementing the 2 or 3 dimension array in raw major order is simple but i i checked the same for large dimension with size d, than i found a linear algebra formula here, which is complcated for me to implement this in c++ code. Can u show me how i can implement an algebric equations in c++. And also how to convert offset value into its corresponding index value in n dimension array. My program right now looks similar to this, but since the dimension of array is too big its not practical for me to use the array. Glad that i found this method.

    Code:
    #include <map>
    #include <fstream>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    
    #include <boost/serialization/map.hpp>
    
    int main(int argc,char** argv) {
      std::ofstream s("filename"); 
      std::map<int,int> m;
      int i1=23; i2=23; i3=11; ... i13=34;
      int offset = ... // function of i1 to i13
      m[offset] = 100; // value at given particular offset <=> array[i1][i2]...[i13] = 100. 
      {
         boost::archive::text_oarchive oa(s);
         oa << m; 
      }
    }
    I tried to use eigen in my c++ code from here, but i didn't find any way to implement same for d dimensional array or matrix. Here i1,i2..i13 are the index value of the array.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: raw major order calculation in c++

    Its row-major order. This is simply how the data contained in a matrix is laid out in memory - so that given the memory address of the start of the matrix, the corresponding address for any specified element can be calculated. This assumes that the memory is contiguous.

    As you are using the container map to simulate a matrix, the concept of row (or column) major order doesn't apply as the memory used by map to store the data is dynamic and not contiguous. Hence also in this case there is no concept of 'offset value'. Likewise, there is no concept of a row or a column like there is in a matrix.

    Unless you want to use an existing 3rd party library (such as the one mentioned in post #1), you are going to have to implement these required matrix functions (eg eigen) yourself (probably based upon the bigarray class from http://forums.codeguru.com/showthrea...read-an-object ??). I doubt any of the 3rd party libraries will work using a map as the basic container for an array.
    Last edited by 2kaud; December 31st, 2016 at 06:45 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2017
    Posts
    3

    Re: raw major order calculation in c++

    Quote Originally Posted by dinesh999lama View Post
    i have an array/map or 13 dimension, where i want to convert the 13 dimension index value into its raw major/offset value and than simply save the 13 dimension array as 1 dimension array or map with one value. I saw that implementing the 2 or 3 dimension array in raw major order is simple but i i checked the same for large dimension with size d, than i found a linear algebra formula here, which is complcated for me to implement this in c++ code. Can u show me how i can implement an algebric equations in c++. And also how to convert offset value into its corresponding index value in n dimension array. My program right now looks similar to this, but since the dimension of array is too big its not practical for me to use the array. Glad that i found this method.

    Code:
    #include <map>
    #include <fstream>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    
    #include <boost/serialization/map.hpp>
    
    int main(int argc,char** argv) {
      std::ofstream s("filename"); 
      std::map<int,int> m;
      int i1=23; i2=23; i3=11; ... i13=34;
      int offset = ... // function of i1 to i13
      m[offset] = 100; // value at given particular offset <=> array[i1][i2]...[i13] = 100. 
      {
         boost::archive::text_oarchive oa(s);
         oa << m; 
      }
    }
    I tried to use eigen in my c++ code from here, but i didn't find any way to implement same for d dimensional array or matrix. Here i1,i2..i13 are the index value of the array.
    thanks for the thread, my compsci assignment had an almost exact same question - this helped.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: raw major order calculation in c++

    Quote Originally Posted by 2kaud View Post
    Its row-major order. This is simply how the data contained in a matrix is laid out in memory - so that given the memory address of the start of the matrix, the corresponding address for any specified element can be calculated. This assumes that the memory is contiguous.

    As you are using the container map to simulate a matrix, the concept of row (or column) major order doesn't apply as the memory used by map to store the data is dynamic and not contiguous. Hence also in this case there is no concept of 'offset value'. Likewise, there is no concept of a row or a column like there is in a matrix.
    Row-major is a general definition of the order of matrix elements. It is not defined by C++, instead C++ has opted to use it as ranking function for the memory layout of arrays.

    A ranking function is any function that takes elements (in a very general sense) and orders them by assigning a unique number to each element from a sequence (in C++ preferably from 0 to N-1 where N is the total number of elements).

    Ranking functions are independent of any C++ data structure (regardless of how the data structure manages memory internally). It is perfectly possible to use a row-major ranking function as key in an std:: map to store matrix elements. Map is a sorted container and if it is ordered on key the elements will be found in row-major order when the map is iterated.
    Last edited by wolle; May 18th, 2017 at 04:53 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured