質問編集履歴

1

修正

2020/07/27 11:21

投稿

NR4200
NR4200

スコア41

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で描いたコードの処理時間を短くしようとしてnumbaを使おうと考えています.
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:4: NumbaWarning: 
23
+ PS C:\Users\NA\Desktop\N\A> python try_fast.py
22
24
 
23
- Compilation is falling back to object mode WITH looplifting enabled because Function "q" failed type inference due to: No conversion from UniTuple(list(array(float64, 1d, C)) x 2) to array(float64, 1d, A) for '$44return_value.17', defined at None
25
+ Traceback (most recent call last):
24
26
 
25
- 
27
+ File "try_fast.py", line 56, in <module>
26
28
 
27
- File "try_fast.py", line 10:
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
- def q(x,y):
51
+ def simulationf(x0, end, step):
30
52
 
31
53
  <source elided>
32
54
 
33
- Y.append(x-y)
34
-
35
-  return X,Y
36
-
37
-  ^
38
-
39
- 
40
-
41
- During: typing of assignment at try_fast.py (10)
42
-
43
- 
44
-
45
- File "try_fast.py", line 10:
46
-
47
- def q(x,y):
48
-
49
- <source elided>
50
-
51
- Y.append(x-y)
52
-
53
-  return X,Y
54
-
55
-  ^
56
-
57
- 
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: Function "q" was compiled in object mode without forceobj=True.
62
-
63
- 
64
-
65
- File "try_fast.py", line 5:
66
-
67
- @jit(f8[:](f8[:],f8[:]))
68
-
69
- def q(x,y):
70
-
71
- ^
72
-
73
- 
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: 
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
- For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
59
+ ^
84
60
 
85
- 
86
61
 
87
- File "try_fast.py", line 5:
88
-
89
- @jit(f8[:](f8[:],f8[:]))
90
-
91
- def q(x,y):
92
-
93
- ^
94
-
95
- 
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
- @jit(f8[:](f8[:],f8[:]))
81
+ def systemf(t, x):
120
82
 
121
- def q(x,y):
83
+
122
84
 
123
- X = []
85
+ # 戻り値用のリスト
124
86
 
125
- Y = []
87
+ y = [0]*4
126
88
 
127
- X.append(x+y)
89
+ # 常微分方程式(状態方程式)の計算
128
90
 
129
- Y.append(x-y)
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
- a1,a2 = q(5.3,4.7)
95
+ dx1 = -x[2]
136
96
 
137
- print(a1)
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(a2)
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