如何对Boost :: uBlas :: vector中的元素子集执行操作?

时间:2013-03-05 12:15:24

标签: c++ boost vectorization ublas boost-ublas

假设您有一个很长的boost :: numeric :: ublas :: vector,并且您希望对元素的子集执行更新操作。应该更新多少元素介于“全部”或“无”之间。 哪个要更新的元素由稀疏的compressed_vector给出,每个元素应该更新一个“1”。

我可以想出两种方法来解决这个问题:

  1. 只需将右侧与面具相乘:

    using namespace boost::numeric::ublas;
    vector<double> x,some,other,stuff;
    compressed_vector<int> update_mask;
    [...]
    noalias(x) += element_prod(update_mask, some+element_div(other,stuff))
    

    问题在于它看起来非常低效:不会ublas计算整个向量,然后在这种情况下丢弃所有未使用的值(即,其中up​​date_mask == 0)?

    我希望它比

    更慢
    noalias(x) += some+element_div(other,stuff)
    

    如果只需要更新一些元素,这将是非常低效的。

  2. 循环显示所有要更新的值

    [....]
    for(compressed_vector<int>::iterator it = update_mask.begin(); it!=update_mask.end(); ++it)
        x[it.index()] += some[it.index()]+other[it.index()]/stuff[it.index()]);
    

    这个问题是:a)看起来很糟糕,b)有点失败了首先使用向量的目的而且c。)如果要更新很多索引和/或操作,应该是非常低效的变得更加复杂。

  3. 有关如何有效执行此操作的任何想法?我很确定这是一个相当常见的问题,但我找不到任何有用的东西(而且ublas文档......并不好玩)。

0 个答案:

没有答案