Boost Library, how to get qualifier from lu_factorize ()?

I am trying to calculate the determinant using the boost C ++ libraries. I found the code for the InvertMatrix () function, which I copied below. Every time I calculate this inverse, I also want a determinant. I have a good idea how to calculate by multiplying the diagonal of the matrix U from the LU decomposition. There is one problem, I can correctly calculate the determinant, except for the sign. Depending on the turn, I get the sign of the wrong half of the time. Does anyone have a suggestion on how to get a badge every time? Thanks in advance.

template<class T>
bool InvertMatrix(const ublas::matrix<T>& input, ublas::matrix<T>& inverse)
{
 using namespace boost::numeric::ublas;
 typedef permutation_matrix<std::size_t> pmatrix;
 // create a working copy of the input
 matrix<T> A(input);
 // create a permutation matrix for the LU-factorization
 pmatrix pm(A.size1());

 // perform LU-factorization
 int res = lu_factorize(A,pm);
 if( res != 0 ) return false;

This is where I put my best shot when calculating the determinant.

 T determinant = 1;

 for(int i = 0; i < A.size1(); i++)
 {
  determinant *= A(i,i);
 }

Complete my piece of code.

 // create identity matrix of "inverse"
 inverse.assign(ublas::identity_matrix<T>(A.size1()));

 // backsubstitute to get the inverse
 lu_substitute(A, pm, inverse);

 return true;
}
+5
2

pm , : .

lu.hpp swap_rows, , . , ( ), , -1:

template <typename size_type, typename A>
int determinant(const permutation_matrix<size_type,A>& pm)
{
    int pm_sign=1;
    size_type size=pm.size();
    for (size_type i = 0; i < size; ++i)
        if (i != pm(i))
            pm_sign* = -1; // swap_rows would swap a pair of rows here, so we change sign
    return pm_sign;
}

lu_factorize lu_substitute, ( , pm lu_factorize lu_substitute). , . : .

+3

http://qiangsong.wordpress.com/2011/07/16/lu-factorisation-in-ublas/:

determinant *= A(i,i) determinant *= (pm(i) == i ? 1 : -1) * A(i,i). , .

, Managu, , , ( "2 1", InvertMatrix).

+1

All Articles