gpt4 book ai didi

c# - 将 Numpy 代码翻译成 C#

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:19 25 4
gpt4 key购买 nike

我怀疑这可能不是一个很好的问题,但我已经完全碰壁了,需要一些帮助。

我正在尝试实现这段代码:

http://www.nathanieltroutman.net/content/calculating-minimum-volume-bounding-box

在 C# 中,原来是在 Python 中。

在我点击这个部分之前一切顺利:

def calcProjections(points, *vectors):
"""Calculates the projection of points (NxD) onto the vectors
(MxD) and return the projections p which is a matrix sized (N, M)
where N is the number of points and M is the number of vectors.
p[i][j], is the projection of points[i] onto vectors[j] (which is
between 0 and 1)."""

u = np.array(vectors)

# project the points onto the vectors into on fell swoop
d = np.dot(points, u.T)

# this is the dot product of each vector with itself
v2 = np.diag(np.inner(u, u))

p = d / v2

return p

我只是在努力破译实际发生的事情。我不确定作者通过投影到特定向量或输出格式(该死的你不要输入)是什么意思。描述对我来说也有点太模糊了。

有人对此有何建议或解释吗?非常感谢任何帮助。

谢谢。

最佳答案

这是一个示例计算,在交互式 Ipython shell 中完成:

In [63]: points=np.arange(12,dtype=float).reshape(4,3)
In [64]: vectors=[np.array([1,0,0],dtype=float),np.array([0,1,1],dtype=float)]
In [65]: u=np.array(vectors)
In [66]: points
Out[66]:
array([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.],
[ 9., 10., 11.]])
In [67]: u
Out[67]:
array([[ 1., 0., 0.],
[ 0., 1., 1.]])
In [68]: d=np.dot(points, u.T)
In [69]: d
Out[69]:
array([[ 0., 3.],
[ 3., 9.],
[ 6., 15.],
[ 9., 21.]])
In [70]: v2=np.diag(np.inner(u,u))
In [71]: d/v2
Out[71]:
array([[ 0. , 1.5],
[ 3. , 4.5],
[ 6. , 7.5],
[ 9. , 10.5]])

如函数文档中所指定,输入是 (4,3) pointsvectors 2 (3 ,) 向量,输出为(4,2) 数组,p

d 是 4x3 矩阵与 2x3(或转置后,3x2)数组的矩阵(点)积,得到 4x2。 v2 也可以计算为 np.sum(u,u, axis=1),即 2 个“向量”的大小。 p 只是被 v2 归一化的点积。

如果您熟悉爱因斯坦求和符号(用于物理学),计算也表示为:

np.einsum('ij,kj->ik',points,u)/np.einsum('ij,ij->i',u,u)

关于c# - 将 Numpy 代码翻译成 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25978106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com