teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

題名の変更

2018/06/14 07:47

投稿

Fallout_18
Fallout_18

スコア124

title CHANGED
@@ -1,1 +1,1 @@
1
- listの縦連結つい
1
+ listから2次元配列仕方苦戦します
body CHANGED
File without changes

1

内容の変更

2018/06/14 07:47

投稿

Fallout_18
Fallout_18

スコア124

title CHANGED
File without changes
body CHANGED
@@ -1,22 +1,133 @@
1
- 以下のlistのデタを縦に連して、2次元配列として表すために
1
+ 以下のドの果を3次元プロットしよう思いまして、ここでx軸がx座標,y座標を時間、z軸を確率とします。
2
2
  ```python
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import math
6
+ from mpl_toolkits.mplot3d import Axes3D
7
+ import matplotlib.animation as animation
8
+ import matplotlib.colors as colors
9
+ from matplotlib import cm
10
+
11
+ #環境設定
12
+ n=3 #x軸
13
+ m=2*n #t
14
+ theta = 3*(math.pi)/12
15
+
16
+ P = [[np.cos(theta),np.sin(theta)],[0,0]]
17
+ Q = [[0,0],[np.sin(theta),-np.cos(theta)]]
18
+ x_list=[]#xline
19
+ t_list=[]#time
3
- p_list[上からi=0~6]とします。
20
+ p_list=[]#probability
21
+ s_list=[]#state
22
+ a = 1/math.sqrt(2)
23
+ b = 1j/math.sqrt(2)
24
+ p_map=np.zeros([m,m])
25
+ #ランダムウォーク
26
+ R=1/2
27
+ L=1/2
28
+ X_list=[]
29
+ P_list=[]
30
+ #step_list=[]
31
+
32
+ #quantumwalk
33
+ for j in range(0,2*n+1):
34
+ if j == n:
35
+ phai = [a ,b]
36
+ pro = 1
37
+ else:
38
+ phai = [0,0]
39
+ pro =0
40
+ p = np.dot(phai,np.conj(phai))
41
+
42
+ x_list.append(j)
43
+ X_list.append(j)
44
+ s_list.append(phai)
45
+ p_list.append(p)
46
+ P_list.append(pro)
47
+
48
+
49
+
50
+ for t in range(0,m+1):
51
+ t_list.append(t)
52
+ if t ==0:
53
+ s_list
54
+ p_list
55
+ P_list
56
+ else:
57
+ next_s_list = [0]*len(s_list)
4
- [0, 0, 0, (0.9999999999999998+0j), 0, 0, 0]
58
+ next_P_list = [0]*len(P_list) #listと同じ要素の数ですべて0を用意(初期化)
59
+ for i in range(0,2*n+1):
60
+ if i == 0:
61
+ next_s_list[i] = np.dot(P, s_list[i+1])
62
+ next_P_list[i] = P_list[i+1]*L
63
+ elif i == 2*n:
64
+ next_s_list[i] = np.dot(Q, s_list[i-1])
65
+ next_P_list[i] = P_list[i-1]*R
66
+ else:
67
+ next_s_list[i] = np.dot(P, s_list[i+1]) + np.dot(Q, s_list[i-1])
5
- [0.0, 0.0, (0.5+0j), 0.0, (0.5+0j), 0.0, 0.0]
68
+ next_P_list[i] = P_list[i+1]*L + P_list[i-1]*R
6
- [0.0, (0.25000000000000006+0j), 0.0, (0.5000000000000001+0j), 0.0, (0.25000000000000006+0j), 0.0]
69
+
7
- [(0.12500000000000006+0j), 0.0, (0.3750000000000001+0j), 0.0, (0.3750000000000001+0j), 0.0, (0.12500000000000006+0j)]
8
- [0.0, (0.3750000000000001+0j), 0.0, (0.12500000000000006+0j), 0.0, (0.3750000000000001+0j), 0.0]
70
+ p_list[i] = np.dot(next_s_list[i],np.conj(next_s_list[i]))
71
+
72
+
73
+ s_list = next_s_list
74
+ P_list = next_P_list
75
+
76
+ p_map =np.array(p_list)
77
+ print(p_map)
78
+
79
+ #plt.xlabel("x")
9
- [(0.3125000000000001+0j), 0.0, (0.12500000000000003+0j), 0.0, (0.12500000000000003+0j), 0.0, (0.3125000000000001+0j)]
80
+ #plt.ylabel("probability")
81
+ #plt.ylim([0,0.1])
82
+ #plt.xlim([-n,3*n])
10
- [0.0, (0.21875000000000008+0j), 0.0, (0.12500000000000006+0j), 0.0, (0.21875000000000008+0j), 0.0]
83
+ #plt.plot( x_list,np.real(p_list),color="red",linewidth=1.0,label="quantum walk")
84
+ #plt.plot(X_list, P_list,color="blue",linewidth=1.0,label="random walk")
85
+ #plt.legend(loc="best")
86
+ #plt.pause(0.01)
87
+ #plt.cla()
88
+
89
+
90
+
91
+ #3次元
92
+ fig = plt.figure()
93
+ ax = Axes3D(fig)
94
+ #X,Y = np.meshgrid(x_list, t_list)
95
+
96
+ ax.set_xlabel("x")
97
+ ax.set_ylabel("t")
98
+ ax.set_zlabel("probability")
99
+
100
+ ax.set_xlim(2*n,0)
101
+ ax.set_ylim(0,m)
102
+ ax.set_zlim(0,1)
103
+ surf = ax.plot_surface(x_list, t_list, p_map, cmap =cm.coolwarm , linewidth=0)
104
+
105
+ fig.colorbar(surf)
106
+ plt.show()
107
+
11
108
  ```
