#!/usr/bin/env python
# coding: utf-8
# # Table of Contents
#
#
#
#
# その他(miscellaneous)
#
#
#
# file:/~/python/doing_math_with_python/miscellaneous.ipynb
#
# cc by Shigeto R. Nishitani 2017
#
# この資料は[わたしのMapleテキスト](https://ist.ksc.kwansei.ac.jp/~nishitani/MapleHiki/) からpythonに逐次訳したものです.で,miscellaneousに書かれている内容は普通のprogramming言語では当たり前の作業をmapleソフトから少し離れてCLIのmapleで実現する工夫を書いています.pythonは普通のprogramming言語ですから,この部分はそれほど必要性が高くなさそうです.おいおい,気が付いた時に加筆して行きます.
# # ファイルの入出力(InputOutput)
#
#
# 自作したanimationをプレゼン資料に貼り付けたり,測定データなどを読み込んでMapleで手軽に表示,加工したくなります.このとき必要となるファイルとのやりとりを紹介します.
#
#
#
# ## path操作(python)
# ### cwd
# In[1]:
import os
cwd = os.getcwd()
cwd
# ### mk, cd, ls
# In[2]:
os.listdir()
# In[3]:
new_dir = 'dir4'
os.mkdir(new_dir)
os.chdir(new_dir)
os.getcwd()
os.listdir()
# In[4]:
os.chdir('..')
print(os.getcwd())
os.listdir()
# ### globでfull_pathで表示
# In[5]:
import glob
files = glob.glob(cwd+'/dir2'+'/*')
for file in files:
print(file)
# ## fileの読み書き
# In[9]:
os.getcwd()
os.chdir('dir4')
# ### 単純な読み書き
# In[10]:
data = ['x', 'y']
text = "\n".join(data)
with open("tmp.txt", "w") as f:
f.write(text)
# In[11]:
with open("tmp.txt", "r") as f:
lines = f.readlines()
print(lines)
# In[12]:
for line in lines:
print(line.rstrip())
# ### sprintfからの出力
# In[13]:
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)
# In[14]:
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()
# In[15]:
file = open("tmp.txt", "r")
lines = file.readlines()
for line in lines:
print(line.rstrip())
# ## animationの出力
#
# animationなどのgif形式のplotを外部ファイルへ出力して表示させるには,以下の一連のコマンドのようにする.
# ```maple
# > plotsetup(gif,plotoutput=file2):
# > display(tmp,insequence=true);
# > plotsetup(default):
# ```
# こいつをquicktimeなどに食わせれば,Maple以外のソフトで動画表示が可能となる.3次元図形の標準規格であるvrmlも同じようにして作成することが可能です(?vrml;参照).
#
#
#
# ## Mapleのフィルターとしての利用法
#
# 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
# ```
#
#
#
#
# # for-loopの基本技(for-loop2)
#
#
#
#
# ## ランダムな配列の生成
#
# 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]
# ```
#
#
# #### 課題:先の和と組み合わせて,全要素の和が1になるように規格化せよ.
#
#
#
# #### 課題:配列Bへ逆順に代入せよ.
#
#
#
#
# ## 一桁の整数5個から5桁の整数を作る
#
# まず,一桁の整数でできるランダムな配列を作成する.
# ```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
# ```
#
#
# #### 課題:上記と同様にして,10桁の2進数を10進数へ変換せよ
#
#
#
#
#
# ## 255以下の10進数をランダムに生成して,8桁の2進数へ変換せよ.
#
# ```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桁の整数のそれぞれの桁の値を配列に格納せよ.
#
# 8桁の整数は以下のようにして作られる.
# ```maple
# > n:=8;
# roll:=rand(10^(n-1)..10^n):
# B:=roll();
# ```
# ```maple
# 8
# 17914675
# ```
#
#
# ## 小数点以下8桁のそれぞれの桁の数を配列に格納せよ
#
# ```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;
# ```
#
#
#
# ## 2つの要素の入れ替え
#
# ```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
# ```
#
#
#
# #### 課題:1..6のサイコロを20回振って,出た目を記録せよ.
#
# 記録には,要素が0の配列を最初に用意し,出た目を示数にして配列の要素をひとつずつ増やす.
#
#
# ## 2次元配列
#
# 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]]
# ```
#