Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. int solveByGaussJordan(float **A, float *B, int N) {
  2.     int i, j, x, row, col;
  3.     float temp;
  4.     int *idr = (int *)malloc(N * sizeof(int));
  5.     int *idc = (int *)malloc(N * sizeof(int));
  6.     int *piv = (int *)malloc(N * sizeof(int));
  7.     for(i=0;i<N;++i)
  8.         piv[0] = 0;
  9.     for(x=0;x<N;++x) {
  10.         temp = 0.0f;
  11.         for(i=0;i<N;++i) {
  12.             if(piv[i] != 0)
  13.                 continue;
  14.             for(j=0;j<N;++j) {
  15.                 if(piv[j] == 0 && fabs(A[i][j]) >= temp) {
  16.                     temp = fabs(A[i][j]);
  17.                     row = i;
  18.                     col = j;
  19.                 }
  20.             }
  21.         }
  22.         if(temp == 0.0f)
  23.             return 0;
  24.         piv[col] = 1;
  25.         if(row != col) {
  26.             for(i=0;i<N;++i)
  27.                 SWAP(A[row][i], A[col][i]);
  28.             SWAP(B[row],B[col]);
  29.         }
  30.         idr[x] = row;
  31.         idc[x] = col;
  32.         temp = 1.0/A[col][col];
  33.         A[col][col] = 1.0f;
  34.         for(i=0;i<N;++i)
  35.             A[col][i] *= temp;
  36.         B[col] *= temp;
  37.         for(i=0;i<N;++i) {
  38.             if(i == col)
  39.                 continue;
  40.             temp = A[i][col];
  41.             A[i][col] = 0.0f;
  42.             for(j=0;j<N;++j)
  43.                 A[i][j] -= A[col][j]*temp;
  44.             B[i] -= B[col]*temp;
  45.         }
  46.     }
  47.     for(i=N-1;i>=0;--i) {
  48.         if(idr[i] == idc[i])
  49.             continue;
  50.         for(j=0;j<N;++j)
  51.             SWAP(A[j][idr[i]],A[j][idc[i]]);
  52.     }
  53.     free(idr);
  54.     free(idc);
  55.     free(piv);
  56.     return 1;
  57. }
  58.  
  59. int solveByGaussElimination(float **A, float *B, int N) {
  60.     int i, j, x, swapRow;
  61.     float temp;
  62.     for(x=0;x<N;++x) {
  63.         temp = 0.0f;
  64.         for(i=x;i<N;++i) {
  65.             if(fabs(A[i][x]) >= temp) {
  66.                 temp = fabs(A[x][i]);
  67.                 swapRow = i;
  68.             }
  69.         }
  70.         if(temp == 0.0f)
  71.             return 0;
  72.         if(swapRow != x) {
  73.             for(i=0;i<N;++i)
  74.                 SWAP(A[x][i], A[swapRow][i]);
  75.             SWAP(B[x],B[swapRow]);
  76.         }
  77.         for(i=x+1;i<N;++i) {
  78.             temp = A[i][x]/A[x][x];
  79.             A[i][x] = 0;
  80.             for(j=0;j<N;++j) {
  81.                 if(j == x)
  82.                     continue;
  83.                 A[i][j] -= temp*A[x][j];
  84.             }
  85.             B[i] -= temp*B[x];
  86.         }
  87.     }
  88.     for(x=N-1;x>=0;--x) {
  89.         temp = 0.0f;
  90.         for(i=x+1;i<N;++i)
  91.             temp += A[x][i]*B[i];
  92.         B[x] = (B[x] - temp)/A[x][x];
  93.     }
  94.     return 1;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement