21 #include "MatrixDouble.h"
26 warningLog.setProceedingText(
"[WARNING MatrixDouble]");
27 errorLog.setProceedingText(
"[ERROR MatrixDouble]");
34 warningLog.setProceedingText(
"[WARNING MatrixDouble]");
35 errorLog.setProceedingText(
"[ERROR MatrixDouble]");
37 if( rows > 0 && cols > 0 ){
43 warningLog.setProceedingText(
"[WARNING MatrixDouble]");
44 errorLog.setProceedingText(
"[ERROR MatrixDouble]");
51 warningLog.setProceedingText(
"[WARNING MatrixDouble]");
52 errorLog.setProceedingText(
"[ERROR MatrixDouble]");
82 if( rhs.size() == 0 )
return *
this;
84 unsigned int M = (
unsigned int)rhs.size();
85 unsigned int N = (
unsigned int)rhs[0].
size();
88 for(
unsigned int i=0; i<M; i++){
89 if( rhs[i].
size() != N ){
93 for(
unsigned int j=0; j<N; j++){
109 if(
dataPtr == NULL )
return false;
112 std::cout << title << endl;
114 for(
unsigned int i=0; i<
rows; i++){
115 for(
unsigned int j=0; j<
cols; j++){
116 std::cout <<
dataPtr[i*cols+j] <<
"\t";
118 std::cout << std::endl;
126 if(
dataPtr == NULL )
return false;
129 for(
unsigned int i=0; i<
rows; i++){
130 for(
unsigned int j=0; j<
cols; j++){
131 temp[j][i] =
dataPtr[i*cols+j];
142 if(
dataPtr == NULL )
return false;
146 return scale(ranges,minTarget,maxTarget);
150 if(
dataPtr == NULL )
return false;
152 if( ranges.size() !=
cols ){
156 unsigned int i,j = 0;
157 for(i=0; i<
rows; i++){
158 for(j=0; j<
cols; j++){
166 if(
dataPtr == NULL )
return false;
169 double mean,
std = 0;
170 for(i=0; i<
rows; i++){
175 for(j=0; j<
cols; j++){
181 for(j=0; j<
cols; j++){
185 std = sqrt( std + alpha );
188 for(j=0; j<
cols; j++){
201 double *d_p = &(d[0][0]);
213 const unsigned int M =
rows;
214 const unsigned int N =
cols;
215 const unsigned int K = (
unsigned int)b.size();
218 warningLog <<
"multiple(vector b) - The size of b (" << b.size() <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
219 return VectorDouble();
223 const double *pb = &b[0];
226 unsigned int i,j = 0;
227 for(i=0; i<
rows; i++){
229 for(j=0; j<
cols; j++){
230 pc[i] +=
dataPtr[i*cols+j]*pb[j];
239 const unsigned int M =
rows;
240 const unsigned int N =
cols;
245 errorLog <<
"multiple(MatrixDouble b) - The number of rows in b (" << K <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
253 unsigned int i,j,k = 0;
274 errorLog <<
"multiple(const MatrixDouble &a,const MatrixDouble &b,const bool aTranspose) - The number of rows in a (" << K <<
") does not match the number of columns in matrix b (" << N <<
")" << std::endl;
279 errorLog <<
"multiple(const MatrixDouble &b,const MatrixDouble &c,const bool bTranspose) - Failed to resize matrix!" << endl;
283 unsigned int i, j, k = 0;
319 errorLog <<
"add(const MatrixDouble &b) - Failed to add matrix! The rows do not match!" << endl;
324 errorLog <<
"add(const MatrixDouble &b) - Failed to add matrix! The rows do not match!" << endl;
331 const double *p_b = &(b[0][0]);
346 errorLog <<
"add(const MatrixDouble &a,const MatrixDouble &b) - Failed to add matrix! The rows do not match!";
347 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << endl;
352 errorLog <<
"add(const MatrixDouble &a,const MatrixDouble &b) - Failed to add matrix! The columns do not match!";
353 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << endl;
377 errorLog <<
"subtract(const MatrixDouble &b) - Failed to add matrix! The rows do not match!" << endl;
378 errorLog <<
" rows: " <<
rows <<
" b rows: " << b.
getNumRows() << endl;
383 errorLog <<
"subtract(const MatrixDouble &b) - Failed to add matrix! The rows do not match!" << endl;
384 errorLog <<
" cols: " <<
cols <<
" b cols: " << b.
getNumCols() << endl;
393 for(i=0; i<
rows; i++){
394 for(j=0; j<
cols; j++){
408 errorLog <<
"subtract(const MatrixDouble &a,const MatrixDouble &b) - Failed to add matrix! The rows do not match!";
409 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << endl;
414 errorLog <<
"subtract(const MatrixDouble &a,const MatrixDouble &b) - Failed to add matrix! The columns do not match!";
415 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << endl;
437 double minValue = 99e+99;
438 for(
unsigned int i=0; i<
rows*
cols; i++){
445 double maxValue = 99e-99;
446 for(
unsigned int i=0; i<
rows*
cols; i++){
454 VectorDouble mean(
cols);
456 for(
unsigned int c=0; c<
cols; c++){
458 for(
unsigned int r=0; r<
rows; r++){
461 mean[c] /= double( rows );
470 VectorDouble stdDev(
cols,0);
472 for(
unsigned int j=0; j<
cols; j++){
473 for(
unsigned int i=0; i<
rows; i++){
474 stdDev[j] += (
dataPtr[i*cols+j]-mean[j])*(
dataPtr[i*cols+j]-mean[j]);
476 stdDev[j] = sqrt( stdDev[j] /
double(rows-1) );
483 vector<double> mean =
getMean();
486 for(
unsigned int j=0; j<
cols; j++){
487 for(
unsigned int k=0; k<
cols; k++){
489 for(
unsigned int i=0; i<
rows; i++){
490 covMatrix[j][k] += (
dataPtr[i*cols+j]-mean[j]) * (
dataPtr[i*cols+k]-mean[k]);
492 covMatrix[j][k] /= double(rows-1);
501 if(
rows == 0 )
return std::vector< MinMax >();
503 vector< MinMax > ranges(
cols);
504 for(
unsigned int i=0; i<
rows; i++){
505 for(
unsigned int j=0; j<
cols; j++){
506 ranges[j].updateMinMax(
dataPtr[i*cols+j] );
515 for(
unsigned int i=0; i < K; i++) {
524 file.open(filename.c_str(), std::ios::out);
526 if( !file.is_open() ){
530 for(UINT i=0; i<
rows; i++){
531 for(UINT j=0; j<
cols; j++){
532 file <<
dataPtr[i*cols+j] << (j<cols-1 ?
"," :
"\n");
546 ifstream file( filename.c_str(), ifstream::in );
547 if ( !file.is_open() ){
548 warningLog <<
"parseFile(...) - Failed to open file: " << filename << endl;
552 vector< string > vec;
553 vector< double > row;
555 string columnString =
"";
556 const int sepValue = seperator;
557 unsigned int rowCounter = 0;
558 unsigned int columnCounter = 0;
559 unsigned int length = 0;
562 if( !getline(file,line) ){
563 warningLog <<
"parseFile(...) - Failed to read first row!" << endl;
567 length = (
unsigned int)line.length();
568 for(
unsigned int i=0; i<length; i++){
569 if(
int(line[i]) == sepValue ){
577 while ( getline(file,line) ){
582 if( !
resize(rowCounter, columnCounter) ){
583 warningLog <<
"parseFile(...) - Failed to resize memory!" << endl;
589 file.open( filename.c_str(), ifstream::in );
593 while ( getline(file,line) )
598 length = (
unsigned int)line.length();
599 for(
unsigned int i=0; i<length; i++){
600 if(
int(line[i]) == sepValue ){
601 vec.push_back( columnString );
603 }
else columnString += line[i];
607 vec.push_back( columnString );
610 if( columnCounter != vec.size() ){
612 warningLog <<
"parseFile(...) - Found inconsistent column size in row " << rowCounter;
613 warningLog <<
" ColumnSize: " << columnCounter <<
" LastColumnSize: " << vec.size() << endl;
618 if( vec.size() >= 1 ){
619 size_t K = vec.size()-1;
620 size_t foundA = vec[ K ].find(
'\n');
621 size_t foundB = vec[K ].find(
'\r');
622 if( foundA != std::string::npos || foundB != std::string::npos ){
623 vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
628 for(
unsigned int j=0; j<columnCounter; j++){
629 dataPtr[rowCounter*
cols+j] = stringToDouble(vec[j]);
641 return save( filename );
645 return load( filename, seperator );
MatrixDouble & operator=(const MatrixDouble &rhs)
static double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
bool subtract(const MatrixDouble &b)
unsigned int getNumCols() const
bool load(const string &filename, const char seperator= ',')
double * dataPtr
A pointer to the raw data.
T ** getDataPointer() const
bool scale(const double minTarget, const double maxTarget)
bool loadFromCSVFile(const string &filename, const char seperator= ',')
VectorDouble getStdDev() const
bool save(const string &filename) const
bool saveToCSVFile(const string &filename) const
virtual bool copy(const Matrix< double > &rhs)
MatrixDouble multiple(const double value) const
MatrixDouble getCovarianceMatrix() const
bool znorm(const double alpha=0.001)
unsigned int size
Stores rows * cols.
bool add(const MatrixDouble &b)
double ** rowPtr
A pointer to each row in the data.
unsigned int getNumRows() const
double getMinValue() const
double getMaxValue() const
VectorDouble getMean() const
unsigned int rows
The number of rows in the Matrix.
std::vector< MinMax > getRanges() const
unsigned int cols
The number of columns in the Matrix.
virtual bool resize(const unsigned int r, const unsigned int c)
bool print(const string title="") const