Gaussian-Jordan elimination method

Source: Internet
Author: User

Link: http://blog.csdn.net/zhangxaochen/article/details/8020668

Routine in the book:

Here: http://is.gd/VoBVUJ

void NR::gaussj(Mat_IO_DP &a, Mat_IO_DP &b){int i,icol,irow,j,k,l,ll;DP big,dum,pivinv;int n=a.nrows();int m=b.ncols();Vec_INT indxc(n),indxr(n),ipiv(n);for (j=0;j<n;j++) ipiv[j]=0; for (i=0;i<n;i++) {big=0.0;for (j=0;j<n;j++){if (ipiv[j] != 1){for (k=0;k<n;k++) {if (ipiv[k] == 0) {if (fabs(a[j][k]) >= big) {big=fabs(a[j][k]);irow=j;icol=k;}}}}}++(ipiv[icol]);if (irow != icol) {for (l=0;l<n;l++) SWAP(a[irow][l],a[icol][l]);for (l=0;l<m;l++) SWAP(b[irow][l],b[icol][l]);}indxr[i]=irow;indxc[i]=icol;if (a[icol][icol] == 0.0) nrerror("gaussj: Singular Matrix");pivinv=1.0/a[icol][icol];//a[icol][icol]=1.0;for (l=0;l<n;l++) a[icol][l] *= pivinv;for (l=0;l<m;l++) b[icol][l] *= pivinv;for (ll=0;ll<n;ll++){if (ll != icol) {dum=a[ll][icol];//a[ll][icol]=0.0;for (l=0;l<n;l++) a[ll][l] -= a[icol][l]*dum;for (l=0;l<m;l++) b[ll][l] -= b[icol][l]*dum;}}}for (l=n-1;l>=0;l--) {if (indxr[l] != indxc[l])for (k=0;k<n;k++)SWAP(a[k][indxr[l]],a[k][indxc[l]]);}}

The above code is hard to understand. There are two places around, as if they were intended to tease you ..

So I wrote it again: (because vs2010 is used on the hand, the two-dimensional array uniform Initialization is not supported when constructing the matrix. For the convenience of parameter passing, so the function is defined as "Double A [] [3)

Void mygaussj (double A [] [3], double B [] [1], int N, int m) {vector <bool> dirty (n); double TMP; int row, Col; For (INT Re = 0; Re <n; re ++) {for (INT I = 0; I <n; I ++) {If (true = dirty [I]) continue; double big = 0; // find the maximum value in a row: For (Int J = 0; j <N; j ++) {If (TMP = FABS (A [I] [J])> big) {big = TMP; ROW = I; Col = J ;}} assert (big! = 0); // if the big value is not in the diagonal line, the line is switched to the dirty [col] = true; // prepare to open if (row! = Col) {for (int c = 0; C <n; C ++) {TMP = A [row] [c]; A [row] [c] = A [col] [c]; A [col] [c] = TMP;} For (int c = 0; C <m; c ++) {TMP = B [row] [c]; B [row] [c] = B [col] [c]; B [col] [c] = TMP ;}// if row! = Col // principal element normalization TMP = A [col] [col]; for (int c = 0; C <n; C ++) {A [col] [c] * = 1.0/tmp;} For (int c = 0; C <m; C ++) {B [col] [c] * = 1.0/tmp;} // other rows in the col column are set to zero. Of course, other columns in the row also lose the corresponding value for (INT r = 0; r <n; r ++) {If (r = col) continue; TMP = A [r] [col]; for (int c = 0; C <N; c ++) {A [r] [c]-= TMP * A [col] [c];} For (int c = 0; C <m; C ++) {B [r] [c]-= TMP * B [col] [c] ;}} break;} // For I} // for RE}

The basic step is:

1. find the largest element in the row and use it as the principal element to check whether the principal element is on the diagonal line. If not, for example, in (), switch the first row and the third row so that the principal element is on) [synchronous operations must be performed on matrix A and matrix B, that is, B must exchange rows 1 and 3], and dirty [3] = true indicates that the third row is ready.

2. (the first row has become the third row.) The (3, 3) Position element is converted into 1, and other elements in the third row are divided by the value of (3, 3) at the same time. The element in the third row of matrix B is divided by the value of (3, 3) of.

3. (the third row (3, 3) has been normalized to 1) use equation 3 to subtract other rows and make the third column of the first two rows 0 because they are not on the diagonal line.

This is the end of a loop. Repeat n times until all N dirty entries are true. At this time, a is the matrix of units, and B is the solution matrix.

 

I am confused: I don't know why to select the largest element as the principal element. I personally think that the primary element can be a non-zero element .. Some people say that stability considerations are not clear for the moment. Suspect

 

Link: http://blog.csdn.net/zhangxaochen/article/details/8020668

{Over }}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.