Advertisement
anechka_ne_plach

Untitled

Nov 7th, 2021
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. template<int N>
  2. constexpr int determinant(const std::array<std::array<int, N>, N>& a) {
  3.     int det = 0;
  4.     std::array<int, N> perm = create_array<int, N>();
  5.     int sign = 1;
  6.     for (int i = 0; i < factorial(N); ++i) {
  7.         int det_perm = sign;
  8.         for (int i = 0; i < N; ++i) {
  9.             det_perm *= (&std::get<0>((&std::get<0>(a))[i]))[(&std::get<0>(perm))[i]];
  10.         }
  11.         det += det_perm;
  12.         sign *= -1;
  13.  
  14.         //next_permutation
  15.         int first = 0;
  16.         int last = N;
  17.         int t = last;
  18.         if (first == --t) {
  19.             break;
  20.         }
  21.         while (true) {
  22.             int i1 = N - 1;
  23.             int i2 = 0;
  24.             if ((&std::get<0>(perm))[--t] < (&std::get<0>(perm))[i1]) {
  25.                 i2 = last;
  26.                 while (!((&std::get<0>(perm))[t] < (&std::get<0>(perm))[--i2]))
  27.                     ;
  28.                 int temp = t;
  29.                 t = i2;
  30.                 i2 = temp;
  31.                 for (--last; i1 < last; ++i1, --last) {
  32.                     int t = (&std::get<0>(perm))[i1];
  33.                     (&std::get<0>(perm))[i1] = (&std::get<0>(perm))[last];
  34.                     (&std::get<0>(perm))[last] = t;
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     return det;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement