Another Question on implementing a matrix

This is a discussion on Another Question on implementing a matrix within the C++ Programming forums, part of the General Programming Boards category; I learnt here about a month ago that plain arrays are the best when making a matrix class(as opposed to ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Another Question on implementing a matrix

    I learnt here about a month ago that plain arrays are the best when making a matrix class(as opposed to linked lists ..etc...as I had a whim to do !!).

    Which one of the following would be a better idea when I'd have to make the matrices do almost all sorts of operations and transformations..within my knowledge..?
    Code:
    template<class X,int R,int C> //Type,No of Rows and Columns 
    class matrix
    {
        public:
        typedef std::array<X,C> row_a;
         //Size of a row == no. of cols
        std::array<row_a,R> dat;
    };
    OR
    Code:
    template<class X,int R,int C> //Type,No of Rows and Coloumns 
    class matrix
    {
        public:
        std::array<X,(R*C)> dat;
    };
    If the former is better ....what is the way to access the individual X objects ..with iterators...?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    If the former is better ....what is the way to access the individual X objects ..with iterators...?
    Why not just brackets?

    Code:
    matrix[j][i]
    The reason for creating such types is not to inflict terrible syntax on everyone.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Would a 1d array respond same to matrix[i][j] compared to a array declared in a 2d fashion?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by manasij7479 View Post
    Would a 1d array respond same to matrix[i][j] compared to a array declared in a 2d fashion?
    No, but the solution to the problem isn't difficult. The j,i'th element (row j, column i) is located at index "j*C+i" (C is your "number of columns" constant).

    You could use an operator such as:

    Code:
    X &operator()(int row, int col) { return dat[row*C + col]; }
    Then you address the matrix as mat(j, i)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..Thank You..
    <finally> What is the difference between *your piece of code and
    Code:
    X operator()(int row, int col) { return *( dat[row*C+col] ); }
    ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by manasij7479 View Post
    ..Thank You..
    <finally> What is the difference between *your piece of code and
    Code:
    X operator()(int row, int col) { return *( dat[row*C+col] ); }
    ?
    The above code is incorrect, because you can't apply the dereferencing "*" operator to an array element (well, if it was a pointer you could, but the result would not be of type X). The reason I made the return type a reference is so that you can assign to the matrix by writing:

    Code:
    mat(j, i) = val;
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-15-2011, 10:36 AM
  2. Noob question - Implementing INI Parsing class
    By Highland Laddie in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2005, 05:47 PM
  3. Matrix question
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-30-2003, 04:35 PM
  4. Replies: 2
    Last Post: 01-18-2003, 12:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21