C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Propose 2 dimensional matrix added to the standard

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
foo
Guest





PostPosted: Fri Aug 08, 2003 2:40 pm    Post subject: Propose 2 dimensional matrix added to the standard Reply with quote



I propose that a 2 dimensional matrix be added to the standard.

The following matrix class can be used to dynamically or statically
create a 2 dimensional matrix object.
template < class T, int ROW_T = 0, int COL_T = 0 >
class matrix_2d
{
public:
matrix_2d(int row, int col):m_row(row),m_col(col),
m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
matrix_2d():m_row(ROW_T),m_col(COL_T), m_data(new T[ROW_T*COL_T])
{if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] =
{{x[0]}};}}
~matrix_2d(){if(m_data) delete []m_data;}
T* operator[](int i) {return (m_data + (m_col*i));}
T const*const operator[](int i) const {return (m_data +
(m_col*i));}
private:
const int m_row;
const int m_col;
T* m_data;
};

A two dimensional object can be created with a vector<vector
type, however there are several draw backs to the vector in vector
method.
The data structure of a vector<vector>> object does not accurately
replicate a C-Style 2 dimensional array.

int MyArray[5][10]; //A single block of data is allocated for C-Style
2D array
matrix_2d<int> MyArray(5, 10); //Single block of data is allocated for
matrix_2d

vector<vector MyArray(5);//This creates an array of pointers to
an array

The vector method requires further iteration through each array to
resize it to the required 2nd dimension of a 2 dimensional array.
In the test I've conducted, the operator[] is faster in the above
matrix_2d object then in a vector<vector> > object.

The matrix_2d object also initializes faster to the two dimensional
requirements.

IMHO, the matrix_2d class does a much better job of creating a two
dimensional array then a vector<vector>> object, and it's easier to
implement.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Thorsten Ottosen
Guest





PostPosted: Tue Aug 12, 2003 6:40 pm    Post subject: Re: Propose 2 dimensional matrix added to the standard Reply with quote



you might want to have a look at boost::multi_array<> first,

best regards

Thorsten


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
foo
Guest





PostPosted: Wed Aug 13, 2003 5:21 am    Post subject: Re: Propose 2 dimensional matrix added to the standard Reply with quote



[email]nesotto (AT) cs (DOT) auc.dk[/email] ("Thorsten Ottosen") wrote in message news:<3f350ccc$0$14559$afc38c87 (AT) news (DOT) optusnet.com.au>...
Quote:
you might want to have a look at boost::multi_array<> first,

best regards

Thorsten


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

The boost::multi_array uses an operator() to iterate through the data.
It also uses a temporary object to pass back the data.
This makes it less efficient then the class I posted, and it does not
carry the same syntax used on a C-Style 2 dimensional array.

The boost::multi_array is better suited for an array larger the 2
dimension, but it's performance and syntax is poor compare to my
posted class.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
foo
Guest





PostPosted: Thu Aug 14, 2003 11:58 pm    Post subject: Re: Propose 2 dimensional matrix added to the standard Reply with quote

[email]apm35 (AT) student (DOT) open.ac.uk[/email] (apm) wrote in message news:<d1a33011.0308140818.1d96d9b8 (AT) posting (DOT) google.com>...
Quote:
maisonave (AT) axter (DOT) com (foo) wrote in message news:<c11783b0.0308080235.61d92d7b (AT) posting (DOT) google.com>...
I propose that a 2 dimensional matrix be added to the standard.

The following matrix class can be used to
[snip]
This does not seem to add much value AFAICS. I would want a matrix
class to support matrix operations such as transpose and inverse and
overloaded arithmetic operators. The newmat library does. See
http://www.robertnz.net/nm_intro.htm for details.

-Andrew Marlow

The functionallity you're referring to is similar to the original
purpose of the valarray class.
The class I posted is not suppose to function as a valarray class.
It's purpose is for functioning as a replacement for a C-Style 2
dimensional array or a vector<vector<>> 2 dimensional array.

Matrix class/libraries like the one you posted usually either do not
have the ability to reference a variable using a C-Style method
mydata[3][3] = 99;
cout << mydata[3][3] << endl;

Or if they do have this syntax available, the class uses a pass by
value method to fetch and set the data. This method is very
inefficient compare to the method used in the class I posted.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.