Q&A
こんにちは.
数式を表すsympyオブジェクト?をlambdifyを使用して, scipy.integrate.quadで使用したいのですが, 可能なのでしょうか.
上を実装した以下のコードは質問のために作成したもので, 実際のコードでは1の数式で「sympy.integrate」を使用しているので, 関数fの戻り値に直接1の式を置けない状況です.
(質問のため簡略なコードにしました)
そして実行すると, errorが出ましたが, エラーからどう修正するべきなのかわかりません.
宜しくお願い致します.
環境 : jupyter notebook上のpython3
Python
1import sympy 2import scipy 3 4x = sympy.symbols('x') 5expr = x^2+4 # *1 6def f(x): 7 return sympy.lambdify(x, expr) 8scipy.integrate.quad(f, 0, 3)
error
1Traceback (most recent call last): 2 3 File "/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code 4 exec(code_obj, self.user_global_ns, self.user_ns) 5 6 File "<ipython-input-41-cdb345e5242c>", line 11, in <module> 7 scipy.integrate.quad(f, 0, 3) 8 9 File "/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py", line 341, in quad 10 points) 11 12 File "/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py", line 448, in _quad 13 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 14 15 File "<ipython-input-41-cdb345e5242c>", line 10, in f 16 return sympy.lambdify(x, expr) 17 18 File "/anaconda3/lib/python3.7/site-packages/sympy/utilities/lambdify.py", line 476, in lambdify 19 c = compile(funcstr, filename, 'exec') 20 21 File "<lambdifygenerated-25>", line 1 22 def _lambdifygenerated(1.5): 23 ^ 24SyntaxError: invalid syntax
あなたの回答
tips
プレビュー