<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
larfg.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
7//
8// This file is part of <T>LAPACK.
9// <T>LAPACK is free software: you can redistribute it and/or modify it under
10// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
11
12#ifndef TLAPACK_LARFG_HH
13#define TLAPACK_LARFG_HH
14
16#include "tlapack/blas/nrm2.hpp"
17#include "tlapack/blas/scal.hpp"
21
22namespace tlapack {
23
72template <TLAPACK_STOREV storage_t, TLAPACK_VECTOR vector_t>
75 vector_t& x,
77{
78 // data traits
79 using T = type_t<vector_t>;
80 using idx_t = size_type<vector_t>;
81
82 // using
83 using real_t = real_type<T>;
84
85 // constants
86 const real_t one(1);
87 const real_t zero(0);
89 const real_t rsafemin = one / safemin;
90
91 // check arguments
92 tlapack_check(storeMode == StoreV::Columnwise ||
93 storeMode == StoreV::Rowwise);
94
95 tau = zero;
96 real_t xnorm = nrm2(x);
97
98 if (xnorm > zero || (imag(alpha) != zero)) {
99 // First estimate of beta
102 real_t beta = (real(alpha) < zero) ? temp : -temp;
103
104 // Scale if needed
105 idx_t knt = 0;
106 if (abs(beta) < safemin) {
107 while ((abs(beta) < safemin) && (knt < 20)) {
108 knt++;
109 scal(rsafemin, x);
110 beta *= rsafemin;
111 alpha *= rsafemin;
112 }
113 xnorm = nrm2(x);
116 beta = (real(alpha) < zero) ? temp : -temp;
117 }
118
119 // compute tau and y
120 tau = (beta - alpha) / beta;
121 rscl(alpha - beta, x);
122 if (storeMode == StoreV::Rowwise) tau = conj(tau);
123
124 // Scale if needed
125 for (idx_t j = 0; j < knt; ++j)
126 beta *= safemin;
127
128 // Store beta in alpha
129 alpha = beta;
130 }
131}
132
191template <TLAPACK_DIRECTION direction_t,
192 TLAPACK_STOREV storage_t,
193 TLAPACK_VECTOR vector_t,
194 enable_if_t<std::is_convertible_v<direction_t, Direction>, int> = 0>
195void larfg(direction_t direction,
197 vector_t& v,
199{
200 using idx_t = size_type<vector_t>;
202
203 // check arguments
204 tlapack_check_false(direction != Direction::Backward &&
205 direction != Direction::Forward);
206
207 const idx_t alpha_idx = (direction == Direction::Forward) ? 0 : size(v) - 1;
208
209 auto x =
210 slice(v, (direction == Direction::Forward) ? range(1, size(v))
211 : range(0, size(v) - 1));
214 v[alpha_idx] = alpha;
215}
216
217} // namespace tlapack
218
219#endif // TLAPACK_LARFG_HH
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_STOREV
Macro for tlapack::concepts::StoreV compatible with C++17.
Definition concepts.hpp:936
#define TLAPACK_DIRECTION
Macro for tlapack::concepts::Direction compatible with C++17.
Definition concepts.hpp:930
#define TLAPACK_VECTOR
Macro for tlapack::concepts::Vector compatible with C++17.
Definition concepts.hpp:906
real_type< TX, TY > lapy2(const TX &x, const TY &y)
Finds , taking care not to cause unnecessary overflow.
Definition lapy2.hpp:32
void rscl(const alpha_t &alpha, vector_t &x)
Scale vector by the reciprocal of a constant, .
Definition rscl.hpp:22
void larfg(storage_t storeMode, type_t< vector_t > &alpha, vector_t &x, type_t< vector_t > &tau)
Generates a elementary Householder reflection.
Definition larfg.hpp:73
real_type< TX, TY, TZ > lapy3(const TX &x, const TY &y, const TZ &z)
Finds , taking care not to cause unnecessary overflow or unnecessary underflow.
Definition lapy3.hpp:38
void scal(const alpha_t &alpha, vector_t &x)
Scale vector by constant, .
Definition scal.hpp:30
auto nrm2(const vector_t &x)
Definition nrm2.hpp:33
#define tlapack_check(cond)
Throw an error if cond is false.
Definition exceptionHandling.hpp:98
#define tlapack_check_false(cond)
Throw an error if cond is true.
Definition exceptionHandling.hpp:113
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