English 中文(简体)
是否有办法对Numpy无结构坐标的梯度进行计算?
原标题:Is there a way to compute gradients on unstructured coordinates with Numpy?

我在<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<条码>、<>>>>、<条码>。

I want to find the gradient of A in the y dimension. This can be done easily with NumPy:

www.un.org/spanish/ga Ady = np.gradient(A, Y, axis=1)

<代码>Y为<代码>y尺寸中的1D矢量。

但是,如果<代码>,这便成为非属物。 Y无结构。 这就是说,每个固定职位的数据“栏目”<代码>(x, z) = (Xi, Zi)都有一套独特的<代码>y坐标。 例如:

A = np.random.random((10, 10, 10))

X = np.arange(10)
Y = np.sort(np.random.random((10, 10, 10)), axis=1)
Z = np.arange(10)

The result above is a 3D dataset A, defined on a structured set of X and Z coordinates, while the value of the Y coordinate is unique for every data point (but is of course monotonic in the y dimension). I want to estimate dA/dy via finite differences.

基本上,我试图把许多独立栏目分级。 是否有办法用NumPy来宣传这一问题? 我尝试了以下迭代办法,但进展缓慢:

# A is the 3D dataset
# Y is the 3D dataset with shape matching that of A; gives the y-position of each datapoint in A
NX, NY, NZ = A.shape[0], A.shape[1], A.shape[2]
dA_dy = np.zeros((NX, NY, NZ))
for i in range(NX):
    for k in range(NZ):
        dA_dy[i, :, k] = np.gradient(A[i,:,k], Y[i,:,k])

我还认为,我可以通过执行链条规则而获得聪明:

dA_dy = np.gradient(A, axis=1) / np.gradient(Y, axis=1)

但对于以下简单检验,这种办法并不可行:

g = np.array([1, 5, 6, 10])  # an unstructured coordinate
f = g**2                     # function value on the points x
grad1 = np.gradient(f, g)                # df/dg
grad2 = np.gradient(f) / np.gradient(g)  # df/dg?

我只获得<代码>grad1=grad2,用于少数简单的线性功能,但并非上述功能。 现在我很想知道,从理论上来说,为什么链条规则一般不赞成以有限差异估算的衍生物。

问题回答

(没有解决问题的答案)

I only get grad1=grad2 for a few simple linear functions

确信:

# np.gradient(f) is equivalent to:
>>> np.gradient(f, np.arange(f.size))
array([24. , 17.5, 37.5, 64. ])

# np.gradient(x) is equivalent to:
>>> np.gradient(x, np.arange(x.size))
array([4. , 2.5, 2.5, 4. ])

# so np.gradient(f) / np.gradient(x) is equivalent to:
>>> np.gradient(f, np.arange(f.size)) / np.gradient(x, np.arange(f.size))
array([ 6.,  7., 15., 16.])

如果x为单吨,grad1等于grad2 即便职能<代码>f不是线性:

x = np.array([1, 3, 5, 7])
f = x**2
grad1 = np.gradient(f, x)
grad2 = np.gradient(f) / np.gradient(x)

产出:

>>> grad1 == grad2
array([ True,  True,  True,  True])




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签