1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| #ifndef MATRIX_H #define MATRIX_H #include <iostream> using namespace std; double matE[4][4]={{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}}; class Matrix{ private: double mat[4][4]; public: Matrix(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ mat[i][j]=0; } } }; Matrix(const Matrix &m){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ mat[i][j]=m.mat[i][j]; } } }; Matrix(double (&m)[4][4]){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ mat[i][j]=m[i][j]; } } };
Matrix operator+(const Matrix &m){ Matrix ansM; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ ansM.mat[i][j]=mat[i][j]+m.mat[i][j]; } } return ansM; }; Matrix operator-(const Matrix &m){ Matrix ansM; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ ansM.mat[i][j]=mat[i][j]-m.mat[i][j]; } } return ansM; }; Matrix operator*(const Matrix &m){ Matrix ansM; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ for(int k=0;k<4;k++){ ansM.mat[i][j]+=mat[i][k]*m.mat[k][j]; } } } return ansM; }; Matrix operator^(int n){ Matrix ansM(matE); bool ifInverse=false; if(n<0){ n=-n; ifInverse=true; } for(int k=0;k<n;k++){ ansM=ansM*(*this); } if(ifInverse)ansM=ansM.Inverse(); return ansM; }; const Matrix &operator=(const Matrix &m){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ mat[i][j]=m.mat[i][j]; } } return *this; };
const double &operator()(int i,int j){ if(i<0||j<0||i>=4||j>=4){ cout<<"Wrong index!"<<endl; } return mat[i][j]; };
friend istream & operator>>(istream &is,Matrix &m); friend ostream & operator<<(ostream &os,const Matrix &m);
void Swap(double &a,double &b){ double tmp=a; a=b; b=tmp; } void SwapRow(int a,int b){ for(int i=0;i<4;i++){ Swap(mat[a][i],mat[b][i]); } } void LineMultiK(int a,double k){ for(int i=0;i<4;i++)mat[a][i]*=k; }
Matrix Inverse(){ Matrix tmpM(mat); Matrix E(matE); for(int i=0;i<4;i++){ int j=i; while(mat[i][j]==0&&j<4)j++; if(j==4){ cout<<"this matrix has no inverse!"<<endl; return *this; } else if(i!=j)tmpM.SwapRow(i,j),E.SwapRow(i,j); E.LineMultiK(i,1.0/tmpM(i,i)); tmpM.LineMultiK(i,1.0/tmpM(i,i)); for(int k=0;k<4;k++){ if(k==i)continue; for(int t=i+1;t<4;t++){ tmpM.mat[k][t]-=tmpM(i,t)*tmpM(k,i); } for(int t=0;t<4;t++){ E.mat[k][t]-=E(i,t)*tmpM(k,i); } tmpM.mat[k][i]=0; } } return E; }
Matrix Transpose(){ Matrix ansM; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ ansM.mat[i][j]=mat[j][i]; } } return ansM; }
Matrix Determinant(){ Matrix ansM(mat); for(int i=0;i<4;i++){ int maxi=i; for(int j=i;j<4;j++){ if(ansM(maxi,i)<ansM(j,i))maxi=j; } if(i!=maxi)ansM.SwapRow(i,maxi); if(ansM.mat[i][i]==0)continue; for(int k=i+1;k<4;k++){ for(int t=i+1;t<4;t++){ ansM.mat[k][t]-=ansM(i,t)*ansM(k,i)/ansM(i,i); } ansM.mat[k][i]=0; } } return ansM; } };
istream & operator>>(istream &is,Matrix &m){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ is>>m.mat[i][j]; } } return is; };
ostream & operator<<(ostream &os,const Matrix &m){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ os<<m.mat[i][j]<<" "; } os<<endl; } cout<<endl; return os; }; #endif
|