xtensor-r icon indicating copy to clipboard operation
xtensor-r copied to clipboard

xt::variance() bug?

Open DavisVaughan opened this issue 5 years ago • 3 comments

Any idea why this compiles without variance but not with it?

// [[Rcpp::depends(xtensorrr)]]
// [[Rcpp::plugins(cpp14)]]

#include <xtensor-r/rarray.hpp>
#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
xt::rarray<double> xtensor_sum(xt::rarray<double> x) {
  std::vector<std::size_t> axes;
  axes.push_back(0);
  return xt::sum(x, axes);
}

// [[Rcpp::export]]
xt::rarray<double> xtensor_variance(xt::rarray<double> x) {
  std::vector<std::size_t> axes;
  axes.push_back(0);
  return xt::variance(x, axes);
}
In file included from test.cpp:4:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor-r/rarray.hpp:17:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xbuffer_adaptor.hpp:20:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xstorage.hpp:21:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xtensor_simd.hpp:14:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xutils.hpp:1183:31: error: implicit instantiation of undefined template 'xt::rebind_container<long, xt::xbuffer_adaptor<int *, xt::no_ownership, std::__1::allocator<int> > >'
        using type = typename rebind_container<std::ptrdiff_t, S>::type;
                              ^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xutils.hpp:1196:5: note: in instantiation of template class 'xt::get_strides_type<xt::xbuffer_adaptor<int *, xt::no_ownership, std::__1::allocator<int> > >' requested here
    using get_strides_t = typename get_strides_type<C>::type;
    ^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xstrided_view.hpp:631:9: note: in instantiation of template type alias 'get_strides_t' requested here
        get_strides_t<shape_type> strides;
        ^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xmath.hpp:1919:24: note: in instantiation of function template specialization 'xt::reshape_view<xt::xfunction<xt::detail::divides, xt::xreducer<xt::xreducer_functors<std::__1::plus<double>, xtl::identity, std::__1::plus<double> >, const xt::rarray<double> &, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > >, xt::xscalar<double> >, xt::xbuffer_adaptor<int *, xt::no_ownership, std::__1::allocator<int> > >' requested here
        auto mrv = xt::reshape_view(std::move(inner_mean), std::move(keep_dim_shape));
                       ^
test.cpp:20:14: note: in instantiation of function template specialization 'xt::variance<xt::rarray<double> &, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > &, xt::evaluation_strategy::lazy, xt::concept_check_successful>' requested here
  return xt::variance(x, axes);
             ^
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xtensorrr/include/xtensor/xutils.hpp:80:12: note: template is declared here
    struct rebind_container;
           ^
1 error generated.
make: *** [test.o] Error 1

DavisVaughan avatar Dec 29 '18 02:12 DavisVaughan

Looks like a bug in either xtensor-r or xtensor. I think the shape type of an rarray should be svector and the shape type of an rtensor should be std::array. Only the inner shape type should be a buffer adaptor.

wolfv avatar Dec 30 '18 11:12 wolfv

Indeed the issue is that

  • xcontainer.shape() returns the inner shape type, which is an adaptor in the case of rarray
  • variance uses reshape_view with a shape returned by shape(), on which get_strides_type in instantiated.

The solution instead of changing any internal shape type might be to drop the use of get_strides_type in xtensor...

SylvainCorlay avatar Jan 01 '19 15:01 SylvainCorlay

Small bump to have this fixed if you get the time. I was planning on doing an example that required stddev() to work.

DavisVaughan avatar Apr 16 '19 15:04 DavisVaughan