最近和张同学一起了用C++写了一个矩阵类,类中包括矩阵的求逆、相乘、转置类的实现并不难,其中网上找了一些资料,通过测试该类运行良好。希望能和大家一起分享,如有错误或更好的建议,请联系我! 测试代码 #pragma once #include <iostream> #include <cmath> using namespace std; class Matrix { public: Matrix(int i,int j); void SetValue(int i, int j,double valuee); double GetValue(int i , int j ); void ShowData(); //矩阵求逆 Matrix* OppMatrix(); //矩阵相乘 Matrix* MultiMatrix(Matrix* matrix); //矩阵转置 Matrix* TransposeMatrix(); ~Matrix(void); private: Matrix* temp; int row; int col; double** data; }; #include "Matrix.h" Matrix::Matrix(int r,int c) { temp = NULL; row = r, col = c; data = new double *[row]; //分配一个 T *[r]类型的数组给data for(int i = 0; i < row; i++){ data[i] = new double[col]; //给data下的每个指针分配内存, 类型为 T [c] } } Matrix::~Matrix(void) { int i; if(temp != NULL){delete temp; } for(i = 0; i < row; i++) delete data[i]; delete data; } void Matrix::SetValue(int i, int j, double values) { if(i < 0 || i >= row || j < 0 || j >= col) { printf("Invalid row/column number./n"); return; }else{ data[i][j] = values; } } double Matrix::GetValue(int i , int j ) { if(i < 0 || i >= row || j < 0 || j >= col) { printf("Invalid row/column number./n"); //return; }else{ return data[i][j]; } } void Matrix::ShowData() { for (int i = 0;i < row ;i++) { for (int j = 0; j < col; j++) { cout << data[i][j] << "/t"; } cout << endl; } } Matrix* Matrix::MultiMatrix(Matrix* a) { Matrix* result = NULL; int i, j, k; double tmp; if(col != a->row){ printf("Unmatch Matrix!/n"); return this; } if(temp != NULL) delete temp; temp = new Matrix(row, a->col); for(i = 0; i < row; i++){ for(j = 0; j < a->col; j++){ tmp = 0; for(k = 0; k < a->row; k++){ tmp += data[i][k] * (a->data[k][j]); } temp->data[i][j] = tmp; } } return temp; } Matrix* Matrix::TransposeMatrix() { if(temp != NULL) delete temp; temp = new Matrix(row, col); int i,j; for(i=0;i<row;i++) for(j=0;j<col;j++) temp->data[i][j]=data[j][i]; return temp; } Matrix* Matrix::OppMatrix() { //int DinV(double A[N][N],int n) if(temp != NULL) delete temp; temp = new Matrix(row, col); for(int i=0;i<row;i++) for(int j=0;j<col;j++) temp->data[i][j]=data[i][j]; double** A = temp->data; int n = col; int i,j,k, N = col; double d; int *JS = new int[N]; int *IS = new int[N]; for (k=0;k<n;k++) { d=0; for (i=k;i<n;i++) for (j=k;j<n;j++) { if (fabs(A[i][j])>d) { d=fabs(A[i][j]); IS[k]=i; JS[k]=j; } } if (d+1.0==1.0) return NULL; if (IS[k]!=k) for (j=0;j<n;j++) swap(A[k][j],A[IS[k]][j]); if (JS[k]!=k) for (i=0;i<n;i++) swap(A[i][k],A[i][JS[k]]); A[k][k]=1/A[k][k]; for (j=0;j<n;j++) if (j!=k) A[k][j]=A[k][j]*A[k][k]; for (i=0;i<n;i++) if (i!=k) for (j=0;j<n;j++) if (j!=k) A[i][j]=A[i][j]-A[i][k]*A[k][j]; for (i=0;i<n;i++) if (i!=k) A[i][k]=-A[i][k]*A[k][k]; } for (k=n-1;k>=0;k--) { for (j=0;j<n;j++) if (JS[k]!=k) swap(A[k][j],A[JS[k]][j]); for (i=0;i<n;i++) if (IS[k]!=k) swap(A[i][k],A[i][IS[k]]); } //for (i=0;i<n;i++) //{ // for (j=0;j<n;j++) // printf(" %1.4f",A[i][j]); // puts(""); //} return temp; } #include <iostream> #include "Matrix.h" using namespace std; int main() { Matrix* matrix = new Matrix(2,2); matrix->SetValue(0,0,1.2); matrix->SetValue(0,1,3.3); matrix->SetValue(1,0,5.5); matrix->SetValue(1,1,6.0); Matrix* a = new Matrix(2,2); a->SetValue(0,0,1.2); a->SetValue(0,1,3.3); a->SetValue(1,0,5.5); a->SetValue(1,1,6.0); matrix->ShowData(); Matrix* b = matrix->MultiMatrix(a); //乘法 cout<< "乘法"<< endl; b->ShowData(); Matrix* c = new Matrix(3,3); c->SetValue(0,0,1); c->SetValue(0,1,2); c->SetValue(0,2,3); c->SetValue(1,0,2); c->SetValue(1,1,2); c->SetValue(1,2,1); c->SetValue(2,0,3); c->SetValue(2,1,4); c->SetValue(2,2,3); //转置 cout<< "转置"<< endl; Matrix* e = c->TransposeMatrix(); e->ShowData(); Matrix* d = c->OppMatrix(); cout<< "求逆"<< endl; d->ShowData(); return 0; }