TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
numeric_ops.hpp
Go to the documentation of this file.
1// Copyright (c) 2014-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2014-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2019 Simons Foundation
4// Copyright (c) 2014-2016 Igor Krivenko
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Igor Krivenko, Nils Wentzell
20
25
26#pragma once
27
28#include "./is_complex.hpp"
29
30#include <cmath>
31#include <complex>
32#include <concepts>
33#include <limits>
34
35namespace triqs::utility {
36
41
49 template <std::integral I> bool is_zero(I const &x) { return x == 0; }
50
59 template <std::floating_point T> bool is_zero(T const &x, T tolerance = 100 * std::numeric_limits<T>::epsilon()) { return std::abs(x) < tolerance; }
60
69 template <typename T> bool is_zero(std::complex<T> const &z, T tolerance = 100 * std::numeric_limits<T>::epsilon()) {
70 return is_zero(std::real(z), tolerance) && is_zero(std::imag(z), tolerance);
71 }
72
80 template <std::integral I> I conj(I const &x) { return x; }
81
89 template <std::floating_point T> T conj(T const &x) { return x; }
90
98 template <typename Z>
100 Z conj(Z const &z) {
101 return std::conj(z);
102 }
103
111 template <std::integral I> I real(I const &x) { return x; }
112
120 template <std::floating_point T> T real(T const &x) { return x; }
121
129 template <typename Z>
131 Z real(Z const &z) {
132 return std::real(z);
133 }
134
142 template <std::integral I> I imag([[maybe_unused]] I const &x) { return I{}; }
143
151 template <std::floating_point T> T imag([[maybe_unused]] T const &x) { return T{}; }
152
160 template <typename Z>
162 Z imag(Z const &z) {
163 return std::imag(z);
164 }
165
167
168} // namespace triqs::utility
I real(I const &x)
Real part of an integral value.
I imag(I const &x)
Imaginary part of an integral value.
I conj(I const &x)
Complex conjugate of an integral value.
bool is_zero(I const &x)
Exact zero check for integral values.
Provides a type trait to check if a type is complex.
Trait that checks if a type is complex.