<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
syrk.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_SYRK_HH
12#define TLAPACK_BLAS_SYRK_HH
13
15
16namespace tlapack {
17
50template <TLAPACK_MATRIX matrixA_t,
51 TLAPACK_MATRIX matrixC_t,
52 TLAPACK_SCALAR alpha_t,
53 TLAPACK_SCALAR beta_t,
54 class T = type_t<matrixC_t>,
55 disable_if_allow_optblas_t<pair<matrixA_t, T>,
56 pair<matrixC_t, T>,
57 pair<alpha_t, T>,
58 pair<beta_t, T> > = 0>
60 Op trans,
61 const alpha_t& alpha,
62 const matrixA_t& A,
63 const beta_t& beta,
64 matrixC_t& C)
65{
66 // data traits
67 using TA = type_t<matrixA_t>;
68 using idx_t = size_type<matrixA_t>;
69
70 // constants
71 const idx_t n = (trans == Op::NoTrans) ? nrows(A) : ncols(A);
72 const idx_t k = (trans == Op::NoTrans) ? ncols(A) : nrows(A);
73
74 // check arguments
75 tlapack_check_false(uplo != Uplo::Lower && uplo != Uplo::Upper &&
76 uplo != Uplo::General);
77 tlapack_check_false(trans != Op::NoTrans && trans != Op::Trans);
78 tlapack_check_false(nrows(C) != ncols(C));
79 tlapack_check_false(nrows(C) != n);
80
81 if (trans == Op::NoTrans) {
82 if (uplo != Uplo::Lower) {
83 // uplo == Uplo::Upper or uplo == Uplo::General
84 for (idx_t j = 0; j < n; ++j) {
85 for (idx_t i = 0; i <= j; ++i)
86 C(i, j) *= beta;
87
88 for (idx_t l = 0; l < k; ++l) {
90 for (idx_t i = 0; i <= j; ++i)
91 C(i, j) += A(i, l) * alphaAjl;
92 }
93 }
94 }
95 else { // uplo == Uplo::Lower
96 for (idx_t j = 0; j < n; ++j) {
97 for (idx_t i = j; i < n; ++i)
98 C(i, j) *= beta;
99
100 for (idx_t l = 0; l < k; ++l) {
102 for (idx_t i = j; i < n; ++i)
103 C(i, j) += A(i, l) * alphaAjl;
104 }
105 }
106 }
107 }
108 else { // trans == Op::Trans
109 if (uplo != Uplo::Lower) {
110 // uplo == Uplo::Upper or uplo == Uplo::General
111 for (idx_t j = 0; j < n; ++j) {
112 for (idx_t i = 0; i <= j; ++i) {
113 TA sum(0);
114 for (idx_t l = 0; l < k; ++l)
115 sum += A(l, i) * A(l, j);
116 C(i, j) = alpha * sum + beta * C(i, j);
117 }
118 }
119 }
120 else { // uplo == Uplo::Lower
121 for (idx_t j = 0; j < n; ++j) {
122 for (idx_t i = j; i < n; ++i) {
123 TA sum(0);
124 for (idx_t l = 0; l < k; ++l)
125 sum += A(l, i) * A(l, j);
126 C(i, j) = alpha * sum + beta * C(i, j);
127 }
128 }
129 }
130 }
131
132 if (uplo == Uplo::General) {
133 for (idx_t j = 0; j < n; ++j) {
134 for (idx_t i = j + 1; i < n; ++i)
135 C(i, j) = C(j, i);
136 }
137 }
138}
139
140#ifdef TLAPACK_USE_LAPACKPP
141
155template <TLAPACK_LEGACY_MATRIX matrixA_t,
156 TLAPACK_LEGACY_MATRIX matrixC_t,
157 TLAPACK_SCALAR alpha_t,
158 TLAPACK_SCALAR beta_t,
159 class T = type_t<matrixC_t>,
160 enable_if_allow_optblas_t<pair<matrixA_t, T>,
161 pair<matrixC_t, T>,
162 pair<alpha_t, T>,
163 pair<beta_t, T> > = 0>
164void syrk(Uplo uplo,
165 Op trans,
166 const alpha_t alpha,
167 const matrixA_t& A,
168 const beta_t beta,
169 matrixC_t& C)
170{
171 // Legacy objects
172 auto A_ = legacy_matrix(A);
173 auto C_ = legacy_matrix(C);
174
175 // Constants to forward
176 constexpr Layout L = layout<matrixC_t>;
177 const auto& n = C_.n;
178 const auto& k = (trans == Op::NoTrans) ? A_.n : A_.m;
179
180 // Warnings for NaNs and Infs
181 if (alpha == alpha_t(0))
183 "Infs and NaNs in A will not propagate to C on output");
184 if (beta == beta_t(0) && !is_same_v<beta_t, StrongZero>)
186 -5,
187 "Infs and NaNs in C on input will not propagate to C on output");
188
189 return ::blas::syrk((::blas::Layout)L, (::blas::Uplo)uplo,
190 (::blas::Op)trans, n, k, alpha, A_.ptr, A_.ldim,
191 (T)beta, C_.ptr, C_.ldim);
192}
193
194#endif
195
227template <TLAPACK_MATRIX matrixA_t,
228 TLAPACK_MATRIX matrixC_t,
229 TLAPACK_SCALAR alpha_t>
230void syrk(
231 Uplo uplo, Op trans, const alpha_t& alpha, const matrixA_t& A, matrixC_t& C)
232{
233 return syrk(uplo, trans, alpha, A, StrongZero(), C);
234}
235
236} // namespace tlapack
237
238#endif // #ifndef TLAPACK_BLAS_SYRK_HH
Op
Definition types.hpp:222
Uplo
Definition types.hpp:45
#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
void syrk(Uplo uplo, Op trans, const alpha_t &alpha, const matrixA_t &A, const beta_t &beta, matrixC_t &C)
Symmetric rank-k update:
Definition syrk.hpp:59
#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