Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int solveByGaussJordan(float **A, float *B, int N) {
- int i, j, x, row, col;
- float temp;
- int *idr = (int *)malloc(N * sizeof(int));
- int *idc = (int *)malloc(N * sizeof(int));
- int *piv = (int *)malloc(N * sizeof(int));
- for(i=0;i<N;++i)
- piv[0] = 0;
- for(x=0;x<N;++x) {
- temp = 0.0f;
- for(i=0;i<N;++i) {
- if(piv[i] != 0)
- continue;
- for(j=0;j<N;++j) {
- if(piv[j] == 0 && fabs(A[i][j]) >= temp) {
- temp = fabs(A[i][j]);
- row = i;
- col = j;
- }
- }
- }
- if(temp == 0.0f)
- return 0;
- piv[col] = 1;
- if(row != col) {
- for(i=0;i<N;++i)
- SWAP(A[row][i], A[col][i]);
- SWAP(B[row],B[col]);
- }
- idr[x] = row;
- idc[x] = col;
- temp = 1.0/A[col][col];
- A[col][col] = 1.0f;
- for(i=0;i<N;++i)
- A[col][i] *= temp;
- B[col] *= temp;
- for(i=0;i<N;++i) {
- if(i == col)
- continue;
- temp = A[i][col];
- A[i][col] = 0.0f;
- for(j=0;j<N;++j)
- A[i][j] -= A[col][j]*temp;
- B[i] -= B[col]*temp;
- }
- }
- for(i=N-1;i>=0;--i) {
- if(idr[i] == idc[i])
- continue;
- for(j=0;j<N;++j)
- SWAP(A[j][idr[i]],A[j][idc[i]]);
- }
- free(idr);
- free(idc);
- free(piv);
- return 1;
- }
- int solveByGaussElimination(float **A, float *B, int N) {
- int i, j, x, swapRow;
- float temp;
- for(x=0;x<N;++x) {
- temp = 0.0f;
- for(i=x;i<N;++i) {
- if(fabs(A[i][x]) >= temp) {
- temp = fabs(A[x][i]);
- swapRow = i;
- }
- }
- if(temp == 0.0f)
- return 0;
- if(swapRow != x) {
- for(i=0;i<N;++i)
- SWAP(A[x][i], A[swapRow][i]);
- SWAP(B[x],B[swapRow]);
- }
- for(i=x+1;i<N;++i) {
- temp = A[i][x]/A[x][x];
- A[i][x] = 0;
- for(j=0;j<N;++j) {
- if(j == x)
- continue;
- A[i][j] -= temp*A[x][j];
- }
- B[i] -= temp*B[x];
- }
- }
- for(x=N-1;x>=0;--x) {
- temp = 0.0f;
- for(i=x+1;i<N;++i)
- temp += A[x][i]*B[i];
- B[x] = (B[x] - temp)/A[x][x];
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement