Program listing for file kernel/src/utils/SiconosAlgebra/InvertMatrix.hpp

Program listing for file kernel/src/utils/SiconosAlgebra/InvertMatrix.hpp#

 1#ifndef INVERT_MATRIX_HPP
 2#define INVERT_MATRIX_HPP
 3
 4
 5#include <boost/numeric/ublas/vector.hpp>
 6#include <boost/numeric/ublas/vector_proxy.hpp>
 7#include <boost/numeric/ublas/matrix.hpp>
 8#include <boost/numeric/ublas/triangular.hpp>
 9#include <boost/numeric/ublas/lu.hpp>
10#include <boost/numeric/ublas/io.hpp>
11
12
13template<class T, class U, class V>
14bool InvertMatrix(const boost::numeric::ublas::matrix<T, U, V>& input,
15                  boost::numeric::ublas::matrix<T, U, V>& inverse)
16{
17  typedef boost::numeric::ublas::permutation_matrix<std::size_t> pmatrix;
18
19  boost::numeric::ublas::matrix<T, U, V> A(input);
20
21  pmatrix pm(A.size1());
22
23
24  int res = lu_factorize(A,pm);
25  if(res != 0) return false;
26
27
28  inverse.assign(boost::numeric::ublas::identity_matrix<T>(A.size1()));
29
30
31  lu_substitute(A, pm, inverse);
32
33  return true;
34}
35#endif