<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
lansy.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_LANSY_HH
13#define TLAPACK_LANSY_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 }
78 else {
79 for (idx_t j = 0; j < n; ++j) {
80 for (idx_t i = j; i < n; ++i) {
81 real_t temp = abs(A(i, j));
82
83 if (temp > norm)
84 norm = temp;
85 else {
86 if (isnan(temp)) return temp;
87 }
88 }
89 }
90 }
91 }
92 else if (normType == Norm::One || normType == Norm::Inf) {
93 if (uplo == Uplo::Upper) {
94 for (idx_t j = 0; j < n; ++j) {
95 real_t temp(0);
96
97 for (idx_t i = 0; i <= j; ++i)
98 temp += abs(A(i, j));
99
100 for (idx_t i = j + 1; i < n; ++i)
101 temp += abs(A(j, i));
102
103 if (temp > norm)
104 norm = temp;
105 else {
106 if (isnan(temp)) return temp;
107 }
108 }
109 }
110 else {
111 for (idx_t j = 0; j < n; ++j) {
112 real_t temp(0);
113
114 for (idx_t i = 0; i <= j; ++i)
115 temp += abs(A(j, i));
116
117 for (idx_t i = j + 1; i < n; ++i)
118 temp += abs(A(i, j));
119
120 if (temp > norm)
121 norm = temp;
122 else {
123 if (isnan(temp)) return temp;
124 }
125 }
126 }
127 }
128 else {
129 // Scaled ssq
130 real_t scale(0), ssq(1);
131
132 // Sum off-diagonals
133 if (uplo == Uplo::Upper) {
134 for (idx_t j = 1; j < n; ++j)
135 lassq(slice(A, range{0, j}, j), scale, ssq);
136 }
137 else {
138 for (idx_t j = 0; j < n - 1; ++j)
139 lassq(slice(A, range{j + 1, n}, j), scale, ssq);
140 }
141 ssq *= real_t(2);
142
143 // Sum diagonal
144 lassq(diag(A, 0), scale, ssq);
145
146 // Compute the scaled square root
147 norm = scale * sqrt(ssq);
148 }
149
150 return norm;
151}
152
153} // namespace tlapack
154
155#endif // TLAPACK_LANSY_HH
constexpr bool isnan(const T &x) noexcept
Extends std::isnan() to complex numbers.
Definition utils.hpp:125
constexpr auto diag(T &A, int diagIdx=0) noexcept
Get the Diagonal of an Eigen Matrix.
Definition eigen.hpp:576
auto lansy(norm_t normType, uplo_t uplo, const matrix_t &A)
Calculates the norm of a symmetric matrix.
Definition lansy.hpp:43
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
#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