ページ

ラベル Python の投稿を表示しています。 すべての投稿を表示
ラベル Python の投稿を表示しています。 すべての投稿を表示

2016年8月3日水曜日

sphinx + ipython notebook (jupyter)

備忘録

ipython notebookで作ったファイルをipython nbconvert --to rst hoo.ipynb
すると
In[1]
Out[1]
などの情報が出力されずに困った.

http://nbsphinx.readthedocs.io/en/latest/usage.html

よりrstにconvertせずにipynbをそのままtoctreeで読み込む事によりこの問題を回避

sphinxの設定はデフォルトの場合,conf.pyがあるディレクトリで

python -m sphinx . _build/html

とすればよい

sphinx-autobuild が超便利

以上

2013年12月1日日曜日

Mathmaticaを用いた数値対角化の計算時間

Mathematicaを用いた対角化の計算速度をplotしてみました.

対角化した行列のclassは
ランダムではなくスパーズではない一般のエルミート行列(赤ドット)と
ランダムではなくスパースではない一般のユニタリー行列(青ドット).
また,行列は200桁精度で近似評価されています.

厳密評価のまま計算するとキットモット遅い.

行列の構成方法は気にしないと言う事でご勘弁.

実線は計算時間を3次の多項式でfittingしたもの.
最後に描画とfittingのプログラムを置いときます.
(もちろん計算時間のデータファイルがないと動きませんが・・・
欲しいなら上げますが,欲しいか?)




import numpy as np
import matplotlib.pyplot as plt

def f(x,para):
    return para[0]*x**3 + para[1]*x**2 + para[2]*x + para[3]
    
data = np.loadtxt("time_prec200.dat").transpose()
fig = plt.figure(figsize=(8,4))
ax0 = fig.add_subplot(1,2,1)
ax1 = fig.add_subplot(1,2,2)
colors=["red", "blue"]
for i, ax in enumerate([ax0,ax1]):
    ax.plot(data[0],data[1]/3600,'o', label='Hermitian matrix',markersize=10, color=colors[0])
    ax.plot(data[0],data[2]/3600,'o', label='Unitary matrix',markersize=10,color=colors[1])
#    ax.plot(data[0],data[3]/3600,'og')
    for j in range(1,3):
        x = np.arange(100, 10000,1)    
        para = np.polyfit(data[0],data[j],3)
        y = f(x,para)
        ax.plot(x,y/3600,'-',linewidth=3,color=colors[j-1])
    if i==1:
        ax.loglog()
        ax.set_xlim(50,10000)
    else:
        ax.set_ylim(0,5)
        ax.set_xlim(0,1000)
        ax.set_ylabel("calculation time (hours)",fontsize=16)            
    le = ax.legend(loc=2,numpoints=1)
    le.draw_frame(False)
    ax.grid()
    ax.set_xlabel("matrix dimension",fontsize=16)
sup = plt.suptitle("diagonalization time with Mathematica (precision=200)",fontsize=15)
sup.set_position((0.5,1.0))
plt.tight_layout()
plt.savefig("calc_time.png")
plt.show()

2013年10月27日日曜日

mpmath 速度比べ

import mpmath
import numpy as np
import time

sample=500
iter=500

x = np.array([1.0/3.0]*sample)
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = x + 1.0/3.0
    else:
        x = x - 1.0/3.0
goal=time.time()
print("---- numpy array -----")
print("%.20f" % init)
print("%.20f" % x[0])
print("time:", goal-start)

mpmath.mp.dps = 18
x = np.array([mpmath.mpf("1.0")/mpmath.mpf("3.0")]*sample)
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = x + mpmath.mpf("1.0")/mpmath.mpf("3.0")
    else:
        x = x - mpmath.mpf("1.0")/mpmath.mpf("3.0")
goal=time.time()
print("---- mpmath with numpy array (dps=%d) -----" % mpmath.mp.dps)
print("%.20s" % init)
print("%.20s" % x[0])
print("time:", goal-start)

mpmath.mp.dps = 100
x = np.array([mpmath.mpf("1.0")/mpmath.mpf("3.0")]*sample)
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = x + mpmath.mpf("1.0")/mpmath.mpf("3.0")
    else:
        x = x - mpmath.mpf("1.0")/mpmath.mpf("3.0")
goal=time.time()
print("---- mpmath with numpy array (dps=%d) -----" % mpmath.mp.dps)
print("%.20s" % init)
print("%.20s" % x[0])
print("time:", goal-start)

mpmath.mp.dps = 18
x = mpmath.matrix([mpmath.mpf("1.0")/mpmath.mpf("3.0")]*sample)
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = x + mpmath.mpf("1.0")/mpmath.mpf("3.0")
    else:
        x = x - mpmath.mpf("1.0")/mpmath.mpf("3.0")
goal=time.time()
print("---- mpmath matrix (dps=%d)-----" % mpmath.mp.dps)
print("%.20s" % init)
print("%.20s" % x[0])
print("time:", goal-start)

mpmath.mp.dps = 100
x = mpmath.matrix([mpmath.mpf("1.0")/mpmath.mpf("3.0")]*sample)
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = x + mpmath.mpf("1.0")/mpmath.mpf("3.0")
    else:
        x = x - mpmath.mpf("1.0")/mpmath.mpf("3.0")
goal=time.time()
print("---- mpmath matrix (dps=%d)-----" % mpmath.mp.dps)
print("%.20s" % init)
print("%.20s" % x[0])
print("time:", goal-start)


x = [mpmath.mpf("1.0")/mpmath.mpf("3.0")]*sample
init=x[0]
start = time.time()
for i in range(iter):
    if i<iter/2:
        x = [xx + mpmath.mpf("1.0")/mpmath.mpf("3.0") for xx in x]
    else:
        x = [xx - mpmath.mpf("1.0")/mpmath.mpf("3.0") for xx in x]
goal=time.time()
print("---- list -----")
print("%.20f" % init)
print("%.20f" % x[0])
print("time:", goal-start)




実行結果
$ python speed_test_array.py 
---- numpy array -----
0.33333333333333331483
0.33333333333332632042
('time:', 0.0022330284118652344)
---- mpmath with numpy array (dps=18) -----
0.333333333333333333
0.333333333333333326
('time:', 1.7795300483703613)
---- mpmath with numpy array (dps=100) -----
0.333333333333333333
0.333333333333333333
('time:', 2.18923282623291)
---- mpmath matrix (dps=18)-----
0.333333333333333333
0.333333333333333326
('time:', 5.815176010131836)
---- mpmath matrix (dps=100)-----
0.333333333333333333
0.333333333333333333
('time:', 6.364114046096802)
---- list -----
0.33333333333333331483
0.33333333333333331483
('time:', 12.696530103683472)