From 6e26844a6f1273b3d8dab1de869eeff3b93624ae Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Mon, 15 Feb 2016 13:01:02 +0100 Subject: [PATCH] Bug fix in gauss --- gauss.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/gauss.c b/gauss.c index 178a108..c0a37ea 100644 --- a/gauss.c +++ b/gauss.c @@ -102,15 +102,16 @@ int solve_gauss_rec(GLS m, int rowCol) { if( ISZERO(glsGet(m,rowCol,rowCol)) ) return 0; for(size_t row = rowCol+1; row < m.size; row++) { - if(ISZERO(glsGet(m,row, rowCol))) continue; - glsTransform(m, row, glsGet(m,rowCol,rowCol), rowCol, - glsGet(m,row,rowCol) ); + if(!ISZERO(glsGet(m,row, rowCol))) + glsTransform(m, row, glsGet(m,rowCol,rowCol), rowCol, - glsGet(m,row,rowCol) ); glsSet(m,row,rowCol, 0.0f); } if(!solve_gauss_rec(m, rowCol+1)) return 0; - for(size_t row = 0; row < rowCol; row--) { - glsTransform(m, row, glsGet(m,rowCol,rowCol), rowCol, - glsGet(m,row,rowCol) ); + for(size_t row = 0; row < rowCol; row++) { + if(!ISZERO(glsGet(m,row,rowCol))) + glsTransform(m, row, glsGet(m,rowCol,rowCol), rowCol, - glsGet(m,row,rowCol) ); glsSet(m,row,rowCol, 0.0f); } @@ -129,9 +130,10 @@ int solve(GLS m) { int main( int argc, char *argv[] ) { - GLS m = glsCreate(2); - glsSet(m,0,0,2); glsSet(m,0,1,3); glsSetRS(m,0, 13); - glsSet(m,1,0,1); glsSet(m,1,1,2); glsSetRS(m,1, 8); + GLS m = glsCreate(3); + glsSet(m,0,0,3); glsSet(m,0,1,-1); glsSet(m,0,2,-1); glsSetRS(m,0, 0); + glsSet(m,1,0,1); glsSet(m,1,1,4); glsSet(m,1,2,2); glsSetRS(m,1, 4); + glsSet(m,2,0,0); glsSet(m,2,1,2); glsSet(m,2,2,1); glsSetRS(m,2, 1); printf("Input: \n"); glsPrint(m);