C++ Library for Competitive Programming
#include "emthrm/math/matrix/determinant.hpp"
$O(N^3)$
名前 | 戻り値 |
---|---|
template <typename T, typename U> U det(const Matrix<T>& a, const U eps);
|
$\lvert A \rvert$ |
https://judge.yosupo.jp/submission/2714
#ifndef EMTHRM_MATH_MATRIX_DETERMINANT_HPP_
#define EMTHRM_MATH_MATRIX_DETERMINANT_HPP_
#include <algorithm>
#include <utility>
#include "emthrm/math/matrix/matrix.hpp"
namespace emthrm {
template <typename T, typename U>
U det(const Matrix<T>& a, const U eps) {
const int n = a.nrow();
Matrix<U> b(n, n);
for (int i = 0; i < n; ++i) {
std::copy(a[i].begin(), a[i].end(), b[i].begin());
}
U res = 1;
for (int j = 0; j < n; ++j) {
int pivot = -1;
U mx = eps;
for (int i = j; i < n; ++i) {
const U abs = (b[i][j] < 0 ? -b[i][j] : b[i][j]);
if (abs > mx) {
pivot = i;
mx = abs;
}
}
if (pivot == -1) return 0;
if (pivot != j) {
std::swap(b[j], b[pivot]);
res = -res;
}
res *= b[j][j];
for (int k = j + 1; k < n; ++k) {
b[j][k] /= b[j][j];
}
for (int i = j + 1; i < n; ++i) {
for (int k = j + 1; k < n; ++k) {
b[i][k] -= b[i][j] * b[j][k];
}
}
}
return res;
}
} // namespace emthrm
#endif // EMTHRM_MATH_MATRIX_DETERMINANT_HPP_
#line 1 "include/emthrm/math/matrix/determinant.hpp"
#include <algorithm>
#include <utility>
#line 1 "include/emthrm/math/matrix/matrix.hpp"
#include <vector>
namespace emthrm {
template <typename T>
struct Matrix {
explicit Matrix(const int m, const int n, const T def = 0)
: data(m, std::vector<T>(n, def)) {}
int nrow() const { return data.size(); }
int ncol() const { return data.empty() ? 0 : data.front().size(); }
Matrix pow(long long exponent) const {
const int n = nrow();
Matrix<T> res(n, n, 0), tmp = *this;
for (int i = 0; i < n; ++i) {
res[i][i] = 1;
}
for (; exponent > 0; exponent >>= 1) {
if (exponent & 1) res *= tmp;
tmp *= tmp;
}
return res;
}
inline const std::vector<T>& operator[](const int i) const { return data[i]; }
inline std::vector<T>& operator[](const int i) { return data[i]; }
Matrix& operator=(const Matrix& x) = default;
Matrix& operator+=(const Matrix& x) {
const int m = nrow(), n = ncol();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
data[i][j] += x[i][j];
}
}
return *this;
}
Matrix& operator-=(const Matrix& x) {
const int m = nrow(), n = ncol();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
data[i][j] -= x[i][j];
}
}
return *this;
}
Matrix& operator*=(const Matrix& x) {
const int m = nrow(), l = ncol(), n = x.ncol();
std::vector<std::vector<T>> res(m, std::vector<T>(n, 0));
for (int i = 0; i < m; ++i) {
for (int k = 0; k < l; ++k) {
for (int j = 0; j < n; ++j) {
res[i][j] += data[i][k] * x[k][j];
}
}
}
data.swap(res);
return *this;
}
Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
private:
std::vector<std::vector<T>> data;
};
} // namespace emthrm
#line 8 "include/emthrm/math/matrix/determinant.hpp"
namespace emthrm {
template <typename T, typename U>
U det(const Matrix<T>& a, const U eps) {
const int n = a.nrow();
Matrix<U> b(n, n);
for (int i = 0; i < n; ++i) {
std::copy(a[i].begin(), a[i].end(), b[i].begin());
}
U res = 1;
for (int j = 0; j < n; ++j) {
int pivot = -1;
U mx = eps;
for (int i = j; i < n; ++i) {
const U abs = (b[i][j] < 0 ? -b[i][j] : b[i][j]);
if (abs > mx) {
pivot = i;
mx = abs;
}
}
if (pivot == -1) return 0;
if (pivot != j) {
std::swap(b[j], b[pivot]);
res = -res;
}
res *= b[j][j];
for (int k = j + 1; k < n; ++k) {
b[j][k] /= b[j][j];
}
for (int i = j + 1; i < n; ++i) {
for (int k = j + 1; k < n; ++k) {
b[i][k] -= b[i][j] * b[j][k];
}
}
}
return res;
}
} // namespace emthrm