Topic: Propose 2 dimensional matrix added to the standard


Author: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Tue, 12 Aug 2003 18:40:37 +0000 (UTC)
Raw View
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                       ]





Author: maisonave@axter.com (foo)
Date: Wed, 13 Aug 2003 05:21:30 +0000 (UTC)
Raw View
nesotto@cs.auc.dk ("Thorsten Ottosen") wrote in message news:<3f350ccc$0$14559$afc38c87@news.optusnet.com.au>...
> 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                       ]





Author: apm35@student.open.ac.uk (apm)
Date: Thu, 14 Aug 2003 18:56:00 +0000 (UTC)
Raw View
maisonave@axter.com (foo) wrote in message news:<c11783b0.0308080235.61d92d7b@posting.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

---
[ 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                       ]





Author: maisonave@axter.com (foo)
Date: Thu, 14 Aug 2003 23:58:39 +0000 (UTC)
Raw View
apm35@student.open.ac.uk (apm) wrote in message news:<d1a33011.0308140818.1d96d9b8@posting.google.com>...
> maisonave@axter.com (foo) wrote in message news:<c11783b0.0308080235.61d92d7b@posting.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                       ]





Author: maisonave@axter.com (foo)
Date: Fri, 8 Aug 2003 14:40:40 +0000 (UTC)
Raw View
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<T> >
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<int> > 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                       ]