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

add xexpression_shaped experiment #867

Closed
wants to merge 2 commits into from

Conversation

wolfv
Copy link
Member

@wolfv wolfv commented May 22, 2018

This will most likely fail on travis etc. but this could be a way to add the feature of #860 ?

this allows for overloads like this:

template <class T>
int fn(xexpression_shaped<T, dynamic_shape<size_t>>)
{
    std::cout << "xarray" << std::endl;
    return 123;
}

template <class T>
int fn(xexpression_shaped<T, std::array<size_t, 2>>& x)
{
    std::cout << "xtensor" << std::endl;
    std::cout << x.derived_cast()[0] << std::endl;
    return 123;
}

we could also add some shorthand forms for xexpression_shaped<T, S> like xtensor_t<...> etc.

@benbovy
Copy link
Contributor

benbovy commented May 28, 2018

This looks nice!

One (naïve) question: would it be possible to put xtensor_t<...> somewhere in the inheritance chain so that x.derived_cast() is not needed in the function body to access the element values, shape, etc. of x?

@JohanMabille
Copy link
Member

@benbovy it depends, the way it is done now ensures this works for arrays, tensors and views. We could move the xexpression_shaped class in the hierarchy and make it inherit from xcontainer for instance. This way, you could use all the container methods (access operator, shape, etc) without typing any x.derived_cast(), but this would exclude the views from this feature. Also you could not use the expression operators without a derived_cast.

@benbovy
Copy link
Contributor

benbovy commented May 28, 2018

OK, I see. I now better understand what is supported / not supported when we use xexpression or xcontainer.

I was actually hesitating on which to choose in my function signatures. Inside these functions I use most of the time the container methods as the algorithms I'd like to implement are not easy to write using basic expressions. But sometimes I need to use views. So I think I'm gonna stick with xexpression. This PR will help a lot having more explicit signatures. I'll have to use derived_cast quite often, which may look a bit verbose, but it is not a big deal I think.

@JohanMabille
Copy link
Member

JohanMabille commented May 28, 2018

You can use method with explicit signatures in your APIs and then forward to private methods accepting anything, so the derived cast would be done a few times only:

namespace detail
{
    template <class E>
    auto f_impl(E&& e)
    {
        // here you can use e(0, 0, ...), e.shape(), and even e+= something
        return something;
    }
}

template <class E>
auto f(const xexpression_shape<E>& e)
{
    return detail::f_impl(e.derived_cast());
}

This way you can also use the same implementation for rvalue references if you want to support them too:

template <class E>
auto f(xexpression<E>&& e)
{
    return detail::f_impl(std::move(e.derived_cast()));
}

@benbovy
Copy link
Contributor

benbovy commented May 28, 2018

Thanks for the tip @JohanMabille.

@wolfv wolfv force-pushed the xexpression_shaped branch from 56e8fe3 to 5e65bf9 Compare July 20, 2018 13:33
@wolfv
Copy link
Member Author

wolfv commented Jul 24, 2018

superseded by #994

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

Successfully merging this pull request may close these issues.

3 participants