<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
lanhe.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_LANHE_HH
13#define TLAPACK_LANHE_HH
14
16
17namespace tlapack {
18
42template <TLAPACK_NORM norm_t, TLAPACK_UPLO uplo_t, TLAPACK_SMATRIX matrix_t>
44{
45 using T = type_t<matrix_t>;
46 using real_t = real_type<T>;
47 using idx_t = size_type<matrix_t>;
49
50 // constants
51 const idx_t n = nrows(A);
52
53 // check arguments
54 tlapack_check_false(normType != Norm::Fro && normType != Norm::Inf &&
55 normType != Norm::Max && normType != Norm::One);
56 tlapack_check_false(uplo != Uplo::Lower && uplo != Uplo::Upper);
57
58 // quick return
59 if (n <= 0) return real_t(0);
60
61 // Norm value
62 real_t norm(0);
63
64 if (normType == Norm::Max) {
65 if (uplo == Uplo::Upper) {
66 for (idx_t j = 0; j < n; ++j) {
67 for (idx_t i = 0; i < j; ++i) {
68 real_t temp = abs(A(i, j));
69
70 if (temp > norm)
71 norm = temp;
72 else {
73 if (isnan(temp)) return temp;
74 }
75 }
76 {
77 real_t temp = abs(real(A(j, j)));
78
79 if (temp > norm)
80 norm = temp;
81 else {
82 if (isnan(temp)) return temp;
83 }
84 }
85 }
86 }
87 else {
88 for (idx_t j = 0; j < n; ++j) {
89 {
90 real_t temp = abs(real(A(j, j)));
91
92 if (temp > norm)
93 norm = temp;
94 else {
95 if (isnan(temp)) return temp;
96 }
97 }
98 for (idx_t i = j + 1; i < n; ++i) {
99 real_t temp = abs(A(i, j));
100
101 if (temp > norm)
102 norm = temp;
103 else {
104 if (isnan(temp)) return temp;
105 }
106 }
107 }
108 }
109 }
110 else if (normType == Norm::One || normType == Norm::Inf) {
111 if (uplo == Uplo::Upper) {
112 for (idx_t j = 0; j < n; ++j) {
113 real_t temp(0);
114
115 for (idx_t i = 0; i < j; ++i)
116 temp += abs(A(i, j));
117
118 temp += abs(real(A(j, j)));
119
120 for (idx_t i = j + 1; i < n; ++i)
121 temp += abs(A(j, i));
122
123 if (temp > norm)
124 norm = temp;
125 else {
126 if (isnan(temp)) return temp;
127 }
128 }
129 }
130 else {
131 for (idx_t j = 0; j < n; ++j) {
132 real_t temp(0);
133
134 for (idx_t i = 0; i < j; ++i)
135 temp += abs(A(j, i));
136
137 temp += abs(real(A(j, j)));
138
139 for (idx_t i = j + 1; i < n; ++i)
140 temp += abs(A(i, j));
141
142 if (temp > norm)
143 norm = temp;
144 else {
145 if (isnan(temp)) return temp;
146 }
147 }
148 }
149 }
150 else {
151 // Scaled ssq
152 real_t scale(0), ssq(1);
153
154 // Sum off-diagonals
155 if (uplo == Uplo::Upper) {
156 for (idx_t j = 1; j < n; ++j)
157 lassq(slice(A, range{0, j}, j), scale, ssq);
158 }
159 else {
160 for (idx_t j = 0; j < n - 1; ++j)
161 lassq(slice(A, range{j + 1, n}, j), scale, ssq);
162 }
163 ssq *= real_t(2);
164
165 // Sum the real part in the diagonal
166 lassq(diag(A, 0), scale, ssq,
167 // Lambda function to get the absolute value of the real part :
168 [](const T& x) { return abs(real(x)); });
169
170 // Compute the scaled square root
171 norm = scale * sqrt(ssq);
172 }
173
174 return norm;
175}
176
177} // namespace tlapack
178
179#endif // TLAPACK_LANHE_HH
constexpr bool isnan(const T &x) noexcept
Extends std::isnan() to complex numbers.
Definition utils.hpp:125
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
constexpr auto diag(T &A, int diagIdx=0) noexcept
Get the Diagonal of an Eigen Matrix.
Definition eigen.hpp:576
void lassq(const vector_t &x, real_type< type_t< vector_t > > &scale, real_type< type_t< vector_t > > &sumsq, abs_f absF)
Updates a sum of squares represented in scaled form.
Definition lassq.hpp:49
auto lanhe(norm_t normType, uplo_t uplo, const matrix_t &A)
Calculates the norm of a hermitian matrix.
Definition lanhe.hpp:43
#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