- // Now come the minor expansion schemes. We always develop such that the
- // smallest minors (i.e, the trivial 1x1 ones) are on the rightmost column.
- // For this to be efficient it turns out that the emptiest columns (i.e.
- // the ones with most zeros) should be the ones on the right hand side.
- // Therefore we presort the columns of the matrix:
- typedef std::pair<unsigned,unsigned> uintpair; // # of zeros, column
- std::vector<uintpair> c_zeros; // number of zeros in column
- for (unsigned c=0; c<col; ++c) {
- unsigned acc = 0;
- for (unsigned r=0; r<row; ++r)
- if (m[r*col+c].is_zero())
- ++acc;
- c_zeros.push_back(uintpair(acc,c));
- }
- sort(c_zeros.begin(),c_zeros.end());
- // unfortunately std::vector<uintpair> can't be used for permutation_sign.
- std::vector<unsigned> pre_sort;
- for (std::vector<uintpair>::iterator i=c_zeros.begin(); i!=c_zeros.end(); ++i)
- pre_sort.push_back(i->second);
- int sign = permutation_sign(pre_sort);
- exvector result(row*col); // represents sorted matrix
- unsigned c = 0;
- for (std::vector<unsigned>::iterator i=pre_sort.begin();
- i!=pre_sort.end();
- ++i,++c) {
- for (unsigned r=0; r<row; ++r)
- result[r*col+c] = m[r*col+(*i)];
+ switch(algo) {
+ case determinant_algo::gauss: {
+ ex det = 1;
+ matrix tmp(*this);
+ int sign = tmp.gauss_elimination();
+ for (unsigned d=0; d<row; ++d)
+ det *= tmp.m[d*col+d];
+ if (normal_flag)
+ return (sign*det).normal();
+ else
+ return (sign*det).expand();
+ }
+ case determinant_algo::bareiss: {
+ matrix tmp(*this);
+ int sign;
+ sign = tmp.fraction_free_elimination(true);
+ if (normal_flag)
+ return (sign*tmp.m[row*col-1]).normal();
+ else
+ return (sign*tmp.m[row*col-1]).expand();
+ }
+ case determinant_algo::laplace:
+ default: {
+ // This is the minor expansion scheme. We always develop such
+ // that the smallest minors (i.e, the trivial 1x1 ones) are on the
+ // rightmost column. For this to be efficient it turns out that
+ // the emptiest columns (i.e. the ones with most zeros) should be
+ // the ones on the right hand side. Therefore we presort the
+ // columns of the matrix:
+ typedef std::pair<unsigned,unsigned> uintpair;
+ std::vector<uintpair> c_zeros; // number of zeros in column
+ for (unsigned c=0; c<col; ++c) {
+ unsigned acc = 0;
+ for (unsigned r=0; r<row; ++r)
+ if (m[r*col+c].is_zero())
+ ++acc;
+ c_zeros.push_back(uintpair(acc,c));
+ }
+ sort(c_zeros.begin(),c_zeros.end());
+ std::vector<unsigned> pre_sort;
+ for (std::vector<uintpair>::iterator i=c_zeros.begin(); i!=c_zeros.end(); ++i)
+ pre_sort.push_back(i->second);
+ int sign = permutation_sign(pre_sort);
+ exvector result(row*col); // represents sorted matrix
+ unsigned c = 0;
+ for (std::vector<unsigned>::iterator i=pre_sort.begin();
+ i!=pre_sort.end();
+ ++i,++c) {
+ for (unsigned r=0; r<row; ++r)
+ result[r*col+c] = m[r*col+(*i)];
+ }
+
+ if (normal_flag)
+ return sign*matrix(row,col,result).determinant_minor().normal();
+ return sign*matrix(row,col,result).determinant_minor();
+ }