]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/squarematrix.h
Add dynamically allocated versions of matrix and vector
[libs/math.git] / source / linal / squarematrix.h
index 5117bb3fa189ee4339d0ce52cdbb4d2ff141b83b..0a97e61172b6489c58e91296421e946cfddfb9d8 100644 (file)
@@ -2,19 +2,12 @@
 #define MSP_LINAL_SQUAREMATRIX_H_
 
 #include <cmath>
-#include <stdexcept>
 #include "matrix.h"
+#include "matrixops.h"
 
 namespace Msp {
 namespace LinAl {
 
-class not_invertible: public std::domain_error
-{
-public:
-       not_invertible(): domain_error(std::string()) { }
-       virtual ~not_invertible() throw() { }
-};
-
 /**
 A mathematical matrix with S rows and columns.  Some operations are provided
 here that are only possible for square matrices.
@@ -53,42 +46,8 @@ SquareMatrix<T, S> &SquareMatrix<T, S>::operator*=(const SquareMatrix<T, S> &m)
 template<typename T, unsigned S>
 SquareMatrix<T, S> &SquareMatrix<T, S>::invert()
 {
-       using std::abs;
-
        SquareMatrix<T, S> r = identity();
-       for(unsigned i=0; i<S; ++i)
-       {
-               unsigned pivot = i;
-               for(unsigned j=i+1; j<S; ++j)
-                       if(abs(this->element(j, i))>abs(this->element(pivot, i)))
-                               pivot = j;
-
-               if(this->element(pivot, i)==T(0))
-                       throw not_invertible();
-
-               if(pivot!=i)
-               {
-                       this->exchange_rows(i, pivot);
-                       r.exchange_rows(i, pivot);
-               }
-
-               for(unsigned j=i+1; j<S; ++j)
-               {
-                       T a = -this->element(j, i)/this->element(i, i);
-                       this->add_row(i, j, a);
-                       r.add_row(i, j, a);
-               }
-
-               T a = T(1)/this->element(i, i);
-               this->multiply_row(i, a);
-               r.multiply_row(i, a);
-       }
-
-       for(unsigned i=S; i-->0; )
-               for(unsigned j=i; j-->0; )
-                       r.add_row(i, j, -this->element(j, i));
-
-       return *this = r;
+       return invert_matrix(*this, r);
 }
 
 template<typename T, unsigned S>