21 #include "MatrixFloat.h"
26 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
27 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
34 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
35 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
37 if( rows > 0 && cols > 0 ){
43 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
44 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
51 warningLog.setProceedingText(
"[WARNING MatrixFloat]");
52 errorLog.setProceedingText(
"[ERROR MatrixFloat]");
82 if( rhs.size() == 0 )
return *
this;
85 unsigned int N = (
unsigned int)rhs[0].
getSize();
88 for(
unsigned int i=0; i<M; i++){
89 if( rhs[i].
size() != N ){
93 for(
unsigned int j=0; j<N; j++){
103 if(
dataPtr == NULL )
return false;
106 std::cout << title << std::endl;
108 for(
unsigned int i=0; i<
rows; i++){
109 for(
unsigned int j=0; j<
cols; j++){
110 std::cout <<
dataPtr[i*cols+j] <<
"\t";
112 std::cout << std::endl;
120 if(
dataPtr == NULL )
return false;
123 for(
unsigned int i=0; i<
rows; i++){
124 for(
unsigned int j=0; j<
cols; j++){
125 temp[j][i] =
dataPtr[i*cols+j];
136 if(
dataPtr == NULL )
return false;
140 return scale(ranges,minTarget,maxTarget);
144 if(
dataPtr == NULL )
return false;
146 if( ranges.size() !=
cols ){
150 unsigned int i,j = 0;
151 for(i=0; i<
rows; i++){
152 for(j=0; j<
cols; j++){
153 dataPtr[i*cols+j] = grt_scale(
dataPtr[i*cols+j],ranges[j].minValue,ranges[j].maxValue,minTarget,maxTarget);
160 if(
dataPtr == NULL )
return false;
164 for(i=0; i<
rows; i++){
169 for(j=0; j<
cols; j++){
175 for(j=0; j<
cols; j++){
179 std = sqrt( std + alpha );
182 for(j=0; j<
cols; j++){
195 Float *d_p = &(d[0][0]);
207 const unsigned int M =
rows;
208 const unsigned int N =
cols;
209 const unsigned int K = (
unsigned int)b.size();
212 warningLog <<
"multiple(vector b) - The size of b (" << b.size() <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
217 const Float *pb = &b[0];
220 unsigned int i,j = 0;
221 for(i=0; i<
rows; i++){
223 for(j=0; j<
cols; j++){
224 pc[i] +=
dataPtr[i*cols+j]*pb[j];
233 const unsigned int M =
rows;
234 const unsigned int N =
cols;
239 errorLog <<
"multiple(MatrixFloat b) - The number of rows in b (" << K <<
") does not match the number of columns in this matrix (" << N <<
")" << std::endl;
247 unsigned int i,j,k = 0;
268 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;
273 errorLog <<
"multiple(const MatrixFloat &b,const MatrixFloat &c,const bool bTranspose) - Failed to resize matrix!" << std::endl;
277 unsigned int i, j, k = 0;
313 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
318 errorLog <<
"add(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
325 const Float *p_b = &(b[0][0]);
340 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
341 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
346 errorLog <<
"add(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
347 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
359 const unsigned int size = M*N;
360 for(i=0; i<
size; i++){
370 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
371 errorLog <<
" rows: " <<
rows <<
" b rows: " << b.
getNumRows() << std::endl;
376 errorLog <<
"subtract(const MatrixFloat &b) - Failed to add matrix! The rows do not match!" << std::endl;
377 errorLog <<
" cols: " <<
cols <<
" b cols: " << b.
getNumCols() << std::endl;
387 for(i=0; i<
size; i++){
400 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The rows do not match!";
401 errorLog <<
" a rows: " << M <<
" b rows: " << b.
getNumRows() << std::endl;
406 errorLog <<
"subtract(const MatrixFloat &a,const MatrixFloat &b) - Failed to add matrix! The columns do not match!";
407 errorLog <<
" a cols: " << N <<
" b cols: " << b.
getNumCols() << std::endl;
429 Float minValue = 99e+99;
430 for(
unsigned int i=0; i<
rows*
cols; i++){
437 Float maxValue = 99e-99;
438 for(
unsigned int i=0; i<
rows*
cols; i++){
448 for(
unsigned int c=0; c<
cols; c++){
450 for(
unsigned int r=0; r<
rows; r++){
453 mean[c] /= Float( rows );
464 for(
unsigned int j=0; j<
cols; j++){
465 for(
unsigned int i=0; i<
rows; i++){
466 stdDev[j] += (
dataPtr[i*cols+j]-mean[j])*(
dataPtr[i*cols+j]-mean[j]);
468 stdDev[j] = sqrt( stdDev[j] / Float(rows-1) );
478 for(
unsigned int j=0; j<
cols; j++){
479 for(
unsigned int k=0; k<
cols; k++){
481 for(
unsigned int i=0; i<
rows; i++){
482 covMatrix[j][k] += (
dataPtr[i*cols+j]-mean[j]) * (
dataPtr[i*cols+k]-mean[k]);
484 covMatrix[j][k] /= Float(rows-1);
496 for(
unsigned int i=0; i<
rows; i++){
497 for(
unsigned int j=0; j<
cols; j++){
498 ranges[j].updateMinMax(
dataPtr[i*cols+j] );
507 for(
unsigned int i=0; i < K; i++) {
516 file.open(filename.c_str(), std::ios::out);
518 if( !file.is_open() ){
522 for(UINT i=0; i<
rows; i++){
523 for(UINT j=0; j<
cols; j++){
524 file <<
dataPtr[i*cols+j] << (j<cols-1 ?
"," :
"\n");
538 std::ifstream file( filename.c_str(), std::ifstream::in );
539 if ( !file.is_open() ){
540 warningLog <<
"parseFile(...) - Failed to open file: " << filename << std::endl;
547 std::string columnString =
"";
548 const int sepValue = seperator;
549 unsigned int rowCounter = 0;
550 unsigned int columnCounter = 0;
551 unsigned int length = 0;
554 if( !getline(file,line) ){
555 warningLog <<
"parseFile(...) - Failed to read first row!" << std::endl;
559 length = (
unsigned int)line.length();
560 for(
unsigned int i=0; i<length; i++){
561 if(
int(line[i]) == sepValue ){
567 std::cout <<
"counting rows..." << std::endl;
571 while ( getline(file,line) ){
575 std::cout <<
"matrix size: " << rowCounter <<
" " << columnCounter << std::endl;
578 if( !
resize(rowCounter, columnCounter) ){
579 warningLog <<
"parseFile(...) - Failed to resize memory!" << std::endl;
585 file.open( filename.c_str(), std::ifstream::in );
589 while ( getline(file,line) )
594 length = (
unsigned int)line.length();
595 for(
unsigned int i=0; i<length; i++){
596 if(
int(line[i]) == sepValue ){
597 vec.push_back( columnString );
599 }
else columnString += line[i];
603 vec.push_back( columnString );
606 if( columnCounter != vec.size() ){
608 warningLog <<
"parseFile(...) - Found inconsistent column size in row " << rowCounter;
609 warningLog <<
" ColumnSize: " << columnCounter <<
" LastColumnSize: " << vec.size() << std::endl;
614 if( vec.size() >= 1 ){
615 size_t K = vec.size()-1;
616 size_t foundA = vec[ K ].find(
'\n');
617 size_t foundB = vec[K ].find(
'\r');
618 if( foundA != std::string::npos || foundB != std::string::npos ){
619 vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
624 for(
unsigned int j=0; j<columnCounter; j++){
625 dataPtr[rowCounter*
cols+j] = grt_from_str< Float >( vec[j] );
637 return save( filename );
641 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)
unsigned int getSize() const
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)