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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef INVERT_MATRIX_HPP
#define INVERT_MATRIX_HPP


#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/io.hpp>


template<class T, class U, class V>
bool InvertMatrix(const boost::numeric::ublas::matrix<T, U, V>& input,
                  boost::numeric::ublas::matrix<T, U, V>& inverse)
{
  typedef boost::numeric::ublas::permutation_matrix<std::size_t> pmatrix;

  boost::numeric::ublas::matrix<T, U, V> A(input);

  pmatrix pm(A.size1());


  int res = lu_factorize(A,pm);
  if(res != 0) return false;


  inverse.assign(boost::numeric::ublas::identity_matrix<T>(A.size1()));


  lu_substitute(A, pm, inverse);

  return true;
}
#endif