TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
tools.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
10
11#pragma once
12
13#include "../concepts.hpp"
14#include "../map.hpp"
16
17#include <complex>
18#include <tuple>
19#include <type_traits>
20#include <utility>
21
22namespace nda {
23
28 using dcomplex = std::complex<double>;
29
30} // namespace nda
31
32namespace nda::blas {
33
38
40 template <typename A>
41 static constexpr bool is_conj_array_expr = false;
42
44 template <MemoryArray A>
46
47 // Specialization of nda::blas::is_conj_array_expr for cvref types.
48 template <typename A>
49 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
50 static constexpr bool is_conj_array_expr<A> = is_conj_array_expr<std::remove_cvref_t<A>>;
51
60 template <Array A>
62 MemoryArray decltype(auto) get_array(A &&a) {
63 if constexpr (is_conj_array_expr<A>) {
64 return std::get<0>(std::forward<A>(a).a);
65 } else {
66 return std::forward<A>(a);
67 }
68 }
69
71 template <Array A>
72 requires(MemoryArray<A> or is_conj_array_expr<A>)
73 static constexpr bool has_F_layout = []() {
74 if constexpr (is_conj_array_expr<A>)
75 return has_F_layout<decltype(std::get<0>(std::declval<A>().a))>;
76 else
77 return std::remove_cvref_t<A>::is_stride_order_Fortran();
78 }();
79
81 template <Array A>
82 requires(MemoryArray<A> or is_conj_array_expr<A>)
83 static constexpr bool has_C_layout = []() {
84 if constexpr (is_conj_array_expr<A>)
85 return has_C_layout<decltype(std::get<0>(std::declval<A>().a))>;
86 else
87 return std::remove_cvref_t<A>::is_stride_order_C();
88 }();
89
97 template <Array A>
98 static constexpr char get_op = []() {
99 auto constexpr conj = is_conj_array_expr<A>;
100 auto constexpr transpose = has_C_layout<A>;
101 static_assert(!(conj and not transpose), "Error in nda::blas::get_op: Cannot use conjugate operation alone in BLAS operations");
102 if constexpr (conj and transpose)
103 return 'C';
104 else if constexpr (transpose)
105 return 'T';
106 else // !conj and !transpose
107 return 'N';
108 }();
109
120 template <MemoryArray A>
121 requires(get_rank<A> == 1 or get_rank<A> == 2)
122 int get_ld(A const &a) {
123 if constexpr (get_rank<A> == 1) {
124 return a.size();
125 } else {
126 return a.indexmap().strides()[has_F_layout<A> ? 1 : 0];
127 }
128 }
129
140 template <MemoryArray A>
141 requires(get_rank<A> == 1 or get_rank<A> == 2)
142 int get_ncols(A const &a) {
143 if constexpr (get_rank<A> == 1) {
144 return 1;
145 } else {
146 return a.shape()[has_F_layout<A> ? 1 : 0];
147 }
148 }
149
151
152} // namespace nda::blas
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:223
Provides concepts for the nda library.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:126
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has a C memory layout.
Definition tools.hpp:83
std::complex< double > dcomplex
Alias for std::complex<double> type.
Definition tools.hpp:28
int get_ncols(A const &a)
Get the number of columns of an nda::MemoryArray with rank 1 or 2 for BLAS/LAPACK calls.
Definition tools.hpp:142
int get_ld(A const &a)
Get the leading dimension of an nda::MemoryArray with rank 1 or 2 for LAPACK calls.
Definition tools.hpp:122
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:41
static constexpr char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
Definition tools.hpp:98
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
Definition tools.hpp:73
MemoryArray decltype(auto) get_array(A &&a)
Get the underlying array of a conjugate lazy expression or return the array itself in case it is an n...
Definition tools.hpp:62
Provides lazy function calls on arrays/views.
Provides some custom implementations of standard mathematical functions used for lazy,...