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 {
45 if constexpr (is_complex_v<S>) {
46 return std::conj(x);
47 } else {
48 return x;
49 }
50 }
51
52 // Get the squared absolute value of a double.
53 inline double abs2(double x) { return x * x; }
54
55 // Get the squared absolute value of a std::complex<double>.
56 inline double abs2(std::complex<double> z) { return (conj(z) * z).real(); }
57
58 // Check if a std::complex<double> is NaN.
59 inline bool isnan(std::complex<double> const &z) { return std::isnan(z.real()) or std::isnan(z.imag()); }
60
61 // Functor for nda::detail::conj.
62 struct conj_f {
63 auto operator()(auto const &x) const { return conj(x); };
64 };
65
66 } // namespace detail
67
76 template <ArrayOrScalar A>
77 auto pow(A &&a, double p) {
78 return nda::map([p](auto const &x) {
79 using std::pow;
80 return pow(x, p);
81 })(std::forward<A>(a));
82 }
83
93 template <ArrayOrScalar A>
94 decltype(auto) conj(A &&a) {
95 if constexpr (is_complex_v<get_value_t<A>>)
96 return nda::map(detail::conj_f{})(std::forward<A>(a));
97 else
98 return std::forward<A>(a);
99 }
100
102
103} // 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).
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:64
Provides lazy function calls on arrays/views.
Provides type traits for the nda library.