BoostC++ライブラリを使用して行列式を計算しようとしています。以下にコピーした関数InvertMatrix()のコードを見つけました。この逆数を計算するたびに、行列式も必要です。LU分解からU行列の対角線を掛けて計算する方法がわかります。1つの問題があります。符号を除いて、行列式を正しく計算できます。ピボットに応じて、半分の時間で間違ったサインが表示されます。誰かが毎回正しいサインを取得する方法についての提案がありますか?前もって感謝します。
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;
ここに、行列式の計算でベストショットを挿入しました。
T determinant = 1;
for(int i = 0; i < A.size1(); i++)
{
determinant *= A(i,i);
}
コードの私の部分を終了します。
// 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;
}