a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper
#include "library/data-structure/matrix.hpp"
行列
Matrix<T>(n, m)
で $n \times m$ の行列を作成する。
+
, -
, *
, /
, %
+
, -
, *
A.pow(x)
で $x$ 乗A.transpose()
で転置A.n_row()
, A.n_col()
で行数、列数A.at(i, j)
で $i$ 行 $j$ 列目Matrix::I(n)
で $n \times n$ の単位行列determinant<U>()
: 行列式を求める。 $O(n^3)$。 U
を指定しないと行列の要素型で求める。valarray で実装しているので、多少高速…なはず
#pragma once
#include <cassert>
#include <concepts>
#include <cstddef>
#include <valarray>
using namespace std;
#include "../_internal/types.hpp"
namespace asalib {
namespace matrix {
template<_internal::numeric_like T>
class Matrix {
public:
constexpr Matrix(): _n_row(0), _n_col(0) {};
constexpr Matrix(size_t n_row, size_t n_col): _n_row(n_row), _n_col(n_col) {
_data.resize(n_row, valarray<T>(n_col));
};
constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col) {
_data.resize(n_row, valarray<T>(n_col, x));
};
inline constexpr T& at(size_t i, size_t j) {
assert(i < _n_row);
assert(j < _n_col);
return _data[i][j];
}
inline constexpr T at(size_t i, size_t j) const {
assert(i < _n_row);
assert(j < _n_col);
return _data[i][j];
}
inline constexpr Matrix& operator+=(const T& x) {
_data += x;
return *this;
}
inline constexpr Matrix& operator-=(const T& x) {
_data -= x;
return *this;
}
inline constexpr Matrix& operator*=(const T& x) {
_data *= x;
return *this;
}
inline constexpr Matrix& operator/=(const T& x) {
_data /= x;
return *this;
}
inline constexpr Matrix& operator%=(const T& x) {
_data %= x;
return *this;
}
inline constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
inline constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
inline constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
inline constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
inline constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }
inline constexpr Matrix& operator+=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data += x._data;
return *this;
}
inline constexpr Matrix& operator-=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data -= x._data;
return *this;
}
inline constexpr Matrix& operator*=(const Matrix& x) {
assert(_n_col == x._n_row);
Matrix res(_n_row, x._n_col);
for (size_t i = 0; i < _n_row; ++i) {
for (size_t k = 0; k < _n_col; ++k) {
for (size_t j = 0; j < x._n_col; ++j) {
res._data[i][j] += _data[i][k] * x._data[k][j];
}
}
}
return *this = res;
}
inline constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
inline constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
inline constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
inline constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
inline constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
inline constexpr bool operator<(const Matrix& x) const { return _data < x._data; }
inline constexpr Matrix transpose() const {
Matrix res(_n_col, _n_row);
for (size_t i = 0; i < _n_row; ++i) {
for (size_t j = 0; j < _n_col; ++j) {
res._data[j][i] = _data[i][j];
}
}
return res;
}
template<integral U>
inline constexpr Matrix pow(U x) const {
assert(_n_row == _n_col);
Matrix res = I(_n_row);
Matrix a(*this);
while (x) {
if (x & 1) res *= a;
a *= a;
x >>= 1;
}
return res;
}
inline static constexpr Matrix I(size_t n) {
Matrix res(n, n);
for (size_t i = 0; i < n; ++i) {
res._data[i][i] = 1;
}
return res;
}
inline constexpr size_t n_row() const { return _n_row; }
inline constexpr size_t n_col() const { return _n_col; }
private:
size_t _n_row, _n_col;
valarray<valarray<T>> _data;
public:
// ---------- prototype ---------- //
inline constexpr T determinant() const;
template<_internal::numeric_like U>
inline constexpr U determinant() const;
};
} // namespace matrix
} // namespace asalib
#line 2 "library/data-structure/matrix.hpp"
#include <cassert>
#include <concepts>
#include <cstddef>
#include <valarray>
using namespace std;
#line 2 "library/_internal/types.hpp"
#line 4 "library/_internal/types.hpp"
using namespace std;
#line 2 "library/_internal/modint-base.hpp"
#line 4 "library/_internal/modint-base.hpp"
#include <type_traits>
using namespace std;
namespace asalib {
namespace _internal {
class modint_base {};
template<typename T>
concept is_modint = is_base_of_v<modint_base, T>;
} // namespace _internal
} // namespace asalib
#line 7 "library/_internal/types.hpp"
namespace asalib {
namespace _internal {
// ---------- concept definition ---------- //
template<class T>
concept integral_like = integral<T> || is_modint<T>;
template<class T>
concept floating_like = floating_point<T>;
template<class T>
concept numeric_like = integral_like<T> || floating_like<T>;
// ---------- function definition ---------- //
template<class T>
T plus(T a, T b) { return a + b; }
template<class T>
T minus(T a, T b) { return a - b; }
// ---------- constant definition ---------- //
template<class T>
T zero() { return 0; }
} // namespace _internal
} // namespace asalib
#line 10 "library/data-structure/matrix.hpp"
namespace asalib {
namespace matrix {
template<_internal::numeric_like T>
class Matrix {
public:
constexpr Matrix(): _n_row(0), _n_col(0) {};
constexpr Matrix(size_t n_row, size_t n_col): _n_row(n_row), _n_col(n_col) {
_data.resize(n_row, valarray<T>(n_col));
};
constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col) {
_data.resize(n_row, valarray<T>(n_col, x));
};
inline constexpr T& at(size_t i, size_t j) {
assert(i < _n_row);
assert(j < _n_col);
return _data[i][j];
}
inline constexpr T at(size_t i, size_t j) const {
assert(i < _n_row);
assert(j < _n_col);
return _data[i][j];
}
inline constexpr Matrix& operator+=(const T& x) {
_data += x;
return *this;
}
inline constexpr Matrix& operator-=(const T& x) {
_data -= x;
return *this;
}
inline constexpr Matrix& operator*=(const T& x) {
_data *= x;
return *this;
}
inline constexpr Matrix& operator/=(const T& x) {
_data /= x;
return *this;
}
inline constexpr Matrix& operator%=(const T& x) {
_data %= x;
return *this;
}
inline constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
inline constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
inline constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
inline constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
inline constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }
inline constexpr Matrix& operator+=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data += x._data;
return *this;
}
inline constexpr Matrix& operator-=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data -= x._data;
return *this;
}
inline constexpr Matrix& operator*=(const Matrix& x) {
assert(_n_col == x._n_row);
Matrix res(_n_row, x._n_col);
for (size_t i = 0; i < _n_row; ++i) {
for (size_t k = 0; k < _n_col; ++k) {
for (size_t j = 0; j < x._n_col; ++j) {
res._data[i][j] += _data[i][k] * x._data[k][j];
}
}
}
return *this = res;
}
inline constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
inline constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
inline constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
inline constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
inline constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
inline constexpr bool operator<(const Matrix& x) const { return _data < x._data; }
inline constexpr Matrix transpose() const {
Matrix res(_n_col, _n_row);
for (size_t i = 0; i < _n_row; ++i) {
for (size_t j = 0; j < _n_col; ++j) {
res._data[j][i] = _data[i][j];
}
}
return res;
}
template<integral U>
inline constexpr Matrix pow(U x) const {
assert(_n_row == _n_col);
Matrix res = I(_n_row);
Matrix a(*this);
while (x) {
if (x & 1) res *= a;
a *= a;
x >>= 1;
}
return res;
}
inline static constexpr Matrix I(size_t n) {
Matrix res(n, n);
for (size_t i = 0; i < n; ++i) {
res._data[i][i] = 1;
}
return res;
}
inline constexpr size_t n_row() const { return _n_row; }
inline constexpr size_t n_col() const { return _n_col; }
private:
size_t _n_row, _n_col;
valarray<valarray<T>> _data;
public:
// ---------- prototype ---------- //
inline constexpr T determinant() const;
template<_internal::numeric_like U>
inline constexpr U determinant() const;
};
} // namespace matrix
} // namespace asalib