質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

329閲覧

2次元格子上のとある計算がおかしい

Fallout_18

総合スコア124

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/05/13 17:24

以下のコードで、確率が計算されません。
大分修正などを試みたのですが、なぜだかわかりませんでした。
【設定】_________________________________________________________________
イメージ説明
2m(今回はm=2)の格子上を中心(2,2)スタートで移動していき、移動するときの式は以下で与えられます。
phi(x,y,t+1) = P
phi(x+1,y,t) + Qphi(x-1,y,t) + Rphi(x,y+1,t) + S*phi(x,y-1,t)
で与えられます。


以上の【設定】をコードで書いたものを以下に示します。
コードは汚いかもしれません、ご了承ください。

python

1import numpy as np 2import matplotlib.pyplot as plt 3import math 4from mpl_toolkits.mplot3d import Axes3D 5 6 7#環境設定 8n=2 #tの範囲 9m=2 #偶数(x、yの範囲) 10 11P = [[-1/2, 1/2, 1/2, 1/2],[0,0,0,0],[0,0,0,0],[0,0,0,0]] 12Q = [[0,0,0,0],[1/2, -1/2, 1/2, 1/2],[0,0,0,0],[0,0,0,0]] 13R = [[0,0,0,0],[0,0,0,0],[1/2, 1/2, -1/2, 1/2],[0,0,0,0]] 14S = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[1/2, 1/2, 1/2, -1/2]] 15 16t_list =[] 17 18#初期設定 19phi_map = np.zeros((2*m+1, 2*m+1,4)) #np.zeros((行,列,[]の中身の数)) 20phi_map[m,m,0]= 1 21 22p_map=np.zeros([2*m+1,2*m+1]) 23 24for i in range(0,2*m+1): 25 p = np.dot(phi_map[i,i], np.conj(phi_map[i,i])) 26 p_map[i,i]=p 27#print(p_map) 28 29#以下は時間発展確率計算 30for t in range(0,n+1): 31 t_list.append(t) 32 if t == 0: 33 phi_map 34 p_map 35 else: 36 next_phi_map = np.zeros((2*m+1,2*m+1, 4)) #エラーを起こさないように、以下の計算を収納するために用意 37 for x in range(0,2*m+1): 38 if x == 0: 39 for y in range(0,2*m+1): 40 if y == 0:  41 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(R, phi_map[x,y+1])])#next_phi_mapのx行y列に計算されたもの[?,?,?,?,]を代入 42 elif y == 2*m: 43 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(S, phi_map[x,y-1])]) 44 else: 45 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(S, phi_map[x,y-1]) + np.inner(R, phi_map[x,y+1])]) 46 elif x == 2*m: 47 for y in range(0,2*m+1): 48 if y == 0: 49 next_phi_map[x,y] = np.array([np.inner(Q, phi_map[x-1,y]) + np.inner(R, phi_map[x,y+1])]) 50 elif y == 2*m: 51 next_phi_map[x,y] = np.array([np.inner(Q, phi_map[x-1,y]) + np.inner(S, phi_map[x,y-1])]) 52 else: 53 next_phi_map[x,y] = np.array([np.inner(Q, phi_map[x-1,y]) + np.inner(S, phi_map[x,y-1]) + np.inner(R, phi_map[x,y+1])]) 54 else: 55 if y == 0: 56 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(Q, phi_map[x-1,y]) + np.inner(R, phi_map[x,y+1])]) 57 elif y == 2*m: 58 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(Q, phi_map[x-1,y]) + np.inner(S, phi_map[x,y-1])]) 59 else: 60 next_phi_map[x,y] = np.array([np.inner(P, phi_map[x+1,y]) + np.inner(Q, phi_map[x-1,y]) + np.inner(R, phi_map[x,y+1]) + np.inner(S, phi_map[x,y-1])]) 61 p_map[x,y] = np.dot(next_phi_map[x,y], np.conj(next_phi_map[x,y])) #確率計算 62 phi_map = next_phi_map 63 64 print(t,p_map)

出力結果は

0 [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] 1 [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] 2 [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]]

となり、t=1以降の求めるべき確率の値

0 [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] 1 [[0. 0. 0. 0. 0.] [0. 0. 1/4. 0. 0.] [0. 1/4. 0. 1/4. 0.] [0. 0. 1/4. 0. 0.] [0. 0. 0. 0. 0.]] 2 [[0. 0. 1/16. 0. 0.] [0. 2/16. 0. 2/16. 0.] [1/16. 0. 1/4. 0. 1/16.] [0. 2/16. 0. 2/16. 0.] [0. 0. 1/16. 0. 0.]]

と、でません。
私的に、
next_phi_map = np.zeros((2m+1,2m+1, 4))
の部分が、計算されずにそのまま処理されていると考えています。
何かしらのエラーが起きているということなのでしょうか?
宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

一箇所 for y in range(0,2*m+1): が抜けてました。

python

1for t in range(0,n+1): 2 # ... 3 if t == 0: 4 # .... 5 else: 6 # .... 7 for x in range(0,2*m+1): 8 if x == 0: 9 for y in range(0,2*m+1): 10 # ... 11 elif x == 2*m: 12 for y in range(0,2*m+1): 13 # ... 14 else: 15 if y == 0: # <-- ここに「for y in range(0,2*m+1):」が抜けております!

上記修正後、正常に動いてるっぽいです。
出力:

python

10 [[0. 0. 0. 0. 0.] 2 [0. 0. 0. 0. 0.] 3 [0. 0. 1. 0. 0.] 4 [0. 0. 0. 0. 0.] 5 [0. 0. 0. 0. 0.]] 61 [[0. 0. 0. 0. 0. ] 7 [0. 0. 0.25 0. 0. ] 8 [0. 0.25 0. 0.25 0. ] 9 [0. 0. 0.25 0. 0. ] 10 [0. 0. 0. 0. 0. ]] 112 [[0. 0. 0. 0. 0. ] 12 [0. 0.125 0. 0.125 0. ] 13 [0. 0. 0.25 0. 0. ] 14 [0. 0.125 0. 0.125 0. ] 15 [0. 0. 0. 0. 0. ]]

投稿2018/05/13 18:06

編集2018/05/13 18:08
set0gut1

総合スコア2413

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Fallout_18

2018/05/14 10:09

ケアレスミスでしたか。。 本当にありがとうございます!
set0gut1

2018/05/14 10:10

おめでとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問