mirror of
https://github.com/triqs/dft_tools
synced 2025-01-12 05:58:18 +01:00
clef: first version of generic sum function
- to improve with in the case where function return an expression template, not a regular type, with a make_regular function
This commit is contained in:
parent
9762b95936
commit
efb00ea5d3
21
test/triqs/clef_examples/sum3.cpp
Normal file
21
test/triqs/clef_examples/sum3.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <triqs/arrays.hpp>
|
||||
/// ---- test ------------
|
||||
|
||||
using triqs::arrays::range;
|
||||
|
||||
int main() {
|
||||
triqs::clef::placeholder<0> a_;
|
||||
triqs::clef::placeholder<1> i_;
|
||||
triqs::clef::placeholder<2> j_;
|
||||
|
||||
auto expr = i_ + 2* j_;
|
||||
|
||||
auto xa = sum(a_*i_ , i_=range(0,5));
|
||||
auto x = eval(xa, a_=2);
|
||||
|
||||
std::cout << x << std::endl;
|
||||
|
||||
auto y = sum(2*i_ + j_ , i_=range(0,3), j_= range (0,2));
|
||||
std::cout << y << std::endl;
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ using namespace triqs::arrays;
|
||||
using namespace triqs::lattice;
|
||||
template <typename Function, typename Mesh>
|
||||
// requires ( is_function_on_mesh<Function,Mesh>())
|
||||
auto sum(Function const &f, Mesh const &m) ->decltype(make_matrix(0*f(*(m.begin())))) {
|
||||
//auto sum(Function const &f, Mesh const &m) {
|
||||
auto sum_gf(Function const &f, Mesh const &m) ->decltype(make_matrix(0*f(*(m.begin())))) {
|
||||
//auto sum_gf(Function const &f, Mesh const &m) {
|
||||
//auto res = typename triqs::regular_type_if_exists_else_type<decltype(f(typename Mesh::mesh_point_t{}))>::type (f(m.begin()));
|
||||
auto res = make_matrix(0*f(*(m.begin())));
|
||||
for (auto const &x : m) res = res + f(x);
|
||||
@ -20,7 +20,7 @@ auto sum(Function const &f, Mesh const &m) ->decltype(make_matrix(0*f(*(m.begin
|
||||
}
|
||||
namespace triqs {
|
||||
namespace clef {
|
||||
TRIQS_CLEF_MAKE_FNT_LAZY(sum);
|
||||
TRIQS_CLEF_MAKE_FNT_LAZY(sum_gf);
|
||||
TRIQS_CLEF_MAKE_FNT_LAZY(conj);
|
||||
}
|
||||
}
|
||||
@ -49,8 +49,8 @@ int main() {
|
||||
|
||||
auto r = G_k_iom(k_t{0, 0}, matsubara_freq{0, beta, Fermion});
|
||||
|
||||
auto r5 = sum(k_ >> G_k_iom(k_,0), g_eps.mesh());
|
||||
G_loc(w_) << sum(k_ >> G_k_iom(k_,w_), g_eps.mesh());
|
||||
auto r5 = sum_gf(k_ >> G_k_iom(k_,0), g_eps.mesh());
|
||||
G_loc(w_) << sum_gf(k_ >> G_k_iom(k_,w_), g_eps.mesh());
|
||||
|
||||
TEST(G_loc(0));
|
||||
|
||||
|
@ -76,6 +76,12 @@ namespace arrays {
|
||||
const_iterator cend() const { return const_iterator(this, true); }
|
||||
};
|
||||
|
||||
// foreach
|
||||
template <typename F> void foreach(range const& r, F const& f) {
|
||||
std::ptrdiff_t i = r.first(), last = r.last(), step = r.step();
|
||||
for (; i < last; i += step) f(i);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
class ellipsis : public range {
|
||||
|
@ -585,5 +585,31 @@ namespace triqs { namespace clef {
|
||||
\
|
||||
template <typename... Args> \
|
||||
auto operator()(Args&&... args) && DECL_AND_RETURN(make_expr_call(std::move(*this), std::forward<Args>(args)...));
|
||||
|
||||
/* --------------------------------------------------------------------------------------------------
|
||||
* sum of expressions
|
||||
* --------------------------------------------------------------------------------------------------- */
|
||||
|
||||
// sum a function f on a domain D, using a simple foreach
|
||||
template <typename F, typename D>
|
||||
auto sum_f_domain_impl(F const& f, D const& d)
|
||||
-> std::c14::enable_if_t<!triqs::clef::is_any_lazy<F, D>::value, decltype(f(*(d.begin())))> {
|
||||
auto res = decltype(f(*(d.begin()))) {};
|
||||
using p_t = typename std::decay<decltype(*(d.begin()))>::type;
|
||||
foreach(d, [&res, &f](p_t const& x) { res = res + f(x); });
|
||||
return res;
|
||||
}
|
||||
|
||||
TRIQS_CLEF_MAKE_FNT_LAZY(sum_f_domain_impl);
|
||||
|
||||
// sum( expression, i = domain)
|
||||
template <typename Expr, int N, typename D> auto sum(Expr const& f, clef::pair<N, D> const& d)
|
||||
DECL_AND_RETURN(sum_f_domain_impl(make_function(f, clef::placeholder<N>()), d.rhs));
|
||||
|
||||
// two or more indices : sum recursively
|
||||
template <typename Expr, typename D0, int N0, typename D1, int N1, typename... D, int... N>
|
||||
auto sum(Expr const& f, clef::pair<N0, D0> const& d0, clef::pair<N1, D1> const& d1, clef::pair<N, D> const&... d)
|
||||
DECL_AND_RETURN(sum(sum(f, d0), d1, d...));
|
||||
|
||||
}} // namespace triqs::clef
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user