12
- 調べたとろ、np.vstackを使う良いみた
109
+ れだけだ、わかりにいので、このコードの結果
13
110
  ```
111
+ t=0 p_list=[0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j]
112
+ t=1 p_list=[0. +0.j 0. +0.j 0.5+0.j 0. +0.j 0.5+0.j 0. +0.j 0. +0.j]
113
+ t=2 p_list=[0. +0.j 0.25+0.j 0. +0.j 0.5 +0.j 0. +0.j 0.25+0.j 0. +0.j]
114
+ t=3 p_list=[0.125+0.j 0. +0.j 0.375+0.j 0. +0.j 0.375+0.j 0. +0.j 0.125+0.j]
115
+ t=4 p_list=[0. +0.j 0.375+0.j 0. +0.j 0.125+0.j 0. +0.j 0.375+0.j 0. +0.j]
116
+ t=5 p_list=[0.3125+0.j 0. +0.j 0.125 +0.j 0. +0.j 0.125 +0.j 0. +0.j
117
+ 0.3125+0.j]
118
+ t=6 p_list=[0. +0.j 0.21875+0.j 0. +0.j 0.125 +0.j 0. +0.j 0.21875+0.j
14
- for i in range(0,7):
119
+ 0. +0.j]
15
- a =np.vstack(p_list[i],p_list[i+1])
120
+
16
121
  ```
122
+ を3次元プロットにしたいということです。
123
+ tをy軸、listの各位置をx、各中身の値をz値としてです。
124
+ ただ、今,p_listは1次元配列なので、3次元プロットする為に、
125
+ p_map=np.array(p_list)
17
- 書いたところ、
126
+ 、2次元配列に直して、プロットしようとしたところ、
18
127
  ```
19
- a = np.vstack(probability(i),probability(i+1))
20
- TypeError: vstack() takes 1 positional argument but 2 were given
128
+ ValueError: Argument Z must be 2-dimensional.
129
+
21
130
  ```
131
+ とでました。
132
+ これは、p_mapが2次元配列になっていないという意味だと思いました。
22
- 出ました。2つのlistの場合は単純に書けば良いのはわかったのですが例えば0~2n個の列縦に繋げられうな一般化した書き方で表したいのですがどのよれば良いのでしょうか?
133
+ 仮にそうだとしたnp.zeros([m.m])持ってきて一つ一つ代入すのも面倒くさそうなので、少し手軽なやり方はありますでしょうか?