![]() |
Prev | Next | det_of_minor.hpp | Headings |
# ifndef CPPAD_DET_OF_MINOR_INCLUDED
# define CPPAD_DET_OF_MINOR_INCLUDED
namespace CppAD { // BEGIN CppAD namespace
template <class Scalar>
Scalar det_of_minor(
const std::vector<Scalar>& a ,
size_t m ,
size_t n ,
std::vector<size_t>& r ,
std::vector<size_t>& c )
{ size_t R0; // R(0)
size_t Cj; // C(j)
size_t Cj1; // C(j-1)
size_t j; // column index in M
Scalar detS; // determinant of sub-minor of M
Scalar M0j; // an element of the first row of M
int s; // sign of the current sub-minor in summation
// value of R(0) and C(0)
R0 = r[m];
Cj = c[m];
// check for 1 by 1 case
if( n == 1 )
return a[ R0 * m + Cj ];
// initialize determinant of the minor M
Scalar detM(0);
// initialize sign of permutation
s = 1;
// remove row with index R0 from sub-minors of M
r[m] = r[R0];
// initialize previous column index; Cj = c[ Cj1 ]
Cj1 = m;
// for each column of M
for(j = 0; j < n; j++)
{ // M(0, j)
M0j = a[ R0 * m + Cj ];
// remove column index j from sub-minor of M
c[Cj1] = c[Cj];
// compute determinant of sub-minor of M
// where row R0 and column Cj are removed
detS = det_of_minor(a, m, n - 1, r, c);
// restore column Cj to sub-minor of M
c[Cj1] = Cj;
// include in summation
if( s > 0 )
detM = detM + M0j * detS;
else detM = detM - M0j * detS;
// advance to next column of M
Cj1 = Cj;
Cj = c[Cj];
s = -s;
}
// restore row index zero to the minor M
r[m] = R0;
// return the determinant of the minor M
return detM;
}
} // END CppAD namespace
# endif