Asa's CP Library

a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: Matrix
(library/data-structure/matrix.hpp)

概要

行列

使い方

Matrix<T>(n, m) で $n \times m$ の行列を作成する。

実装は別ファイル

Tip

valarray で実装しているので、多少高速…なはず

Depends on

Required by

Verified with

Code

#pragma once

#include <cassert>
#include <concepts>
#include <cstddef>
#include <valarray>
using namespace std;

#include "../_internal/types.hpp"

namespace asalib::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>(x, n_col));
    };

    constexpr T& at(size_t i, size_t j) {
      assert(i < _n_row);
      assert(j < _n_col);
      return _data[i][j];
    }
    constexpr T at(size_t i, size_t j) const {
      assert(i < _n_row);
      assert(j < _n_col);
      return _data[i][j];
    }

    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 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;
    }
    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 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>
    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;
    }

    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;
    }

    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<valarray<T>> _data;

    public:
    // ---------- prototype ---------- //
    constexpr T determinant() const;
    template<_internal::numeric_like U>
    constexpr U determinant() const;
  };
}  // namespace asalib::matrix
#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"

#include <type_traits>
using namespace std;

namespace asalib::_internal {
  class modint_base {};

  template<typename T>
  concept is_modint = is_base_of_v<modint_base, T>;
}  // namespace asalib::_internal
#line 7 "library/_internal/types.hpp"

namespace asalib::_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 asalib::_internal
#line 10 "library/data-structure/matrix.hpp"

namespace asalib::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>(x, n_col));
    };

    constexpr T& at(size_t i, size_t j) {
      assert(i < _n_row);
      assert(j < _n_col);
      return _data[i][j];
    }
    constexpr T at(size_t i, size_t j) const {
      assert(i < _n_row);
      assert(j < _n_col);
      return _data[i][j];
    }

    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 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;
    }
    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 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>
    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;
    }

    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;
    }

    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<valarray<T>> _data;

    public:
    // ---------- prototype ---------- //
    constexpr T determinant() const;
    template<_internal::numeric_like U>
    constexpr U determinant() const;
  };
}  // namespace asalib::matrix
Back to top page