Advertisement
anechka_ne_plach

determinant.h

Nov 6th, 2021
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <array>
  2. #include <numeric>
  3. #include <algorithm>
  4. #include <utility>
  5.  
  6. template<typename T, int N, std::size_t... I>
  7. constexpr std::array<T, N> istoarr(std::index_sequence<I...>) {
  8.     return std::array<T, N>{ {I...} };
  9. }
  10.  
  11. template<typename T, int N>
  12. constexpr std::array<T, N> create_array() {
  13.     return istoarr<T, N>(std::make_index_sequence<N>{});
  14. }
  15.  
  16. template<int N>
  17. constexpr int determinant(const std::array<std::array<int, N>, N>& a) {
  18.     int det = 0;
  19.     std::array<int, N> perm = create_array<int, N>();
  20.     int sign = 1;
  21.     do {
  22.         int det_perm = sign;
  23.         for (int i = 0; i < N; ++i) {
  24.             det_perm *= a[i][perm[i]];
  25.         }
  26.         det += det_perm;
  27.         sign *= -1;
  28.     } while (std::next_permutation(perm.begin(), perm.end()));
  29.     return det;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement