From e4b75401bd773201deb00eff672ee34794479671 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 31 Oct 2014 10:08:04 +0200 Subject: [PATCH] Always pivot rows when inverting for better numerical stability --- source/linal/squarematrix.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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); } -- 2.43.0