<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
potf2.hpp
Go to the documentation of this file.
1
4//
5// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
6//
7// This file is part of <T>LAPACK.
8// <T>LAPACK is free software: you can redistribute it and/or modify it under
9// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
10
11#ifndef TLAPACK_POTF2_HH
12#define TLAPACK_POTF2_HH
13
15#include "tlapack/blas/dot.hpp"
16#include "tlapack/blas/gemv.hpp"
17#include "tlapack/blas/scal.hpp"
18
19namespace tlapack {
20
55template <TLAPACK_UPLO uplo_t,
56 TLAPACK_SMATRIX matrix_t,
57 disable_if_allow_optblas_t<matrix_t> = 0>
59{
60 using T = type_t<matrix_t>;
61 using real_t = real_type<T>;
62 using idx_t = size_type<matrix_t>;
64
65 // Constants
66 const real_t one(1);
67 const real_t zero(0);
68 const idx_t n = nrows(A);
69
70 // check arguments
71 tlapack_check(uplo == Uplo::Lower || uplo == Uplo::Upper);
72 tlapack_check(nrows(A) == ncols(A));
73
74 // Quick return
75 if (n <= 0) return 0;
76
77 if (uplo == Uplo::Upper) {
78 // Compute the Cholesky factorization A = U^H * U
79 for (idx_t j = 0; j < n; ++j) {
80 auto colj = slice(A, range{0, j}, j);
81
82 // Compute U(j,j) and test for non-positive-definiteness
83 real_t ajj = real(A(j, j)) - real(dot(colj, colj));
84 if (ajj > zero) {
85 ajj = sqrt(ajj);
86 A(j, j) = T(ajj);
87 }
88 else {
90 j + 1,
91 "The leading minor of order j+1 is not positive definite,"
92 " and the factorization could not be completed.");
93 return j + 1;
94 }
95
96 // Compute elements j+1:n of row j
97 if (j + 1 < n) {
98 auto Ajj = slice(A, range{0, j}, range{j + 1, n});
99 auto rowj = slice(A, j, range{j + 1, n});
100
101 // rowj := rowj - conj(colj) * Ajj
102 for (idx_t i = 0; i < j; ++i)
103 colj[i] = conj(colj[i]);
105 for (idx_t i = 0; i < j; ++i)
106 colj[i] = conj(colj[i]);
107
109 scal(one / ajj, rowj);
110 }
111 }
112 }
113 else {
114 // Compute the Cholesky factorization A = L * L^H
115 for (idx_t j = 0; j < n; ++j) {
116 auto rowj = slice(A, j, range{0, j});
117
118 // Compute L(j,j) and test for non-positive-definiteness
119 real_t ajj = real(A(j, j)) - real(dot(rowj, rowj));
120 if (ajj > zero) {
121 ajj = sqrt(ajj);
122 A(j, j) = T(ajj);
123 }
124 else {
126 j + 1,
127 "The leading minor of order j+1 is not positive definite,"
128 " and the factorization could not be completed.");
129 return j + 1;
130 }
131
132 // Compute elements j+1:n of column j
133 if (j + 1 < n) {
134 auto Ajj = slice(A, range{j + 1, n}, range{0, j});
135 auto colj = slice(A, range{j + 1, n}, j);
136
137 // colj := colj - Ajj * conj(rowj)
138 for (idx_t i = 0; i < j; ++i)
139 rowj[i] = conj(rowj[i]);
141 for (idx_t i = 0; i < j; ++i)
142 rowj[i] = conj(rowj[i]);
143
145 scal(one / ajj, colj);
146 }
147 }
148 }
149
150 return 0;
151}
152
153#ifdef TLAPACK_USE_LAPACKPP
154
155template <TLAPACK_UPLO uplo_t,
156 TLAPACK_LEGACY_MATRIX matrix_t,
157 enable_if_allow_optblas_t<matrix_t> = 0>
158int potf2(uplo_t uplo, matrix_t& A)
159{
160 // Legacy objects
161 auto A_ = legacy_matrix(A);
162
163 // Constants to forward
164 const auto& n = A_.n;
165
166 if constexpr (layout<matrix_t> == Layout::ColMajor) {
167 return ::lapack::potf2((::blas::Uplo)(Uplo)uplo, n, A_.ptr, A_.ldim);
168 }
169 else {
170 return ::lapack::potf2(
171 ((uplo == Uplo::Lower) ? ::blas::Uplo::Upper : ::blas::Uplo::Lower),
172 n, A_.ptr, A_.ldim);
173 }
174}
175
176#endif
177
178} // namespace tlapack
179
180#endif // TLAPACK_POTF2_HH
constexpr internal::Transpose TRANSPOSE
transpose
Definition types.hpp:257
@ Lower
0 <= i <= m, 0 <= j <= i.
constexpr internal::NoTranspose NO_TRANS
no transpose
Definition types.hpp:255
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
constexpr T conj(const T &x) noexcept
Extends std::conj() to real datatypes.
Definition utils.hpp:100
#define TLAPACK_UPLO
Macro for tlapack::concepts::Uplo compatible with C++17.
Definition concepts.hpp:942
#define TLAPACK_SMATRIX
Macro for tlapack::concepts::SliceableMatrix compatible with C++17.
Definition concepts.hpp:899
#define TLAPACK_LEGACY_MATRIX
Macro for tlapack::concepts::LegacyMatrix compatible with C++17.
Definition concepts.hpp:951
auto dot(const vectorX_t &x, const vectorY_t &y)
Definition dot.hpp:32
void scal(const alpha_t &alpha, vector_t &x)
Scale vector by constant, .
Definition scal.hpp:30
void gemv(Op trans, const alpha_t &alpha, const matrixA_t &A, const vectorX_t &x, const beta_t &beta, vectorY_t &y)
General matrix-vector multiply:
Definition gemv.hpp:57
int potf2(uplo_t uplo, matrix_t &A)
Computes the Cholesky factorization of a Hermitian positive definite matrix A using a level-2 algorit...
Definition potf2.hpp:58
#define tlapack_check(cond)
Throw an error if cond is false.
Definition exceptionHandling.hpp:98
#define tlapack_error(info, detailedInfo)
Error handler.
Definition exceptionHandling.hpp:142
Concept for types that represent tlapack::Uplo.
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