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--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
14#include "../basic_array.hpp"
15#include "../concepts.hpp"
16#include "../declarations.hpp"
17#include "../exceptions.hpp"
18#include "../macros.hpp"
20#include "../traits.hpp"
21
22#include <algorithm>
23#include <cmath>
24#include <complex>
25
26namespace nda::lapack {
27
63 template <MemoryMatrix A, MemoryArray B, MemoryVector S>
65 int gelss(A &&a, B &&b, S &&s, double rcond, int &rank) { // NOLINT (temporary views are allowed here)
66 static_assert(has_F_layout<A> and has_F_layout<B>, "Error in nda::lapack::gelss: C order not supported");
67 static_assert(MemoryVector<B> or MemoryMatrix<B>, "Error in nda::lapack::gelss: B must be a vector or a matrix");
68
69 auto dm = std::min(a.extent(0), a.extent(1));
70 if (s.size() < dm) s.resize(dm);
71
72 // must be lapack compatible
73 EXPECTS(a.indexmap().min_stride() == 1);
74 EXPECTS(b.indexmap().min_stride() == 1);
75 EXPECTS(s.indexmap().min_stride() == 1);
76
77 // first call to get the optimal bufferSize
78 using value_type = get_value_t<A>;
79 value_type bufferSize_T{};
80 auto rwork = array<double, 1>(5 * dm);
81 int info = 0;
82 int nrhs = 1, ldb = b.size(); // defaults for B MemoryVector
83 if constexpr (MemoryMatrix<B>) {
84 nrhs = b.extent(1);
85 ldb = get_ld(b);
86 }
87 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);
88 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
89
90 // allocate work buffer and perform actual library call
91 array<value_type, 1> work(bufferSize);
92 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(),
93 info);
94
95 if (info) NDA_RUNTIME_ERROR << "Error in nda::lapack::gelss: info = " << info;
96 return info;
97 }
98
99} // namespace nda::lapack
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
Check if a given type is a memory matrix, i.e. an nda::MemoryArrayOfRank<2>.
Definition concepts.hpp:306
Check if a given type is a memory vector, i.e. an nda::MemoryArrayOfRank<1>.
Definition concepts.hpp:314
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.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' 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
int gelss(A &&a, B &&b, S &&s, double rcond, int &rank)
Interface to the LAPACK gelss routine.
Definition gelss.hpp:65
static constexpr bool on_host
Constexpr variable that is true if all given types have a Host address space.
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Provides a C++ interface for various LAPACK routines.
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:98
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
Definition tools.hpp:55
Macros used in the nda library.
Provides type traits for the nda library.