TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
mapped_functions.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2023 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Olivier Parcollet, Nils Wentzell
16
23#pragma once
24
25#include "./concepts.hpp"
26#include "./map.hpp"
27#include "./traits.hpp"
28
29#include <cmath>
30#include <complex>
31#include <type_traits>
32#include <utility>
33
34namespace nda {
35
48 template <typename T>
49 auto real(T t)
50 requires(nda::is_scalar_v<T>)
51 {
52 if constexpr (is_complex_v<T>) {
53 return std::real(t);
54 } else {
55 return t;
56 }
57 }
58
66 template <typename T>
67 auto conj(T t)
68 requires(nda::is_scalar_v<T>)
69 {
70 if constexpr (is_complex_v<T>) {
71 return std::conj(t);
72 } else {
73 return t;
74 }
75 }
76
83 inline double abs2(double x) { return x * x; }
84
91 inline double abs2(std::complex<double> z) { return (conj(z) * z).real(); }
92
99 inline bool isnan(std::complex<double> const &z) { return std::isnan(z.real()) or std::isnan(z.imag()); }
100
109 template <typename T>
110 T pow(T x, int n)
111 requires(std::is_integral_v<T>)
112 {
113 T r = 1;
114 for (int i = 0; i < n; ++i) r *= x;
115 return r;
116 }
117
126 template <Array A>
127 auto pow(A &&a, double p) {
128 return nda::map([p](auto const &x) {
129 using std::pow;
130 return pow(x, p);
131 })(std::forward<A>(a));
132 }
133
135 struct conj_f {
137 auto operator()(auto const &x) const { return conj(x); };
138 };
139
147 template <Array A>
148 decltype(auto) conj(A &&a) {
149 if constexpr (is_complex_v<get_value_t<A>>)
150 return nda::map(conj_f{})(std::forward<A>(a));
151 else
152 return std::forward<A>(a);
153 }
154
157} // namespace nda
Provides concepts for the nda library.
bool isnan(std::complex< double > const &z)
Check if a std::complex<double> is NaN.
double abs2(double x)
Get the squared absolute value of a double.
auto real(T t)
Get the real part of a scalar.
auto conj(T t)
Get the complex conjugate of a scalar.
T pow(T x, int n)
Calculate the integer power of an integer.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:199
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:75
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Definition traits.hpp:79
Provides lazy function calls on arrays/views.
Wrapper for nda::conj.
auto operator()(auto const &x) const
Function call operator that forwards the call to nda::conj.
Provides type traits for the nda library.