TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
k_expr.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Simons Foundation
2// Copyright (c) 2023 Hugo U.R. Strand
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You may obtain a copy of the License at
15// https://www.gnu.org/licenses/gpl-3.0.txt
16//
17// Authors: Olivier Parcollet, Hugo U. R. Strand, Nils Wentzell
18
23
24#pragma once
25
26#include <nda/nda.hpp>
27
28#include <cstdint>
29#include <utility>
30
31namespace triqs::mesh {
32
37
44 template <char OP, typename L>
45 requires(OP == '-')
46 struct k_expr_unary {
47 // Operand to be negated.
48 L l;
49
51 [[nodiscard]] uint64_t mesh_hash() const { return l.mesh_hash(); };
52
54 [[nodiscard]] auto value() const { return -l.value(); }
55
57 [[nodiscard]] auto index() const { return -l.index(); }
58 };
59
67 template <char OP, typename L, typename R> struct k_expr {
68 // Left hand side operand.
69 L l;
70
71 // Right hand side operand.
72 R r;
73
80 template <typename L1, typename R1> k_expr(L1 &&l1, R1 &&r1) : l{std::forward<L1>(l1)}, r{std::forward<R1>(r1)} {}
81
92 [[nodiscard]] auto value() const {
93 if constexpr (OP == '+') {
94 return nda::make_regular(l.value() + r.value());
95 } else if constexpr (OP == '-') {
96 return nda::make_regular(l.value() - r.value());
97 } else {
98 return nda::make_regular(l * r.value());
99 }
100 }
101
109 [[nodiscard]] auto index() const {
110 // check that the mesh hashes are the same
111 if constexpr (requires { l.mesh_hash(); }) { EXPECTS(l.mesh_hash() == r.mesh_hash()) };
112 if constexpr (OP == '+') {
113 return l.index() + r.index();
114 } else if constexpr (OP == '-') {
115 return l.index() - r.index();
116 } else {
117 return l * r.index();
118 }
119 }
120
122 [[nodiscard]] uint64_t mesh_hash() const { return r.mesh_hash(); };
123 };
124
129 template <typename T> constexpr bool is_k_expr = false;
130
131 // Specialization of triqs::mesh::is_k_expr for triqs::mesh::k_expr.
132 template <char OP, typename L, typename R> constexpr bool is_k_expr<k_expr<OP, L, R>> = true;
133
134 // Specialization of triqs::mesh::is_k_expr for triqs::mesh::k_expr_unary.
135 template <char OP, typename L> constexpr bool is_k_expr<k_expr_unary<OP, L>> = true;
136
138
139} // namespace triqs::mesh
constexpr bool is_k_expr
Type trait to check if a type is a triqs::mesh::k_expr or triqs::mesh::k_expr_unary.
Definition k_expr.hpp:129
Unary minus -vector expression.
Definition k_expr.hpp:46
auto value() const
Get the reciprocal vector (see triqs::mesh::brzone::mesh_point_t::value()).
Definition k_expr.hpp:54
auto index() const
Get the index of the reciprocal vector (see triqs::mesh::brzone::mesh_point_t::index()).
Definition k_expr.hpp:57
uint64_t mesh_hash() const
Get the hash value of the mesh to which the operand belongs.
Definition k_expr.hpp:51
uint64_t mesh_hash() const
Get the hash value of the mesh to which the right hand side operand belongs.
Definition k_expr.hpp:122
auto index() const
Get the index of the -vector corresponding to the evaluated expression.
Definition k_expr.hpp:109
k_expr(L1 &&l1, R1 &&r1)
Construct a binary -vector expression with the given operands.
Definition k_expr.hpp:80
auto value() const
Evaluate the expression template depending on the operator tag.
Definition k_expr.hpp:92