c++ - How do I implement matrix.Transpose() for my non-static Transpose function? -


i have been given following code in test.cpp file implement:

cout << "case 2: non-static transpose function" << endl; {     double column[4] = {2, 1, 0, -1};     double row[3] = {2, 0, -1};     matrix matrix = matrix::toeplitz(column, 4, row, 3);     cout << "the original matrix = " << endl;     cout << matrix << endl;  //this part of code works       matrix.transpose();  //how implement this?     cout << "the transposed version = " << endl;     cout << matrix << endl;     cout << "press key continue ..." << flush;     system("read");     cout << endl; } 

the way matrix::toeplitz(column, 4, row, 3) works follows:

matrix matrix::toeplitz(const double* column, const int noofrows, const double* row, const int noofcolumns){     matrix outt(column, noofrows, row, noofcolumns);     return outt; } 

so how implement matrix.transpose()? code far follows:

matrix& matrix::transpose () {  double newrow[noofrows]; for(int i=0; i<noofrows; i++){     int index = getindex(i,0);     newrow[i] = data[index]; }  double newcol[noofcolumns]; for(int i=0; i<noofcolumns; i++){     int index = getindex(0,i);     newcol[i] = data[index]; }  matrix outt(newcol, noofcolumns, newrow, noofrows); } 

this has no effect on cout<<matrix<<endl;

i thinking matrix outt(newcol, noofcolumns, newrow, noofrows); should give new information (i.e. switching column , row arrays) matrix object when implementing matrix.transpose hasn't been working.

is correct format matrix& matrix::transpose () implementing matrix.transpose()?

matrix::transpose can't return reference locally declared object. lead many problems.

see c++ returning reference local variable.

it must return copy (then, function can const, current object not being modified):

matrix matrix::transpose() const {     double newrow[noofrows];     for(int i=0; i<noofrows; i++){         int index = getindex(i,0);         newrow[i] = data[index];     }      double newcol[noofcolumns];     for(int i=0; i<noofcolumns; i++){         int index = getindex(0,i);         newcol[i] = data[index];     }      return matrix(newcol, noofcolumns, newrow, noofrows); } 

then, use way:

matrix transposed = matrix.transpose(); // not modify matrix object cout << "the transposed version = " << endl; cout << transposed << endl; 

if returning matrix&, need have method transpose current object , return (return *this), useful caller chain many operators (like doing m.transpose().transpose() instance).

then, (not tested):

matrix& matrix::transpose()  {     // backup old content     double* backupdata = new double[noofrows*noofcolumns];     memcpy( backupdata, data, sizeof(double)*noofrows*noofcolumns );      // change matrix geometry     int oldrowcount = noofrows;     noofrows = noofcolumns;     noofcolumns = oldrowcount ;      // transpose matrix copying backup content     ( unsigned int line = 0; line < noofrows ; ++line )     {         ( unsigned int col = line; col < noofcolumns; ++col )         {             data[line * noofcolumns + col] = backupdata[col * noofrows  + line];         }     }      delete [] backupdata;      return *this; } 

then, use way:

matrix.transpose(); // modifies matrix object cout << "the transposed version = " << endl; cout << transposed << endl; 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -