Commit d3b482aa by Matthias Braun

### gaussjordan: Cleanup

parent 7863c88e
 ... @@ -15,13 +15,14 @@ ... @@ -15,13 +15,14 @@ */ */ /** /** * solves a system of linear equations and returns 0 if successful * Solves a system of linear equations. * * * @param A the linear equations as matrix * @param matrix the linear equations as matrix (square matrix, \t n x \p n) * @param b the result vector, will contain the result if successful * @param result the result vector, will contain the result if successful * @param nsize the size of the equation system * @param n the size of the equation system * @returns 0 if successful, -1 if ill-conditioned matrix */ */ FIRM_API int firm_gaussjordansolve(double *A, double *b, int nsize); FIRM_API int firm_gaussjordansolve(double *matrix, double *result, unsigned n); /** @} */ /** @} */ ... ...
 ... @@ -24,47 +24,41 @@ ... @@ -24,47 +24,41 @@ /* Inverse Theory--1984 p210 ), but performs actual */ /* Inverse Theory--1984 p210 ), but performs actual */ /* row switching to simplify the programming. */ /* row switching to simplify the programming. */ /* Partial pivoting is used. */ /* Partial pivoting is used. */ /* */ /* A[][] is a square matrix, N x N */ /* vec[] is N x 1 of the matrix */ /* nsize is the size of the equation system */ /* */ /* returns 0 if successful */ /* returns -1 if ill-conditioned matrix */ /*------------------------------------------------------*/ /*------------------------------------------------------*/ #include "gaussjordan.h" #include #include #include #include #include "xmalloc.h" #include "xmalloc.h" #include "gaussjordan.h" #define SMALL 0.00001 #define SMALL 0.00001 int firm_gaussjordansolve(double *A, double *vec, int nsize) int firm_gaussjordansolve(double *A, double *vec, unsigned nsize) { { int i, j, row, col, col2, biggest_r = 0, biggest_c = 0, t; double *scramvec = XMALLOCN(double, nsize); double big, temp, sum; unsigned *x = XMALLOCN(unsigned, nsize); double *scramvec = XMALLOCN(double, nsize); int res = 0; int *x = XMALLOCN(int, nsize); int res = 0; #define _A(row,col) A[(row)*nsize + (col)] #define _A(row,col) A[(row)*nsize + (col)] /* init x[] */ /* init x[] */ for (i = 0; i < nsize; ++i) for (unsigned i = 0; i < nsize; ++i) { x[i] = i; x[i] = i; } /* triangularize A */ /* triangularize A */ /* ie A has zeros below its diagonal */ /* ie A has zeros below its diagonal */ for (col = 0; col < nsize - 1; ++col) { unsigned biggest_r = 0; big = 0; unsigned biggest_c = 0; for (unsigned col = 0; col < nsize - 1; ++col) { double big = 0; /* find the largest left in LRH box */ /* find the largest left in LRH box */ for (row = col; row < nsize; ++row) { for (unsigned row = col; row < nsize; ++row) { for (col2 = col; col2 < nsize; ++col2) { for (unsigned col2 = col; col2 < nsize; ++col2) { temp = fabs(_A(row,col2)); double temp = fabs(_A(row,col2)); if (temp > big) { if (temp > big) { biggest_r = row; biggest_r = row; biggest_c = col2; biggest_c = col2; big = temp; /* largest element left */ big = temp; /* largest element left */ } } } } } } ... @@ -74,36 +68,35 @@ int firm_gaussjordansolve(double *A, double *vec, int nsize) ... @@ -74,36 +68,35 @@ int firm_gaussjordansolve(double *A, double *vec, int nsize) } } /* swap rows */ /* swap rows */ for (i=0;i=0;i--) { for (unsigned i = nsize - 1; i-- > 0;) { sum = 0; int sum = 0; for (j=i+1;j
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!