Here I'm trying to do a very basic operation with the xtensor library in C++. I have the xarray a
, and with the index related function xt::where
, I want to get an array of indexes where the condition holds True (beware, there is another xt::where function, but it is an operator function and I don't want it).
When I try to compile it, with this line, I get a lot of errors:
g++ -I/usr/include/xtensor -I/usr/local/include/xtl getindx.cpp -o getindx
Curiously, when I try to use the other xt::where function (the operator function), it works and compiles and runs. I'm clearly missing something; I search for it, but I can't get through, please help me! Thank you.
Here is the code:
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xoperation.hpp"
#include "xtensor/xtensor.hpp"
using namespace std;
int main(int argc, char** argv){
xt::xarray<double> arr {5.0, 6.0, 7.0};
auto idx = xt::where(arr >= 6);
std::cout << idx << std::endl;
return 0;
}
EDIT: the error.
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<std::vector<long unsigned int>, std::allocator<std::vector<long unsigned int> > >’)
std::cout << idx << std::endl;
EDIT2: solved without xtensor. Maybe it will be a bit slower.
int main(int argc, char** argv){
std::vector<double> arr{5.0,6.0,7.0};
std::vector<unsigned int> indices;
auto ptr = &bits[0];
for (int i = 0; i<arr.size(); i++, ptr++)
{
if (*ptr>=6) indices.push_back (i);
}
for (int i=0; i<indices.size(); i++){
cout << "indices= "indices[i] << endl;
} //output: indices=1, indices=2.
return 0;
}
The problem is that
xt::where
(or maybe the syntaxxt::argwhere
is more descriptive here) returns anstd::vector
or array indices for which there is nooperator<<
overload e.g. for printing.To deal with this
xt::from_indices
was created. From the relevant docs page:In this case
idx
can also be typedxt::xarray<size_t>
orxt::xtensor<size_t, 2>
if you'd want to be more verbose that withauto
.