2013-07-17 19:24:07 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
|
|
*
|
2013-09-29 21:05:17 +02:00
|
|
|
* Copyright (C) 2011-2013 by O. Parcollet
|
2013-07-17 19:24:07 +02:00
|
|
|
*
|
|
|
|
* TRIQS is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#ifndef TRIQS_ARRAYS_EXPRESSION_ARRAY_ALGEBRA_H
|
|
|
|
#define TRIQS_ARRAYS_EXPRESSION_ARRAY_ALGEBRA_H
|
|
|
|
#include "./tools.hpp"
|
|
|
|
namespace triqs { namespace arrays {
|
|
|
|
|
2013-09-29 21:05:17 +02:00
|
|
|
// a trait to compute the return type of the operator()(0,...0) const for anything with the ImmutableCuboidArray concept
|
|
|
|
template<typename A, int R = get_rank<A>::value> struct get_call_const_return_type;
|
|
|
|
template<typename A> struct get_call_const_return_type<A,0> { typedef decltype(std::declval<A const>()()) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,1> { typedef decltype(std::declval<A const>()(0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,2> { typedef decltype(std::declval<A const>()(0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,3> { typedef decltype(std::declval<A const>()(0,0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,4> { typedef decltype(std::declval<A const>()(0,0,0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,5> { typedef decltype(std::declval<A const>()(0,0,0,0,0)) type; };
|
2014-01-30 20:26:19 +01:00
|
|
|
template<typename A> struct get_call_const_return_type<A,6> { typedef decltype(std::declval<A const>()(0,0,0,0,0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,7> { typedef decltype(std::declval<A const>()(0,0,0,0,0,0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,8> { typedef decltype(std::declval<A const>()(0,0,0,0,0,0,0,0)) type; };
|
|
|
|
template<typename A> struct get_call_const_return_type<A,9> { typedef decltype(std::declval<A const>()(0,0,0,0,0,0,0,0,0)) type; };
|
2013-09-29 21:05:17 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
template<typename Tag, typename L, typename R>
|
2013-08-29 18:02:19 +02:00
|
|
|
struct array_expr : TRIQS_CONCEPT_TAG_NAME(ImmutableArray) {
|
2013-09-29 21:05:17 +02:00
|
|
|
typedef typename std::remove_reference<L>::type L_t;
|
|
|
|
typedef typename std::remove_reference<R>::type R_t;
|
2013-07-17 19:24:07 +02:00
|
|
|
static_assert( get_rank<R_t>::value==0 || get_rank<L_t>::value==0 || get_rank<L_t>::value == get_rank<R_t>::value, "rank mismatch in array operations");
|
2013-09-29 21:05:17 +02:00
|
|
|
typedef typename std::result_of<utility::operation<Tag>(typename get_call_const_return_type<L_t>::type,typename get_call_const_return_type<R_t>::type)>::type value_type;
|
|
|
|
//typedef typename std::result_of<utility::operation<Tag>(typename L_t::value_type,typename R_t::value_type)>::type value_type;
|
2013-07-17 19:24:07 +02:00
|
|
|
typedef typename std::remove_reference<typename std::result_of<combine_domain(L_t,R_t)>::type>::type domain_type;
|
2013-09-29 21:05:17 +02:00
|
|
|
|
|
|
|
L l; R r;
|
2013-07-17 19:24:07 +02:00
|
|
|
template<typename LL, typename RR> array_expr(LL && l_, RR && r_) : l(std::forward<LL>(l_)), r(std::forward<RR>(r_)) {}
|
2013-09-29 21:05:17 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
domain_type domain() const { return combine_domain()(l,r); }
|
2013-09-29 21:05:17 +02:00
|
|
|
//template<typename ... Args> auto operator()(Args && ... args) const DECL_AND_RETURN( utility::operation<Tag>()(l(std::forward<Args>(args)...) , r(std::forward<Args>(args)...)));
|
|
|
|
template<typename ... Args> value_type operator()(Args && ... args) const { return utility::operation<Tag>()(l(std::forward<Args>(args)...) , r(std::forward<Args>(args)...));}
|
|
|
|
|
|
|
|
friend std::ostream &operator <<(std::ostream &sout, array_expr const &expr){return sout << "("<<expr.l << " "<<utility::operation<Tag>::name << " "<<expr.r<<")" ; }
|
|
|
|
friend array<value_type, domain_type::rank> make_array(array_expr const & e) { return e;}
|
2013-07-17 19:24:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// a special case : the unary operator !
|
2013-09-29 21:05:17 +02:00
|
|
|
template<typename L>
|
|
|
|
struct array_unary_m_expr : TRIQS_CONCEPT_TAG_NAME(ImmutableArray) {
|
|
|
|
typedef typename std::remove_reference<L>::type L_t;
|
|
|
|
typedef typename L_t::value_type value_type;
|
|
|
|
typedef typename L_t::domain_type domain_type;
|
|
|
|
|
|
|
|
L l;
|
|
|
|
template<typename LL> array_unary_m_expr(LL && l_) : l(std::forward<LL>(l_)) {}
|
|
|
|
|
|
|
|
domain_type domain() const { return l.domain(); }
|
|
|
|
template<typename ... Args> value_type operator()(Args && ... args) const { return -l(std::forward<Args>(args)...);}
|
|
|
|
|
|
|
|
friend std::ostream &operator <<(std::ostream &sout, array_unary_m_expr const &expr){return sout << '-'<<expr.l; }
|
|
|
|
friend array<value_type, domain_type::rank> make_array(array_unary_m_expr const & e) { return e;}
|
|
|
|
};
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
// Now we can define all the C++ operators ...
|
|
|
|
#define DEFINE_OPERATOR(TAG, OP, TRAIT1, TRAIT2) \
|
|
|
|
template<typename A1, typename A2>\
|
2013-09-29 21:05:17 +02:00
|
|
|
typename std::enable_if<TRAIT1<A1>::value && TRAIT2 <A2>::value, \
|
|
|
|
array_expr<utility::tags::TAG, typename node_t<A1,false>::type, typename node_t<A2,false>::type>>::type\
|
|
|
|
operator OP (A1 && a1, A2 && a2) { return {std::forward<A1>(a1),std::forward<A2>(a2)};}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2013-09-29 21:05:17 +02:00
|
|
|
DEFINE_OPERATOR(plus, +, ImmutableArray,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(minus, -, ImmutableArray,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(multiplies, *, ImmutableArray,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(multiplies, *, is_in_ZRC,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(multiplies, *, ImmutableArray,is_in_ZRC);
|
|
|
|
DEFINE_OPERATOR(divides, /, ImmutableArray,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(divides, /, is_in_ZRC,ImmutableArray);
|
|
|
|
DEFINE_OPERATOR(divides, /, ImmutableArray,is_in_ZRC);
|
2013-07-17 19:24:07 +02:00
|
|
|
#undef DEFINE_OPERATOR
|
|
|
|
|
|
|
|
// the unary is special
|
2013-09-29 21:05:17 +02:00
|
|
|
template<typename A1>
|
|
|
|
typename std::enable_if<
|
|
|
|
ImmutableArray<A1>::value,
|
|
|
|
array_unary_m_expr<typename node_t<A1,false>::type >
|
|
|
|
>::type
|
|
|
|
operator - (A1 && a1) { return {std::forward<A1>(a1)};}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
}}//namespace triqs::arrays
|
|
|
|
#endif
|