<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
herk.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2017-2021, University of Tennessee. All rights reserved.
5// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
6//
7// This file is part of <T>LAPACK.
8// <T>LAPACK is free software: you can redistribute it and/or modify it under
9// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
10
11#ifndef TLAPACK_BLAS_HERK_HH
12#define TLAPACK_BLAS_HERK_HH
13
15
16namespace tlapack {
17
55template <TLAPACK_MATRIX matrixA_t,
56 TLAPACK_MATRIX matrixC_t,
57 TLAPACK_REAL alpha_t,
58 TLAPACK_REAL beta_t,
59 enable_if_t<(
60 /* Requires: */
61 is_real<alpha_t> && is_real<beta_t>),
62 int> = 0,
63 class T = type_t<matrixC_t>,
64 disable_if_allow_optblas_t<pair<matrixA_t, T>,
65 pair<matrixC_t, T>,
66 pair<alpha_t, real_type<T> >,
67 pair<beta_t, real_type<T> > > = 0>
69 Op trans,
70 const alpha_t& alpha,
71 const matrixA_t& A,
72 const beta_t& beta,
73 matrixC_t& C)
74{
75 // data traits
76 using TA = type_t<matrixA_t>;
77 using TC = type_t<matrixC_t>;
78 using idx_t = size_type<matrixA_t>;
79
80 // constants
81 const idx_t n = (trans == Op::NoTrans) ? nrows(A) : ncols(A);
82 const idx_t k = (trans == Op::NoTrans) ? ncols(A) : nrows(A);
83
84 // check arguments
85 tlapack_check_false(uplo != Uplo::Lower && uplo != Uplo::Upper &&
86 uplo != Uplo::General);
87 tlapack_check_false(trans != Op::NoTrans && trans != Op::ConjTrans);
88 tlapack_check_false(nrows(C) != ncols(C));
89 tlapack_check_false(nrows(C) != n);
90
91 if (trans == Op::NoTrans) {
92 if (uplo != Uplo::Lower) {
93 // uplo == Uplo::Upper or uplo == Uplo::General
94 for (idx_t j = 0; j < n; ++j) {
95 for (idx_t i = 0; i < j; ++i)
96 C(i, j) *= beta;
97 C(j, j) = TC(beta * real(C(j, j)));
98
99 for (idx_t l = 0; l < k; ++l) {
101 alpha * conj(A(j, l));
102
103 for (idx_t i = 0; i < j; ++i)
104 C(i, j) += A(i, l) * alphaConjAjl;
105 C(j, j) += real(A(j, l)) * real(alphaConjAjl) -
106 imag(A(j, l)) * imag(alphaConjAjl);
107 }
108 }
109 }
110 else { // uplo == Uplo::Lower
111 for (idx_t j = 0; j < n; ++j) {
112 C(j, j) = TC(beta * real(C(j, j)));
113 for (idx_t i = j + 1; i < n; ++i)
114 C(i, j) *= beta;
115
116 for (idx_t l = 0; l < k; ++l) {
118 alpha * conj(A(j, l));
119
120 C(j, j) += real(A(j, l)) * real(alphaConjAjl) -
121 imag(A(j, l)) * imag(alphaConjAjl);
122 for (idx_t i = j + 1; i < n; ++i)
123 C(i, j) += A(i, l) * alphaConjAjl;
124 }
125 }
126 }
127 }
128 else { // trans == Op::ConjTrans
129 if (uplo != Uplo::Lower) {
130 // uplo == Uplo::Upper or uplo == Uplo::General
131 for (idx_t j = 0; j < n; ++j) {
132 for (idx_t i = 0; i < j; ++i) {
133 TA sum(0);
134 for (idx_t l = 0; l < k; ++l)
135 sum += conj(A(l, i)) * A(l, j);
136 C(i, j) = alpha * sum + beta * C(i, j);
137 }
139 for (idx_t l = 0; l < k; ++l)
140 sum += real(A(l, j)) * real(A(l, j)) +
141 imag(A(l, j)) * imag(A(l, j));
142 C(j, j) = alpha * sum + beta * real(C(j, j));
143 }
144 }
145 else { // uplo == Uplo::Lower
146 for (idx_t j = 0; j < n; ++j) {
147 for (idx_t i = j + 1; i < n; ++i) {
148 TA sum(0);
149 for (idx_t l = 0; l < k; ++l)
150 sum += conj(A(l, i)) * A(l, j);
151 C(i, j) = alpha * sum + beta * C(i, j);
152 }
154 for (idx_t l = 0; l < k; ++l)
155 sum += real(A(l, j)) * real(A(l, j)) +
156 imag(A(l, j)) * imag(A(l, j));
157 C(j, j) = alpha * sum + beta * real(C(j, j));
158 }
159 }
160 }
161
162 if (uplo == Uplo::General) {
163 for (idx_t j = 0; j < n; ++j) {
164 for (idx_t i = j + 1; i < n; ++i)
165 C(i, j) = conj(C(j, i));
166 }
167 }
168}
169
170#ifdef TLAPACK_USE_LAPACKPP
171
185template <TLAPACK_LEGACY_MATRIX matrixA_t,
186 TLAPACK_LEGACY_MATRIX matrixC_t,
187 TLAPACK_REAL alpha_t,
188 TLAPACK_REAL beta_t,
189 class T = type_t<matrixC_t>,
190 enable_if_allow_optblas_t<pair<matrixA_t, T>,
191 pair<matrixC_t, T>,
192 pair<alpha_t, real_type<T> >,
193 pair<beta_t, real_type<T> > > = 0>
194void herk(Uplo uplo,
195 Op trans,
196 const alpha_t alpha,
197 const matrixA_t& A,
198 const beta_t beta,
199 matrixC_t& C)
200{
201 // Legacy objects
202 auto A_ = legacy_matrix(A);
203 auto C_ = legacy_matrix(C);
204
205 // Constants to forward
206 constexpr Layout L = layout<matrixC_t>;
207 const auto& n = C_.n;
208 const auto& k = (trans == Op::NoTrans) ? A_.n : A_.m;
209
210 // Warnings for NaNs and Infs
211 if (alpha == alpha_t(0))
213 "Infs and NaNs in A will not propagate to C on output");
214 if (beta == beta_t(0) && !is_same_v<beta_t, StrongZero>)
216 -5,
217 "Infs and NaNs in C on input will not propagate to C on output");
218
219 return ::blas::herk((::blas::Layout)L, (::blas::Uplo)uplo,
220 (::blas::Op)trans, n, k, alpha, A_.ptr, A_.ldim,
221 (real_type<T>)beta, C_.ptr, C_.ldim);
222}
223
224#endif
225
260template <TLAPACK_MATRIX matrixA_t,
261 TLAPACK_MATRIX matrixC_t,
262 TLAPACK_SCALAR alpha_t>
263void herk(
264 Uplo uplo, Op trans, const alpha_t& alpha, const matrixA_t& A, matrixC_t& C)
265{
266 return herk(uplo, trans, alpha, A, StrongZero(), C);
267}
268
269} // namespace tlapack
270
271#endif // #ifndef TLAPACK_BLAS_HERK_HH
Op
Definition types.hpp:222
Uplo
Definition types.hpp:45
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
constexpr T conj(const T &x) noexcept
Extends std::conj() to real datatypes.
Definition utils.hpp:100
constexpr real_type< T > imag(const T &x) noexcept
Extends std::imag() to real datatypes.
Definition utils.hpp:86
#define TLAPACK_SCALAR
Macro for tlapack::concepts::Scalar compatible with C++17.
Definition concepts.hpp:915
#define TLAPACK_LEGACY_MATRIX
Macro for tlapack::concepts::LegacyMatrix compatible with C++17.
Definition concepts.hpp:951
#define TLAPACK_MATRIX
Macro for tlapack::concepts::Matrix compatible with C++17.
Definition concepts.hpp:896
#define TLAPACK_REAL
Macro for tlapack::concepts::Real compatible with C++17.
Definition concepts.hpp:918
void herk(Uplo uplo, Op trans, const alpha_t &alpha, const matrixA_t &A, const beta_t &beta, matrixC_t &C)
Hermitian rank-k update:
Definition herk.hpp:68
#define tlapack_check_false(cond)
Throw an error if cond is true.
Definition exceptionHandling.hpp:113
#define tlapack_warning(info, detailedInfo)
Warning handler.
Definition exceptionHandling.hpp:156
Concept for types that represent tlapack::Op.
Concept for types that represent tlapack::Uplo.
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
Strong zero type.
Definition StrongZero.hpp:43