29 #ifndef GRT_MATRIX_HEADER
30 #define GRT_MATRIX_HEADER
36 #include "GRTException.h"
61 Matrix(
const unsigned int rows,
const unsigned int cols):errorLog(
"[ERROR Matrix]"){
89 Matrix(
const std::vector< std::vector< T > > &data):errorLog(
"[ERROR Matrix]"){
97 unsigned int tempRows = (
unsigned int)data.size();
98 unsigned int tempCols = 0;
101 if( tempRows == 0 )
return;
104 for(
unsigned int i=0; i<tempRows; i++){
105 if( i == 0 ) tempCols = data[i].size();
107 if( data[i].
size() != tempCols ){
113 if( tempCols == 0 )
return;
116 if(
resize(tempRows,tempCols) ){
117 for(
unsigned int i=0; i<tempRows; i++){
118 for(
unsigned int j=0; j<tempCols; j++){
174 std::vector<T> rowVector(
cols);
175 for(
unsigned int c=0; c<
cols; c++)
176 rowVector[c] =
dataPtr[r*cols+c];
187 std::vector<T> columnVector(
rows);
188 for(
unsigned int r=0; r<
rows; r++)
204 if(
rows == 0 ||
cols == 0 )
return std::vector<T>();
206 std::vector<T> vectorData(
rows*
cols);
211 for( i=0; i<
rows; i++){
212 for(j=0; j<
cols; j++){
217 for(j=0; j<
cols; j++){
218 for(i=0; i<
rows; i++){
234 virtual bool resize(
const unsigned int r,
const unsigned int c){
237 errorLog <<
"resize(...) - Failed to resize matrix, rows and cols == zero!" << std::endl;
249 if( r > 0 && c > 0 ){
264 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
265 throw Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
274 errorLog <<
"resize(const unsigned r,const unsigned int c) - Failed to allocate memory! r: " << r <<
" c: " << c << std::endl;
275 throw Exception(
"Matrix::resize(const unsigned int r,const unsigned int c) - Failed to allocate memory!");
282 for(i=0; i<
rows; i++){
289 }
catch( std::exception& e ){
290 errorLog <<
"resize: Failed to allocate memory. Error: " << e.what() <<
" rows: " << r <<
" cols: " << c << std::endl;
294 errorLog <<
"resize: Failed to allocate memory." << std::endl;
314 throw Exception(
"Matrix::copy( const Matrix<T> &rhs ) - Failed to allocate resize matrix!");
321 for(i=0; i<
size; i++){
338 for(i=0; i<
size; i++)
353 bool setRowVector(
const std::vector<T> &row,
const unsigned int rowIndex){
354 if(
dataPtr == NULL )
return false;
355 if( row.size() !=
cols )
return false;
356 if( rowIndex >=
rows )
return false;
359 for(j=0; j<
cols; j++)
360 dataPtr[ rowIndex * cols + j ] = row[ j ];
372 bool setColVector(
const std::vector<T> &column,
const unsigned int colIndex){
373 if(
dataPtr == NULL )
return false;
374 if( column.size() !=
rows )
return false;
375 if( colIndex >=
cols )
return false;
377 for(
unsigned int i=0; i<
rows; i++)
392 unsigned int i,j = 0;
396 cols = (
unsigned int)sample.size();
401 for(j=0; j<
cols; j++)
407 if(sample.size() !=
cols ){
414 for(j=0; j<
cols; j++)
419 const unsigned int tmpRows =
rows + 1;
420 T* tmpDataPtr =
new T[tmpRows*
cols];
421 T** tmpRowPtr =
new T*[tmpRows];
423 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
428 T *p = &(tmpDataPtr[0]);
429 for(i=0; i<tmpRows; i++){
439 for(j=0; j<
cols; j++)
440 tmpDataPtr[
rows*cols+j] = sample[j];
472 if(
cols == 0 )
return false;
476 T* tmpDataPtr =
new T[ capacity *
cols ];
478 if( tmpDataPtr == NULL || tmpRowPtr == NULL ){
483 T *p = &(tmpDataPtr[0]);
490 for(i=0; i<
size; i++)
562 throw Exception(
"Matrix::getDataPointer() - Matrix has not been initialized!");
581 #endif //GRT_MATRIX_HEADER
bool setColVector(const std::vector< T > &column, const unsigned int colIndex)
T * operator[](const unsigned int r)
bool setRowVector(const std::vector< T > &row, const unsigned int rowIndex)
bool setAllValues(const T &value)
bool push_back(const std::vector< T > &sample)
Matrix & operator=(const Matrix &rhs)
unsigned int getNumCols() const
T * dataPtr
A pointer to the raw data.
T ** getDataPointer() const
const T * operator[](const unsigned int r) const
Matrix(const unsigned int rows, const unsigned int cols)
unsigned int capacity
The capacity of the Matrix, this will be the number of rows, not the actual memory size...
virtual bool copy(const Matrix< T > &rhs)
unsigned int size
Stores rows * cols.
T ** rowPtr
A pointer to each row in the data.
std::vector< T > getConcatenatedVector(const bool concatByRow=true) const
unsigned int getNumRows() const
bool reserve(const unsigned int capacity)
std::vector< T > getColVector(const unsigned int c) const
Matrix(const std::vector< std::vector< T > > &data)
Matrix(const Matrix &rhs)
unsigned int rows
The number of rows in the Matrix.
std::vector< T > getRowVector(const unsigned int r) const
unsigned int cols
The number of columns in the Matrix.
virtual bool resize(const unsigned int r, const unsigned int c)
unsigned int getCapacity() const
unsigned int getSize() const