<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
trmv.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_TRMV_HH
12#define TLAPACK_BLAS_TRMV_HH
13
16
17namespace tlapack {
18
55template <
56 TLAPACK_MATRIX matrixA_t,
57 TLAPACK_VECTOR vectorX_t,
58 class T = type_t<vectorX_t>,
59 disable_if_allow_optblas_t<pair<matrixA_t, T>, pair<vectorX_t, T> > = 0>
60void trmv(Uplo uplo, Op trans, Diag diag, const matrixA_t& A, vectorX_t& x)
61{
62 // data traits
63 using TA = type_t<matrixA_t>;
64 using TX = type_t<vectorX_t>;
65 using idx_t = size_type<matrixA_t>;
66
67 // constants
68 const idx_t n = nrows(A);
69 const bool nonunit = (diag == Diag::NonUnit);
70
71 // check arguments
72 tlapack_check_false(uplo != Uplo::Lower && uplo != Uplo::Upper);
73 tlapack_check_false(trans != Op::NoTrans && trans != Op::Trans &&
74 trans != Op::ConjTrans && trans != Op::Conj);
75 tlapack_check_false(diag != Diag::NonUnit && diag != Diag::Unit);
76 tlapack_check_false(nrows(A) != ncols(A));
77 tlapack_check_false((idx_t)size(x) != n);
78
79 if (trans == Op::NoTrans) {
80 // Form x := A*x
81 if (uplo == Uplo::Upper) {
82 // upper
83 for (idx_t j = 0; j < n; ++j) {
84 // note: NOT skipping if x[j] is zero, for consistent NAN
85 // handling
86 for (idx_t i = 0; i < j; ++i)
87 x[i] += x[j] * A(i, j);
88 if (nonunit) x[j] *= A(j, j);
89 }
90 }
91 else {
92 // lower
93 for (idx_t j = n - 1; j != idx_t(-1); --j) {
94 // note: NOT skipping if x[j] is zero ...
95 for (idx_t i = n - 1; i >= j + 1; --i)
96 x[i] += x[j] * A(i, j);
97 if (nonunit) x[j] *= A(j, j);
98 }
99 }
100 }
101 else if (trans == Op::Conj) {
102 // Form x := A*x
103 if (uplo == Uplo::Upper) {
104 // upper
105 for (idx_t j = 0; j < n; ++j) {
106 // note: NOT skipping if x[j] is zero, for consistent NAN
107 // handling
108 for (idx_t i = 0; i < j; ++i)
109 x[i] += x[j] * conj(A(i, j));
110 if (nonunit) x[j] *= conj(A(j, j));
111 }
112 }
113 else {
114 // lower
115 for (idx_t j = n - 1; j != idx_t(-1); --j) {
116 // note: NOT skipping if x[j] is zero ...
117 for (idx_t i = n - 1; i >= j + 1; --i)
118 x[i] += x[j] * conj(A(i, j));
119 if (nonunit) x[j] *= conj(A(j, j));
120 }
121 }
122 }
123 else if (trans == Op::Trans) {
124 // Form x := A^T * x
125
127
128 if (uplo == Uplo::Upper) {
129 // upper
130 for (idx_t j = n - 1; j != idx_t(-1); --j) {
131 scalar_t tmp = x[j];
132 if (nonunit) tmp *= A(j, j);
133 for (idx_t i = j - 1; i != idx_t(-1); --i)
134 tmp += A(i, j) * x[i];
135 x[j] = tmp;
136 }
137 }
138 else {
139 // lower
140 for (idx_t j = 0; j < n; ++j) {
141 scalar_t tmp = x[j];
142 if (nonunit) tmp *= A(j, j);
143 for (idx_t i = j + 1; i < n; ++i)
144 tmp += A(i, j) * x[i];
145 x[j] = tmp;
146 }
147 }
148 }
149 else {
150 // Form x := A^H * x
151 // same code as above A^T * x case, except add conj()
152
154
155 if (uplo == Uplo::Upper) {
156 // upper
157 for (idx_t j = n - 1; j != idx_t(-1); --j) {
158 scalar_t tmp = x[j];
159 if (nonunit) tmp *= conj(A(j, j));
160 for (idx_t i = j - 1; i != idx_t(-1); --i)
161 tmp += conj(A(i, j)) * x[i];
162 x[j] = tmp;
163 }
164 }
165 else {
166 // lower
167 for (idx_t j = 0; j < n; ++j) {
168 scalar_t tmp = x[j];
169 if (nonunit) tmp *= conj(A(j, j));
170 for (idx_t i = j + 1; i < n; ++i)
171 tmp += conj(A(i, j)) * x[i];
172 x[j] = tmp;
173 }
174 }
175 }
176}
177
178#ifdef TLAPACK_USE_LAPACKPP
179
180template <
181 TLAPACK_LEGACY_MATRIX matrixA_t,
182 TLAPACK_LEGACY_VECTOR vectorX_t,
183 class T = type_t<vectorX_t>,
184 enable_if_allow_optblas_t<pair<matrixA_t, T>, pair<vectorX_t, T> > = 0>
185void trmv(Uplo uplo, Op trans, Diag diag, const matrixA_t& A, vectorX_t& x)
186{
187 // Legacy objects
188 auto A_ = legacy_matrix(A);
189 auto x_ = legacy_vector(x);
190
191 // Constants to forward
192 constexpr Layout L = layout<matrixA_t>;
193 const auto& n = A_.n;
194
195 if (trans != Op::Conj)
196 ::blas::trmv((::blas::Layout)L, (::blas::Uplo)uplo, (::blas::Op)trans,
197 (::blas::Diag)diag, n, A_.ptr, A_.ldim, x_.ptr, x_.inc);
198 else {
199 conjugate(x);
200 ::blas::trmv((::blas::Layout)L, (::blas::Uplo)uplo, ::blas::Op::NoTrans,
201 (::blas::Diag)diag, n, A_.ptr, A_.ldim, x_.ptr, x_.inc);
202 conjugate(x);
203 }
204}
205
206#endif
207
208} // namespace tlapack
209
210#endif // #ifndef TLAPACK_BLAS_TRMV_HH
Diag
Definition types.hpp:192
Op
Definition types.hpp:222
Uplo
Definition types.hpp:45
constexpr T conj(const T &x) noexcept
Extends std::conj() to real datatypes.
Definition utils.hpp:100
#define TLAPACK_LEGACY_VECTOR
Macro for tlapack::concepts::LegacyVector compatible with C++17.
Definition concepts.hpp:954
#define TLAPACK_LEGACY_MATRIX
Macro for tlapack::concepts::LegacyMatrix compatible with C++17.
Definition concepts.hpp:951
#define TLAPACK_VECTOR
Macro for tlapack::concepts::Vector compatible with C++17.
Definition concepts.hpp:906
#define TLAPACK_MATRIX
Macro for tlapack::concepts::Matrix compatible with C++17.
Definition concepts.hpp:896
constexpr auto diag(T &A, int diagIdx=0) noexcept
Get the Diagonal of an Eigen Matrix.
Definition eigen.hpp:576
void conjugate(vector_t &x)
Conjugates a vector.
Definition conjugate.hpp:24
void trmv(Uplo uplo, Op trans, Diag diag, const matrixA_t &A, vectorX_t &x)
Triangular matrix-vector multiply:
Definition trmv.hpp:60
#define tlapack_check_false(cond)
Throw an error if cond is true.
Definition exceptionHandling.hpp:113
Concept for types that represent tlapack::Diag.
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