21 #define GRT_DLL_EXPORTS
22 #include "MatrixFloat.h"
27 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
28 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
35 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
36 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
38 if( rows > 0 && cols > 0 ){
44 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
45 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
52 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
53 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
83 if( rhs.size() == 0 )
return *
this;
86 unsigned int N = (
unsigned int)rhs[0].
getSize();
89 for(
unsigned int i=0; i<M; i++){
90 if( rhs[i].
size() != N ){
94 for(
unsigned int j=0; j<N; j++){
104 if(
dataPtr == NULL )
return false;
107 std::cout << title << std::endl;
109 for(
unsigned int i=0; i<
rows; i++){
110 for(
unsigned int j=0; j<
cols; j++){
111 std::cout <<
dataPtr[i*cols+j] <<
"\t";
113 std::cout << std::endl;
121 if(
dataPtr == NULL )
return false;
124 for(
unsigned int i=0; i<
rows; i++){
125 for(
unsigned int j=0; j<
cols; j++){
126 temp[j][i] =
dataPtr[i*cols+j];
137 if(
dataPtr == NULL )
return false;
141 return scale(ranges,minTarget,maxTarget);
145 if(
dataPtr == NULL )
return false;
147 if( ranges.size() !=
cols ){
151 unsigned int i,j = 0;
152 for(i=0; i<
rows; i++){
153 for(j=0; j<
cols; j++){
154 dataPtr[i*cols+j] = grt_scale(
dataPtr[i*cols+j],ranges[j].minValue,ranges[j].maxValue,minTarget,maxTarget);
161 if(
dataPtr == NULL )
return false;
165 for(i=0; i<
rows; i++){
170 for(j=0; j<
cols; j++){
176 for(j=0; j<
cols; j++){
180 std = sqrt( std + alpha );
183 for(j=0; j<
cols; j++){
196 Float *d_p = &(d[0][0]);
208 const unsigned int M =
rows;
209 const unsigned int N =
cols;
210 const unsigned int K = (
unsigned int)b.size();
213 warningLog <<
"multiple(vector b) - The size of b (" << b.size() <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
218 const Float *pb = &b[0];
221 unsigned int i,j = 0;
222 for(i=0; i<
rows; i++){
224 for(j=0; j<
cols; j++){
225 pc[i] +=
dataPtr[i*cols+j]*pb[j];
234 const unsigned int M =
rows;
235 const unsigned int N =
cols;
240 errorLog <<
"multiple(MatrixFloat b) - The number of rows in b (" << K <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
248 unsigned int i,j,k = 0;
269 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;
274 errorLog <<
"multiple(const MatrixFloat &b,const MatrixFloat &c,const bool bTranspose) - Failed to resize matrix!" << std::endl;
278 unsigned int i, j, k = 0;
314 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
319 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
326 const Float *p_b = &(b[0][0]);
341 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
342 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
347 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
348 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
360 const unsigned int size = M*N;
361 for(i=0; i<
size; i++){
371 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
372 errorLog <<
" rows: " <<
rows <<
" b rows: " << b.
getNumRows() << std::endl;
377 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
378 errorLog <<
" cols: " <<
cols <<
" b cols: " << b.
getNumCols() << std::endl;
388 for(i=0; i<
size; i++){
401 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
402 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
407 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
408 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
430 Float minValue = 99e+99;
431 for(
unsigned int i=0; i<
rows*
cols; i++){
438 Float maxValue = 99e-99;
439 for(
unsigned int i=0; i<
rows*
cols; i++){
449 for(
unsigned int c=0; c<
cols; c++){
451 for(
unsigned int r=0; r<
rows; r++){
454 mean[c] /= Float( rows );
465 for(
unsigned int j=0; j<
cols; j++){
466 for(
unsigned int i=0; i<
rows; i++){
467 stdDev[j] += (
dataPtr[i*cols+j]-mean[j])*(
dataPtr[i*cols+j]-mean[j]);
469 stdDev[j] = sqrt( stdDev[j] / Float(rows-1) );
479 for(
unsigned int j=0; j<
cols; j++){
480 for(
unsigned int k=0; k<
cols; k++){
482 for(
unsigned int i=0; i<
rows; i++){
483 covMatrix[j][k] += (
dataPtr[i*cols+j]-mean[j]) * (
dataPtr[i*cols+k]-mean[k]);
485 covMatrix[j][k] /= Float(rows-1);
497 for(
unsigned int i=0; i<
rows; i++){
498 for(
unsigned int j=0; j<
cols; j++){
499 ranges[j].updateMinMax(
dataPtr[i*cols+j] );
508 for(
unsigned int i=0; i < K; i++) {
517 file.open(filename.c_str(), std::ios::out);
519 if( !file.is_open() ){
523 for(UINT i=0; i<
rows; i++){
524 for(UINT j=0; j<
cols; j++){
525 file <<
dataPtr[i*cols+j] << (j<cols-1 ?
"," :
"\n");
539 std::ifstream file( filename.c_str(), std::ifstream::in );
540 if ( !file.is_open() ){
541 warningLog <<
"parseFile(...) - Failed to open file: " << filename << std::endl;
548 std::string columnString =
"";
549 const int sepValue = seperator;
550 unsigned int rowCounter = 0;
551 unsigned int columnCounter = 0;
552 unsigned int length = 0;
555 if( !getline(file,line) ){
556 warningLog <<
"parseFile(...) - Failed to read first row!" << std::endl;
560 length = (
unsigned int)line.length();
561 for(
unsigned int i=0; i<length; i++){
562 if(
int(line[i]) == sepValue ){
568 std::cout <<
"counting rows..." << std::endl;
572 while ( getline(file,line) ){
576 std::cout <<
"matrix size: " << rowCounter <<
" " << columnCounter << std::endl;
579 if( !
resize(rowCounter, columnCounter) ){
580 warningLog <<
"parseFile(...) - Failed to resize memory!" << std::endl;
586 file.open( filename.c_str(), std::ifstream::in );
590 while ( getline(file,line) )
595 length = (
unsigned int)line.length();
596 for(
unsigned int i=0; i<length; i++){
597 if(
int(line[i]) == sepValue ){
598 vec.push_back( columnString );
600 }
else columnString += line[i];
604 vec.push_back( columnString );
607 if( columnCounter != vec.size() ){
609 warningLog <<
"parseFile(...) - Found inconsistent column size in row " << rowCounter;
610 warningLog <<
" ColumnSize: " << columnCounter <<
" LastColumnSize: " << vec.size() << std::endl;
615 if( vec.size() >= 1 ){
616 size_t K = vec.size()-1;
617 size_t foundA = vec[ K ].find(
'\n');
618 size_t foundB = vec[K ].find(
'\r');
619 if( foundA != std::string::npos || foundB != std::string::npos ){
620 vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
625 for(
unsigned int j=0; j<columnCounter; j++){
626 dataPtr[rowCounter*
cols+j] = grt_from_str< Float >( vec[j] );
638 return save( filename );
642 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)
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)