Learn the basic algorithms of matrices today
Gaussian elimination is a powerful tool for solving linear equations.
The basic idea is to transform the augmented matrix into a simplified ladder-shaped matrix by passing the elementary changes.
The following is the column main element Gaussian elimination method, the complexity of O (n^3).
It is easy to derive the determinant and rank algorithm according to the Gaussian elimination method.
Code:
/********************************************************** ------------------ ** Author Abyssalfish ***********************************************************/#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>#include<cmath>//#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const DoubleEPS = 1e-8; typedef vector<Double>vec;typedef Vector<vec>Mat;//O (n^3)VEC Gauss_jordan (Constmat& A,Constvec&b) { intn =a.size (); Mat B (N,vec (n+1));//Augment Matrix for(inti =0; I < n; i++) for(intj =0; J < N; J + +) B[i][j] =A[i][j]; for(inti =0; I < n; i++) B[i][n] =B[i]; for(inti =0; I < n; i++){ intPIV = i;//maximum to determine the solution of no solution or infinite number for(intj = i; J < N; J + +){ if(ABS (B[j][i] > abs (b[piv][i))) Piv =J; } if(I! =piv) Swap (B[i],b[piv]); if(ABS (B[i][i]) < EPS)returnVec (); //assuming that the coefficients are changed to 1, only the part that affects the back is calculated. for(intj = N; J > i; j--) B[i][j]/=B[i][i]; for(intj =0; J < N; J + +)if(I! =j) { for(intK = i+1; K <= N; k++) b[j][k]-= b[j][i]*B[i][k]; }} Vec x (n); for(inti =0; I < n; i++) X[i] =B[i][n]; returnx;}DoubleDeterminant (Constmat&A) { intn =a.size (); Mat B=A; DoubleDet =1; intSign =0; for(inti =0; I < n; i++){ intPIV =i; for(intj = i; J < N; J + +){ if(ABS (B[j][i] > abs (b[piv][i))) Piv =J; } if(I! = PIV) swap (B[I],B[PIV]), sign ^=1; if(ABS (B[i][i]) < EPS)return 0; Det*=B[i][i]; for(intj = i+1; J < N; J + +) B[i][j]/=B[i][i]; for(intj = i+1; J < N; J + +) { for(intK = i+1; K < n; k++) b[j][k]-= b[j][i]*B[i][k]; } } returnsign?-Det:det;}intRank_of_mat (Constmat&A) { intn =a.size (); Mat B=A; for(inti =0; I < n; i++){ intPIV =i; for(intj = i; J < N; J + +){ if(ABS (B[j][i] > abs (b[piv][i))) Piv =J; } if(I! =piv) Swap (B[i],b[piv]); if(ABS (B[i][i]) < EPS)returni; for(intj = N; --j > i;) B[I][J]/=B[i][i]; for(intj = i+1; J < N; J + +) { for(intK = i+1; K < n; k++) b[j][k]-= b[j][i]*B[i][k]; } } returnN;}DoubleRead () {DoubleT scanf"%LF", &t);returnt;}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endifVEC Alpha; Mat A; intN; Puts ("Input a matrix"); while(~SCANF ("%d", &n) && n <=0) puts ("Input Invalid"); A.resize (n); for(inti =0; I < n; i++){ for(intj =0; J < N; J + +{a[i].push_back (read ()); }} puts ("input a vector"); for(inti =0; I < n; i++) Alpha.push_back (read ()); Puts ("\nsolution"); Vec Sol=Gauss_jordan (A,alpha); if(Sol.size ()) { for(inti =0; I < n; i++) printf ("%lf%c", Sol[i], i!=n-1?' ':'\ n'); }Else{puts ("Not unique"); } puts ("\ndeterminant"); printf ("%lf\n", determinant (A)); Puts ("\nrank"); printf ("%d\n", Rank_of_mat (A)); return 0;}
Test examples
In practice, if you need this information at the same time to calculate the B-matrix only once, it improves efficiency.
The rank of matrix algorithm Gaussian elimination determinant matrix