TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
geqp3.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Jason Kaye, Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the LAPACK `geqp3` routine.
20 */
21
22#pragma once
23
24#include "./interface/cxx_interface.hpp"
25#include "../basic_array.hpp"
26#include "../concepts.hpp"
27#include "../declarations.hpp"
28#include "../exceptions.hpp"
29#include "../macros.hpp"
30#include "../mem/address_space.hpp"
31#include "../traits.hpp"
32
33#include <algorithm>
34#include <cmath>
35#include <complex>
36#include <type_traits>
37
38namespace nda::lapack {
39
40 /**
41 * @ingroup linalg_lapack
42 * @brief Interface to the LAPACK `geqp3` routine.
43 *
44 * @details Computes a QR factorization with column pivoting of a matrix \f$ \mathbf{A} \f$:
45 * \f[
46 * \mathbf{A P} = \mathbf{Q R}
47 * \f]
48 * using Level 3 BLAS.
49 *
50 * @tparam A nda::MemoryMatrix type.
51 * @tparam JPVT nda::MemoryVector type.
52 * @tparam TAU nda::MemoryVector type.
53 * @param a Input/output matrix. On entry, the m-by-n matrix \f$ \mathbf{A} \f$. On exit, the upper triangle of the
54 * array contains the `min(m,n)`-by-n upper trapezoidal matrix \f$ \mathbf{R} \f$; the elements below the diagonal,
55 * together with the array `tau`, represent the unitary matrix \f$ \mathbf{Q} \f$ as a product of `min(m,n)`
56 * elementary reflectors.
57 * @param jpvt Input/output vector. On entry, if `jpvt(j) != 0`, the j-th column of \f$ \mathbf{A} \f$ is permuted to
58 * the front of \f$ \mathbf{A P} \f$ (a leading column); if `jpvt(j) == 0`, the j-th column of \f$ \mathbf{A} \f$ is a
59 * free column. On exit, if `jpvt(j) == k`, then the j-th column of \f$ \mathbf{A P} \f$ was the the k-th column of
60 * \f$ \mathbf{A} \f$.
61 * @param tau Output vector. The scalar factors of the elementary reflectors.
62 * @return Integer return code from the LAPACK call.
63 */
64 template <MemoryMatrix A, MemoryVector JPVT, MemoryVector TAU>
65 requires(mem::on_host<A> and is_blas_lapack_v<get_value_t<A>> and have_same_value_type_v<A, TAU>
66 and mem::have_compatible_addr_space<A, JPVT, TAU>)
67 int geqp3(A &&a, JPVT &&jpvt, TAU &&tau) { // NOLINT (temporary views are allowed here)
68 static_assert(has_F_layout<A>, "Error in nda::lapack::geqp3: C order not supported");
69 static_assert(std::is_same_v<get_value_t<JPVT>, int>, "Error in nda::lapack::geqp3: Pivoting array must have elements of type int");
70 static_assert(mem::have_host_compatible_addr_space<A, JPVT, TAU>, "Error in nda::lapack::geqp3: Only CPU is supported");
71
72 auto [m, n] = a.shape();
73 EXPECTS(tau.size() >= std::min(m, n));
74
75 // must be lapack compatible
76 EXPECTS(a.indexmap().min_stride() == 1);
77 EXPECTS(jpvt.indexmap().min_stride() == 1);
78 EXPECTS(tau.indexmap().min_stride() == 1);
79
80 // first call to get the optimal buffersize
81 using value_type = get_value_t<A>;
82 value_type bufferSize_T{};
83 int info = 0;
84 array<double, 1> rwork(2 * n);
85 lapack::f77::geqp3(m, n, a.data(), get_ld(a), jpvt.data(), tau.data(), &bufferSize_T, -1, rwork.data(), info);
86 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
87
88 // allocate work buffer and perform actual library call
89 nda::array<value_type, 1> work(bufferSize);
90 lapack::f77::geqp3(m, n, a.data(), get_ld(a), jpvt.data(), tau.data(), work.data(), bufferSize, rwork.data(), info);
91 jpvt -= 1; // Shift to 0-based indexing
92
93 if (info) NDA_RUNTIME_ERROR << "Error in nda::lapack::geqp3: info = " << info;
94 return info;
95 }
96
97} // namespace nda::lapack
ValueType * data() noexcept
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view...
#define NDA_RUNTIME_ERROR
int geqp3(A &&a, JPVT &&jpvt, TAU &&tau)
Interface to the LAPACK geqp3 routine.
Definition geqp3.hpp:67
#define EXPECTS(X)
Definition macros.hpp:59