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--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
11
12#pragma once
13
14#include "./concepts.hpp"
15#include "./map.hpp"
16#include "./traits.hpp"
17
18#include <cmath>
19#include <complex>
20#include <utility>
21
22namespace nda {
23
28
29 namespace detail {
30
31 // Get the real part of a scalar.
32 template <nda::Scalar S>
33 auto real(S x) {
34 if constexpr (is_complex_v<S>) {
35 return std::real(x);
36 } else {
37 return x;
38 }
39 }
40
41 // Get the complex conjugate of a scalar.
42 template <nda::Scalar S>
43 auto conj(S x) {
44 if constexpr (is_complex_v<S>) {
45 return std::conj(x);
46 } else {
47 return x;
48 }
49 }
50
51 // Get the squared absolute value of a double.
52 inline double abs2(double x) { return x * x; }
53
54 // Get the squared absolute value of a std::complex<double>.
55 inline double abs2(std::complex<double> z) { return (conj(z) * z).real(); }
56
57 // Check if a std::complex<double> is NaN.
58 inline bool isnan(std::complex<double> const &z) { return std::isnan(z.real()) or std::isnan(z.imag()); }
59
60 // Functor for nda::detail::conj.
61 struct conj_f {
62 auto operator()(auto const &x) const { return conj(x); };
63 };
64
65 } // namespace detail
66
75 template <ArrayOrScalar A>
76 auto pow(A &&a, double p) {
77 return nda::map([p](auto const &x) {
78 using std::pow;
79 return pow(x, p);
80 })(std::forward<A>(a));
81 }
82
92 template <ArrayOrScalar A>
93 decltype(auto) conj(A &&a) {
94 if constexpr (is_complex_v<get_value_t<A>>)
95 return nda::map(detail::conj_f{})(std::forward<A>(a));
96 else
97 return std::forward<A>(a);
98 }
99
108 template <ArrayOrScalar A>
109 auto reciprocal(A &&a) {
110 return nda::map([](auto const &x) {
111 if constexpr (Scalar<decltype(x)>) {
112 return 1.0 / x;
113 } else {
114 return reciprocal(x);
115 }
116 })(std::forward<A>(a));
117 }
118
120
121} // namespace nda
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:83
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).
auto reciprocal(A &&a)
Reciprocal function for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:202
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
Provides lazy function calls on arrays/views.
Provides type traits for the nda library.