如何在 xt::filter 中使用相等性?

How can I use equality in xt::filter?

下面的代码,

#include <iostream>

#include "xtensor/xadapt.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xindex_view.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xmasked_view.hpp"
#include "xtensor/xview.hpp"

using namespace std;

int main() {
    xt::xarray<float> a = {{1, 2, 3}, {4, 2, 6}, {9, 0, 2}};
    cout << a << endl;
    xt::filter(a, a == 2) = 10;
    cout << a << endl;
}

编译失败:error: no match for ‘operator==’ (operand types are ‘xt::xarray<float>’ ... and ‘int’)

但是,其他比较运算符按预期工作(>、<、>=、<=)。我不确定 operator== 是否有意未实现,但直到它实现(如果曾经实现),是否有解决方法,它是什么?

xtensor 提供逻辑运算符,如 &&||。将这些与允许的比较相结合会产生与“==”或“!=”预期相同的输出。

特别是 a >= 2 && a <= 2 <=> a == 2a > 2 || a < 2 <=> a != 2

所以我的最终计划是

#include <iostream>

#include "xtensor/xadapt.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xindex_view.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xmasked_view.hpp"
#include "xtensor/xview.hpp"

using namespace std;

int main() {
    xt::xarray<float> a = {{1, 2, 3}, {4, 2, 6}, {9, 0, 2}};
    cout << a << endl;
    xt::filter(a, a >= 2 && a <= 2) = 10;
    cout << a << endl;
}

它的输出是

{{ 1.,  2.,  3.},
 { 4.,  2.,  6.},
 { 9.,  0.,  2.}}
{{  1.,  10.,   3.},
 {  4.,  10.,   6.},
 {  9.,   0.,  10.}}

您可以使用 xt::equal(a, b) 而不是 a == b。即

xt::filter(a, xt::equal(a, 2)) = 10;

做你想做的。