TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
qr.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./utils.hpp"
14#include "../basic_array.hpp"
16#include "../blas/tools.hpp"
17#include "../concepts.hpp"
18#include "../declarations.hpp"
19#include "../exceptions.hpp"
20#include "../lapack/geqp3.hpp"
21#include "../lapack/orgqr.hpp"
22#include "../lapack/ungqr.hpp"
24#include "../layout/range.hpp"
25#include "../macros.hpp"
27#include "../traits.hpp"
28
29#include <algorithm>
30#include <tuple>
31#include <type_traits>
32
33namespace nda::linalg {
34
39
70 template <blas_lapack::BlasArray<2> A, blas_lapack::BlasArrayFor<A, 1> TAU>
72 auto get_qr_matrices(A const &a, TAU const &tau, bool complete = false) {
73 auto const [m, n] = a.shape();
74 auto const min_mn = std::min(m, n);
75 auto const k = (complete ? m : min_mn);
78
79 // compute Q matrix
80 Q(range::all, range(min_mn)) = a(range::all, range(min_mn));
81 int info{};
82 if constexpr (is_complex_v<get_value_t<A>>) {
83 info = lapack::ungqr(Q, tau);
84 } else {
85 info = lapack::orgqr(Q, tau);
86 }
87 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::get_qr_matrices: orgqr/ungqr returned a non-zero value: info = " << info;
88
89 // extract R matrix
90 for (int i = 0; i < min_mn; ++i) R(range(i + 1), i) = a(range(i + 1), i);
91 for (int i = min_mn; i < n; ++i) R(range::all, i) = a(range::all, i);
92
93 return std::make_tuple(Q, R);
94 }
95
128 template <blas_lapack::BlasArray<2> A>
130 auto qr_in_place(A &&a, bool complete = false) { // NOLINT (temporary views are allowed here)
131 auto const [m, n] = a.shape();
132
133 // permutation and tau vector
134 auto jpvt = vector<int>::zeros(n);
135 auto tau = vector<get_value_t<A>>(std::min(m, n));
136
137 // call lapack geqp3
138 int info = lapack::geqp3(a, jpvt, tau);
139 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::qr_in_place: geqp3 returned a non-zero value: info = " << info;
140
141 // extract Q and R from the output
142 auto [Q, R] = get_qr_matrices(a, tau, complete);
143
144 // shift permutation vector to zero-based indexing
145 jpvt -= 1;
146
147 return std::make_tuple(jpvt, Q, R);
148 }
149
166 template <Matrix A>
168 auto qr(A const &a, bool complete = false) {
169 auto a_copy = matrix<get_value_t<A>, F_layout>(a);
170 if constexpr (blas_lapack::has_F_layout<A>) {
171 return qr_in_place(a_copy, complete);
172 } else {
173 auto [sigma, Q, R] = qr_in_place(a_copy, complete);
174 return std::make_tuple(sigma, matrix<get_value_t<A>, C_layout>{Q}, matrix<get_value_t<A>, C_layout>{R});
175 }
176 }
177
179
180} // 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.
Provides various traits and utilities for the BLAS interface.
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.
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:212
static constexpr bool has_F_layout
Constexpr variable that is true if all given nda::Array types have nda::F_layout.
Definition tools.hpp:79
auto get_qr_matrices(A const &a, TAU const &tau, bool complete=false)
Get the and matrices from the output of nda::lapack::geqp3 or nda::lapack::geqrf.
Definition qr.hpp:72
auto qr_in_place(A &&a, bool complete=false)
Compute the QR factorization of a matrix in place.
Definition qr.hpp:130
auto qr(A const &a, bool complete=false)
Compute the QR factorization of a matrix.
Definition qr.hpp:168
int ungqr(A &&a, TAU &&tau, W &&work=vector_value_t< A >{})
Interface to the LAPACK/cuSOLVER ungqr routine.
Definition ungqr.hpp:67
int geqp3(A &&a, JPVT &&jpvt, TAU &&tau, W1 &&work=vector_value_t< A >{}, W2 &&rwork=vector_fp_t< A >{})
Interface to the LAPACK geqp3 routine.
Definition geqp3.hpp:81
int orgqr(A &&a, TAU &&tau, W &&work=vector_value_t< A >{})
Interface to the LAPACK/cuSOLVER orgqr routine.
Definition orgqr.hpp:66
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
Constexpr variable that is true if type T is either of type 'float', double, std::complex<float>' or ...
Definition traits.hpp:95
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/cuSOLVER 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 type traits for the nda library.
Provides a generic interface to the LAPACK/cuSOLVER ungqr routine.