मुझे अजगर में 6 डी में संख्यात्मक एकीकरण करने की आवश्यकता है। चूंकि scipy.integrate.nquad फ़ंक्शन धीमा है, इसलिए मैं वर्तमान में इंटीग्रैंड को परिभाषित करके चीजों को गति देने की कोशिश कर रहा हूं। कम से कम Numba के साथ सक्षम।एनक्वाड के साथ कुशल एकीकरण के लिए आप निंबा से सी कॉल करने योग्य कैसे कार्यान्वित कर सकते हैं?
मैं उदाहरण here दी नकल द्वारा scipy.integrate.quad साथ -1 डी में ऐसा करने में सक्षम था:
import numpy as np
from numba import cfunc
from scipy import integrate
def integrand(t):
return np.exp(-t)/t**2
nb_integrand = cfunc("float64(float64)")(integrand)
# regular integration
%timeit integrate.quad(integrand, 1, np.inf)
10000 छोरों, 3 का सबसे अच्छा: पाश प्रति 128 μs
# integration with compiled function
%timeit integrate.quad(nb_integrand.ctypes, 1, np.inf)
100000 छोरों, 3 का सबसे अच्छा: पाश
जब मैं nquad के साथ अब ऐसा करना चाहते हैं प्रति 7.08 μs, nquad प्रलेखन कहते हैं:
If the user desires improved integration performance, then f may be a scipy.LowLevelCallable with one of the signatures:
double func(int n, double *xx) double func(int n, double *xx, void *user_data)
where n is the number of extra parameters and args is an array of doubles of the additional parameters, the xx array contains the coordinates. The user_data is the data contained in the scipy.LowLevelCallable.
लेकिन निम्नलिखित कोड मुझे एक त्रुटि देता है:
from numba import cfunc
import ctypes
def func(n_arg,x):
xe = x[0]
xh = x[1]
return np.sin(2*np.pi*xe)*np.sin(2*np.pi*xh)
nb_func = cfunc("float64(int64,CPointer(float64))")(func)
integrate.nquad(nb_func.ctypes, [[0,1],[0,1]], full_output=True)
त्रुटि: ट्रैक्टर: पहला तर्क गलत हस्ताक्षर
के साथ एक ctypes समारोह सूचक है यह Numba के साथ एक समारोह संकलित करने के लिए संभव है कि सीधे कोड में और बाहरी फाइल में फ़ंक्शन को परिभाषित किए बिना nquad के साथ उपयोग किया जा सकता है?
अग्रिम में बहुत बहुत धन्यवाद!