特征张量广播语法

时间:2017-07-31 18:30:23

标签: c++ numpy eigen

我有以下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重新实现。

以下是我提出的建议:

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 个答案:

答案 0 :(得分:1)

您可以尝试xtensor C ++模板库,它支持动态和静态维度http://xtensor.readthedocs.io

xtensor的API与numpy的API非常相似,包括矢量化,广播,通用功能。这里有xtensor备忘单:http://xtensor.readthedocs.io/en/latest/numpy.html

最后,您可以点击https://github.com/QuantStack/xtensor/顶部的活页夹徽章,在C ++ Jupyter笔记本中试用它。

xtensor还附带了科学计算主要语言(R,Julia,Python)的绑定。