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

raw_view and quat_view are not lazy in the xtensor-sense #1

Open
nunupeke opened this issue Nov 8, 2019 · 2 comments
Open

raw_view and quat_view are not lazy in the xtensor-sense #1

nunupeke opened this issue Nov 8, 2019 · 2 comments

Comments

@nunupeke
Copy link
Owner

nunupeke commented Nov 8, 2019

// view an n-d xcontainer of quaternions as a "plain" (n+1)-d xcontainer
// (the last dimension of the returned view will be 4)
template <class E>
auto raw_view(E&& in) {
using value_type = typename std::decay_t<E>::value_type::value_type;
auto shape = in.shape();
std::vector<std::size_t> vshape(shape.begin(), shape.end());
vshape.push_back(4);
return xt::adapt(reinterpret_cast<value_type*>(in.data()),
in.size()*4, xt::no_ownership(), vshape);
}
// view an n-d xcontainer as an (n-1)-d xcontainer of quaternions
// (the last dimension of the input container must be 4)
template <class E>
auto quat_view(E&& in) {
using value_type = typename std::decay_t<E>::value_type;
auto shape = in.shape();
std::vector<std::size_t> vshape(shape.begin(), shape.end());
assert(vshape.back() == 4);
vshape.pop_back();
return xt::adapt(reinterpret_cast<xt::quaternion<value_type>*>(in.data()),
in.size()/4, xt::no_ownership(), vshape);
}

I need to evaluate before I can use q.data().

@nunupeke nunupeke changed the title raw_view and quat_view are not lazy in the xtensor-sense raw_view and quat_view are not lazy in the xtensor-sense Nov 8, 2019
@nunupeke
Copy link
Owner Author

nunupeke commented Nov 8, 2019

@wolfv, can you give me a hint on this one (again). See also here.

@wolfv
Copy link

wolfv commented Nov 13, 2019

Hi @nunupeke sorry for being late to respond here. Indeed, that's not lazy in the xtensor sense.

I am trying to figure out a way to do it but for the time being I also don't have an elegant solution. I think right now you'd probably need to implement your own view-like class for a quaternion-array.

Another possibility would be to implement a free function access operator like so:

template <class E1>
inline auto get_x(E1&& e1) noexcept
{
    auto fnct = [](auto& quat) -> decltype(quat.x()) {
        return quat.x();
    };
    return xt::make_lambda_xfunction(std::move(fnct), std::forward<E1>(e1));
}

This would be lazy, and you could even assign to it (if quat.x() returns a reference)...

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