001 #include <iostream>
002 #include <iomanip>
003 #include <cmath>
004
005 using namespace std;
006
007 #define DataType double
008
010 typedef struct {
011 int row,col;
012 DataType** mat;
超级管道
013 }Matrix;
014
015 /*初始化矩阵,即创建矩阵*/
016 int Init_Mat(Matrix* m) {
017 int i=0,j=0;
018 cout<<"请输入行、列数:";
019 cin>>m->row>>m->col;
020 double **pdata=m->mat=new DataType*[m->row];//申请行指针数组 021
022 if (NULL==pdata) {
023 cout<<"ERROR!\n";
024 exit(1);
025 }
026 cout<<"请按行优先输入矩阵:\n";
027 for (i=0;i<m->row;i++) {
028 pdata[i]=new DataType[m->col];//申请行
029
030 for (j=0;j<m->col;j++)
031 cin>>pdata[i][j];
032 }
033 return 0;
034 }
035
036 /*输出矩阵*/
037 void Out_Mat(Matrix* m) {
038 int i,j;
039 cout<<endl;
040 for (i=0;i<m->row;i++) {
041 for (j=0;j<m->col;j++) {
042 cout<<setiosflags(ios::fixed)<<setprecision(5)<<setw(12)<<(m->mat)[i][j];
043 }
044 cout<<endl;
045 }
046 }
047
048 /*矩阵乘法,t=m1*m2*/ 视讯系统
049 int Mul_Mat(Matrix* m1,Matrix* m2,Matrix* t) {
050 int i=0,j=0,p=0;
051 double sum=0;
052 if (m1->col != m2->row) {
053 cout<<"\n行、列数不匹配!";
054 exit(0);
055 }
056 t->row=m1->row;
057 t->col=m2->col;
058 t->mat=new DataType*[m1->row];
059 if (NULL==t->mat) {
060 cout<<"ERROR!\n";
061 exit(0);
062 }
063 for (i=0;i<t->row;i++) {
064 t->mat[i]=new DataType[t->col];
065 for (j=0;j<t->col;j++) {
066 for (t->mat[i][j]=0,p=0;p<m1->col;p++) {
067 t->mat[i][j]+=m1->mat[i][p]*m2->mat[p][j];
068 }
069 }
070 }
071 return 0;
072 }
073
074 /*矩阵求逆——全选主元高斯-约当(Gauss-Jordan)法*/ 075 int Inv_Mat(Matrix *m,Matrix *t) {
076 int i,j,n,*is,*js,k;
温度远程监控077 DataType d,p;
078 if(m->row!=m->col) {
咖啡加工079 cout<<"ERROR! 必须是方阵才能求逆!\n";
080 exit(1);
081 }
082 t->mat=new DataType*[m->row];//申请行指针数组
083
084 if(NULL==t->mat){
085 cout<<"ERROR! 申请内存出错!\n";
086 exit(1);
087 }
088 for (i=0;i<m->row;i++) {
089 t->mat[i]=new DataType[m->col];//申请行
090
091 for (j=0;j<m->col;j++)
092
t->mat[i][j]=m->mat[i][j];
093 }
094 n=m->row;
095 t->row=m->row;
096 t->col=m->col;
097 is=new int[n];
098 js=new int[n];
099 if (NULL==is || NULL==js){
100 cout<<"ERROR! 申请内存出错!\n";
101 return 1;
102 }
103 for (k=0;k<=n-1;k++){ //全选主元
104
105 d=0.000;
106 for (i=k;i<=n-1;i++){
107 for (j=k;j<=n-1;j++){
108 p=fabs(t->mat[i][j]);
109 if (p>d){
110 d=p;
111 is[k]=i;
112 js[k]=j;
113 }
114 }
115 }
116 if(1.0==d+1.0){
117 delete []is;
118 delete []js;
119 printf ("ERROR ! 矩阵求逆出错!");
120 return 1;
121 }
122 if(is[k]!=k){ /*行交换*/
123 for (j=0;j<=n-1;j++){
124 p=t->mat[k][j];
125 t->mat[k][j]=t->mat[is[k]][j];
126 t->mat[is[k]][j]=p;
127 }
128 }
129 if(js[k] != k) { /*列交换*/
130 for (i=0;i<=n-1;i++) {
粽子机
131 p=t->mat[i][k];
132 t->mat[i][k]=t->mat[i][js[k]];
133 t->mat[i][js[k]]=p;
134 }
135 }
136 t->mat[k][k]=1/t->mat[k][k];
137 for (j=0;j<=n-1;j++){
138 if (j != k){
139 t->mat[k][j]=t->mat[k][j]*t->mat[k][k];
筋膜放进B里面
140 }
141 }
142 for (i=0;i<=n-1;i++){
143 if(i!=k){
144 for (j=0;j<=n-1;j++){
145 if(j!=k){
146 t->mat[i][j]=t->mat[i][j]-t->mat[i][k]*t->mat[k][j];
147 }
148 }
149 }
150 }
151 for (i=0;i<=n-1;i++){
152 if (i!=k){
153 t->mat[i][k]=-t->mat[i][k]*t->mat[k][k];
154 }
155 }
156 }
157
158 for (k=n-1;k>=0;k--){
159 if (js[k]!=k){
160 for (j=0;j<=n-1;j++){
161 p=t->mat[k][j];
162 t->mat[k][j]=t->mat[js[k]][j];
163 t->mat[js[k]][j]=p;
164 }
165 }
166 if (is[k] != k){
167 for (i=0;i<=n-1;i++){
168 p=t->mat[i][k];
169 t->mat[i][k]=t->mat[i][is[k]];
170 t->mat[i][is[k]]=p;
171 }
172 }
173 }
174 delete []is;
175 delete []js;
176 return
0;
177 }
178
179 /*求矩阵行列式值——全选主元高斯消去法*/
180 DataType Det_Mat(Matrix *m,Matrix *t) {
181 int i,j,k,is,js,n;
182 DataType f=1.0,det=1.0,q,d;
183 if(m->row!=m->col) {
184 cout<<"ERROR! 必须是方阵才能求行列式值!\n";
185 exit(1);
186 }
187 t->mat=new DataType*[m->row];//申请行指针数组
188
189 if(NULL==t->mat){
190 cout<<"ERROR! 申请内存出错!\n";
191 exit(1);
192 }
193 for (i=0;i<m->row;i++) {
194 t->mat[i]=new DataType[m->col];//申请行
195
196 for (j=0;j<m->col;j++)
197 t->mat[i][j]=m->mat[i][j];
198 }
199 n=m->row;
200 t->row=m->row;
201 t->col=m->col;
202
203 for(k=0;k<=n-2;k++){
204 q=0.0;
205 for(i=k;i<=n-1;i++){
206 for(j=k;j<=n-1;j++){
207 d=fabs(t->mat[i][j]);
208 if(d>q){
209 q=d;
210 is=i;
211 js=j;
212 }
213 }
214 if(1.0==q+1.0){
215 det=0.0;
216 return(det);
217 }
218 if(is!=k){
219 f=-f;
220 for(j=k;j<=n-1;j++){
221 d=t->mat[k][j];
222 t->mat[k][j]=t->mat[is][j];
223 t->mat[is][j]=d;
224 }
225 }
226 if(js!=k){
227 f=-f;
228 for(i=k;i<=n-1;i++){
229 d=t->mat[i][js];
230 t->mat[i][js]=t->mat[i][k];
231 t->mat[i][k]=d;
232 }
233 }
234 det=det*t->mat[k][k];
235 for(i=k+1;i<=n-1;i++){
236 d=t->mat[i][k]/t->mat[k][k];
237 for(j=k+1;j<=n-1;j++){
238 t->mat[i][j]-=d*t->mat[k][j];
239 }
240 }
241 }
242 }
243 det=f*det*t->mat[n-1][n-1];
244 return (det);
245 }
246
247
248 int main() {
249 Matrix m1,ans;
250 Init_Mat(&m1);
251 Out_Mat(&m1);
252 cout<<Det_Mat(&m1,&ans)<<endl;
253 Inv_Mat(&m1,&ans);
254 Out_Mat(&ans);
255 return 0;
256 }