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]"){
72 Matrix(
const unsigned int rows,
const unsigned int cols,
const T &data ):errorLog(
"[ERROR Matrix]"){
108 unsigned int tempRows = data.getSize();
109 unsigned int tempCols = 0;
112 if( tempRows == 0 )
return;
115 for(
unsigned int i=0; i<tempRows; i++){
116 if( i == 0 ) tempCols = data[i].getSize();
118 if( data[i].
getSize() != tempCols ){
124 if( tempCols == 0 )
return;
127 if(
resize(tempRows,tempCols) ){
128 for(
unsigned int i=0; i<tempRows; i++){
129 for(
unsigned int j=0; j<tempCols; j++){
186 for(
unsigned int c=0; c<
cols; c++)
187 rowVector[c] =
dataPtr[r*cols+c];
199 for(
unsigned int r=0; r<
rows; r++)
222 for( i=0; i<
rows; i++){
223 for(j=0; j<
cols; j++){
228 for(j=0; j<
cols; j++){
229 for(i=0; i<
rows; i++){
245 virtual bool resize(
const unsigned int r,
const unsigned int c){
248 errorLog <<
"resize(...) - Failed to resize matrix, rows and cols == zero!" << std::endl;
260 if( r > 0 && c > 0 ){
275 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
276 throw GRT::Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
285 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
286 throw Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
293 for(i=0; i<
rows; i++){
300 }
catch( std::exception& e ){
301 errorLog <<
"resize: Failed to allocate memory. Error: " << e.what() <<
" rows: " << r <<
" cols: " << c << std::endl;
305 errorLog <<
"resize: Failed to allocate memory." << std::endl;
321 virtual bool resize(
const unsigned int r,
const unsigned int c,
const T &value){
342 throw Exception(
"Matrix::copy( const Matrix<T> &rhs ) - Failed to allocate resize matrix!");
349 for(i=0; i<
size; i++){
379 for(i=0; i<
size; i++)
395 if(
dataPtr == NULL )
return false;
396 if( row.size() !=
cols )
return false;
397 if( rowIndex >=
rows )
return false;
400 for(j=0; j<
cols; j++)
401 dataPtr[ rowIndex * cols + j ] = row[ j ];
414 if(
dataPtr == NULL )
return false;
415 if( column.size() !=
rows )
return false;
416 if( colIndex >=
cols )
return false;
418 for(
unsigned int i=0; i<
rows; i++)
433 unsigned int i,j = 0;
437 cols = (
unsigned int)sample.size();
442 for(j=0; j<
cols; j++)
448 if(sample.size() !=
cols ){
455 for(j=0; j<
cols; j++)
460 const unsigned int tmpRows =
rows + 1;
461 T* tmpDataPtr =
new T[tmpRows*
cols];
462 T** tmpRowPtr =
new T*[tmpRows];
464 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
469 T *p = &(tmpDataPtr[0]);
470 for(i=0; i<tmpRows; i++){
480 for(j=0; j<
cols; j++)
481 tmpDataPtr[
rows*cols+j] = sample[j];
513 if(
cols == 0 )
return false;
517 T* tmpDataPtr =
new T[ capacity *
cols ];
519 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
524 T *p = &(tmpDataPtr[0]);
531 for(i=0; i<
size; i++)
605 throw Exception(
"Matrix::getDataPointer() - Matrix has not been initialized!");
631 #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)
Matrix(const unsigned int rows, const unsigned int cols, const T &data)
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)
virtual bool resize(const unsigned int r, const unsigned int c, const T &value)
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...