mber_of_column != this->Number_of_column || A.Number_of_row != this->Number_of_row){
for(int i = 0; i < this->Number_of_row; i++){
delete[] p_Matrix[i];
}
delete[] p_Matrix;
p_Matrix = new double*[A.Number_of_row];
for( int i = 0; i < A.Number_of_row; i++){
p_Matrix[i] = new double[A.Number_of_column];
}
this->Number_of_row = A.Number_of_row;
this->Number_of_column = A.Number_of_column;
for(int i = 0; i < this->Number_of_row; i++){
for(int j = 0; j < this->Number_of_column; j++){
this->p_Matrix[i][j] = A.p_Matrix[i][j];
}
}
}
else{
for(int i = 0; i < this->Number_of_row; i++){
for(int j = 0; j < this->Number_of_column; j++){
this->p_Matrix[i][j] = A.p_Matrix[i][j];
}
}
}
return *this;
}
//重载减法
Matrix operator- (const Matrix &A){
Matrix tempMatrix(A.Number_of_row, A.Number_of_column);
if (A.Number_of_column != this->Number_of_column || A.Number_of_row != this->Number_of_row){
cout << "Matrices are in different size!" << endl;
}
else{
for(int i = 0; i < this->Number_of_row; i++){
for(int j = 0; j < A.Number_of_column; j++){
tempMatrix.p_Matrix[i][j] = this->p_Matrix[i][j] - A.p_Matrix[i][j];
}
}
}
return tempMatrix;
}
//重载乘法
//数乘
Matrix operator*(double value){
Matrix tempMatrix(this->Number_of_row, this->Number_of_column);
for(int i = 0; i < this->Number_of_row; i++){
for(int j = 0; j < this->Number_of_column; j++){
tempMatrix.p_Matrix[i][j] = value*this->p_Matrix[i][j];
}
}
return tempMatrix;
}
friend Matrix operator*(double value, const Matrix &A){
Matrix tempMatrix(A.Number_of_row, A.Number_of_column);
for(int i = 0; i < A.Number_of_row; i++){
for(int j = 0; j < A.Number_of_column; j++){
tempMatrix.p_Matrix[i][j] = value*A.p_Matrix[i][j];
}
}
return tempMatrix;
}
//矩阵相乘
friend Matrix operator*(Matrix &A, Matrix &B){
Matrix tempMatrix(A.Number_of_row, B.Number_of_column);
if(A.Number_of_column != B.Number_of_row){
cout<<"Invalid!"<<endl;
}
else{
double s;
for(int i = 0; i < A.Number_of_row; i++){
for(int k = 0; k < A.Number_of_column; k++){
s=A.p_Matrix[i][k];
for(int j = 0; j < B.Number_of_column; j++){
tempMatrix.p_Matrix[i][j] += s*B.p_Matrix[k][j];
}
}
}
}
return tempMatrix;
}
到此已经实现了矩阵的一些基本功能,在接下来的博客中,我将完善其他矩阵的功能,并且逐步实现一些古老的数值代数算法。
|