模板高斯消元
Posted greenty1208
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板高斯消元相关的知识,希望对你有一定的参考价值。
出处:http://www.cnblogs.com/autsky-jadek/
求逆矩阵
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const double EPS=0.00000001; 7 #define N 105 8 int n; 9 double B[N][N*2],A[N][N*2],C[N][N]; 10 bool guass_jordan() 11 { 12 memcpy(B,A,sizeof(A)); 13 for(int i=1;i<=n;++i){ 14 for(int j=n+1;j<=n*2;++j){ 15 if(i==j-n){ 16 B[i][j]=1.0; 17 } 18 else{ 19 B[i][j]=0.0; 20 } 21 } 22 } 23 for(int i=1;i<=n;++i){ 24 int pivot=i; 25 for(int j=i+1;j<=n;++j){ 26 if(fabs(B[j][i])>fabs(B[pivot][i])){ 27 pivot=j; 28 } 29 } 30 swap(B[i],B[pivot]); 31 if(fabs(B[i][i])<EPS){ 32 return 0; 33 } 34 for(int j=i+1;j<=n*2;++j){ 35 B[i][j]/=B[i][i]; 36 } 37 for(int j=1;j<=n;++j){ 38 if(i!=j){ 39 for(int k=i+1;k<=n*2;++k){ 40 B[j][k]-=B[j][i]*B[i][k]; 41 } 42 } 43 } 44 } 45 for(int i=1;i<=n;++i){ 46 for(int j=n+1;j<=n*2;++j){ 47 C[i][j-n]=B[i][j]; 48 } 49 } 50 } 51 int T; 52 int main() 53 { 54 // freopen("nijuzhen2.in","r",stdin); 55 // freopen("nijuzhen2.out","w",stdout); 56 char t[10]; 57 while(scanf("%d",&n)!=EOF){ 58 memset(A,0,sizeof(A)); 59 for(int i=1;i<=n;++i){ 60 for(int j=1;j<=n;++j){ 61 scanf("%lf",&A[i][j]); 62 } 63 } 64 if(guass_jordan()){ 65 for(int i=1;i<=n;++i){ 66 for(int j=1;j<=n;++j){ 67 sprintf(t,"%.3f",C[i][j]); 68 if(t[0]==‘-‘ && t[1]==‘0‘ && t[2]==‘.‘ && t[3]==‘0‘ && t[4]==‘0‘ && t[5]==‘0‘){ 69 printf("0.000%c",j==n ? ‘ ‘ : ‘ ‘); 70 } 71 else{ 72 printf("%.3f%c",C[i][j],j==n ? ‘ ‘ : ‘ ‘); 73 } 74 } 75 } 76 } 77 else{ 78 puts("NO"); 79 } 80 } 81 return 0; 82 }
求矩阵秩
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const double EPS=0.00000001; 7 #define N 105 8 int n,m; 9 double B[N][N],A[N][N]; 10 int guass_jordan() 11 { 12 memcpy(B,A,sizeof(A)); 13 for(int i=1;i<=n;++i) 14 { 15 int pivot=i; 16 for(int j=i+1;j<=n;++j) 17 if(fabs(B[j][i])>fabs(B[pivot][i])) 18 pivot=j; 19 swap(B[i],B[pivot]); 20 if(fabs(B[i][i])<EPS){ 21 return i-1; 22 } 23 for(int j=i+1;j<=m;++j) 24 B[i][j]/=B[i][i]; 25 for(int j=1;j<=n;++j) 26 if(i!=j) 27 for(int k=i+1;k<=m;++k) 28 B[j][k]-=B[j][i]*B[i][k]; 29 } 30 return n; 31 } 32 int main() 33 { 34 while(scanf("%d%d",&n,&m)!=EOF){ 35 memset(A,0,sizeof(A)); 36 for(int i=1;i<=n;++i){ 37 for(int j=1;j<=m;++j){ 38 scanf("%lf",&A[i][j]); 39 } 40 } 41 printf("%d ",guass_jordan()); 42 } 43 return 0; 44 }
求行列式
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const double EPS=0.00000001; 7 #define N 105 8 int n; 9 double B[N][N],A[N][N]; 10 double guass_jordan() 11 { 12 double res=1.0; 13 memcpy(B,A,sizeof(A)); 14 for(int i=1;i<=n;++i) 15 { 16 int pivot=i; 17 for(int j=i+1;j<=n;++j) 18 if(fabs(B[j][i])>fabs(B[pivot][i])) 19 pivot=j; 20 swap(B[i],B[pivot]); 21 if(i!=pivot){ 22 res*=(-1.0); 23 } 24 if(fabs(B[i][i])<EPS){ 25 return 0.0; 26 } 27 for(int j=i+1;j<=n;++j) 28 B[i][j]/=B[i][i]; 29 res*=B[i][i]; 30 for(int j=1;j<=n;++j) 31 if(i!=j) 32 for(int k=i+1;k<=n;++k) 33 B[j][k]-=B[j][i]*B[i][k]; 34 } 35 return res; 36 } 37 int main() 38 { 39 // freopen("hanglieshi3.in","r",stdin); 40 // freopen("hanglieshi3.out","w",stdout); 41 char t[1000]; 42 while(scanf("%d",&n)!=EOF){ 43 memset(A,0,sizeof(A)); 44 for(int i=1;i<=n;++i){ 45 for(int j=1;j<=n;++j){ 46 scanf("%lf",&A[i][j]); 47 } 48 } 49 double ans=guass_jordan(); 50 sprintf(t,"%.3f",ans); 51 if(t[0]==‘-‘ && t[1]==‘0‘ && t[2]==‘.‘ && t[3]==‘0‘ && t[4]==‘0‘ && t[5]==‘0‘){ 52 puts("0.000"); 53 } 54 else{ 55 printf("%.3f ",ans); 56 } 57 } 58 return 0; 59 }
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using
namespace
std;
const
double
EPS=0.00000001;
#define N 105
int
n;
double
B[N][N*2],A[N][N*2],C[N][N];
bool
guass_jordan()
{
memcpy
(B,A,
sizeof
(A));
for
(
int
i=1;i<=n;++i){
for
(
int
j=n+1;j<=n*2;++j){
if
(i==j-n){
B[i][j]=1.0;
}
else
{
B[i][j]=0.0;
}
}
}
for
(
int
i=1;i<=n;++i){
int
pivot=i;
for
(
int
j=i+1;j<=n;++j){
if
(
fabs
(B[j][i])>
fabs
(B[pivot][i])){
pivot=j;
}
}
swap(B[i],B[pivot]);
if
(
fabs
(B[i][i])<EPS){
return
0;
}
for
(
int
j=i+1;j<=n*2;++j){
B[i][j]/=B[i][i];
}
for
(
int
j=1;j<=n;++j){
if
(i!=j){
for
(
int
k=i+1;k<=n*2;++k){
B[j][k]-=B[j][i]*B[i][k];
}
}
}
}
for
(
int
i=1;i<=n;++i){
for
(
int
j=n+1;j<=n*2;++j){
C[i][j-n]=B[i][j];
}
}
}
int
T;
int
main()
{
// freopen("nijuzhen2.in","r",stdin);
// freopen("nijuzhen2.out","w",stdout);
char
t[10];
while
(
scanf
(
"%d"
,&n)!=EOF){
memset
(A,0,
sizeof
(A));
for
(
int
i=1;i<=n;++i){
for
(
int
j=1;j<=n;++j){
scanf
(
"%lf"
,&A[i][j]);
}
}
if
(guass_jordan()){
for
(
int
i=1;i<=n;++i){
for
(
int
j=1;j<=n;++j){
sprintf
(t,
"%.3f"
,C[i][j]);
if
(t[0]==
‘-‘
&& t[1]==
‘0‘
&& t[2]==
‘.‘
&& t[3]==
‘0‘
&& t[4]==
‘0‘
&& t[5]==
‘0‘
){
printf
(
"0.000%c"
,j==n ?
‘
‘
:
‘ ‘
);
}
else
{
printf
(
"%.3f%c"
,C[i][j],j==n ?
‘
‘
:
‘ ‘
);
}
}
}
}
else
{
puts
(
"NO"
);
}
}
return
0;
}
以上是关于模板高斯消元的主要内容,如果未能解决你的问题,请参考以下文章