この資料はわたしのMapleテキスト からpythonに逐次訳したものです.で,miscellaneousに書かれている内容は普通のprogramming言語では当たり前の作業をmapleソフトから少し離れてCLIのmapleで実現する工夫を書いています.pythonは普通のprogramming言語ですから,この部分はそれほど必要性が高くなさそうです.おいおい,気が付いた時に加筆して行きます.
自作したanimationをプレゼン資料に貼り付けたり,測定データなどを読み込んでMapleで手軽に表示,加工したくなります.このとき必要となるファイルとのやりとりを紹介します.
import os
cwd = os.getcwd()
cwd
'/Users/bob/python/doing_math_with_python/symbolic_math'
os.listdir()
['miscellaneous.ipynb', 'gradient_descent.gif', 'differential.ipynb', 'README.html~', 'lecture_trial_memo.org', 'validate.data', 'first_leaf.ipynb', 'train_b.data', '.DS_Store', 'linear_solve.ipynb', 'group_works_2_ans.ipynb', 'dir2', 'figs', 'linear_algebra_sympy.ipynb', 'integral.ipynb', 'validate_b.data', 'python_files_and_dir.ipynb', 'group_works_3_ans.ipynb', 'linear_algebra_scipy.ipynb', 'README.html', 'equals.ipynb', 'Rakefile', 'functions.ipynb', 'validate_A.data', 'group_works_3_breast_cancer.ipynb', 'group_works_1.ipynb', 'equation_manipulation.ipynb', '.ipynb_checkpoints', 'README.org', 'cg.ipynb', 'train_A.data', 'group_works_2.ipynb', 'group_works_4.pdf', 'group_works_1_ans.ipynb', 'group_works_ans', 'group_works_3.pdf', 'train.data']
new_dir = 'dir4'
os.mkdir(new_dir)
os.chdir(new_dir)
os.getcwd()
os.listdir()
[]
os.chdir('..')
print(os.getcwd())
os.listdir()
/Users/bob/python/doing_math_with_python/symbolic_math
['miscellaneous.ipynb', 'gradient_descent.gif', 'differential.ipynb', 'README.html~', 'lecture_trial_memo.org', 'validate.data', 'first_leaf.ipynb', 'train_b.data', '.DS_Store', 'linear_solve.ipynb', 'group_works_2_ans.ipynb', 'dir2', 'figs', 'linear_algebra_sympy.ipynb', 'dir4', 'integral.ipynb', 'validate_b.data', 'python_files_and_dir.ipynb', 'group_works_3_ans.ipynb', 'linear_algebra_scipy.ipynb', 'README.html', 'equals.ipynb', 'Rakefile', 'functions.ipynb', 'validate_A.data', 'group_works_3_breast_cancer.ipynb', 'group_works_1.ipynb', 'equation_manipulation.ipynb', '.ipynb_checkpoints', 'README.org', 'cg.ipynb', 'train_A.data', 'group_works_2.ipynb', 'group_works_4.pdf', 'group_works_1_ans.ipynb', 'group_works_ans', 'group_works_3.pdf', 'train.data']
import glob
files = glob.glob(cwd+'/dir2'+'/*')
for file in files:
print(file)
/Users/bob/python/doing_math_with_python/symbolic_math/dir2/tmp.txt /Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir2 /Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir4 /Users/bob/python/doing_math_with_python/symbolic_math/dir2/dir3 /Users/bob/python/doing_math_with_python/symbolic_math/dir2/test.txt
os.getcwd()
os.chdir('dir4')
data = ['x', 'y']
text = "\n".join(data)
with open("tmp.txt", "w") as f:
f.write(text)
with open("tmp.txt", "r") as f:
lines = f.readlines()
print(lines)
['x\n', 'y']
for line in lines:
print(line.rstrip())
x y
data = [['x', 0, 'y', 0],
['x', 1, 'y', 1]]
with open("tmp.txt", "w") as f:
for dd in data:
print(dd)
string = '%4s:%3d, %4s:%3d\n' % tuple(dd)
print(string)
f.writelines(string)
['x', 0, 'y', 0] x: 0, y: 0 ['x', 1, 'y', 1] x: 1, y: 1
data = [['x', 0, 'y', 0],
['x', 1, 'y', 1]]
f = open("tmp.txt", "w")
for dd in data:
print(dd)
string = '%4s:%3d, %4s:%3d\n' % tuple(dd)
print(string)
f.writelines(string)
f.close()
['x', 0, 'y', 0] x: 0, y: 0 ['x', 1, 'y', 1] x: 1, y: 1
file = open("tmp.txt", "r")
lines = file.readlines()
for line in lines:
print(line.rstrip())
x: 0, y: 0 x: 1, y: 1
animationなどのgif形式のplotを外部ファイルへ出力して表示させるには,以下の一連のコマンドのようにする.
maple
> plotsetup(gif,plotoutput=file2):
> display(tmp,insequence=true);
> plotsetup(default):
こいつをquicktimeなどに食わせれば,Maple以外のソフトで動画表示が可能となる.3次元図形の標準規格であるvrmlも同じようにして作成することが可能です(?vrml;参照).
linux版やMac版では文字ベースのmapleを使って,filterとして高度な作業をさせることが出来ます.スクリプトの中に外部ファイルとの入出力を組み込めば,いままで紹介してきた複雑な動作をブラックボックスの内部処理としてそのまま使えます.
maple
[bob@asura0 ~/test]$ cat test.txt
T:=readdata("./data101");
interface(quiet=true);
writeto("./result");
print(T[1]);
writeto(terminal);
interface(quiet=false);
とすれば,data101から読み込んだデータに何らかの処理を施した結果をresultに打ち出すことが可能.interface(quiet=true)で余計な出力を抑止しています.これをmapleに食わせると
maple
[bob@asura0 ~/test]$ /usr/local/maple9.5/bin/maple < test.txt
|\^/| Maple 9.5 (IBM INTEL LINUX)
._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2004
\ MAPLE / All rights reserved. Maple is a trademark of
<____ ____> Waterloo Maple Inc.
| Type ? for help.
> T:=readdata("./data101");
T := [1.23, 2.35]
> interface(quiet=true);
false
true
> quit
bytes used=211000, alloc=262096, time=0.00
めでたく出力されているはず.
maple
[bob@asura0 ~/test]$ cat result
1.23
Mac版でのパス(path)は下記を参照.
maple
bob% /Library/Frameworks/Maple.framework/Versions/15/bin/maple
|\^/| Maple 15 (APPLE UNIVERSAL OSX)
._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2011
\ MAPLE / All rights reserved. Maple is a trademark of
<____ ____> Waterloo Maple Inc.
| Type ? for help.
> quit
memory used=1.2MB, alloc=1.4MB, time=0.07
1から100までの整数5個からなる配列の生成.
maple
> restart:
roll:=rand(1..100):
n:=5:
A:=[seq(roll(),i=1..n)];
maple
[93, 45, 96, 6, 98]
for-loopで配列を使うときには,配列の大きさ(要素数)がfor-loopの終了条件になることが多い. リスト構造では単純にnopsとすればよい.
maple
> nops(A);
maple
5
配列はおなじ箱が沢山用意されていると考えればよい.配列をfor-loopで使うときは,箱を指す数(示数,index)をいじっているのか,箱の中身(要素)をいじっているのかを意識すれば,動作を理解しやすい.
maple
> for i from 1 to n do
print(i,A[i]);
end do;
maple
1, 93
2, 45
3, 96
4, 6
5, 98
逆順の表示
maple
> for i from n by -1 to 1 do
print(i,A[i]);
end do;
maple
5, 98
4, 6
3, 96
2, 45
1, 93
逆順の表示2
maple
> for i from 1 to n do
print(n-i+1,A[n-i+1]);
end do;
maple
5, 98
4, 6
3, 96
2, 45
1, 93
maple
> sum1:=0:
for i from 1 to n do
sum1:=sum1+A[i];
end do:
sum1;
maple
338
maple
> k:=64:
for i from 1 to n do
A[i]:=A[i]/k;
end do:
A;
maple
[93/64, 45/64, 3/2, 3/32, 49/32]
まず,一桁の整数でできるランダムな配列を作成する.
maple
> roll:=rand(0..9): n:=5: A:=[seq(roll(),i=1..n)];
maple
A := [3, 5, 4, 0, 7]
maple
> sum1:=0;
for i from 1 to n do
sum1:=sum1*10+A[i];
end do:
sum1;
maple
0
35407
maple
> n:=8: 2^n;
maple
256
maple
> roll:=rand(0..255):
B:=roll();
maple
161
ちょっとカンニング.
maple
> convert(B,binary);
maple
10100001
maple
> A:=[]:
for i from 1 to n do
A:=[irem(B,2),op(A)];
B:=iquo(B,2);
end do:
A;
$$
[1, 0, 1, 0, 0, 0, 0, 1]
$$
8桁の整数は以下のようにして作られる.
maple
> n:=8;
roll:=rand(10^(n-1)..10^n):
B:=roll();
maple
8
17914675
maple
> n:=8:
roll:=rand(10^(n-1)..10^n):
B:=evalf(roll()/10^n);
maple
0.6308447100
maple
> B:=10*B:
A:=[]:
for i from 1 to n do
A:=[op(A),floor(B)];
B:=(B-A[i])*10;
end do:
A;
$$
[6, 3, 0, 8, 4, 4, 7, 1]
$$
maple
> roll:=rand(1..100):
n:=5:
A:=[seq(roll(),i=1..n)];
i_max:=A[1]:
for i from 2 to n do
if (A[i]>i_max) then
i_max:=A[i];
end if;
end do:
i_max;
maple
64
maple
> roll:=rand(1..100):
n:=5: A:=[seq(roll(),i=1..n)];
i_div:=50:i_low:=0:i_high:=0:
for i from 1 to n do
if (A[i]>i_div) then
i_high:=i_high+1;
else
i_low:=i_low+1;
end if
end do;
print(i_low,i_high);
maple
2, 3
maple
> n:=10;
for i from 1 to n do
if (isprime(i)) then
print(i);
end if;
end do;
maple
> roll:=rand(1..100): n:=5: A:=[seq(roll(),i=1..n)]; sel:=rand(1..n):
isel:=sel();
jsel:=sel();
a:=A[isel]; b:=A[jsel]; A[isel]:=b; A[jsel]:=a;
A;
maple
[60, 93, 14, 50, 47]
2
4
93
50
50
93
[60, 50, 14, 93, 47]
より短くするには,
maple
> roll:=rand(1..100):
n:=5:
A:=[seq(roll(),i=1..n)];
sel:=rand(1..n):
isel:=sel();
jsel:=sel();
a:=A[isel];
A[isel]:=A[jsel];
A[jsel]:=a;
A;
maple
[9, 77, 59, 16, 1]
5
4
1
16
1
[9, 77, 59, 1, 16]
maple
> roll:=rand(0..1):
n:=10:
up:=0:
for i from 1 to n do
trial:=roll();
if (trial=1) then
up:=up+1;
end if;
end do:
up;
maple
5
記録には,要素が0の配列を最初に用意し,出た目を示数にして配列の要素をひとつずつ増やす.
2次元配列に対しても同様の操作ができる.ここでは列に対する規格化を示す.
maple
> roll:=rand(1..5):
n:=3:
A:=[seq([seq(roll(),i=1..n)],j=1..n)];
maple
A := [[5, 2, 2], [2, 3, 2], [4, 2, 1]]
maple
> roll:=rand(1..5):
n:=3:
A:=[seq([seq(roll(),i=1..n)],j=1..n)];
maple
1, 1, 5
1, 2, 2
1, 3, 2
2, 1, 2
2, 2, 3
2, 3, 2
3, 1, 4
3, 2, 2
3, 3, 1
i,jの順序に注意.
maple
> for j from 1 to n do
tmp:=0;
for i from 1 to n do
tmp:=tmp+A[i,j];
end do;
for i from 1 to n do
A[i,j]:=A[i,j]/tmp;
end do;
end do:
A;
maple
[[5/11, 2/7, 2/5], [2/11, 3/7, 2/5], [4/11, 2/7, 1/5]]