<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
getri.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_GETRI_HH
11#define TLAPACK_GETRI_HH
12
14#include "tlapack/blas/swap.hpp"
17
18namespace tlapack {
19
21enum class GetriVariant : char {
22 UILI = 'D',
23 UXLI = 'C'
24};
25
27struct GetriOpts {
28 GetriVariant variant = GetriVariant::UILI;
29};
30
46template <class T, TLAPACK_SMATRIX matrix_t, TLAPACK_VECTOR piv_t>
48 const piv_t& piv,
49 const GetriOpts& opts = {})
50{
51 if (opts.variant == GetriVariant::UXLI)
52 return getri_uxli_worksize<T>(A, opts);
53
54 return WorkInfo(0);
55}
56
65template <TLAPACK_SMATRIX matrix_t,
66 TLAPACK_VECTOR piv_t,
67 TLAPACK_WORKSPACE work_t>
69 const piv_t& piv,
70 work_t& work,
71 const GetriOpts& opts = {})
72{
73 using idx_t = size_type<matrix_t>;
74
75 // Constants
76 const idx_t n = ncols(A);
77
78 // Call variant
79 int info;
80 if (opts.variant == GetriVariant::UXLI)
81 info = getri_uxli_work(A, work);
82 else
83 info = getri_uili(A);
84
85 // Return is matrix is not invertible
86 if (info != 0) return info;
87
88 // swap columns of X to find A^{-1} since A^{-1}=X P
89 for (idx_t j = n; j-- > 0;) {
90 if (piv[j] != j) {
91 auto vect1 = tlapack::col(A, j);
92 auto vect2 = tlapack::col(A, piv[j]);
93 tlapack::swap(vect1, vect2);
94 }
95 }
96
97 return 0;
98}
99
121template <TLAPACK_SMATRIX matrix_t, TLAPACK_VECTOR piv_t>
122int getri(matrix_t& A, const piv_t& piv, const GetriOpts& opts = {})
123{
124 using idx_t = size_type<matrix_t>;
125
126 // Constants
127 const idx_t n = ncols(A);
128
129 // Call variant
130 int info;
131 if (opts.variant == GetriVariant::UXLI)
132 info = getri_uxli(A);
133 else
134 info = getri_uili(A);
135
136 // Return is matrix is not invertible
137 if (info != 0) return info;
138
139 // swap columns of X to find A^{-1} since A^{-1}=X P
140 for (idx_t j = n; j-- > 0;) {
141 if (piv[j] != j) {
142 auto vect1 = tlapack::col(A, j);
143 auto vect2 = tlapack::col(A, piv[j]);
144 tlapack::swap(vect1, vect2);
145 }
146 }
147
148 return 0;
149}
150
151} // namespace tlapack
152
153#endif // TLAPACK_GETRI_HH
#define TLAPACK_SMATRIX
Macro for tlapack::concepts::SliceableMatrix compatible with C++17.
Definition concepts.hpp:899
#define TLAPACK_WORKSPACE
Macro for tlapack::concepts::Workspace compatible with C++17.
Definition concepts.hpp:912
#define TLAPACK_VECTOR
Macro for tlapack::concepts::Vector compatible with C++17.
Definition concepts.hpp:906
GetriVariant
Variants of the algorithm to compute the inverse of a matrix.
Definition getri.hpp:21
@ UILI
Method D from doi:10.1137/1.9780898718027.
@ UXLI
Method C from doi:10.1137/1.9780898718027.
int getri_uxli(matrix_t &A)
getri computes inverse of a general n-by-n matrix A by solving for X in the following equation
Definition getri_uxli.hpp:115
void swap(vectorX_t &x, vectorY_t &y)
Swap vectors, .
Definition swap.hpp:31
int getri_uili(matrix_t &A)
getri_uili calculates the inverse of a general n-by-n matrix A
Definition getri_uili.hpp:39
int getri_uxli_work(matrix_t &A, work_t &work)
getri computes inverse of a general n-by-n matrix A by solving for X in the following equation Work...
Definition getri_uxli.hpp:46
int getri_work(matrix_t &A, const piv_t &piv, work_t &work, const GetriOpts &opts={})
getri computes inverse of a general n-by-n matrix A Workspace is provided as an argument.
Definition getri.hpp:68
int getri(matrix_t &A, const piv_t &piv, const GetriOpts &opts={})
getri computes inverse of a general n-by-n matrix A
Definition getri.hpp:122
constexpr WorkInfo getri_worksize(const matrix_t &A, const piv_t &piv, const GetriOpts &opts={})
Worspace query of getri()
Definition getri.hpp:47
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
Options struct for getri()
Definition getri.hpp:27
Output information in the workspace query.
Definition workspace.hpp:16