Skip to content

Fix linalg_bitset template argument deduction #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions linear_algebra_matrix/linalg_bitset.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once
#include <bitset>
#include <cassert>
#include <cstddef>
#include <tuple>
#include <utility>
#include <vector>

// Gauss-Jordan elimination of n * m matrix M
// Complexity: O(nm + nm rank(M) / 64)
// Verified: abc276_h (2000 x 8000)
template <int Wmax>
template <std::size_t Wmax>
std::vector<std::bitset<Wmax>> f2_gauss_jordan(int W, std::vector<std::bitset<Wmax>> M) {
assert(W <= Wmax);
int H = M.size(), c = 0;
Expand All @@ -33,7 +34,8 @@ std::vector<std::bitset<Wmax>> f2_gauss_jordan(int W, std::vector<std::bitset<Wm
}

// Rank of Gauss-Jordan eliminated matrix
template <int Wmax> int f2_rank_gauss_jordan(int W, const std::vector<std::bitset<Wmax>> &M) {
template <std::size_t Wmax>
int f2_rank_gauss_jordan(int W, const std::vector<std::bitset<Wmax>> &M) {
assert(W <= Wmax);
for (int h = (int)M.size() - 1; h >= 0; h--) {
int j = 0;
Expand All @@ -46,7 +48,7 @@ template <int Wmax> int f2_rank_gauss_jordan(int W, const std::vector<std::bitse
// determinant of F2 matrix.
// Return 0 if the matrix is singular, otherwise return 1.
// Complexity: O(W^3 / 64)
template <int Wmax> int f2_determinant(const std::vector<std::bitset<Wmax>> &M) {
template <std::size_t Wmax> int f2_determinant(const std::vector<std::bitset<Wmax>> &M) {
const int H = M.size();
if (H > Wmax) return 0;

Expand All @@ -70,7 +72,7 @@ template <int Wmax> int f2_determinant(const std::vector<std::bitset<Wmax>> &M)
return 1; // nonsingular
}

template <int W1, int W2>
template <std::size_t W1, std::size_t W2>
std::vector<std::bitset<W2>>
f2_matmul(const std::vector<std::bitset<W1>> &A, const std::vector<std::bitset<W2>> &B) {
int H = A.size(), K = B.size();
Expand All @@ -83,7 +85,7 @@ f2_matmul(const std::vector<std::bitset<W1>> &A, const std::vector<std::bitset<W
return C;
}

template <int Wmax>
template <std::size_t Wmax>
std::vector<std::bitset<Wmax>> f2_matpower(std::vector<std::bitset<Wmax>> X, long long n) {
int D = X.size();
std::vector<std::bitset<Wmax>> ret(D);
Expand All @@ -99,7 +101,7 @@ std::vector<std::bitset<Wmax>> f2_matpower(std::vector<std::bitset<Wmax>> X, lon
// - retval: {true, one of the solutions, {freedoms}} (if solution exists)
// {false, {}, {}} (otherwise)
// Complexity: O(HW + HW rank(A) / 64 + W^2 len(freedoms))
template <int Wmax, class Vec>
template <std::size_t Wmax, class Vec>
std::tuple<bool, std::bitset<Wmax>, std::vector<std::bitset<Wmax>>>
f2_system_of_linear_equations(std::vector<std::bitset<Wmax>> A, Vec b, int W) {
int H = A.size();
Expand Down
4 changes: 2 additions & 2 deletions linear_algebra_matrix/linalg_bitset.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ vector<bitset<Wmax>> A;
vector<bool> b;

// Solve Ax = b (x: F_2^W)
auto [feasible, x0, freedoms] = f2_system_of_linear_equations<Wmax, vector<bool>>(A, b, W);
auto [feasible, x0, freedoms] = f2_system_of_linear_equations(A, b, W);

// Calc determinant (or check whether A is regular)
int det = f2_determinant<dim>(mat);
int det = f2_determinant(mat);
```

## 問題例
Expand Down
Loading