3
0
mirror of https://github.com/triqs/dft_tools synced 2025-01-12 22:18:23 +01:00

arrays: details while writing doc

- add factories for matrices
- add make_immutablef from a lambda
- details
This commit is contained in:
Olivier Parcollet 2013-08-28 11:15:26 +02:00
parent 009f0ea829
commit b88f62d50e
6 changed files with 60 additions and 21 deletions

View File

@ -36,5 +36,11 @@ int main(int argc, char **argv) {
TEST( (tqa::array<int,2>(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<double,2>(b) << std::endl;
return 0;
}

View File

@ -26,8 +26,6 @@
#include "./functional/fold.hpp"
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#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<class A>
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<T(*)(T,T)>(std::max<T>)) ( a, get_first_element(a));
#else
return fold ( std::max<typename A::value_type const & >) ( a, get_first_element(a));
#endif
//typedef typename A::value_type const & T; return fold ( static_cast<T(*)(T,T)>(std::max<T>)) ( a, get_first_element(a));
return fold (std::max<typename A::value_type const & >) (a, get_first_element(a));
}
template<class A>
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<T(*)(T,T)>(std::min<T>)) ( a, get_first_element(a));
#else
return fold ( std::min<typename A::value_type const & >) ( a, get_first_element(a));
#endif
// typedef typename A::value_type const & T; return fold ( static_cast<T(*)(T,T)>(std::min<T>)) ( a, get_first_element(a));
return fold (std::min<typename A::value_type const & >) (a, get_first_element(a));
}
template <class A>
@ -65,9 +55,7 @@ namespace triqs { namespace arrays {
template <class A>
typename A::value_type prod(A const & a) { return fold ( std::multiplies<typename A::value_type>()) (a,1); }
}}//namespace triqs::arrays
#endif

View File

@ -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_;

View File

@ -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__) {}

View File

@ -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<typename Expr, int ... ph>
class immutable_array_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) {
class immutable_array_expr_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) {
template<int I> struct _si { typedef size_t type;};
public :
immutable_array_impl(Expr e_, clef::pair<ph,range> ... p):
immutable_array_expr_impl(Expr e_, clef::pair<ph,range> ... p):
f(clef::make_function(e_, clef::placeholder<ph>()...)), dom_(make_shape(p.rhs().size()...)) {};
typedef typename triqs::clef::result_of::make_function<Expr,clef::placeholder<ph>... >::type function_type;
typedef typename std::result_of<function_type(typename _si<ph>::type...)>::type value_type;
typedef indexmaps::cuboid::domain_t<sizeof...(ph)> domain_type;
domain_type domain() const { return dom_;}
template<typename ... Args> 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 : "<<x.domain();}
friend std::ostream & operator<<(std::ostream & out, immutable_array_expr_impl const & x){return out<<" immutable_array on domain : "<<x.domain();}
protected:
function_type f;
domain_type dom_;
@ -47,10 +49,34 @@ namespace triqs { namespace arrays {
* \return A lazy object implementing the ImmutableCuboidArray concept with the domain built from the ranges.
*/
template<typename Expr, int ... ph>
immutable_array_impl<Expr,ph...> make_immutable_array( Expr const & expr, clef::pair<ph,range> ... p) {
return immutable_array_impl<Expr,ph...> (expr, p...);
immutable_array_expr_impl<Expr,ph...> make_immutable_array( Expr const & expr, clef::pair<ph,range> ... p) {
return immutable_array_expr_impl<Expr,ph...> (expr, p...);
}
// if ones prefers to use a function...
template<typename Function, typename ... Ranges>
class immutable_array_fun_impl : TRIQS_CONCEPT_TAG_NAME(ImmutableCuboidArray) {
template<int I> 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<Function(typename Ranges::index_type...)>::result_type value_type;
typedef decltype( triqs::tuple::apply(std::declval<Function>(),std::make_tuple(typename Ranges::index_type()...))) value_type;
typedef indexmaps::cuboid::domain_t<sizeof...(Ranges)> domain_type;
domain_type domain() const { return dom_;}
template<typename ... Args> 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 : "<<x.domain();}
protected:
Function f_;
domain_type dom_;
};
template<typename Function, typename ... Ranges>
immutable_array_fun_impl<Function,Ranges...> make_immutable_array(Function f, Ranges ... r) {
return immutable_array_fun_impl<Function,Ranges...> (std::move(f), r...);
}
}}//namespace triqs::arrays
#endif

View File

@ -193,6 +193,21 @@ namespace triqs { namespace arrays {
#undef _IMPL_MATRIX_COMMON
#undef IMPL_TYPE
template<typename ArrayType>
matrix_view<typename ArrayType::value_type, ArrayType::opt_flags, ArrayType::traversal_order>
make_matrix_view(ArrayType const & a) {
static_assert(ArrayType::rank ==2, "make_matrix_view only works for array of rank 2");
return a;
}
template<typename ArrayType>
matrix<typename ArrayType::value_type, ArrayType::opt_flags, ArrayType::traversal_order>
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.