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-2024 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: Thomas Hahn, 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 <utility>
32
33namespace nda {
34
40 namespace detail {
41
42 // Get the real part of a scalar.
43 template <nda::Scalar S>
44 auto real(S x) {
45 if constexpr (is_complex_v<S>) {
46 return std::real(x);
47 } else {
48 return x;
49 }
50 }
51
52 // Get the complex conjugate of a scalar.
53 template <nda::Scalar S>
54 auto conj(S x)
55 {
56 if constexpr (is_complex_v<S>) {
57 return std::conj(x);
58 } else {
59 return x;
60 }
61 }
62
63 // Get the squared absolute value of a double.
64 inline double abs2(double x) { return x * x; }
65
66 // Get the squared absolute value of a std::complex<double>.
67 inline double abs2(std::complex<double> z) { return (conj(z) * z).real(); }
68
69 // Check if a std::complex<double> is NaN.
70 inline bool isnan(std::complex<double> const &z) { return std::isnan(z.real()) or std::isnan(z.imag()); }
71
72 // Functor for nda::detail::conj.
73 struct conj_f {
74 auto operator()(auto const &x) const { return conj(x); };
75 };
76
77 } // namespace detail
78
87 template <ArrayOrScalar A>
88 auto pow(A &&a, double p) {
89 return nda::map([p](auto const &x) {
90 using std::pow;
91 return pow(x, p);
92 })(std::forward<A>(a));
93 }
94
104 template <ArrayOrScalar A>
105 decltype(auto) conj(A &&a) {
106 if constexpr (is_complex_v<get_value_t<A>>)
107 return nda::map(detail::conj_f{})(std::forward<A>(a));
108 else
109 return std::forward<A>(a);
110 }
111
114} // namespace nda
Provides concepts for the nda library.
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
auto imag(A &&a)
Function imag for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:213
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:75
Provides lazy function calls on arrays/views.
Provides type traits for the nda library.