Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ranges::to eigen #1803

Open
hwelzel opened this issue Nov 29, 2023 · 2 comments
Open

ranges::to eigen #1803

hwelzel opened this issue Nov 29, 2023 · 2 comments

Comments

@hwelzel
Copy link

hwelzel commented Nov 29, 2023

Is there a way to collect to an Eigen::VectorXd?
If I do...

const auto xs = std::vector{1.2, 3.4, 5.6};
const auto ys = xs | ranges::to<Eigen::VectorXd>;

...I get among other things...

...
/opt/compiler-explorer/libs/rangesv3/0.12.0/include/range/v3/range/conversion.hpp:319:24: note: in instantiation of function template specialization 'Eigen::Matrix<double, -1, 1, 0>::Matrix<__gnu_cxx::__normal_iterator<const double *, std::vector<double>>, __gnu_cxx::__normal_iterator<const double *, std::vector<double>>>' requested here
                return Cont(I{ranges::begin(rng)}, I{ranges::end(rng)});
...

...which is understandable because eigen doesn't have that ctor. Nor does it have an assign(begin, end) for the other overload. I am not even sure Eigen::VectorXd complies to the range concept.

Still, I was wondering if there is a way to hook in a custom container to struct/function overload which would be called to initialise custom containers like eigen. I would need to do something more or less like:

Eigen::VectorXd to(std::distance(std::begin(from), std::end(from)));
std::copy(std::begin(from), std::end(from), std::begin(to));
return to;
@brevzin
Copy link
Collaborator

brevzin commented Dec 1, 2023

I'm not familiar with Eigen.

to() was standardized from C++23, you can find the mechanism for how it works here. In short, if C is the destination type and r is the source range, it'll try to do (in order):

  1. C(r)
  2. C(from_range, r)
  3. C(ranges::begin(r), ranges::end(r))
  4. C c; /* try to reserve */; ranges::copy(r, /* inserter into c */);
  5. A more complicated thing for recursion

It looks like option (4) there might work for Eigen::VectorXd, does it? If so we could try to extend the implementation here to do something similar.

range-v3 right now tries to do

  1. C c; r.reserve(ranges::size(r)); c.assign(ranges::begin(r), ranges::end(r));
  2. C(ranges::begin(r), ranges::end(r))

@hwelzel
Copy link
Author

hwelzel commented Dec 1, 2023

Thanks for all the info!
Indeed option 4 should work with Eigen. Its pretty much what I do in my custom function in the original comment as you noted.

I tried with c++23 std::ranges::to (should've done before) but unfortunately it doesn't compile.
https://godbolt.org/z/rsd8bdEKc
So I am not sure that in the end it would work with ranges-v3 either. Maybe Eigen would need to do something as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants