TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
qr.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-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: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
21
22#pragma once
23
24#include "./utils.hpp"
25#include "../basic_array.hpp"
27#include "../blas/tools.hpp"
28#include "../concepts.hpp"
29#include "../declarations.hpp"
30#include "../exceptions.hpp"
31#include "../lapack/geqp3.hpp"
32#include "../lapack/orgqr.hpp"
33#include "../lapack/ungqr.hpp"
35#include "../layout/range.hpp"
36#include "../macros.hpp"
38#include "../traits.hpp"
39
40#include <algorithm>
41#include <tuple>
42#include <type_traits>
43
44namespace nda::linalg {
45
50
75 template <MemoryMatrix A, MemoryVector TAU>
77 auto get_qr_matrices(A const &a, TAU const &tau, bool complete = false) {
78 auto const [m, n] = a.shape();
79 auto const min_mn = std::min(m, n);
80 auto const k = (complete ? m : min_mn);
83
84 // compute Q matrix
85 Q(range::all, range(min_mn)) = a(range::all, range(min_mn));
86 int info{};
87 if constexpr (is_complex_v<get_value_t<A>>) {
88 info = lapack::ungqr(Q, tau);
89 } else {
90 info = lapack::orgqr(Q, tau);
91 }
92 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::qr_in_place: orgqr/ungqr returned a non-zero value: info = " << info;
93
94 // extract R matrix
95 for (int i = 0; i < min_mn; ++i) R(range(i + 1), i) = a(range(i + 1), i);
96 for (int i = min_mn; i < n; ++i) R(range::all, i) = a(range::all, i);
97
98 return std::make_tuple(Q, R);
99 }
100
129 template <MemoryMatrix A>
131 auto qr_in_place(A &&a, bool complete = false) { // NOLINT (temporary views are allowed here)
132 auto const [m, n] = a.shape();
133
134 // permutation and tau vector
135 auto jpvt = nda::vector<int>::zeros(n);
136 auto tau = nda::vector<get_value_t<A>>(std::min(m, n));
137
138 // call lapack geqp3
139 int info = lapack::geqp3(a, jpvt, tau);
140 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::qr_in_place: geqp3 returned a non-zero value: info = " << info;
141
142 // extract Q and R from the output
143 auto [Q, R] = get_qr_matrices(a, tau, complete);
144
145 // shift permutation vector to zero-based indexing
146 jpvt -= 1;
147
148 return std::make_tuple(jpvt, Q, R);
149 }
150
163 template <Matrix A>
165 auto qr(A const &a, bool complete = false) {
166 auto a_copy = matrix<get_value_t<A>, F_layout>(a);
167 if constexpr (nda::blas::has_F_layout<A>) {
168 return qr_in_place(a_copy, complete);
169 } else {
170 auto [sigma, Q, R] = qr_in_place(a_copy, complete);
171 return std::make_tuple(sigma, matrix<get_value_t<A>, C_layout>{Q}, matrix<get_value_t<A>, C_layout>{R});
172 }
173 }
174
176
177} // namespace nda::linalg
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
Provides basic functions to create and manipulate arrays and views.
static basic_array zeros(std::array< Int, Rank > const &shape)
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
Provides a generic interface to the LAPACK geqp3 routine.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:186
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:182
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has nda::F_layout.
Definition tools.hpp:73
int ungqr(A &&a, TAU &&tau)
Interface to the LAPACK ungqr routine.
Definition ungqr.hpp:60
int orgqr(A &&a, TAU &&tau)
Interface to the LAPACK orgqr routine.
Definition orgqr.hpp:59
int geqp3(A &&a, JPVT &&jpvt, TAU &&tau)
Interface to the LAPACK geqp3 routine.
Definition geqp3.hpp:72
auto qr(A const &a, bool complete=false)
Compute the QR factorization of a matrix.
Definition qr.hpp:165
auto qr_in_place(A &&a, bool complete=false)
Compute the QR factorization of a matrix in place.
Definition qr.hpp:131
auto get_qr_matrices(A const &a, TAU const &tau, bool complete=false)
Get the and matrices from the output of nda::lapack::geqp3.
Definition qr.hpp:77
static constexpr bool have_host_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Host.
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Provides definitions of various layout policies.
Provides utility functions for the nda::linalg namespace.
Macros used in the nda library.
Provides a generic interface to the LAPACK orgqr routine.
Includes the itertools header and provides some additional utilities.
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:36
Contiguous layout policy with Fortran-order (column-major order).
Definition policies.hpp:52
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.
Provides a generic interface to the LAPACK ungqr routine.