计算具有不均匀间隔点的 3D 梯度

2024-04-28

我目前有一个由几百万个不均匀间隔的粒子组成的体积,每个粒子都有一个属性(对于那些好奇的人来说是潜力),我想计算其局部力(加速度)。

np.gradient 仅适用于均匀分布的数据,我在这里查看:numpy 中的二阶梯度 https://stackoverflow.com/questions/23419193/second-order-gradient-in-numpy其中需要插值,但我无法在 Numpy 中找到 3D 样条实现。

一些将产生代表性数据的代码:

import numpy as np    
from scipy.spatial import cKDTree

x = np.random.uniform(-10, 10, 10000)
y = np.random.uniform(-10, 10, 10000)
z = np.random.uniform(-10, 10, 10000)
phi = np.random.uniform(-10**9, 0, 10000)

kdtree = cKDTree(np.c_[x,y,z])
_, index = kdtree.query([0,0,0], 32) #find 32 nearest particles to the origin
#find the gradient at (0,0,0) by considering the 32 nearest particles?  

(我的问题非常类似于计算具有不均匀间隔样本位置的 3D 梯度的函数 https://stackoverflow.com/questions/36781698/compute-3d-gradient-with-unevenly-spaced-sample-locations但那里似乎没有解决方案,所以我想我会再问一次。)

任何帮助,将不胜感激。


这是一个 Julia 实现,可以满足您的要求

using NearestNeighbors

n = 3;
k = 32; # for stability use  k > n*(n+3)/2

# Take a point near the center of cube
point = 0.5 + rand(n)*1e-3;
data = rand(n, 10^4);
kdtree = KDTree(data);
idxs, dists = knn(kdtree, point, k, true);

# Coords of the k-Nearest Neighbors
X = data[:,idxs];

# Least-squares recipe for coefficients
 C = point * ones(1,k); # central node
dX = X - C;  # diffs from central node
 G = dX' * dX;
 F =  G .* G;
 v = diag(G);
 N = pinv(G) * G;
 N = eye(N) - N;
 a =  N * pinv(F*N) * v;  # ...these are the coeffs

# Use a temperature distribution of  T = 25.4 * r^2
# whose analytical gradient is   gradT = 25.4 * 2*x
X2 = X .* X;
C2 = C .* C;
T  = 25.4 * n * mean(X2, 1)';
Tc = 25.4 * n * mean(C2, 1)'; # central node
dT = T - Tc;       # diffs from central node

y = dX * (a .* dT);   # Estimated gradient
g = 2 * 25.4 * point; # Analytical

# print results
@printf "Estimated  Grad  = %s\n" string(y')
@printf "Analytical Grad  = %s\n" string(g')
@printf "Relative Error   = %.8f\n" vecnorm(g-y)/vecnorm(g)


该方法的相对误差约为1%。以下是几次运行的结果...

Estimated  Grad  = [25.51670916224472 25.421038632006926 25.6711949674633]
Analytical Grad  = [25.41499027802736 25.44913042322385  25.448202594123806]
Relative Error   = 0.00559934

Estimated  Grad  = [25.310574056859014 25.549736360607493 25.368056350800604]
Analytical Grad  = [25.43200914200516  25.43243178887198  25.45061497749628]
Relative Error   = 0.00426558


Update
我不太了解 Python,但这里有一个似乎有效的翻译

import numpy as np
from scipy.spatial import KDTree

n = 3;
k = 32;

# fill the cube with random points
data = np.random.rand(10000,n)
kdtree = KDTree(data)

# pick a point (at the center of the cube)
point = 0.5 * np.ones((1,n))

# Coords of k-Nearest Neighbors
dists, idxs = kdtree.query(point, k)
idxs = idxs[0]
X = data[idxs,:]

# Calculate coefficients
C = (np.dot(point.T, np.ones((1,k)))).T # central node
dX= X - C                    # diffs from central node
G = np.dot(dX, dX.T)
F = np.multiply(G, G)
v = np.diag(G);
N = np.dot(np.linalg.pinv(G), G)
N = np.eye(k) - N;
a = np.dot(np.dot(N, np.linalg.pinv(np.dot(F,N))), v)  # these are the coeffs

#  Temperature distribution is  T = 25.4 * r^2
X2 = np.multiply(X, X)
C2 = np.multiply(C, C)
T  = 25.4 * n * np.mean(X2, 1).T
Tc = 25.4 * n * np.mean(C2, 1).T # central node
dT = T - Tc;       # diffs from central node

# Analytical gradient ==>  gradT = 2*25.4* x
g = 2 * 25.4 * point;
print( "g[]: %s" % (g) )

# Estimated gradient
y = np.dot(dX.T, np.multiply(a, dT))
print( "y[]: %s,   Relative Error = %.8f" % (y, np.linalg.norm(g-y)/np.linalg.norm(g)) )


更新#2
我想我可以使用格式化的 ASCII 而不是 LaTeX 编写一些易于理解的内容...



`Given a set of M vectors in n-dimensions (call them b_k), find a set of
`coeffs (call them a_k) which yields the best estimate of the identity
`matrix and the zero vector
`
`                                 M
` (1) min ||E - I||,  where  E = sum  a_k b_k b_k
`     a_k                        k=1
`
`                                 M
` (2) min ||z - 0||,  where  z = sum  a_k b_k
`     a_k                        k=1
`
`
`Note that the basis vectors {b_k} are not required
`to be normalized, orthogonal, or even linearly independent.
`
`First, define the following quantities:
`
`  B             ==> matrix whose columns are the b_k
`  G = B'.B      ==> transpose of B times B
`  F = G @ G     ==> @ represents the hadamard product
`  v = diag(G)   ==> vector composed of diag elements of G
`
`The above minimizations are equivalent to this linearly constrained problem
`
`  Solve  F.a = v
`  s.t.   G.a = 0
`
`Let {X} denote the Moore-Penrose inverse of X.
`Then the solution of the linear problem can be written:
`
`  N = I - {G}.G       ==> projector into nullspace of G
`  a = N . {F.N} . v
`
`The utility of these coeffs is that they allow you to write
`very simple expressions for the derivatives of a tensor field.
`
`
`Let D be the del (or nabla) operator
`and d be the difference operator wrt the central (aka 0th) node,
`so that, for any scalar/vector/tensor quantity Y, we have:
`  dY = Y - Y_0
`
`Let x_k be the position vector of the kth node.
`And for our basis vectors, take
`  b_k = dx_k  =  x_k - x_0.
`
`Assume that each node has a field value associated with it
` (e.g. temperature), and assume a quadratic model [about x = x_0]
` for the field [g=gradient, H=hessian, ":" is the double-dot product]
`
`     Y = Y_0 + (x-x_0).g + (x-x_0)(x-x_0):H/2
`    dY = dx.g + dxdx:H/2
`   D2Y = I:H            ==> Laplacian of Y
`
`
`Evaluate the model at the kth node 
`
`    dY_k = dx_k.g  +  dx_k dx_k:H/2
`
`Multiply by a_k and sum
`
`     M               M                  M
`    sum a_k dY_k =  sum a_k dx_k.g  +  sum a_k dx_k dx_k:H/2
`    k=1             k=1                k=1
`
`                 =  0.g   +  I:H/2
`                 =  D2Y / 2
`
`Thus, we have a second order estimate of the Laplacian
`
`                M
`   Lap(Y_0) =  sum  2 a_k dY_k
`               k=1
`
`
`Now play the same game with a linear model
`    dY_k = dx_k.g
`
`But this time multiply by (a_k dx_k) and sum
`
`     M                    M
`    sum a_k dx_k dY_k =  sum a_k dx_k dx_k.g
`    k=1                  k=1
`
`                      =  I.g
`                      =  g
`
`
`In general, the derivatives at the central node can be estimated as
`
`           M
`    D#Y = sum  a_k dx_k#dY_k
`          k=1
`
`           M
`    D2Y = sum  2 a_k dY_k
`          k=1
`
` where
`   # stands for the {dot, cross, or tensor} product
`       yielding the {div, curl,  or grad} of Y
` and
`   D2Y stands for the Laplacian of Y
`   D2Y = D.DY = Lap(Y)
  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

计算具有不均匀间隔点的 3D 梯度 的相关文章

随机推荐

  • 正确使用Optional.ifPresent()

    我正在尝试理解ifPresent 的方法OptionalJava 8 中的 API 我有一个简单的逻辑 Optional
  • 帮助加入 Rails 3

    我有以下型号 class Event lt ActiveRecord Base has many action items end class ActionItem lt ActiveRecord Base belongs to event
  • 有丰富的领域模型示例吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个简单的示例来说明使用富域模型的好处 理想情况下 我想要一个之前和之后的代码列表 应该尽可能
  • 如何在 Mac 上的 Sublime Text 2 上运行 C++?

    我尝试在 Mac 上的 Sublime Text 2 上用 C 运行 hello world I typed include iostream int main cout lt lt Hello WOrld return 0 但这给了我一个
  • facebook graph api 确定用户是否喜欢 url

    如果用户当前登录 Facebook 并喜欢当前页面 我想显示不同的消息 我明白 FB Event subscribe edge create function response you like this 当用户喜欢该页面时 它将触发 但是
  • 结合 codeigniter 和 laravel

    我在用着codeigniter框架 但我想使用的功能laravel像下面的代码这样的框架 我可以打印变量而无需 Hello name 我能怎么做 Codeigniter 是一个 PHP 框架 Laravel 也是 php 框架 而这两者并不
  • 在 JS 中的浏览器中输入 url 时,偏移哈希标签链接以调整固定标头

    我希望创建一个页面 允许哈希标签跳转到页面的某些内容 e g http example com page1 http example com page1是一个普通页面 http example com page1 info http exa
  • 如何在iOS中根据文本长度更改UILabel宽度?

    我想在 UILabel 旁边显示图像 但是 UILabel 的文本长度可变 所以我不知道将图像放置在哪里 图像应根据标签的大小移动 我怎样才能做到这一点 虽然 Keshav 答案可以工作 但它已被弃用 try NSDictionary at
  • 其中 Py_FileSystemDefaultEncoding 在 python 源代码中设置

    我很好奇python源代码如何设置Py FileSystemDefaultEncoding的值 我收到了一件奇怪的事情 自从Pythondoc https docs python org 2 library sys html sys get
  • Django 模型选择不会因无效选择而引发错误

    我在 Django 中有一个带有选择字段的对象 class CustomFieldType models Model STRING STRING DATE DATE BOOLEAN BOOLEAN NUMERIC NUMERIC EMAIL
  • `[$injector:nomod] 模块“google-maps”不可用`

    我正在使用 angular google maps 在角度应用程序中处理谷歌地图 为此 我必须添加angular google maps js到项目 如果我按以下方式添加脚本 该页面可以正常工作 不会出现任何错误 但如果我使用本地副本 它将
  • L"" 和 u8"" 之间的区别

    以下有什么区别吗 auto s1 L 你好 auto s2 u8 你好 Are s1 and s2指的是同一类型 如果不是 有什么区别以及首选哪一个 它们不是同一类型 s2是 UTF 8 或窄字符串文字 这C 11标准草案 http www
  • 多个 tableView 单元格中的多个 collectionView

    我有一个tableView其中我有 2 个自定义单元格 在这两个单元格中我有不同的CollectionView 两者的数据源和委托CollectionViews is my ViewController 那么现在我如何检查哪个Collect
  • Mono 的 DNS 刷新超时

    虽然目前Mono项目的ServicePointManager类有DnsRefreshTimeout属性启用到其接口中 相关属性未实现 调用示例 ServicePointManager DnsRefreshTimeout 10 60 1000
  • 天蓝色媒体服务-请求正文太大并超出最大允许限制

    我正在使用 java SDK 并遵循以下示例 https learn microsoft com en us azure media services media services java how to use https learn m
  • 无需重新计算即可获取字典键哈希

    有没有办法从字典中提取现有的密钥哈希 而无需再次重新计算它们 暴露它们并因此通过哈希而不是密钥访问字典会有什么风险 我认为 Python 的字典对象没有任何公共 API 可以让您查看存储其对象的哈希值 您无法在 Python 代码中直接通过
  • Scala 恢复或recoverWith

    我们公司正在用Scala开发一些系统 我们有一些疑问 我们正在讨论如何映射未来的异常 但我们不知道何时应该使用选项 1 或选项 2 val created Future 选项1 val a created recover case e da
  • 具有运行空间池的 SessionStateProxy 变量

    我想在 PowerShell 中使用运行空间池来执行后台操作 但我需要从主线程访问 WPF 窗口变量 普通运行空间有以下选项 runspace SessionStateProxy SetVariable xamGUI xamGUI 但是我如
  • 内容更改时 DataGridView 样式不更新

    好的 这是我的情况 我有一个DataGridView含有Messages 应用以下样式
  • 计算具有不均匀间隔点的 3D 梯度

    我目前有一个由几百万个不均匀间隔的粒子组成的体积 每个粒子都有一个属性 对于那些好奇的人来说是潜力 我想计算其局部力 加速度 np gradient 仅适用于均匀分布的数据 我在这里查看 numpy 中的二阶梯度 https stackov