简体   繁体   中英

Function to compute inverse of numpy.gradient

I have a Gaussian and I compute its gradient using numpy.gradient function. I want to make slight changes in the gradient and get back a slightly modified Gaussian.

高斯及其梯度

Is there any function in python which can calculate the inverse of the gradient?

I already checked this link Inverse of n-dimensional numpy.gradient but could not find the solution.

In 1D the following snippet reverses np.gradient :

>>> A = scipy.stats.norm().pdf(np.linspace(-1, 1, 19))
>>> A
array([0.24197072, 0.26874286, 0.29481487, 0.31944801, 0.34189229,
       0.36142383, 0.37738323, 0.38921247, 0.39648726, 0.39894228,
       0.39648726, 0.38921247, 0.37738323, 0.36142383, 0.34189229,
       0.31944801, 0.29481487, 0.26874286, 0.24197072])
>>> 
>>> a = np.gradient(A)
>>> 
>>> A[0] + 2 * np.c_[np.r_[0, a[1:-1:2].cumsum()], a[::2].cumsum() - a[0] / 2].ravel()[:len(a)]
array([0.24197072, 0.26874286, 0.29481487, 0.31944801, 0.34189229,
       0.36142383, 0.37738323, 0.38921247, 0.39648726, 0.39894228,
       0.39648726, 0.38921247, 0.37738323, 0.36142383, 0.34189229,
       0.31944801, 0.29481487, 0.26874286, 0.24197072])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM