Skip to content

Commit

Permalink
update structure/matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
idat50me committed Nov 29, 2023
1 parent 3fc6a95 commit 29c7959
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 6 additions & 4 deletions structure/docs/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ documentation_of: ../matrix.cpp
## コンストラクタ
- `matrix()`:$0$ 行 $0$ 列の行列を作る.
- `matrix(h, w)``h``w` 列の行列を作る.
- `matrix(h, w, init)``init` を初期値とする `h``w` 列の行列を作る.
- `matrix(m_init)``m_init`を初期値とする行列を作る.

## メンバ関数
Expand All @@ -19,9 +20,10 @@ documentation_of: ../matrix.cpp
- `in(h, w)``h``w` 列の行列を作り標準入力する.
- `out()`:要素を空白区切りで標準出力する.
- `operator[](idx)``idx` 番目の要素である $1$ 次元配列の参照を返す.`vector` を用いた多次元配列と同様に扱える.
- `identity(n)``n` 次単位行列を返す.
- 各算術演算子:行列和・行列差・行列積を行う.
- `pow(a, ex)`:行列累乗の結果を返す.
- `operator+, operator-, operator*`:行列和・行列差・行列積.通常の算術演算子と同様に扱える.
- `pow(ex)`:行列累乗の結果を返す.

- `static identity(n)``n` 次単位行列を返す.

## 計算量
$H$ 行 $W$ 列の行列を扱うとする.
Expand All @@ -38,7 +40,7 @@ $H_1$ 行 $W_1$ 列の行列と $H_2$ 行 $W_2$ 列の行列を扱うとする
- 行列積:$O(H_1W_1W_2)$

$k$ 次正方行列を扱うとする.
- `pow(a, ex)`:$O(k^3)$
- `pow(ex)`:$O(k^3 \log \mathrm{ex})$

## 参考
- [初期化子リスト - cpprefjp C++日本語リファレンス](https://cpprefjp.github.io/lang/cpp11/initializer_lists.html)
17 changes: 9 additions & 8 deletions structure/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ template<typename T> struct matrix {
public:
matrix() {}
matrix(int h, int w) : m(h, vector<T>(w)) {}
matrix(int h, int w, T init) : m(h, vector(w, init)) {}
matrix(int h, int w, T init) : m(h, vector<T>(w, init)) {}
matrix(const initializer_list<initializer_list<T>> m_init) : m(m_init.begin(), m_init.end()) {}

void assign(int h, int w) {
Expand Down Expand Up @@ -59,12 +59,6 @@ template<typename T> struct matrix {
return m[idx];
}

static matrix identity(int n) {
matrix res(n, n, 0);
for(int i = 0; i < n; i++) res[i][i] = 1;
return res;
}

matrix &operator+=(const matrix &a) {
int h = height(), w = width();
assert(h == a.height() && w == a.width());
Expand Down Expand Up @@ -100,7 +94,8 @@ template<typename T> struct matrix {
return matrix(*this) *= a;
}

static matrix pow(matrix a, long long ex) {
matrix pow(long long ex) {
matrix a = this->m;
assert(a.height() == a.width());
matrix res = identity(a.height());
while(ex > 0) {
Expand All @@ -110,4 +105,10 @@ template<typename T> struct matrix {
}
return res;
}

static matrix identity(int n) {
matrix res(n, n, 0);
for(int i = 0; i < n; i++) res[i][i] = 1;
return res;
}
};

0 comments on commit 29c7959

Please sign in to comment.