Reputation: 55
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.
Upvotes: 2
Views: 2097
Reputation: 53089
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])
Upvotes: 3