21 #define GRT_DLL_EXPORTS 22 #include "MatrixFloat.h" 27 warningLog.setKey(
"[WARNING MatrixFloat]");
28 errorLog.
setKey(
"[ERROR MatrixFloat]");
36 warningLog.setKey(
"[WARNING MatrixFloat]");
37 errorLog.
setKey(
"[ERROR MatrixFloat]");
42 if( rows > 0 && cols > 0 ){
48 warningLog.setKey(
"[WARNING MatrixFloat]");
49 errorLog.
setKey(
"[ERROR MatrixFloat]");
58 warningLog.setKey(
"[WARNING MatrixFloat]");
59 errorLog.
setKey(
"[ERROR MatrixFloat]");
68 warningLog.setKey(
"[WARNING MatrixFloat]");
69 errorLog.
setKey(
"[ERROR MatrixFloat]");
75 if( rhs.size() == 0 )
return;
78 unsigned int N = (
unsigned int)rhs[0].
getSize();
81 for(
unsigned int i=0; i<M; i++){
82 if( rhs[i].
size() != N ){
86 for(
unsigned int j=0; j<N; j++){
116 if( rhs.size() == 0 )
return *
this;
118 unsigned int M = rhs.
getSize();
119 unsigned int N = (
unsigned int)rhs[0].
getSize();
122 for(
unsigned int i=0; i<M; i++){
123 if( rhs[i].
size() != N ){
127 for(
unsigned int j=0; j<N; j++){
137 if(
dataPtr == NULL )
return false;
140 std::cout << title << std::endl;
142 for(
unsigned int i=0; i<
rows; i++){
143 for(
unsigned int j=0; j<
cols; j++){
144 std::cout <<
dataPtr[i*cols+j] <<
"\t";
146 std::cout << std::endl;
154 if(
dataPtr == NULL )
return false;
157 for(
unsigned int i=0; i<
rows; i++){
158 for(
unsigned int j=0; j<
cols; j++){
159 temp[j][i] =
dataPtr[i*cols+j];
170 if(
dataPtr == NULL )
return false;
174 return scale(ranges,minTarget,maxTarget);
178 if(
dataPtr == NULL )
return false;
180 if( ranges.size() !=
cols ){
184 unsigned int i,j = 0;
185 for(i=0; i<
rows; i++){
186 for(j=0; j<
cols; j++){
187 dataPtr[i*cols+j] = grt_scale(
dataPtr[i*cols+j],ranges[j].minValue,ranges[j].maxValue,minTarget,maxTarget);
194 if(
dataPtr == NULL )
return false;
198 for(i=0; i<
rows; i++){
203 for(j=0; j<
cols; j++){
209 for(j=0; j<
cols; j++){
213 std = sqrt( std + alpha );
216 for(j=0; j<
cols; j++){
229 Float *d_p = &(d[0][0]);
241 const unsigned int M =
rows;
242 const unsigned int N =
cols;
243 const unsigned int K = (
unsigned int)b.size();
246 warningLog <<
"multiple(vector b) - The size of b (" << b.size() <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
251 const Float *pb = &b[0];
254 unsigned int i,j = 0;
255 for(i=0; i<
rows; i++){
257 for(j=0; j<
cols; j++){
258 pc[i] +=
dataPtr[i*cols+j]*pb[j];
267 const unsigned int M =
rows;
268 const unsigned int N =
cols;
273 errorLog <<
"multiple(MatrixFloat b) - The number of rows in b (" << K <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
281 unsigned int i,j,k = 0;
302 errorLog <<
"multiple(const MatrixFloat &a,const MatrixFloat &b,const bool aTranspose) - The number of rows in a (" << K <<
") does not match the number of columns in matrix b (" << N <<
")" << std::endl;
307 errorLog <<
"multiple(const MatrixFloat &b,const MatrixFloat &c,const bool bTranspose) - Failed to resize matrix!" << std::endl;
311 unsigned int i, j, k = 0;
347 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
352 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
359 const Float *p_b = &(b[0][0]);
374 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
375 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
380 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
381 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
393 const unsigned int size = M*N;
394 for(i=0; i<
size; i++){
404 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
405 errorLog <<
" rows: " <<
rows <<
" b rows: " << b.
getNumRows() << std::endl;
410 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
411 errorLog <<
" cols: " <<
cols <<
" b cols: " << b.
getNumCols() << std::endl;
421 for(i=0; i<
size; i++){
434 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
435 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
440 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
441 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
463 Float minValue = 99e+99;
464 for(
unsigned int i=0; i<
rows*
cols; i++){
471 Float maxValue = 99e-99;
472 for(
unsigned int i=0; i<
rows*
cols; i++){
482 for(
unsigned int c=0; c<
cols; c++){
484 for(
unsigned int r=0; r<
rows; r++){
487 mean[c] /= Float( rows );
498 for(
unsigned int j=0; j<
cols; j++){
499 for(
unsigned int i=0; i<
rows; i++){
500 stdDev[j] += (
dataPtr[i*cols+j]-mean[j])*(
dataPtr[i*cols+j]-mean[j]);
502 stdDev[j] = sqrt( stdDev[j] / Float(rows-1) );
512 for(
unsigned int j=0; j<
cols; j++){
513 for(
unsigned int k=0; k<
cols; k++){
515 for(
unsigned int i=0; i<
rows; i++){
516 covMatrix[j][k] += (
dataPtr[i*cols+j]-mean[j]) * (
dataPtr[i*cols+k]-mean[k]);
518 covMatrix[j][k] /= Float(rows-1);
530 for(
unsigned int i=0; i<
rows; i++){
531 for(
unsigned int j=0; j<
cols; j++){
532 ranges[j].updateMinMax(
dataPtr[i*cols+j] );
541 for(
unsigned int i=0; i < K; i++) {
550 file.open(filename.c_str(), std::ios::out);
552 if( !file.is_open() ){
556 for(UINT i=0; i<
rows; i++){
557 for(UINT j=0; j<
cols; j++){
558 file <<
dataPtr[i*cols+j] << (j<cols-1 ?
"," :
"\n");
572 std::ifstream file( filename.c_str(), std::ifstream::in );
573 if ( !file.is_open() ){
574 warningLog <<
"parseFile(...) - Failed to open file: " << filename << std::endl;
581 std::string columnString =
"";
582 const int sepValue = seperator;
583 unsigned int rowCounter = 0;
584 unsigned int columnCounter = 0;
585 unsigned int length = 0;
588 if( !getline(file,line) ){
589 warningLog <<
"parseFile(...) - Failed to read first row!" << std::endl;
593 length = (
unsigned int)line.length();
594 for(
unsigned int i=0; i<length; i++){
595 if(
int(line[i]) == sepValue ){
601 std::cout <<
"counting rows..." << std::endl;
605 while ( getline(file,line) ){
609 std::cout <<
"matrix size: " << rowCounter <<
" " << columnCounter << std::endl;
612 if( !
resize(rowCounter, columnCounter) ){
613 warningLog <<
"parseFile(...) - Failed to resize memory!" << std::endl;
619 file.open( filename.c_str(), std::ifstream::in );
623 while ( getline(file,line) )
628 length = (
unsigned int)line.length();
629 for(
unsigned int i=0; i<length; i++){
630 if(
int(line[i]) == sepValue ){
631 vec.push_back( columnString );
633 }
else columnString += line[i];
637 vec.push_back( columnString );
640 if( columnCounter != vec.size() ){
642 warningLog <<
"parseFile(...) - Found inconsistent column size in row " << rowCounter;
643 warningLog <<
" ColumnSize: " << columnCounter <<
" LastColumnSize: " << vec.size() << std::endl;
648 if( vec.size() >= 1 ){
649 size_t K = vec.size()-1;
650 size_t foundA = vec[ K ].find(
'\n');
651 size_t foundB = vec[K ].find(
'\r');
652 if( foundA != std::string::npos || foundB != std::string::npos ){
653 vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
658 for(
unsigned int j=0; j<columnCounter; j++){
659 dataPtr[rowCounter*
cols+j] = grt_from_str< Float >( vec[j] );
671 return save( filename );
675 return load( filename, seperator );
bool save(const std::string &filename) const
Float getMaxValue() const
bool saveToCSVFile(const std::string &filename) const
unsigned int getSize() const
bool load(const std::string &filename, const char seperator= ',')
bool scale(const Float minTarget, const Float maxTarget)
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format 'key ...
Float ** rowPtr
A pointer to each row in the data.
bool add(const MatrixFloat &b)
bool subtract(const MatrixFloat &b)
unsigned int rows
The number of rows in the Matrix.
bool print(const std::string title="") const
VectorFloat getMean() const
MatrixFloat & operator=(const MatrixFloat &rhs)
MatrixFloat getCovarianceMatrix() const
bool loadFromCSVFile(const std::string &filename, const char seperator= ',')
unsigned int getNumRows() const
unsigned int getNumCols() const
Float getMinValue() const
T ** getDataPointer() const
virtual bool copy(const Matrix< Float > &rhs)
unsigned int size
Stores rows * cols.
Vector< MinMax > getRanges() const
Float * dataPtr
A pointer to the raw data.
unsigned int cols
The number of columns in the Matrix.
virtual bool resize(const unsigned int r, const unsigned int c)
VectorFloat getStdDev() const
MatrixFloat multiple(const Float value) const
bool znorm(const Float alpha=0.001)