template<typename T>
-inline T &invert_matrix(T &m, T &r)
+inline T &gauss_jordan(T &m, T &r)
{
typedef typename T::ElementType V;
using std::abs;
for(unsigned j=i; j-->0; )
r.add_row(i, j, -m.element(j, i));
- return m = r;
+ return r;
}
} // namespace LinAl
SquareMatrix<T, S> &SquareMatrix<T, S>::invert()
{
SquareMatrix<T, S> r = identity();
- return invert_matrix(*this, r);
+ gauss_jordan(*this, r);
+ return *this = r;
}
template<typename T, unsigned S>
inline SquareMatrix<T, S> invert(const SquareMatrix<T, S> &m)
{
- SquareMatrix<T, S> r = m;
- return r.invert();
+ SquareMatrix<T, S> temp = m;
+ SquareMatrix<T, S> r = SquareMatrix<T, S>::identity();
+ return gauss_jordan(temp, r);
}
} // namespace LinAl