<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
inv_house3.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
5//
6// This file is part of <T>LAPACK.
7// <T>LAPACK is free software: you can redistribute it and/or modify it under
8// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
9
10#ifndef TLAPACK_INV_HOUSE_HH
11#define TLAPACK_INV_HOUSE_HH
12
15
16namespace tlapack {
17
43template <TLAPACK_SMATRIX matrix_t, TLAPACK_VECTOR vector_t>
45{
46 using T = type_t<matrix_t>;
47 using real_t = real_type<T>;
48
50
51 T a11, a12, a21, a22;
52
53 // Swap rows if necessary
54 real_t temp1 = max<real_t>(abs1(A(0, 1)), abs1(A(0, 2)));
55 real_t temp2 = max<real_t>(abs1(A(1, 1)), abs1(A(1, 2)));
56
57 if (temp1 < safemin and temp2 < safemin) {
58 v[0] = (T)0;
59 v[1] = (T)1;
60 v[2] = (T)0;
62 return;
63 }
64 if (temp1 >= temp2) {
65 a11 = A(0, 1);
66 a12 = A(0, 2);
67 a21 = A(1, 1);
68 a22 = A(1, 2);
69 v[1] = -A(0, 0);
70 v[2] = -A(1, 0);
71 }
72 else {
73 a11 = A(1, 1);
74 a12 = A(1, 2);
75 a21 = A(0, 1);
76 a22 = A(0, 2);
77 v[1] = -A(1, 0);
78 v[2] = -A(0, 0);
79 }
80
81 // Swap columns if necessary
82 bool colswapped = false;
83 if (abs1(a12) > abs1(a11)) {
84 colswapped = true;
85 std::swap(a11, a12);
86 std::swap(a21, a22);
87 }
88
89 // Calculate LU factorization
90 T u11 = a11;
91 T u12 = a12;
92 T l21 = a21 / u11;
93 T u22 = a22 - l21 * u12;
94
95 // Solve lower triangular system
96 v[2] = v[2] - v[1] * l21;
97 // Solve upper triangular system
98 real_t scale = (real_t)1;
99 if (abs1(u22) < safemin) {
100 v[0] = (T)0;
101 v[1] = (T)1;
102 v[2] = -u12 / u11;
104 return;
105 }
106 if (abs1(u22) < abs1(v[2])) scale = abs1(u22 / v[2]);
107 if (abs1(u11) < abs1(v[1])) scale = min(scale, abs1(u11 / v[1]));
108 v[2] = (scale * v[2]) / u22;
109 v[1] = (scale * v[1] - u12 * v[2]) / u11;
110 v[0] = scale;
111 if (colswapped) std::swap(v[1], v[2]);
112
113 // Calculate Reflector
115}
116
117} // namespace tlapack
118
119#endif // TLAPACK_INV_HOUSE_HH
constexpr internal::Forward FORWARD
Forward direction.
Definition types.hpp:376
constexpr internal::ColumnwiseStorage COLUMNWISE_STORAGE
Columnwise storage.
Definition types.hpp:409
constexpr real_type< T > abs1(const T &x)
1-norm absolute value, |Re(x)| + |Im(x)|
Definition utils.hpp:133
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
void inv_house3(const matrix_t &A, vector_t &v, type_t< vector_t > &tau)
Inv_house calculates a reflector to reduce the first column in a 2x3 matrix A from the right to zero.
Definition inv_house3.hpp:44
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