From b88f62d50e097b2385cb903c88fbf44ac32a333f Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Wed, 28 Aug 2013 11:15:26 +0200 Subject: [PATCH] arrays: details while writing doc - add factories for matrices - add make_immutablef from a lambda - details --- test/triqs/arrays/make_immutables.cpp | 6 ++++ triqs/arrays/algorithms.hpp | 20 +++--------- triqs/arrays/impl/indexmap_storage_pair.hpp | 2 ++ triqs/arrays/indexmaps/range.hpp | 2 ++ triqs/arrays/make_immutable_array.hpp | 36 ++++++++++++++++++--- triqs/arrays/matrix.hpp | 15 +++++++++ 6 files changed, 60 insertions(+), 21 deletions(-) diff --git a/test/triqs/arrays/make_immutables.cpp b/test/triqs/arrays/make_immutables.cpp index dd93af3e..4421e146 100644 --- a/test/triqs/arrays/make_immutables.cpp +++ b/test/triqs/arrays/make_immutables.cpp @@ -36,5 +36,11 @@ int main(int argc, char **argv) { TEST( (tqa::array(tqa::make_immutable_array( i_ + j_, i_= range(0,2), j_=range(0,2))))); + // or if you prefer using a lambda... + auto b = make_immutable_array ( [](int i, int j) { return i-j;}, range (0,2), range(0,2)); + + std::cerr << "b = " << b << std::endl; + std::cerr << "b = " << tqa::array(b) << std::endl; + return 0; } diff --git a/triqs/arrays/algorithms.hpp b/triqs/arrays/algorithms.hpp index 9059d0ae..8779c6ca 100644 --- a/triqs/arrays/algorithms.hpp +++ b/triqs/arrays/algorithms.hpp @@ -26,8 +26,6 @@ #include "./functional/fold.hpp" #include -#define TRIQS_FIX_CLANG30_linux - namespace triqs { namespace arrays { // get_first_element(a) returns .. the first element, i.e. a(0,0,0...) @@ -41,22 +39,14 @@ namespace triqs { namespace arrays { template typename A::value_type max_element(A const &a) { -#ifdef TRIQS_FIX_CLANG30_linux - typedef typename A::value_type const & T; - return fold ( static_cast(std::max)) ( a, get_first_element(a)); -#else - return fold ( std::max) ( a, get_first_element(a)); -#endif + //typedef typename A::value_type const & T; return fold ( static_cast(std::max)) ( a, get_first_element(a)); + return fold (std::max) (a, get_first_element(a)); } template typename A::value_type min_element(A const &a) { -#ifdef TRIQS_FIX_CLANG30_linux - typedef typename A::value_type const & T; - return fold ( static_cast(std::min)) ( a, get_first_element(a)); -#else - return fold ( std::min) ( a, get_first_element(a)); -#endif + // typedef typename A::value_type const & T; return fold ( static_cast(std::min)) ( a, get_first_element(a)); + return fold (std::min) (a, get_first_element(a)); } template @@ -65,9 +55,7 @@ namespace triqs { namespace arrays { template typename A::value_type prod(A const & a) { return fold ( std::multiplies()) (a,1); } - }}//namespace triqs::arrays - #endif diff --git a/triqs/arrays/impl/indexmap_storage_pair.hpp b/triqs/arrays/impl/indexmap_storage_pair.hpp index 0628c602..470802be 100644 --- a/triqs/arrays/impl/indexmap_storage_pair.hpp +++ b/triqs/arrays/impl/indexmap_storage_pair.hpp @@ -63,6 +63,8 @@ namespace triqs { namespace arrays { typedef IndexMapType indexmap_type; static constexpr unsigned int rank = IndexMapType::domain_type::rank; static constexpr ull_t opt_flags = OptionFlags; + static constexpr ull_t traversal_order = TraversalOrder; + protected: indexmap_type indexmap_; diff --git a/triqs/arrays/indexmaps/range.hpp b/triqs/arrays/indexmaps/range.hpp index bb85a4c7..ca58576f 100644 --- a/triqs/arrays/indexmaps/range.hpp +++ b/triqs/arrays/indexmaps/range.hpp @@ -30,6 +30,8 @@ namespace triqs { namespace arrays { std::ptrdiff_t first_, last_, step_; public: + typedef std::ptrdiff_t index_type; + range():first_(0),last_(-1),step_(1) {} // i.e. all range(const range& r):first_(r.first_), last_(r.last_), step_(r.step_) {} range(std::ptrdiff_t first__, std::ptrdiff_t last__, std::ptrdiff_t step__=1):first_(first__), last_(last__), step_(step__) {} diff --git a/triqs/arrays/make_immutable_array.hpp b/triqs/arrays/make_immutable_array.hpp index 8b7aac8b..4d1df7f4 100644 --- a/triqs/arrays/make_immutable_array.hpp +++ b/triqs/arrays/make_immutable_array.hpp @@ -21,20 +21,22 @@ #ifndef TRIQS_ARRAYS_MAKE_IMMUTABLE_ARRAY_H #define TRIQS_ARRAYS_MAKE_IMMUTABLE_ARRAY_H #include "./array.hpp" +#include "../utility/tuple_tools.hpp" + namespace triqs { namespace arrays { template - class immutable_array_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) { + class immutable_array_expr_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) { template struct _si { typedef size_t type;}; public : - immutable_array_impl(Expr e_, clef::pair ... p): + immutable_array_expr_impl(Expr e_, clef::pair ... p): f(clef::make_function(e_, clef::placeholder()...)), dom_(make_shape(p.rhs().size()...)) {}; typedef typename triqs::clef::result_of::make_function... >::type function_type; typedef typename std::result_of::type...)>::type value_type; typedef indexmaps::cuboid::domain_t domain_type; domain_type domain() const { return dom_;} template value_type operator() (Args const & ... args) const { return f(args...); } - friend std::ostream & operator<<(std::ostream & out, immutable_array_impl const & x){return out<<" immutable_array on domain : "< - immutable_array_impl make_immutable_array( Expr const & expr, clef::pair ... p) { - return immutable_array_impl (expr, p...); + immutable_array_expr_impl make_immutable_array( Expr const & expr, clef::pair ... p) { + return immutable_array_expr_impl (expr, p...); } + // if ones prefers to use a function... + + template + class immutable_array_fun_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) { + template struct _si { typedef size_t type;}; + public : + immutable_array_fun_impl(Function f, Ranges ... r) : f_(std::move(f)), dom_(make_shape(r.size()...)) {}; + //typedef typename std::function::result_type value_type; + typedef decltype( triqs::tuple::apply(std::declval(),std::make_tuple(typename Ranges::index_type()...))) value_type; + typedef indexmaps::cuboid::domain_t domain_type; + domain_type domain() const { return dom_;} + template value_type operator() (Args const & ... args) const { return f_(args...); } + friend std::ostream & operator<<(std::ostream & out, immutable_array_fun_impl const & x){return out<<" immutable_array on domain : "< + immutable_array_fun_impl make_immutable_array(Function f, Ranges ... r) { + return immutable_array_fun_impl (std::move(f), r...); + } + + }}//namespace triqs::arrays #endif diff --git a/triqs/arrays/matrix.hpp b/triqs/arrays/matrix.hpp index 8eef4d4f..f7f61f96 100644 --- a/triqs/arrays/matrix.hpp +++ b/triqs/arrays/matrix.hpp @@ -193,6 +193,21 @@ namespace triqs { namespace arrays { #undef _IMPL_MATRIX_COMMON #undef IMPL_TYPE + + template + matrix_view + make_matrix_view(ArrayType const & a) { + static_assert(ArrayType::rank ==2, "make_matrix_view only works for array of rank 2"); + return a; + } + + template + matrix + make_matrix(ArrayType const & a) { + static_assert(ArrayType::rank ==2, "make_matrix_view only works for array of rank 2"); + return a; + } + }}//namespace triqs::arrays // The std::swap is WRONG for a view because of the copy/move semantics of view.