28 #ifndef GRT_MATRIX_HEADER
29 #define GRT_MATRIX_HEADER
31 #include "../Util/GRTTypedefs.h"
34 #include "../Util/GRTException.h"
35 #include "../Util/ErrorLog.h"
59 Matrix(
const unsigned int rows,
const unsigned int cols):errorLog(
"[ERROR Matrix]"){
95 unsigned int tempRows = data.getSize();
96 unsigned int tempCols = 0;
99 if( tempRows == 0 )
return;
102 for(
unsigned int i=0; i<tempRows; i++){
103 if( i == 0 ) tempCols = data[i].getSize();
105 if( data[i].
getSize() != tempCols ){
111 if( tempCols == 0 )
return;
114 if(
resize(tempRows,tempCols) ){
115 for(
unsigned int i=0; i<tempRows; i++){
116 for(
unsigned int j=0; j<tempCols; j++){
173 for(
unsigned int c=0; c<
cols; c++)
174 rowVector[c] =
dataPtr[r*cols+c];
186 for(
unsigned int r=0; r<
rows; r++)
209 for( i=0; i<
rows; i++){
210 for(j=0; j<
cols; j++){
215 for(j=0; j<
cols; j++){
216 for(i=0; i<
rows; i++){
232 virtual bool resize(
const unsigned int r,
const unsigned int c){
235 errorLog <<
"resize(...) - Failed to resize matrix, rows and cols == zero!" << std::endl;
247 if( r > 0 && c > 0 ){
262 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
263 throw GRT::Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
272 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
273 throw Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
280 for(i=0; i<
rows; i++){
287 }
catch( std::exception& e ){
288 errorLog <<
"resize: Failed to allocate memory. Error: " << e.what() <<
" rows: " << r <<
" cols: " << c << std::endl;
292 errorLog <<
"resize: Failed to allocate memory." << std::endl;
312 throw Exception(
"Matrix::copy( const Matrix<T> &rhs ) - Failed to allocate resize matrix!");
319 for(i=0; i<
size; i++){
349 for(i=0; i<
size; i++)
365 if(
dataPtr == NULL )
return false;
366 if( row.size() !=
cols )
return false;
367 if( rowIndex >=
rows )
return false;
370 for(j=0; j<
cols; j++)
371 dataPtr[ rowIndex * cols + j ] = row[ j ];
384 if(
dataPtr == NULL )
return false;
385 if( column.size() !=
rows )
return false;
386 if( colIndex >=
cols )
return false;
388 for(
unsigned int i=0; i<
rows; i++)
403 unsigned int i,j = 0;
407 cols = (
unsigned int)sample.size();
412 for(j=0; j<
cols; j++)
418 if(sample.size() !=
cols ){
425 for(j=0; j<
cols; j++)
430 const unsigned int tmpRows =
rows + 1;
431 T* tmpDataPtr =
new T[tmpRows*
cols];
432 T** tmpRowPtr =
new T*[tmpRows];
434 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
439 T *p = &(tmpDataPtr[0]);
440 for(i=0; i<tmpRows; i++){
450 for(j=0; j<
cols; j++)
451 tmpDataPtr[
rows*cols+j] = sample[j];
483 if(
cols == 0 )
return false;
487 T* tmpDataPtr =
new T[ capacity *
cols ];
489 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
494 T *p = &(tmpDataPtr[0]);
501 for(i=0; i<
size; i++)
573 throw Exception(
"Matrix::getDataPointer() - Matrix has not been initialized!");
599 #endif //Header guard
unsigned int getSize() const
The Vector class is a basic class for storing any type of data. The default Vector is an interface fo...
T * operator[](const unsigned int r)
bool setRowVector(const Vector< T > &row, const unsigned int rowIndex)
bool setAll(const T &value)
Matrix(const Matrix &rhs)
unsigned int getCapacity() const
T ** rowPtr
A pointer to each row in the data.
bool setAllValues(const T &value)
bool reserve(const unsigned int capacity)
Matrix & operator=(const Matrix &rhs)
bool setColVector(const Vector< T > &column, const unsigned int colIndex)
Vector< T > getRowVector(const unsigned int r) const
unsigned int rows
The number of rows in the Matrix.
Vector< T > getConcatenatedVector(const bool concatByRow=true) const
unsigned int getNumRows() const
unsigned int getNumCols() const
Matrix(const Vector< Vector< T > > &data)
T ** getDataPointer() const
virtual bool copy(const Matrix< T > &rhs)
unsigned int size
Stores rows * cols.
Vector< T > getColVector(const unsigned int c) const
const T * operator[](const unsigned int r) const
T * dataPtr
A pointer to the raw data.
unsigned int cols
The number of columns in the Matrix.
Matrix(const unsigned int rows, const unsigned int cols)
virtual bool resize(const unsigned int r, const unsigned int c)
bool push_back(const Vector< T > &sample)
unsigned int capacity
The capacity of the Matrix, this will be the number of rows, not the actual memory size...