<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
lange.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_LANGE_HH
13#define TLAPACK_LANGE_HH
14
16
17namespace tlapack {
18
37template <TLAPACK_NORM norm_t, TLAPACK_SMATRIX matrix_t>
39{
40 using T = type_t<matrix_t>;
41 using real_t = real_type<T>;
42 using idx_t = size_type<matrix_t>;
43
44 // constants
45 const idx_t m = nrows(A);
46 const idx_t n = ncols(A);
47
48 // check arguments
49 tlapack_check_false(normType != Norm::Fro && normType != Norm::Inf &&
50 normType != Norm::Max && normType != Norm::One);
51
52 // quick return
53 if (m == 0 || n == 0) return real_t(0);
54
55 // Norm value
56 real_t norm(0);
57
58 if (normType == Norm::Max) {
59 for (idx_t j = 0; j < n; ++j) {
60 for (idx_t i = 0; i < m; ++i) {
61 real_t temp = abs(A(i, j));
62
63 if (temp > norm)
64 norm = temp;
65 else {
66 if (isnan(temp)) return temp;
67 }
68 }
69 }
70 }
71 else if (normType == Norm::Inf) {
72 for (idx_t i = 0; i < m; ++i) {
73 real_t sum(0);
74 for (idx_t j = 0; j < n; ++j)
75 sum += abs(A(i, j));
76
77 if (sum > norm)
78 norm = sum;
79 else {
80 if (isnan(sum)) return sum;
81 }
82 }
83 }
84 else if (normType == Norm::One) {
85 for (idx_t j = 0; j < n; ++j) {
86 real_t sum(0);
87 for (idx_t i = 0; i < m; ++i)
88 sum += abs(A(i, j));
89
90 if (sum > norm)
91 norm = sum;
92 else {
93 if (isnan(sum)) return sum;
94 }
95 }
96 }
97 else {
98 real_t scale(0), sum(1);
99 for (idx_t j = 0; j < n; ++j)
100 lassq(col(A, j), scale, sum);
101 norm = scale * sqrt(sum);
102 }
103
104 return norm;
105}
106
107} // namespace tlapack
108
109#endif // TLAPACK_LANGE_HH
constexpr bool isnan(const T &x) noexcept
Extends std::isnan() to complex numbers.
Definition utils.hpp:125
auto lange(norm_t normType, const matrix_t &A)
Calculates the norm of a matrix.
Definition lange.hpp:38
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