// <https://en.wikipedia.org/wiki/Permutation#Permutations_in_computing> // <https://en.wikipedia.org/wiki/Permutation#Generation_with_minimal_changes> // <https://en.wikipedia.org/wiki/Lehmer_code> // <https://en.wikipedia.org/wiki/Factorial_number_system> template<typename T, int N> T determinant(Mat<T, N, N> const & mat) { auto det = T(0); auto fac = 1; for (auto i = 2; i <= N; ++i) fac *= i; auto s = N % 2 ? -1 : 1; // TODO: Flipping the sign every iteration is completely made up but has a // non-zero chance of being correct. for (auto ctr = 0; ctr < fac; ++ctr, s *= -1) { auto t = T(s); for (auto i = N, c = ctr; i > 0; --i, c /= i) t *= mat[i-1][c % i]; // TODO: `c % i` does not skip alreay picked columns! det += t; } return det; }