cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 逆行列 (inverse matrix)
(include/emthrm/math/matrix/inverse_matrix.hpp)

時間計算量

$O(M^2 N)$

仕様

名前 戻り値
template <typename T, typename U = double>
Matrix<U> inverse_matrix(const Matrix<T>& a, const U eps = 1e-8);
行列 $A$ の逆行列。ただし存在しないときは空行列を返す。

Submissons

https://judge.yosupo.jp/submission/50854

Depends on

Verified with

Code

#ifndef EMTHRM_MATH_MATRIX_INVERSE_MATRIX_HPP_
#define EMTHRM_MATH_MATRIX_INVERSE_MATRIX_HPP_

#include <algorithm>
#include <iterator>
#include <utility>

#include "emthrm/math/matrix/matrix.hpp"

namespace emthrm {

template <typename T, typename U = double>
Matrix<U> inverse_matrix(const Matrix<T>& a, const U eps = 1e-8) {
  const int n = a.nrow();
  Matrix<U> b(n, n << 1, 0);
  for (int i = 0; i < n; ++i) {
    std::copy(a[i].begin(), a[i].end(), b[i].begin());
    b[i][n + i] = 1;
  }
  for (int col = 0; col < n; ++col) {
    int pivot = -1;
    U mx = eps;
    for (int row = col; row < n; ++row) {
      const U abs = (b[row][col] < 0 ? -b[row][col] : b[row][col]);
      if (abs > mx) {
        pivot = row;
        mx = abs;
      }
    }
    if (pivot == -1) return Matrix<U>(0, 0);
    std::swap(b[col], b[pivot]);
    U tmp = b[col][col];
    for (int col2 = 0; col2 < (n << 1); ++col2) {
      b[col][col2] /= tmp;
    }
    for (int row = 0; row < n; ++row) {
      if (row != col && (b[row][col] < 0 ? -b[row][col] : b[row][col]) > eps) {
        tmp = b[row][col];
        for (int col2 = 0; col2 < (n << 1); ++col2) {
          b[row][col2] -= b[col][col2] * tmp;
        }
      }
    }
  }
  Matrix<U> inv(n, n);
  for (int i = 0; i < n; ++i) {
    std::copy(std::next(b[i].begin(), n), b[i].end(), inv[i].begin());
  }
  return inv;
}

}  // namespace emthrm

#endif  // EMTHRM_MATH_MATRIX_INVERSE_MATRIX_HPP_
#line 1 "include/emthrm/math/matrix/inverse_matrix.hpp"



#include <algorithm>
#include <iterator>
#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 9 "include/emthrm/math/matrix/inverse_matrix.hpp"

namespace emthrm {

template <typename T, typename U = double>
Matrix<U> inverse_matrix(const Matrix<T>& a, const U eps = 1e-8) {
  const int n = a.nrow();
  Matrix<U> b(n, n << 1, 0);
  for (int i = 0; i < n; ++i) {
    std::copy(a[i].begin(), a[i].end(), b[i].begin());
    b[i][n + i] = 1;
  }
  for (int col = 0; col < n; ++col) {
    int pivot = -1;
    U mx = eps;
    for (int row = col; row < n; ++row) {
      const U abs = (b[row][col] < 0 ? -b[row][col] : b[row][col]);
      if (abs > mx) {
        pivot = row;
        mx = abs;
      }
    }
    if (pivot == -1) return Matrix<U>(0, 0);
    std::swap(b[col], b[pivot]);
    U tmp = b[col][col];
    for (int col2 = 0; col2 < (n << 1); ++col2) {
      b[col][col2] /= tmp;
    }
    for (int row = 0; row < n; ++row) {
      if (row != col && (b[row][col] < 0 ? -b[row][col] : b[row][col]) > eps) {
        tmp = b[row][col];
        for (int col2 = 0; col2 < (n << 1); ++col2) {
          b[row][col2] -= b[col][col2] * tmp;
        }
      }
    }
  }
  Matrix<U> inv(n, n);
  for (int i = 0; i < n; ++i) {
    std::copy(std::next(b[i].begin(), n), b[i].end(), inv[i].begin());
  }
  return inv;
}

}  // namespace emthrm
Back to top page