TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
dot.hpp
Go to the documentation of this file.
1// Copyright (c) 2022 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: Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a dot product for two arrays, a scalar and an array, or two scalars.
20 */
21
22#pragma once
23
24#include "../blas/dot.hpp"
25#include "../declarations.hpp"
26#include "../layout/policies.hpp"
27#include "../mem/address_space.hpp"
28#include "../mem/policies.hpp"
29
30#include <type_traits>
31
32namespace nda {
33
34 /**
35 * @addtogroup linalg_tools
36 * @{
37 */
38
39 /**
40 * @brief Compute the dot product of two real arrays/views.
41 *
42 * @details It is generic in the sense that it allows the input arrays to belong to a different nda::mem::AddressSpace
43 * (as long as they are compatible).
44 *
45 * If possible, it uses nda::blas::dot, otherwise it calls nda::blas::dot_generic.
46 *
47 * @tparam X Type of the left hand side array/view.
48 * @tparam Y Type of the right hand side array/view.
49 * @param x Left hand side array/view.
50 * @param y Right hand side array/view.
51 * @return The dot product of the two arrays/views.
52 */
53 template <typename X, typename Y>
54 auto dot(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
55 // check address space compatibility
56 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
57 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
58 static_assert(L_adr_spc != mem::None);
59 static_assert(R_adr_spc != mem::None);
60
61 // get resulting value type and vector type
62 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
63 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
64
65 if constexpr (is_blas_lapack_v<value_t>) {
66 // for double value types we use blas::dot
67 // lambda to form a new vector with the correct value type if necessary
68 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
69 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
70 return a;
71 else
72 return vector_t{a};
73 };
74
75 return blas::dot(as_container(x), as_container(y));
76 } else {
77 // for other value types we use a generic implementation
78 return blas::dot_generic(x, y);
79 }
80 }
81
82 /**
83 * @brief Compute the dot product of two complex arrays/views.
84 *
85 * @details It is generic in the sense that it allows the input arrays to belong to a different nda::mem::AddressSpace
86 * (as long as they are compatible).
87 *
88 * If possible, it uses nda::blas::dotc, otherwise it calls nda::blas::dotc_generic.
89 *
90 * @tparam X Type of the left hand side array/view.
91 * @tparam Y Type of the right hand side array/view.
92 * @param x Left hand side array/view.
93 * @param y Right hand side array/view.
94 * @return The dot product of the two arrays/views.
95 */
96 template <typename X, typename Y>
97 auto dotc(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
98 // check address space compatibility
99 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
100 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
101 static_assert(L_adr_spc != mem::None);
102 static_assert(R_adr_spc != mem::None);
103
104 // get resulting value type and vector type
105 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
106 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
107
108 if constexpr (is_blas_lapack_v<value_t>) {
109 // for double or complex value types we use blas::dotc
110 // lambda to form a new vector with the correct value type if necessary
111 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
112 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
113 return a;
114 else
115 return vector_t{a};
116 };
117
118 return blas::dotc(as_container(x), as_container(y));
119 } else {
120 // for other value types we use a generic implementation
121 return blas::dotc_generic(x, y);
122 }
123 }
124
125 /** @} */
126
127} // namespace nda
A generic multi-dimensional array.
auto dotc(X &&x, Y &&y)
Compute the dot product of two complex arrays/views.
Definition dot.hpp:97
auto dot(X &&x, Y &&y)
Compute the dot product of two real arrays/views.
Definition dot.hpp:54
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:47