a01sa01to's competitive programming library.
#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.row(i)
で $i$ 行目A.col(j)
で $j$ 列目A.at(i, j)
で $i$ 行 $j$ 列目 (そのうち A[i, j]
で取得できるかも?わからん)Matrix::I(n)
で $n \times n$ の単位行列determinant<U>()
: 行列式を求める。 $O(n^3)$。 U
を指定しないと行列の要素型で求める。valarray で実装しているので、多少高速…なはず
#pragma once
#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(n_row * n_col) {};
constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col), _data(n_row * n_col, x) {};
// constexpr T& operator[](size_t i, size_t j) { return _data[i * _n_col + j]; }
// constexpr const T& operator[](size_t i, size_t j) const { return _data[i * _n_col + j]; }
// 使えないっぽいので at で代用
constexpr inline T& at(size_t i, size_t j) { return _data[i * _n_col + j]; }
constexpr T at(size_t i, size_t j) const { return _data[i * _n_col + j]; }
constexpr valarray<T> row(size_t i) const { return valarray<T>(_data[slice(i * _n_col, _n_col, 1)]); }
constexpr valarray<T> col(size_t j) const { return valarray<T>(_data[slice(j, _n_row, _n_col)]); }
constexpr Matrix operator+=(const T& x) {
_data += x;
return *this;
}
constexpr Matrix operator-=(const T& x) {
_data -= x;
return *this;
}
constexpr Matrix operator*=(const T& x) {
_data *= x;
return *this;
}
constexpr Matrix operator/=(const T& x) {
_data /= x;
return *this;
}
constexpr Matrix operator%=(const T& x) {
_data %= x;
return *this;
}
constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }
constexpr Matrix operator+=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data += x._data;
return *this;
}
constexpr Matrix operator-=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data -= x._data;
return *this;
}
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 j = 0; j < x._n_col; ++j) {
res.at(i, j) = (this->_data[slice(i * _n_col, _n_col, 1)] * x._data[slice(j, x._n_row, x._n_col)]).sum();
}
}
return *this = res;
}
constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
constexpr bool operator<(const Matrix& x) const { return _data < x._data; }
constexpr const Matrix transpose() const {
Matrix res(_n_col, _n_row);
for (size_t i = 0; i < _n_row; ++i) res._data[slice(i, _n_col, _n_row)] = _data[slice(i * _n_col, _n_col, 1)];
return res;
}
template<integral U>
constexpr Matrix pow(U x) {
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;
}
constexpr static Matrix I(size_t n) {
Matrix res(n, n);
res._data[slice(0, n, n + 1)] = 1;
return res;
}
constexpr size_t n_row() const { return _n_row; }
constexpr size_t n_col() const { return _n_col; }
private:
size_t _n_row, _n_col;
valarray<T> _data;
public:
// ---------- prototype ---------- //
constexpr T determinant() const;
template<_internal::numeric_like U>
constexpr U determinant() const;
};
} // namespace matrix
} // namespace asalib
#line 2 "library/data-structure/matrix.hpp"
#include <valarray>
using namespace std;
#line 2 "library/_internal/types.hpp"
#include <concepts>
#include <type_traits>
using namespace std;
#line 2 "library/_internal/modint-base.hpp"
#line 5 "library/_internal/modint-base.hpp"
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 8 "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 7 "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(n_row * n_col) {};
constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col), _data(n_row * n_col, x) {};
// constexpr T& operator[](size_t i, size_t j) { return _data[i * _n_col + j]; }
// constexpr const T& operator[](size_t i, size_t j) const { return _data[i * _n_col + j]; }
// 使えないっぽいので at で代用
constexpr inline T& at(size_t i, size_t j) { return _data[i * _n_col + j]; }
constexpr T at(size_t i, size_t j) const { return _data[i * _n_col + j]; }
constexpr valarray<T> row(size_t i) const { return valarray<T>(_data[slice(i * _n_col, _n_col, 1)]); }
constexpr valarray<T> col(size_t j) const { return valarray<T>(_data[slice(j, _n_row, _n_col)]); }
constexpr Matrix operator+=(const T& x) {
_data += x;
return *this;
}
constexpr Matrix operator-=(const T& x) {
_data -= x;
return *this;
}
constexpr Matrix operator*=(const T& x) {
_data *= x;
return *this;
}
constexpr Matrix operator/=(const T& x) {
_data /= x;
return *this;
}
constexpr Matrix operator%=(const T& x) {
_data %= x;
return *this;
}
constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }
constexpr Matrix operator+=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data += x._data;
return *this;
}
constexpr Matrix operator-=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data -= x._data;
return *this;
}
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 j = 0; j < x._n_col; ++j) {
res.at(i, j) = (this->_data[slice(i * _n_col, _n_col, 1)] * x._data[slice(j, x._n_row, x._n_col)]).sum();
}
}
return *this = res;
}
constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
constexpr bool operator<(const Matrix& x) const { return _data < x._data; }
constexpr const Matrix transpose() const {
Matrix res(_n_col, _n_row);
for (size_t i = 0; i < _n_row; ++i) res._data[slice(i, _n_col, _n_row)] = _data[slice(i * _n_col, _n_col, 1)];
return res;
}
template<integral U>
constexpr Matrix pow(U x) {
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;
}
constexpr static Matrix I(size_t n) {
Matrix res(n, n);
res._data[slice(0, n, n + 1)] = 1;
return res;
}
constexpr size_t n_row() const { return _n_row; }
constexpr size_t n_col() const { return _n_col; }
private:
size_t _n_row, _n_col;
valarray<T> _data;
public:
// ---------- prototype ---------- //
constexpr T determinant() const;
template<_internal::numeric_like U>
constexpr U determinant() const;
};
} // namespace matrix
} // namespace asalib