Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/02advanced/index.mdx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sidebar_position: 1
import ViewSource from "@site/src/components/ViewSource";
import Answer from "@site/src/components/Answer";
import LifeGame from "@site/src/components/LifeGame";
import PlayArrowIcon from "@mui/icons-material/PlayArrow";

# ライフゲーム

Expand Down Expand Up @@ -86,7 +87,7 @@ import LifeGame from "@site/src/components/LifeGame";

## ライフゲームのプログラム

早速、ライフゲームの Python でのプログラムを見てみましょう。この後、詳しく解説するので、分からなくても構いません。`Open in Colab` と書いてあるボタンを押してから実行して、実際に動く様子を確認してみてください
早速、ライフゲームの Python でのプログラムを見てみましょう。この後、詳しく解説するので、分からなくても構いません。ブラウザ上で動かせるようにしたので、<PlayArrowIcon/>ボタンを押して実際に動く様子を確認してみてください

<ViewSource path="/life-game/life_game.ipynb" />

Expand Down Expand Up @@ -120,8 +121,10 @@ board = [[1, 1, 0],

<ViewSource path="/life-game/is_alive.ipynb" />

`neighbors_cnt in [2, 3]` は `neighbors_cnt == 2 or neighbors_cnt == 3` と同じです。

次に、次の世代の盤面を計算する関数を考えます。
引数は `board` として、次の世代の様子を格納する配列を作ってそこに結果を格納していきましょう。配列の作り方はいろいろありますが、ここでは大学が用意した ita ライブラリを使いましょう。ita ライブラリを使うには、プログラムの冒頭に次のように書いてライブラリをインポートする必要があります
引数は `board` として、次の世代の様子を格納する配列を作ってそこに結果を格納していきましょう。配列の作り方はいろいろありますが、ここでは大学が用意した ita ライブラリを使いましょう。ita ライブラリを使うには、パッケージのインストールの項で述べたようにプログラムの冒頭に次のように書いてライブラリをインポートする必要があります

```python
!pip install ita
Expand Down Expand Up @@ -158,7 +161,7 @@ ita.array.make1d(要素数)
まず、次のようなコードを書いておきましょう。これは、おまじないだと思ってください。Google Colaboratory 上でうまく表示するために、必要です。

```python
%matplotlib notebook
%matplotlib inline
```

次に、次のように書くことでサンプルデータを取得することができます。これはライフゲームにおいて有名なデータで、グライダーが移動していくように見えるものです。
Expand All @@ -167,7 +170,7 @@ ita.array.make1d(要素数)
data = ita.lifegame_glider()
```

画像を作るのは、ita を使えば簡単です。三次元配列を次のように渡してあげれば、1 のところは黒で 0 のところは白にしてアニメーションをつくってくれます。
画像を作るのは、ita ライブラリを使えば簡単です。三次元配列を次のように渡してあげれば、1 のところは黒で 0 のところは白にしてアニメーションをつくってくれます。

```python
ita.plot.animation_show(三次元配列)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Python で画像を表現してみましょう。

## 白黒の表現

東京大学がアルゴリズム入門の授業用に作った `ita` ライブラリを使えば、簡単に画像を表現できます。
大学がアルゴリズム入門の授業用に作った `ita` ライブラリを使えば、簡単に画像を表現できます。

次のように、0 と 1 が格納された二次元配列を作って、それを `ita` ライブラリの `image_show` 関数に与えれば、白黒の画像を表現できます。0 が黒、1 が白となります。

Expand Down Expand Up @@ -77,7 +77,11 @@ Python で画像を表現してみましょう。
![black to green](black_to_green.png)

<Answer>
<ViewSource path="/image/black_to_green.ipynb" />

<ViewSource path="/image/black_to_green.ipynb" />

`配列.append(要素)` とすることで、配列の末尾に要素を追加できます。

</Answer>

### 練習問題 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ $$

<ViewSource path="/simulation/uniform_linear_motion.ipynb" />

`int` 関数は引数に指定した数値を整数値にします。

:::note
この微分方程式は、次のようにすれば簡単に解けます。確かにこの結果を用いれば、簡単にプログラムを書けるので、差分方程式を使う必要性があまり感じられないかもしれません。しかし、世の中には解析的に解けない微分方程式も多いので、差分方程式を使って近似解を求めることも時には重要になってきます。

Expand Down Expand Up @@ -71,6 +73,8 @@ $$

<ViewSource path="/simulation/uniform_linear_motion_return_array.ipynb" />

`配列.append(要素)` とすることで、配列の末尾に要素を追加できます。

物体のあるセルを白く塗る関数を作って、可視化してみましょう。

物体のある $x$ 座標と $y$ 座標を取得して、そこのセルだけ白く塗る関数を作ってみましょう。次のようになります。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import DocCardList from "@theme/DocCardList";

今回は、物体の運動をシミュレーションしてみましょう。

はじめに簡単なシミュレーションをして、その後複雑なシミュレーションを行いましょう。
はじめは簡単なシミュレーションをして、その後複雑なシミュレーションを行います。

<DocCardList />

:::tip

Expand All @@ -17,5 +19,3 @@ import DocCardList from "@theme/DocCardList";
英語では simulation と綴るのを考えれば、すぐにわかりますね。

:::

<DocCardList />
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ students = [

次のようにして、簡単に計算することができますね。

<ViewSource path="/search-algorithm/linear_search.ipynb" />
<ViewSource path="/search/linear_search.ipynb" />

このプログラムは、配列の先頭から順に目的の値と一致するかを調べていくようになっています。このようなデータの探索アルゴリズムは、線型探索と呼ばれます。
このプログラムの時間計算量は、$O(n)$ です。
Expand All @@ -81,7 +81,7 @@ students = [
例えば、数列が `[1, 2, 3, 4, 5, 5, 7, 8, 10]` で $N = 5$ ならば、4 となります。

<Answer>
<ViewSource path="/search-algorithm/linear_practice.ipynb" />
<ViewSource path="/search/linear_practice.ipynb" />
</Answer>

## 二分探索
Expand Down Expand Up @@ -171,19 +171,19 @@ sequenceDiagram

次のようになります。

<ViewSource path="/search-algorithm/binary_search.ipynb" />
<ViewSource path="/search/binary_search.ipynb" />

### 二分探索のライブラリ

実は、二分探索を行うには [`bisect`](https://docs.python.org/ja/3/library/bisect.html) というライブラリがあります。

`bisect_left` 関数は、ある数を配列に挿入しようとした時、すでに配列に同じ数がある場合それらの数の中で最**小**の要素の左側に挿入した時のインデックスを返します。つまり、次のようになります。

<ViewSource path="/search-algorithm/bisect_left.ipynb" />
<ViewSource path="/search/bisect_left.ipynb" />

`bisect_right` 関数は、ある数を配列に挿入しようとした時、すでに配列に同じ数がある場合それらの数の中で最**大**の要素の左側に挿入した時のインデックスを返します。つまり、次のようになります。

<ViewSource path="/search-algorithm/bisect_right.ipynb" />
<ViewSource path="/search/bisect_right.ipynb" />

:::info
二分探索のアイディアは非常に簡単ですが、正確に実装することはとても難しいことで知られています。
Expand All @@ -206,7 +206,7 @@ sequenceDiagram
例えば、数列が `[1, 2, 3, 4, 5, 5, 7, 8, 10]` で $N = 5$ ならば、4 となります。

<Answer>
<ViewSource path="/search-algorithm/binary_left_practice.ipynb" />
<ViewSource path="/search/binary_left_practice.ipynb" />
</Answer>

### 練習問題 2
Expand All @@ -216,5 +216,5 @@ sequenceDiagram
例えば、数列が `[1, 2, 3, 4, 5, 5, 7, 8, 10]` で $N = 5$ ならば、5 となります。

<Answer>
<ViewSource path="/search-algorithm/binary_right_practice.ipynb" />
<ViewSource path="/search/binary_right_practice.ipynb" />
</Answer>
File renamed without changes.
7 changes: 7 additions & 0 deletions docs/02algorithms/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
sidebar_position: 3
---

# 様々なアルゴリズム

Python の基礎は学ぶことができたので、ここからは Python を用いて実際にさまざまなアルゴリズムについて見ていきます。
95 changes: 42 additions & 53 deletions static/life-game/life_game.ipynb

Large diffs are not rendered by default.