TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
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 <algorithm>
19
#include <cmath>
20
#include <complex>
21
#include <utility>
22
23
namespace
nda {
24
29
30
namespace
detail {
31
32
// Get the real part of a scalar.
33
template
<nda::Scalar S>
34
auto
real(S x) {
35
if
constexpr
(
is_complex_v<S>
) {
36
return
std::real(x);
37
}
else
{
38
return
x;
39
}
40
}
41
42
// Get the complex conjugate of a scalar.
43
template
<nda::Scalar S>
44
auto
conj(S x) {
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
109
template
<ArrayOrScalar A>
110
auto
reciprocal
(A &&a) {
111
return
nda::map
([](
auto
const
&x) {
112
if
constexpr
(
Scalar
<
decltype
(x)>) {
113
return
1.0 / x;
114
}
else
{
115
return
reciprocal
(x);
116
}
117
})(std::forward<A>(a));
118
}
119
129
template
<ArrayOrScalar A, ArrayOrScalar B>
130
requires
(((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> &&
get_rank<A>
==
get_rank<B>
))
131
&& !
is_complex_v<get_value_t<A>
> && !
is_complex_v<get_value_t<B>
>)
132
[[nodiscard]]
auto
max
(A &&a, B &&b) {
133
return
nda::map
([](
auto
const
&x,
auto
const
&y) {
134
using
std::max;
135
return
max
(x, y);
136
})(std::forward<A>(a), std::forward<B>(b));
137
}
138
148
template
<ArrayOrScalar A, ArrayOrScalar B>
149
requires
(((Scalar<A> && Scalar<B>) || (Array<A> && Array<B> &&
get_rank<A>
==
get_rank<B>
))
150
&& !
is_complex_v<get_value_t<A>
> && !
is_complex_v<get_value_t<B>
>)
151
[[nodiscard]]
auto
min
(A &&a, B &&b) {
152
return
nda::map
([](
auto
const
&x,
auto
const
&y) {
153
using
std::min;
154
return
min
(x, y);
155
})(std::forward<A>(a), std::forward<B>(b));
156
}
157
159
160
}
// namespace nda
nda::Scalar
Check if a given type is either an arithmetic or complex type.
Definition
concepts.hpp:83
concepts.hpp
Provides concepts for the nda library.
nda::pow
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hpp:77
nda::min
auto min(A &&a, B &&b)
Function min for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hpp:151
nda::imag
auto imag(A &&a)
Function imag for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hxx:110
nda::reciprocal
auto reciprocal(A &&a)
Reciprocal function for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hpp:110
nda::max
auto max(A &&a, B &&b)
Function max for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
Definition
mapped_functions.hpp:132
nda::conj
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
Definition
mapped_functions.hpp:94
nda::map
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition
map.hpp:206
nda::get_rank
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition
traits.hpp:147
nda::is_complex_v
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition
traits.hpp:65
map.hpp
Provides lazy function calls on arrays/views.
traits.hpp
Provides type traits for the nda library.
nda
mapped_functions.hpp
Generated by
1.17.0