Thread: Another Question on implementing a matrix

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    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...?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Would a 1d array respond same to matrix[i][j] compared to a array declared in a 2d fashion?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..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] ); }
    ?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 05:35 PM
  4. Replies: 2
    Last Post: 01-18-2003, 01:32 AM