質問編集履歴
1
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
python ライブラリ Numbaの使い方について
|
1
|
+
python ライブラリ odeを高速化するためにNumbaの使い方について
|
test
CHANGED
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
pythonで描いたコードの処理時間を短くしようとして
|
5
|
+
pythonで描いたコードの処理時間を短くしようとして考えています.
|
6
6
|
|
7
|
-
|
7
|
+
一階微分方程式を解くときに処理時間がかかっているので,odeでの積分時にnumbaを使おうと考えています.
|
8
8
|
|
9
|
-
|
9
|
+
そのため試しに簡単なコードで使い方を理解しようと考えたのですが,エラーが出て動かすことができません.
|
10
|
+
|
11
|
+
|
10
12
|
|
11
13
|
|
12
14
|
|
@@ -18,87 +20,45 @@
|
|
18
20
|
|
19
21
|
```
|
20
22
|
|
21
|
-
try_fast.py
|
23
|
+
PS C:\Users\NA\Desktop\N\A> python try_fast.py
|
22
24
|
|
23
|
-
|
25
|
+
Traceback (most recent call last):
|
24
26
|
|
25
|
-
|
27
|
+
File "try_fast.py", line 56, in <module>
|
26
28
|
|
27
|
-
|
29
|
+
t_f= simulationf(x0, end, step)
|
28
30
|
|
31
|
+
File "C:\Users\NA\AppData\Local\Programs\Python\Python37\lib\site-packages\numba\core\dispatcher.py", line 415, in _compile_for_args
|
32
|
+
|
33
|
+
error_rewrite(e, 'typing')
|
34
|
+
|
35
|
+
File "C:\Users\NA\AppData\Local\Programs\Python\Python37\lib\site-packages\numba\core\dispatcher.py", line 358, in error_rewrite
|
36
|
+
|
37
|
+
reraise(type(e), e, None)
|
38
|
+
|
39
|
+
File "C:\Users\NA\AppData\Local\Programs\Python\Python37\lib\site-packages\numba\core\utils.py", line 80, in reraise
|
40
|
+
|
41
|
+
raise value.with_traceback(tb)
|
42
|
+
|
43
|
+
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
|
44
|
+
|
45
|
+
Untyped global name 'systemf': cannot determine Numba type of <class 'function'>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
File "try_fast.py", line 39:
|
50
|
+
|
29
|
-
|
51
|
+
def simulationf(x0, end, step):
|
30
52
|
|
31
53
|
<source elided>
|
32
54
|
|
33
|
-
Y.append(x-y)
|
34
|
-
|
35
|
-
[1m return X,Y
|
36
|
-
|
37
|
-
[0m [1m^[0m[0m
|
38
|
-
|
39
|
-
[0m
|
40
|
-
|
41
|
-
[0m[1mDuring: typing of assignment at try_fast.py (10)[0m
|
42
|
-
|
43
|
-
[1m
|
44
|
-
|
45
|
-
File "try_fast.py", line 10:[0m
|
46
|
-
|
47
|
-
[1mdef q(x,y):
|
48
|
-
|
49
|
-
<source elided>
|
50
|
-
|
51
|
-
Y.append(x-y)
|
52
|
-
|
53
|
-
[1m return X,Y
|
54
|
-
|
55
|
-
[0m [1m^[0m[0m
|
56
|
-
|
57
|
-
[0m
|
58
|
-
|
59
|
-
@jit(f8[:](f8[:],f8[:]))
|
60
|
-
|
61
|
-
C:\Users\NA\AppData\Local\Programs\Python\Python38\lib\site-packages\numba\core\object_mode_passes.py:177: NumbaWarning: [1mFunction "q" was compiled in object mode without forceobj=True.
|
62
|
-
|
63
|
-
[1m
|
64
|
-
|
65
|
-
File "try_fast.py", line 5:[0m
|
66
|
-
|
67
|
-
[1m@jit(f8[:](f8[:],f8[:]))
|
68
|
-
|
69
|
-
[1mdef q(x,y):
|
70
|
-
|
71
|
-
[0m[1m^[0m[0m
|
72
|
-
|
73
|
-
[0m
|
74
|
-
|
75
|
-
warnings.warn(errors.NumbaWarning(warn_msg,
|
76
|
-
|
77
|
-
C:\Users\NA\AppData\Local\Programs\Python\Python38\lib\site-packages\numba\core\object_mode_passes.py:187: NumbaDeprecationWarning: [1m
|
78
|
-
|
79
|
-
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
|
80
55
|
|
81
56
|
|
57
|
+
ode = sp.ode(systemf)
|
82
58
|
|
83
|
-
|
59
|
+
^
|
84
60
|
|
85
|
-
[1m
|
86
61
|
|
87
|
-
File "try_fast.py", line 5:[0m
|
88
|
-
|
89
|
-
[1m@jit(f8[:](f8[:],f8[:]))
|
90
|
-
|
91
|
-
[1mdef q(x,y):
|
92
|
-
|
93
|
-
[0m[1m^[0m[0m
|
94
|
-
|
95
|
-
[0m
|
96
|
-
|
97
|
-
warnings.warn(errors.NumbaDeprecationWarning(msg,
|
98
|
-
|
99
|
-
[10.0]
|
100
|
-
|
101
|
-
[0.5999999999999996]
|
102
62
|
|
103
63
|
```
|
104
64
|
|
@@ -112,31 +72,99 @@
|
|
112
72
|
|
113
73
|
import numpy as np
|
114
74
|
|
115
|
-
from numba import jit, f8
|
75
|
+
from numba import jit, f8,f4
|
76
|
+
|
77
|
+
import scipy.integrate as sp
|
116
78
|
|
117
79
|
|
118
80
|
|
119
|
-
|
81
|
+
def systemf(t, x):
|
120
82
|
|
121
|
-
|
83
|
+
|
122
84
|
|
123
|
-
|
85
|
+
# 戻り値用のリスト
|
124
86
|
|
125
|
-
|
87
|
+
y = [0]*4
|
126
88
|
|
127
|
-
|
89
|
+
# 常微分方程式(状態方程式)の計算
|
128
90
|
|
129
|
-
|
91
|
+
#x[0] = x1, x[1] = x2, x[2] = y1, x[3] = y2
|
130
|
-
|
131
|
-
return X,Y
|
132
92
|
|
133
93
|
|
134
94
|
|
135
|
-
|
95
|
+
dx1 = -x[2]
|
136
96
|
|
137
|
-
|
97
|
+
dx2 = -x[3]
|
138
98
|
|
99
|
+
dy1 = x[0]
|
100
|
+
|
101
|
+
dy2 = x[1]
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
# 計算結果を返す
|
108
|
+
|
109
|
+
y[0] = dx1
|
110
|
+
|
111
|
+
y[1] = dx2
|
112
|
+
|
113
|
+
y[2] = dy1
|
114
|
+
|
115
|
+
y[3] = dy2
|
116
|
+
|
117
|
+
return y
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
@jit(nopython=True)
|
122
|
+
|
123
|
+
def simulationf(x0, end, step):
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
t_f = []
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
ode = sp.ode(systemf)
|
132
|
+
|
133
|
+
ode.set_integrator('dopri5', method='bdf', atol=1.e-4)#nsteps=1000,
|
134
|
+
|
135
|
+
ode.set_initial_value(x0, 0.0)
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
#初期値の設定
|
140
|
+
|
141
|
+
t_f.append(0.0)#時間
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
while ode.successful() and ode.t < end - step:
|
146
|
+
|
147
|
+
ode.integrate(ode.t + step)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
#出力
|
152
|
+
|
153
|
+
t_f.append(ode.t)#時間
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
return t_f
|
158
|
+
|
159
|
+
x0 = [0,0,0,0]
|
160
|
+
|
161
|
+
end = 3
|
162
|
+
|
163
|
+
step = 0.01
|
164
|
+
|
165
|
+
t_f= simulationf(x0, end, step)
|
166
|
+
|
139
|
-
print(
|
167
|
+
print(t_f)
|
140
168
|
|
141
169
|
```
|
142
170
|
|
@@ -146,7 +174,7 @@
|
|
146
174
|
|
147
175
|
|
148
176
|
|
149
|
-
こ
|
177
|
+
調べたところ@jitから@jit(nopython=True)にすることで動くと書いてある記事がありましたが,動きませんでした
|
150
178
|
|
151
179
|
|
152
180
|
|