MCPcopy Create free account
hub / github.com/FreeFem/FreeFem-sources / GaussElimination

Function GaussElimination

plugin/seq/plotPDF.cpp:748–796  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

746// End of SimplePDFModule
747//----------------------------------------------------------------------
748
749
750void GaussElimination(double *const x, double *const *const a, const int N)
751{
752 const double EPS = 1e-10;
753
754 // forward elimination
755 for(int k = 0; k < N-1; k++){
756
757 { // begin pivoting
758 int row = k;
759
760 for(int r = k+1; r < N; r++)
761 if(fabs(a[row][k]) < fabs(a[r][k]))
762 row = r;
763
764 if(fabs(a[row][k]) < EPS){
765 std::cerr << "singular matrix : " << row << std::endl;
766 exit(1);
767 }
768
769 // k == row: no pivoting
770 if(k != row){
771 for(int j = 0; j <= N; j++){
772 const double tmp = a[k][j];
773 a[k][j] = a[row][j];
774 a[row][j] = tmp;
775 }
776 }
777 } // end pivoting
778
779 const double d = 1 / a[k][k];
780 for(int i = k+1; i < N; i++){
781 for(int j = k+1; j <= N; j++)
782 a[i][j] -= a[i][k] * a[k][j] *d;
783 a[i][k] = 0;
784 }
785 }
786
787 // backward substitution
788 for(int i = N-1; i >= 0; i--){
789 for(int j = i+1; j < N; j++)
790 a[i][N] -= a[i][j]*a[j][N];
791 a[i][N] /= a[i][i];
792 }
793
794 for(int i = 0; i < N; i++)
795 x[i] = (fabs(a[i][N]) < EPS)? 0: a[i][N];
796
797 return;
798}
799

Callers 1

findQuadraticPolynomialFunction · 0.85

Calls 1

fabsFunction · 0.50

Tested by

no test coverage detected