将Numpy代码转换为C#

2024-04-24 21:11:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在尝试实现以下代码:

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

我只是在努力解释到底发生了什么。我不知道作者通过投影到特定的向量上是什么意思,也不知道输出的格式(该死的鸭子打字)。这个描述对我来说也有点太模糊了。在

有人对这件事有什么建议或解释吗?非常感谢任何帮助。在

谢谢。在


Tags: andofthenumberwhichreturnisnp
1条回答
网友
1楼 · 发布于 2024-04-24 21:11:58

下面是一个在交互式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]])

如函数doc中指定的,输入是(4,3)pointsvectors一个2(3,)向量的列表,输出是(4,2)数组,p。在

d是4x3矩阵与2x3(或转置后,3x2)数组的矩阵(点)乘积,结果是4x2。v2也可以计算为np.sum(u,u, axis=1),即两个向量的大小。p只是v2规范化的点积。在

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

^{pr2}$

相关问题 更多 >