2014-09-22 147 views
2

我懷疑這可能不是一個很好的問題,但我已經完全打破了它的牆壁並可以使用一些幫助。將Numpy代碼翻譯成C#

我想實現這個代碼:

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 

而我只是在努力破譯什麼是真正發生的事情。我不知道作者通過投影到特定的向量或輸出的格式(該死的你打字)意味着什麼。對我來說descprition也太模糊了。

有沒有人有任何建議或解釋這是做什麼?任何幫助不勝感激。

謝謝。

+3

閱讀文檔。 [numpy.dot](http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html),[numpy.diag](http://docs.scipy.org/doc/numpy /reference/generated/numpy.diag.html),[numpy.inner](http://docs.scipy.org/doc/numpy/reference/generated/numpy.inner.html)。 – CoryKramer 2014-09-22 15:47:18

+3

_「......我討厭這個部分」_Fuudian滑倒? :) – 2014-09-22 15:48:42

+0

哈哈,可能。關於文件。說實話,這有點讓我失望,尤其是它在處理兩個點陣(點生產?)在一起的數組時,它正在做什麼,但即使如此,這部分代碼的總體目標是做一點點我不清楚。 – djcmm476 2014-09-22 15:50:42

回答

1

這裏的一個樣品的計算,在交互式IPython的殼來完成:

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是4×3矩陣與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)