Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Algorithm: Gaussian Elimination
- * Complexity : O( N^3 )
- */
- typedef vector<double> VD;
- /* indexin 0 base */
- /* return 1 if unique sol ,0 if no sol , INF if multiple sol */
- long Gauss( vector<VD> A,vector< double> &Ans )
- {
- long Row = (long)A.size();
- long Col = (long)A[0].size() - 1;
- vector<long> Where( Col,-1 );
- long r,c,i,j;
- for( c=0,r=0;c<Col && r<Row;c++ ){
- long sel = r;
- for( i=r;i<Row;i++ ){
- if( fabs( A[i][c] ) > fabs( A[sel][c] ) ) sel = i;
- }
- if( fabs( A[sel][c] ) < EPS ) continue ;
- swap( A[sel] ,A[r] ) ;
- Where[c] = r;
- for( i=0;i<Row;i++ ){
- if( i==r ) continue;
- double CON = A[i][c] / A[r][c] ;
- for( j=c;j<=Col;j++ ) A[i][j] -= A[r][j]*CON;
- }
- r++;
- }
- Ans.assign( Col,0 ) ;
- for( i=0;i<Col;i++ ){
- if( Where[i]==-1 ) continue;
- Ans[i] = A[Where[i]][Col] / A[Where[i]][i];
- }
- for( i=0;i<Row;i++ ){
- double Where = 0;
- for( j=0;j<Col;j++ ) Where += Ans[j] * A[i][j];
- /* Found a row which hs all zero co-eff */
- if( fabs( Where - A[i][Col] ) > EPS ){
- return 0;
- }
- }
- for( i=0;i<Col;i++ ){
- if( Where[i] == -1 ){
- return INF ;
- }
- }
- return 1 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement