2017-07-31 37 views
0

我有以下numpy的代碼:徵張廣播語法

# q.shape == (fxs, ks) 
# E.shape == (fxs, fxs) 
C = q[:, np.newaxis, :] * E[:, :, np.newaxis] * q[np.newaxis, :, :] 
# C.shape == (fxs, fxs, ks) 

,我在我的本徵重新實現。

這就是我想出了:

Eigen::Tensor<T, 3> C = 
      q.reshape(Eigen::array<int, 3> {fxs, 1, ks}).broadcast(Eigen::array<int, 3> {1, fxs, 1}) 
     * E.reshape(Eigen::array<int, 3> {fxs, fxs, 1 }).broadcast(Eigen::array<int, 3> {1, 1, ks}) 
     * q.reshape(Eigen::array<int, 3> {1, fxs, ks}).broadcast(Eigen::array<int, 3> {fxs, 1, 1}); 

但這似乎相當冗長。這是正確的翻譯?

回答

1

你可以嘗試xtensor C++模板庫,它支持動態和靜態尺寸http://xtensor.readthedocs.io

xtensor有一個API,這是非常相似numpy的,包括矢量,廣播,通用功能。還有就是xtensor小抄這裏numpy的:http://xtensor.readthedocs.io/en/latest/numpy.html

最後,你可以嘗試通過點擊粘結劑徽章在https://github.com/QuantStack/xtensor/

xtensor頂部生活在一個C++ Jupyter筆記本還配備了綁定主科學計算語言(R,Julia,Python)。