如何为我的非静态转置函数实现 matrix.Transpose()?
How do I implement matrix.Transpose() for my non-static Transpose function?
我在 test.cpp 文件中得到了以下代码来实现:
cout << "Case 2: the 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 the code works
matrix.Transpose(); //How do I implement this?
cout << "The transposed version = " << endl;
cout << matrix << endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
Matrix::Toeplitz(column, 4, row, 3) 的工作方式如下:
Matrix Matrix::Toeplitz(const double* column, const int noOfRows, const double* row, const int noOfColumns){
Matrix outT(column, noOfRows, row, noOfColumns);
return outT;
}
那么我将如何实施 matrix.Transpose()?到目前为止我的代码如下:
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);
}
这对cout<<matrix<<endl;
没有影响
我在想 Matrix outT(newcol, noOfColumns, newrow, noOfRows);
应该在实现 matrix.Transpose 时向矩阵对象提供新信息(即切换列和行数组),但它一直没有工作。
这是实现 matrix.Transpose() 的正确格式 Matrix& Matrix::Transpose () 吗?
Matrix::Transpose
不能 return 引用本地声明的对象。这会导致很多问题。
见C++ Returning reference to local variable。
必须return复制(然后,函数可以是const
,因为当前对象没有被修改):
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);
}
那么,你这样使用:
Matrix transposed = matrix.Transpose(); // does not modify matrix object
cout << "The transposed version = " << endl;
cout << transposed << endl;
如果returning Matrix&
,你需要让你的方法转置当前对象和return它(return *this
),只对帮助调用者链接有用许多运算符(例如 m.Transpose().Transpose()
)。
那么,可以(未测试):
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 by copying from backup content
for ( unsigned int line = 0; line < noOfRows ; ++line )
{
for ( unsigned int col = line; col < noOfColumns; ++col )
{
data[line * noOfColumns + col] = backupData[col * noOfRows + line];
}
}
delete [] backupData;
return *this;
}
那么,你这样使用:
matrix.Transpose(); // modifies matrix object
cout << "The transposed version = " << endl;
cout << transposed << endl;
我在 test.cpp 文件中得到了以下代码来实现:
cout << "Case 2: the 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 the code works
matrix.Transpose(); //How do I implement this?
cout << "The transposed version = " << endl;
cout << matrix << endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
Matrix::Toeplitz(column, 4, row, 3) 的工作方式如下:
Matrix Matrix::Toeplitz(const double* column, const int noOfRows, const double* row, const int noOfColumns){
Matrix outT(column, noOfRows, row, noOfColumns);
return outT;
}
那么我将如何实施 matrix.Transpose()?到目前为止我的代码如下:
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);
}
这对cout<<matrix<<endl;
我在想 Matrix outT(newcol, noOfColumns, newrow, noOfRows);
应该在实现 matrix.Transpose 时向矩阵对象提供新信息(即切换列和行数组),但它一直没有工作。
这是实现 matrix.Transpose() 的正确格式 Matrix& Matrix::Transpose () 吗?
Matrix::Transpose
不能 return 引用本地声明的对象。这会导致很多问题。
见C++ Returning reference to local variable。
必须return复制(然后,函数可以是const
,因为当前对象没有被修改):
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);
}
那么,你这样使用:
Matrix transposed = matrix.Transpose(); // does not modify matrix object
cout << "The transposed version = " << endl;
cout << transposed << endl;
如果returning Matrix&
,你需要让你的方法转置当前对象和return它(return *this
),只对帮助调用者链接有用许多运算符(例如 m.Transpose().Transpose()
)。
那么,可以(未测试):
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 by copying from backup content
for ( unsigned int line = 0; line < noOfRows ; ++line )
{
for ( unsigned int col = line; col < noOfColumns; ++col )
{
data[line * noOfColumns + col] = backupData[col * noOfRows + line];
}
}
delete [] backupData;
return *this;
}
那么,你这样使用:
matrix.Transpose(); // modifies matrix object
cout << "The transposed version = " << endl;
cout << transposed << endl;