2017/10/24 ~ 2017/12/1

更新サボってたら記憶が消えた

latexのalign環境で行列の下に説明文を追加する

DeepLearning Bookの式7.46を再現したかった.
mathopによって実現した.
ただ,行列のサイズが異なる場合説明文が上下で微妙にずれてしまう問題が残っている.
今回でいえば \mathbf{x}だけ少し下にずれている.
どうすれば良いのかはあとで調べる.

\mathop{\begin{bmatrix}
  18 \\
  5  \\
  15 \\
  -9 \\
  -3
\end{bmatrix}}_{\textstyle \mathbf{y}} &=& 
\mathop{\begin{bmatrix}
  4 & 0 &  0 & -2 &  0 &  0 \\
  0 & 0 & -1 &  0 &  3 &  0 \\
  0 & 5 &  0 &  0 &  0 &  0 \\
  1 & 0 &  0 & -1 &  0 & -4 \\
  1 & 0 &  0 &  0 & -5 &  0
\end{bmatrix}}_{\textstyle \mathbf{A}} & &
\mathop{\begin{bmatrix}
  2 \\
  3 \\
  -2 \\
  -5 \\
  1 \\
  4
\end{bmatrix}}_{\textstyle \mathbf{x}}
\end{align}

  \begin{align}
    \mathop{\begin{bmatrix}
        18 \\
        5  \\
        15 \\
        -9 \\
        -3
      \end{bmatrix}}_{\textstyle \mathbf{y}} &=& 
    \mathop{\begin{bmatrix}
          4 & 0 &  0 & -2 &  0 &  0 \\
          0 & 0 & -1 &  0 &  3 &  0 \\
          0 & 5 &  0 &  0 &  0 &  0 \\
          1 & 0 &  0 & -1 &  0 & -4 \\
          1 & 0 &  0 &  0 & -5 &  0
        \end{bmatrix}}_{\textstyle \mathbf{A}} & &
    \mathop{\begin{bmatrix}
        2 \\
        3 \\
        -2 \\
        -5 \\
        1 \\
        4
      \end{bmatrix}}_{\textstyle \mathbf{x}}
  \end{align}

tex.stackexchange.com

Amazing Mazes

入力データからグラフを作る部分を慎重に作る.
あとは幅優先or深さ優先を書く.

```cpp
//#define _GRIBCXX_DEBUG
#include
# define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;


class Graph {
public:
int n_node;
vector> edge;

Graph(int n_node) : n_node(n_node){
edge.resize(n_node);
};

void add_edge(int i, int j);
};

void Graph::add_edge(int i, int j) {
// cout << i << ", " << j << endl;
edge[i].push_back(j);
}

int main() {
int w, h, c;
int block;


while (cin >> w >> h, w) {
Graph g(w*h);

rep (i, 2*h-1) {
if (i%2 == 0) {
// yoko
rep (j, w-1) {
cin >> block;
int offset = w * (i / 2);
int c = offset + j;
if (block == 0) g.add_edge(c, c+1);
if (block == 0) g.add_edge(c+1, c);
}
}
else {
// tate
rep (j, w) {
int offset = w * *1 {
int current = que.front(); que.pop();

for (int next_ : g.edge[current]) {
if (cost[next_] == 1001001001) {
que.push(next_);
cost[next_] = cost[current] + 1;
// cout << "current: " << current << ", next: " << next_ << endl;
// cout << "current_cost: " << cost[current] << endl;
}
}
}

if (cost[w*h-1] == 1001001001) {
cout << 0 << endl;
}
else {
cout << cost[w*h-1] << endl;
}
}
return 0;
}
```

Amazing Mazes | Aizu Online Judge

Pythonにはオブジェクトの`__dict__`を返してくれる`vars`という組み込み関数がある.
これを使うと`argparse`の`Namespace`オブジェクトを辞書型のオブジェクトとして持ちたいときに便利.

stackoverflow.com

エイリアスを張っているコマンドに対しても呼び出し元を確認したいときは`type`コマンドが使える.

unix.stackexchange.com

PRMLの実装

github.com

Jupyter Notebook(Python)とPythonのプログラムがある.


argparseのdescriptionの中で改行する

RawTextHelpFormatterというクラスをformatterとして指定する必要がある.

stackoverflow.com

*1:i - 1) / 2); cin >> block; c = j + offset; if (block == 0) g.add_edge(c, c+w); if (block == 0) g.add_edge(c+w, c); } } } // bfs queue que; que.push(0); vector cost(w*h); rep (i, w*h) cost[i] = 1001001001; cost[0] = 1; while (!que.empty(