TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gelss.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-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, Jason Kaye, Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the LAPACK `gelss` 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
37namespace nda::lapack {
38
39 /**
40 * @ingroup linalg_lapack
41 * @brief Interface to the LAPACK `gelss` routine.
42 *
43 * @details Computes the minimum norm solution to a complex linear least squares problem:
44 * \f[
45 * \min_x | \mathbf{b} - \mathbf{A x} |_2
46 * \f]
47 * using the singular value decomposition (SVD) of \f$ \mathbf{A} \f$. \f$ \mathbf{A} \f$ is an m-by-n matrix which
48 * may be rank-deficient.
49 *
50 * Several right hand side vectors \f$ \mathbf{b} \f$ and solution vectors \f$ \mathbf{x} \f$ can be handled in a
51 * single call; they are stored as the columns of the m-by-nrhs right hand side matrix \f$ \mathbf{B} \f$ and the
52 * n-by-nrhs solution matrix \f$ \mathbf{X} \f$.
53 *
54 * The effective rank of \f$ \mathbf{A} \f$ is determined by treating as zero those singular values which are less
55 * than `rcond` times the largest singular value.
56 *
57 * @tparam A nda::MemoryMatrix type.
58 * @tparam B nda::MemoryArray type.
59 * @tparam S nda::MemoryVector type.
60 * @param a Input/output matrix. On entry, the m-by-n matrix \f$ \mathbf{A} \f$. On exit, the first `min(m,n)` rows of
61 * \f$ \mathbf{A} \f$ are overwritten with its right singular vectors, stored rowwise.
62 * @param b Input/output array. On entry, the m-by-nrhs right hand side matrix \f$ \mathbf{B} \f$. On exit,
63 * \f$ \mathbf{B} \f$ is overwritten by the n-by-nrhs solution matrix \f$ \mathbf{X} \f$. If `m >= n` and `RANK == n`,
64 * the residual sum-of-squares for the solution in the i-th column is given by the sum of squares of the modulus of
65 * elements `n+1:m` in that column.
66 * @param s Output vector. The singular values of \f$ \mathbf{A} \f$ in decreasing order. The condition number of A in
67 * the 2-norm is `s(1)/s(min(m,n))`.
68 * @param rcond It is used to determine the effective rank of \f$ \mathbf{A} \f$. Singular values `s(i) <= rcond *
69 * s(1)` are treated as zero. If `rcond < 0`, machine precision is used instead.
70 * @param rank Output variable of the effective rank of \f$ \mathbf{A} \f$, i.e., the number of singular values which
71 * are greater than `rcond * s(1)`.
72 * @return Integer return code.
73 */
74 template <MemoryMatrix A, MemoryArray B, MemoryVector S>
75 requires(have_same_value_type_v<A, B> and mem::on_host<A, B, S> and is_blas_lapack_v<get_value_t<A>>)
76 int gelss(A &&a, B &&b, S &&s, double rcond, int &rank) { // NOLINT (temporary views are allowed here)
77 static_assert(has_F_layout<A> and has_F_layout<B>, "Error in nda::lapack::gelss: C order not supported");
78 static_assert(MemoryVector<B> or MemoryMatrix<B>, "Error in nda::lapack::gelss: B must be a vector or a matrix");
79
80 auto dm = std::min(a.extent(0), a.extent(1));
81 if (s.size() < dm) s.resize(dm);
82
83 // must be lapack compatible
84 EXPECTS(a.indexmap().min_stride() == 1);
85 EXPECTS(b.indexmap().min_stride() == 1);
86 EXPECTS(s.indexmap().min_stride() == 1);
87
88 // first call to get the optimal bufferSize
89 using value_type = get_value_t<A>;
90 value_type bufferSize_T{};
91 auto rwork = array<double, 1>(5 * dm);
92 int info = 0;
93 int nrhs = 1, ldb = b.size(); // defaults for B MemoryVector
94 if constexpr (MemoryMatrix<B>) {
95 nrhs = b.extent(1);
96 ldb = get_ld(b);
97 }
98 f77::gelss(a.extent(0), a.extent(1), nrhs, a.data(), get_ld(a), b.data(), ldb, s.data(), rcond, rank, &bufferSize_T, -1, rwork.data(), info);
99 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
100
101 // allocate work buffer and perform actual library call
102 array<value_type, 1> work(bufferSize);
103 f77::gelss(a.extent(0), a.extent(1), nrhs, a.data(), get_ld(a), b.data(), ldb, s.data(), rcond, rank, work.data(), bufferSize, rwork.data(),
104 info);
105
106 if (info) NDA_RUNTIME_ERROR << "Error in nda::lapack::gelss: info = " << info;
107 return info;
108 }
109
110} // 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 gelss(A &&a, B &&b, S &&s, double rcond, int &rank)
Interface to the LAPACK gelss routine.
Definition gelss.hpp:76
#define EXPECTS(X)
Definition macros.hpp:59