Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<int N>
- constexpr int determinant(const std::array<std::array<int, N>, N>& a) {
- int det = 0;
- std::array<int, N> perm = create_array<int, N>();
- int sign = 1;
- for (int i = 0; i < factorial(N); ++i) {
- int det_perm = sign;
- for (int i = 0; i < N; ++i) {
- det_perm *= (&std::get<0>((&std::get<0>(a))[i]))[(&std::get<0>(perm))[i]];
- }
- det += det_perm;
- sign *= -1;
- //next_permutation
- int first = 0;
- int last = N;
- int t = last;
- if (first == --t) {
- break;
- }
- while (true) {
- int i1 = N - 1;
- int i2 = 0;
- if ((&std::get<0>(perm))[--t] < (&std::get<0>(perm))[i1]) {
- i2 = last;
- while (!((&std::get<0>(perm))[t] < (&std::get<0>(perm))[--i2]))
- ;
- int temp = t;
- t = i2;
- i2 = temp;
- for (--last; i1 < last; ++i1, --last) {
- int t = (&std::get<0>(perm))[i1];
- (&std::get<0>(perm))[i1] = (&std::get<0>(perm))[last];
- (&std::get<0>(perm))[last] = t;
- }
- }
- }
- }
- return det;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement