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;
matrix<T> A(input);
pmatrix pm(A.size1());
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.
inverse.assign(ublas::identity_matrix<T>(A.size1()));
lu_substitute(A, pm, inverse);
return true;
}