{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "

1  ファイルの入出力(InputOutput)
1.1  path操作(python)
1.1.1  cwd
1.1.2  mk, cd, ls
1.1.3  globでfull_pathで表示
1.2  fileの読み書き
1.2.1  単純な読み書き
1.2.2  sprintfからの出力
1.3  animationの出力
1.4  Mapleのフィルターとしての利用法
2  for-loopの基本技(for-loop2)
2.1  ランダムな配列の生成
2.2  要素数の取り出し
2.3  すべての要素の表示
2.4  
2.4.0.1  課題:積を求めよ.
2.5  値の代入
2.5.0.1  課題:先の和と組み合わせて,全要素の和が1になるように規格化せよ.
2.5.0.2  課題:配列Bへ逆順に代入せよ.
2.6  一桁の整数5個から5桁の整数を作る
2.6.0.1  課題:上記と同様にして,10桁の2進数を10進数へ変換せよ
2.7  255以下の10進数をランダムに生成して,8桁の2進数へ変換せよ.
2.7.0.1  課題:8桁の整数のそれぞれの桁の値を配列に格納せよ.
2.8  小数点以下8桁のそれぞれの桁の数を配列に格納せよ
2.9  最大数
2.9.0.1  課題:最小値を求めよ.
2.10  ある値の上下で分けた個数
2.11  素数かどうかの判定
2.12  2つの要素の入れ替え
2.13  コインの表向きの枚数
2.13.0.1  課題:1..6のサイコロを20回振って,出た目を記録せよ.
2.14  2次元配列
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "
\n", "その他(miscellaneous)\n", "
\n", "
\n", "
\n", "file:/~/python/doing_math_with_python/miscellaneous.ipynb\n", "
\n", "cc by Shigeto R. Nishitani 2017 \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "この資料は[わたしのMapleテキスト](https://ist.ksc.kwansei.ac.jp/~nishitani/MapleHiki/) からpythonに逐次訳したものです.で,miscellaneousに書かれている内容は普通のprogramming言語では当たり前の作業をmapleソフトから少し離れてCLIのmapleで実現する工夫を書いています.pythonは普通のprogramming言語ですから,この部分はそれほど必要性が高くなさそうです.おいおい,気が付いた時に加筆して行きます." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ファイルの入出力(InputOutput)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "自作したanimationをプレゼン資料に貼り付けたり,測定データなどを読み込んでMapleで手軽に表示,加工したくなります.このとき必要となるファイルとのやりとりを紹介します.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## path操作(python)" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### cwd" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "'/Users/bob/python/doing_math_with_python/symbolic_math'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", " \n", "cwd = os.getcwd()\n", "cwd" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### mk, cd, ls" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "['miscellaneous.ipynb',\n", " 'gradient_descent.gif',\n", " 'differential.ipynb',\n", " 'README.html~',\n", " 'lecture_trial_memo.org',\n", " 'validate.data',\n", " 'first_leaf.ipynb',\n", " 'train_b.data',\n", " '.DS_Store',\n", " 'linear_solve.ipynb',\n", " 'group_works_2_ans.ipynb',\n", " 'dir2',\n", " 'figs',\n", " 'linear_algebra_sympy.ipynb',\n", " 'integral.ipynb',\n", " 'validate_b.data',\n", " 'python_files_and_dir.ipynb',\n", " 'group_works_3_ans.ipynb',\n", " 'linear_algebra_scipy.ipynb',\n", " 'README.html',\n", " 'equals.ipynb',\n", " 'Rakefile',\n", " 'functions.ipynb',\n", " 'validate_A.data',\n", " 'group_works_3_breast_cancer.ipynb',\n", " 'group_works_1.ipynb',\n", " 'equation_manipulation.ipynb',\n", " '.ipynb_checkpoints',\n", " 'README.org',\n", " 'cg.ipynb',\n", " 'train_A.data',\n", " 'group_works_2.ipynb',\n", " 'group_works_4.pdf',\n", " 'group_works_1_ans.ipynb',\n", " 'group_works_ans',\n", " 'group_works_3.pdf',\n", " 'train.data']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "hidden": true }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_dir = 'dir4'\n", "os.mkdir(new_dir)\n", "os.chdir(new_dir)\n", "os.getcwd()\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "hidden": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/bob/python/doing_math_with_python/symbolic_math\n" ] }, { "data": { "text/plain": [ "['miscellaneous.ipynb',\n", " 'gradient_descent.gif',\n", " 'differential.ipynb',\n", " 'README.html~',\n", " 'lecture_trial_memo.org',\n", " 'validate.data',\n", " 'first_leaf.ipynb',\n", " 'train_b.data',\n", " '.DS_Store',\n", " 'linear_solve.ipynb',\n", " 'group_works_2_ans.ipynb',\n", " 'dir2',\n", " 'figs',\n", " 'linear_algebra_sympy.ipynb',\n", " 'dir4',\n", " 'integral.ipynb',\n", " 'validate_b.data',\n", " 'python_files_and_dir.ipynb',\n", " 'group_works_3_ans.ipynb',\n", " 'linear_algebra_scipy.ipynb',\n", " 'README.html',\n", " 'equals.ipynb',\n", " 'Rakefile',\n", " 'functions.ipynb',\n", " 'validate_A.data',\n", " 'group_works_3_breast_cancer.ipynb',\n", " 'group_works_1.ipynb',\n", " 'equation_manipulation.ipynb',\n", " '.ipynb_checkpoints',\n", " 'README.org',\n", " 'cg.ipynb',\n", " 'train_A.data',\n", " 'group_works_2.ipynb',\n", " 'group_works_4.pdf',\n", " 'group_works_1_ans.ipynb',\n", " 'group_works_ans',\n", " 'group_works_3.pdf',\n", " 'train.data']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.chdir('..')\n", "print(os.getcwd())\n", "os.listdir()" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### globでfull_pathで表示" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "hidden": true, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/bob/python/doing_math_with_python/symbolic_math/dir2/tmp.txt\n", "/Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir2\n", "/Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir4\n", "/Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir3\n", "/Users/bob/python/doing_math_with_python/symbolic_math/dir2/test.txt\n" ] } ], "source": [ "import glob\n", "\n", "files = glob.glob(cwd+'/dir2'+'/*')\n", "for file in files:\n", " print(file)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## fileの読み書き" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "hidden": true }, "outputs": [], "source": [ "os.getcwd()\n", "os.chdir('dir4')" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### 単純な読み書き" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true, "hidden": true }, "outputs": [], "source": [ "data = ['x', 'y']\n", "text = \"\\n\".join(data)\n", "with open(\"tmp.txt\", \"w\") as f:\n", " f.write(text)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "hidden": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x\\n', 'y']\n" ] } ], "source": [ "with open(\"tmp.txt\", \"r\") as f:\n", " lines = f.readlines()\n", " print(lines)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "hidden": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x\n", "y\n" ] } ], "source": [ "for line in lines:\n", " print(line.rstrip())" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### sprintfからの出力" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "hidden": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x', 0, 'y', 0]\n", " x: 0, y: 0\n", "\n", "['x', 1, 'y', 1]\n", " x: 1, y: 1\n", "\n" ] } ], "source": [ "data = [['x', 0, 'y', 0],\n", " ['x', 1, 'y', 1]]\n", "\n", "with open(\"tmp.txt\", \"w\") as f:\n", " for dd in data:\n", " print(dd)\n", " string = '%4s:%3d, %4s:%3d\\n' % tuple(dd)\n", " print(string)\n", " f.writelines(string)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "hidden": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x', 0, 'y', 0]\n", " x: 0, y: 0\n", "\n", "['x', 1, 'y', 1]\n", " x: 1, y: 1\n", "\n" ] } ], "source": [ "data = [['x', 0, 'y', 0],\n", " ['x', 1, 'y', 1]]\n", "\n", "f = open(\"tmp.txt\", \"w\")\n", "for dd in data:\n", " print(dd)\n", " string = '%4s:%3d, %4s:%3d\\n' % tuple(dd)\n", " print(string)\n", " f.writelines(string)\n", "\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "hidden": true, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " x: 0, y: 0\n", " x: 1, y: 1\n" ] } ], "source": [ "file = open(\"tmp.txt\", \"r\")\n", "lines = file.readlines()\n", "for line in lines:\n", " print(line.rstrip())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## animationの出力 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "animationなどのgif形式のplotを外部ファイルへ出力して表示させるには,以下の一連のコマンドのようにする.\n", "```maple\n", "> plotsetup(gif,plotoutput=file2): \n", "> display(tmp,insequence=true);\n", "> plotsetup(default):\n", "```\n", "こいつをquicktimeなどに食わせれば,Maple以外のソフトで動画表示が可能となる.3次元図形の標準規格であるvrmlも同じようにして作成することが可能です(?vrml;参照).\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mapleのフィルターとしての利用法 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "linux版やMac版では文字ベースのmapleを使って,filterとして高度な作業をさせることが出来ます.スクリプトの中に外部ファイルとの入出力を組み込めば,いままで紹介してきた複雑な動作をブラックボックスの内部処理としてそのまま使えます.\n", "```maple\n", "[bob@asura0 ~/test]$ cat test.txt\n", "T:=readdata(\"./data101\");\n", "interface(quiet=true);\n", "writeto(\"./result\");\n", "print(T[1]);\n", "writeto(terminal);\n", "interface(quiet=false);\n", "```\n", "とすれば,data101から読み込んだデータに何らかの処理を施した結果をresultに打ち出すことが可能.interface(quiet=true)で余計な出力を抑止しています.これをmapleに食わせると\n", "```maple\n", "[bob@asura0 ~/test]$ /usr/local/maple9.5/bin/maple < test.txt\n", " |\\^/| Maple 9.5 (IBM INTEL LINUX)\n", "._|\\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2004\n", " \\ MAPLE / All rights reserved. Maple is a trademark of\n", " <____ ____> Waterloo Maple Inc.\n", " | Type ? for help.\n", "> T:=readdata(\"./data101\");\n", " T := [1.23, 2.35]\n", "> interface(quiet=true);\n", " false\n", " true\n", "> quit\n", "bytes used=211000, alloc=262096, time=0.00\n", "```\n", "めでたく出力されているはず.\n", "```maple\n", "[bob@asura0 ~/test]$ cat result\n", "1.23\n", "```\n", "\n", "Mac版でのパス(path)は下記を参照.\n", "```maple\n", "bob% /Library/Frameworks/Maple.framework/Versions/15/bin/maple\n", " |\\^/| Maple 15 (APPLE UNIVERSAL OSX)\n", "._|\\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2011\n", " \\ MAPLE / All rights reserved. Maple is a trademark of\n", " <____ ____> Waterloo Maple Inc.\n", " | Type ? for help.\n", "> quit\n", "memory used=1.2MB, alloc=1.4MB, time=0.07\n", "```\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# for-loopの基本技(for-loop2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ランダムな配列の生成 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1から100までの整数5個からなる配列の生成.\n", "```maple\n", "> restart: \n", " roll:=rand(1..100): \n", " n:=5: \n", " A:=[seq(roll(),i=1..n)];\n", "```\n", "```maple\n", " [93, 45, 96, 6, 98]\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 要素数の取り出し \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for-loopで配列を使うときには,配列の大きさ(要素数)がfor-loopの終了条件になることが多い.\n", "リスト構造では単純にnopsとすればよい.\n", "```maple\n", "> nops(A);\n", "```\n", "```maple\n", " 5\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## すべての要素の表示 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "配列はおなじ箱が沢山用意されていると考えればよい.配列をfor-loopで使うときは,箱を指す数(示数,index)をいじっているのか,箱の中身(要素)をいじっているのかを意識すれば,動作を理解しやすい.\n", "```maple\n", "> for i from 1 to n do \n", " print(i,A[i]); \n", " end do;\n", "```\n", "```maple\n", " 1, 93\n", " 2, 45\n", " 3, 96\n", " 4, 6\n", " 5, 98\n", "```\n", "逆順の表示\n", "```maple\n", "> for i from n by -1 to 1 do\n", " print(i,A[i]); \n", " end do;\n", "```\n", "```maple\n", " 5, 98\n", " 4, 6\n", " 3, 96\n", " 2, 45\n", " 1, 93\n", "```\n", "逆順の表示2\n", "```maple\n", "> for i from 1 to n do\n", " print(n-i+1,A[n-i+1]); \n", " end do;\n", "```\n", "```maple\n", " 5, 98\n", " 4, 6\n", " 3, 96\n", " 2, 45\n", " 1, 93\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 和 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> sum1:=0: \n", " for i from 1 to n do \n", " sum1:=sum1+A[i]; \n", " end do: \n", " sum1;\n", "```\n", "```maple\n", " 338\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:積を求めよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 値の代入 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> k:=64: \n", " for i from 1 to n do \n", " A[i]:=A[i]/k; \n", " end do: \n", " A;\n", "```\n", "```maple\n", " [93/64, 45/64, 3/2, 3/32, 49/32]\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:先の和と組み合わせて,全要素の和が1になるように規格化せよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:配列Bへ逆順に代入せよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 一桁の整数5個から5桁の整数を作る \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "まず,一桁の整数でできるランダムな配列を作成する.\n", "```maple\n", "> roll:=rand(0..9): n:=5: A:=[seq(roll(),i=1..n)];\n", "```\n", "```maple\n", " A := [3, 5, 4, 0, 7]\n", "```\n", "```maple\n", "> sum1:=0; \n", " for i from 1 to n do \n", " sum1:=sum1*10+A[i]; \n", " end do: \n", " sum1;\n", "```\n", "```maple\n", " 0\n", " 35407\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:上記と同様にして,10桁の2進数を10進数へ変換せよ \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 255以下の10進数をランダムに生成して,8桁の2進数へ変換せよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> n:=8: 2^n;\n", "```\n", "```maple\n", " 256\n", "```\n", "```maple\n", "> roll:=rand(0..255):\n", " B:=roll();\n", "```\n", "```maple\n", " 161\n", "```\n", "ちょっとカンニング.\n", "```maple\n", "> convert(B,binary);\n", "```\n", "```maple\n", " 10100001\n", "```\n", "\n", "```maple\n", "> A:=[]:\n", " for i from 1 to n do\n", " A:=[irem(B,2),op(A)];\n", " B:=iquo(B,2);\n", " end do:\n", " A;\n", "```\n", "$$\n", "[1, 0, 1, 0, 0, 0, 0, 1]\n", "$$\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:8桁の整数のそれぞれの桁の値を配列に格納せよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "8桁の整数は以下のようにして作られる.\n", "```maple\n", "> n:=8; \n", " roll:=rand(10^(n-1)..10^n): \n", " B:=roll();\n", "```\n", "```maple\n", " 8\n", " 17914675\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 小数点以下8桁のそれぞれの桁の数を配列に格納せよ \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> n:=8: \n", " roll:=rand(10^(n-1)..10^n): \n", " B:=evalf(roll()/10^n);\n", "```\n", "```maple\n", " 0.6308447100\n", "```\n", "```maple\n", "> B:=10*B:\n", " A:=[]:\n", " for i from 1 to n do\n", " A:=[op(A),floor(B)];\n", " B:=(B-A[i])*10;\n", " end do:\n", " A;\n", "```\n", "$$\n", "[6, 3, 0, 8, 4, 4, 7, 1]\n", "$$\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 最大数 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> roll:=rand(1..100): \n", " n:=5: \n", " A:=[seq(roll(),i=1..n)]; \n", " i_max:=A[1]: \n", " for i from 2 to n do \n", " if (A[i]>i_max) then \n", " i_max:=A[i]; \n", " end if; \n", " end do: \n", " i_max;\n", "```\n", "```maple\n", " 64\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:最小値を求めよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ある値の上下で分けた個数 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> roll:=rand(1..100): \n", " n:=5: A:=[seq(roll(),i=1..n)];\n", " i_div:=50:i_low:=0:i_high:=0: \n", " for i from 1 to n do \n", " if (A[i]>i_div) then\n", " i_high:=i_high+1; \n", " else \n", " i_low:=i_low+1; \n", " end if \n", " end do; \n", " print(i_low,i_high);\n", "```\n", "```maple\n", " 2, 3\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 素数かどうかの判定 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> n:=10; \n", " for i from 1 to n do \n", " if (isprime(i)) then \n", " print(i); \n", " end if; \n", " end do;\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2つの要素の入れ替え \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> roll:=rand(1..100): n:=5: A:=[seq(roll(),i=1..n)]; sel:=rand(1..n):\n", " isel:=sel(); \n", " jsel:=sel(); \n", " a:=A[isel]; b:=A[jsel]; A[isel]:=b; A[jsel]:=a; \n", " A;\n", "```\n", "```maple\n", " [60, 93, 14, 50, 47]\n", " 2\n", " 4\n", " 93\n", " 50\n", " 50\n", " 93\n", " [60, 50, 14, 93, 47]\n", "```\n", "より短くするには,\n", "```maple\n", "> roll:=rand(1..100):\n", " n:=5:\n", " A:=[seq(roll(),i=1..n)];\n", " sel:=rand(1..n):\n", " isel:=sel();\n", " jsel:=sel();\n", " a:=A[isel];\n", " A[isel]:=A[jsel];\n", " A[jsel]:=a;\n", " A;\n", "```\n", "```maple\n", " [9, 77, 59, 16, 1]\n", " 5\n", " 4\n", " 1\n", " 16\n", " 1\n", " [9, 77, 59, 1, 16]\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## コインの表向きの枚数 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```maple\n", "> roll:=rand(0..1):\n", " n:=10:\n", " up:=0:\n", " for i from 1 to n do\n", " trial:=roll();\n", " if (trial=1) then \n", " up:=up+1;\n", " end if;\n", " end do:\n", " up;\n", "```\n", "```maple\n", " 5\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 課題:1..6のサイコロを20回振って,出た目を記録せよ. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "記録には,要素が0の配列を最初に用意し,出た目を示数にして配列の要素をひとつずつ増やす.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2次元配列 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2次元配列に対しても同様の操作ができる.ここでは列に対する規格化を示す.\n", "```maple\n", "> roll:=rand(1..5):\n", " n:=3:\n", " A:=[seq([seq(roll(),i=1..n)],j=1..n)];\n", "```\n", "```maple\n", " A := [[5, 2, 2], [2, 3, 2], [4, 2, 1]]\n", "```\n", "```maple\n", "> roll:=rand(1..5):\n", " n:=3:\n", " A:=[seq([seq(roll(),i=1..n)],j=1..n)];\n", "```\n", "```maple\n", " 1, 1, 5\n", " 1, 2, 2\n", " 1, 3, 2\n", " 2, 1, 2\n", " 2, 2, 3\n", " 2, 3, 2\n", " 3, 1, 4\n", " 3, 2, 2\n", " 3, 3, 1\n", "```\n", "i,jの順序に注意.\n", "```maple\n", "> for j from 1 to n do\n", " tmp:=0;\n", " for i from 1 to n do\n", " tmp:=tmp+A[i,j];\n", " end do;\n", " for i from 1 to n do\n", " A[i,j]:=A[i,j]/tmp;\n", " end do;\n", " end do:\n", " A;\n", "```\n", "```maple\n", " [[5/11, 2/7, 2/5], [2/11, 3/7, 2/5], [4/11, 2/7, 1/5]]\n", "```\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }