From: Mikko Rasa Date: Fri, 31 Oct 2014 08:08:04 +0000 (+0200) Subject: Always pivot rows when inverting for better numerical stability X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e4b75401bd773201deb00eff672ee34794479671;p=libs%2Fmath.git Always pivot rows when inverting for better numerical stability --- diff --git a/source/linal/squarematrix.h b/source/linal/squarematrix.h index 1cdc567..5117bb3 100644 --- a/source/linal/squarematrix.h +++ b/source/linal/squarematrix.h @@ -58,16 +58,16 @@ SquareMatrix &SquareMatrix::invert() SquareMatrix r = identity(); for(unsigned i=0; ielement(i, i)==T(0)) - { - unsigned pivot = i; - for(unsigned j=i+1; jelement(j, i))>abs(this->element(pivot, i))) - pivot = j; + unsigned pivot = i; + for(unsigned j=i+1; jelement(j, i))>abs(this->element(pivot, i))) + pivot = j; - if(pivot==i) - throw not_invertible(); + if(this->element(pivot, i)==T(0)) + throw not_invertible(); + if(pivot!=i) + { this->exchange_rows(i, pivot); r.exchange_rows(i, pivot); }